Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to specify "USE INDEX" or "FORCE INDEX" in CodeIgniter

Is it possible to specify "USE INDEX" or "FORCE INDEX" in CodeIgniter, other than using

$this->db->query()

What I mean is if it's possible to insert "FORCE INDEX" somewhere in one of ActiveRecord's methods.

like image 503
NaturalBornCamper Avatar asked Sep 24 '12 01:09

NaturalBornCamper


People also ask

Which operation on a column prefers an index on that column?

In general, you should create an index on a column in any of the following situations: The column is queried frequently. A referential integrity constraint exists on the column. A UNIQUE key integrity constraint exists on the column.

How do you force a index to be used in SQL statement?

In case the query optimizer ignores the index, you can use the FORCE INDEX hint to instruct it to use the index instead. In this syntax, you put the FORCE INDEX clause after the FROM clause followed by a list of named indexes that the query optimizer must use.

What is force index in MySQL?

The FORCE INDEX hint acts like USE INDEX ( index_list ) , with the addition that a table scan is assumed to be very expensive. In other words, a table scan is used only if there is no way to use one of the named indexes to find rows in the table. As of MySQL 8.0.

Does join use index MySQL?

Because MySQL uses only one index for each table in one execution, we need to decide using index for join or group by. Let give GROUP BY a chance and see what happens. According to the explain chart, MySQL will join `answer` table into `user` table.


1 Answers

You can use the from() active record method to add this in queries like this:

$this->db->like('name', 'user', 'after')->from('users use index (name)')->get();

produces sql query like this:

 SELECT * FROM (`users` use index (name)) WHERE  `name`  LIKE 'user%'

One caveat is that the from() method tries to find identifiers and multiple tables so adding one or more , to it's input is most likely to end up as an SQL syntax error.

like image 126
complex857 Avatar answered Oct 20 '22 11:10

complex857