如何强制mysql UPDATE查询使用索引?如何让mysql引擎自动使用索引而不是强制它?

时间:2017-01-08 06:36:11

标签: mysql explain mysql-cluster

以下是未使用最近创建的复合索引的更新查询/查询计划。解释说明它没有使用名为radacctupdate的复合索引,我认为这将使更新查询更快。表上还有其他索引,供其他查询使用。

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

以下是show index from <table>输出

+---------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| 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      |         |               |
+---------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

1 个答案:

答案 0 :(得分:2)

可以使用以下方法强制更新查询使用索引:

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';