Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force mysql UPDATE query to use index? How to enable mysql engine to automatically use the index instead of forcing it?

Below is the update query/query plan that is not using the compound index that was created recently. The explain shows that its not using the compound index named radacctupdate which i think will make the update query faster. There are other indexes on table too which are used by other queries.

EXPLAIN UPDATE radacct SET acctstoptime = '2017-01-08 11:52:24',
acctsessiontime = unix_timestamp('2017-01-08 11:52:24') - unix_timestamp(acctstarttime),
acctterminatecause = '', acctstopdelay = 14855646 
WHERE acctstoptime IS NULL AND
nasipaddress = '102.34.56.234' AND acctstarttime <= '2017-01-08 11:52:24';

************* 1. row ***********
           id: 1
  select_type: SIMPLE
        table: radacct
         type: range
possible_keys: acctstarttime,acctstoptime,nasipaddress,radacctupdate
          key: nasipaddress
      key_len: 17
          ref: const
         rows: 94
        Extra: Using where; Using MRR

Below is show index from <table> output

+---------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name        | Seq_in_index | Column_name     | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| radacct |          0 | PRIMARY         |            1 | radacctid       | A         |    29299212 |     NULL | NULL   |      | BTREE      |         |               |
| radacct |          1 | username        |            1 | username        | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radacct |          1 | framedipaddress |            1 | framedipaddress | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radacct |          1 | acctsessionid   |            1 | acctsessionid   | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radacct |          1 | acctsessiontime |            1 | acctsessiontime | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
| radacct |          1 | acctstarttime   |            1 | acctstarttime   | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
| radacct |          1 | acctstoptime    |            1 | acctstoptime    | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
| radacct |          1 | nasipaddress    |            1 | nasipaddress    | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radacct |          1 | acctuniqueid    |            1 | acctuniqueid    | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radacct |          1 | radacctupdate   |            1 | acctstoptime    | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
| radacct |          1 | radacctupdate   |            2 | nasipaddress    | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radacct |          1 | radacctupdate   |            3 | acctstarttime   | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
+---------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
like image 568
sherpaurgen Avatar asked Jan 08 '17 06:01

sherpaurgen


People also ask

How do I force an index query in MySQL?

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.

Does MySQL use index automatically?

Mostly we create index when creating table. Any column in creating table statement declared as PRIMARY KEY, KEY, UNIQUE or INDEX will be indexed automatically by MySQL. In addition, you can add indexes to the tables which has data.

How do I force an index in SQL query?

FORCE INDEX works by only considering the given indexes (like with USE_INDEX) but in addition it tells the optimizer to regard a table scan as something very expensive. However if none of the 'forced' indexes can be used, then a table scan will be used anyway.

Does index help in update query?

An index on the where column will speed up updates, and selects, but slow down some insertions. Well updating an index is done entirely in memory which is pretty fast. The update query is one which simply increments a column.


1 Answers

The update query can be forced to use index using below method:

UPDATE radacct use index(indexname_indx100) 
SET acctstoptime=  '2017-01-08 14:58:27',
acctsessiontime =  unix_timestamp('2017-01-08 14:58:27')-                                   unix_timestamp(acctstarttime),acctterminatecause = '',              acctstopdelay=1483866808
WHERE acctstoptime IS NULL AND nasipaddress='22.194.36.2' AND
acctstarttime <= '2017-01-08 14:58:27';
like image 187
sherpaurgen Avatar answered Sep 24 '22 18:09

sherpaurgen