【问题标题】:How to force mysql UPDATE query to use index? How to enable mysql engine to automatically use the index instead of forcing it?如何强制 mysql UPDATE 查询使用索引?如何使mysql引擎自动使用索引而不是强制使用它?
【发布时间】:2017-05-22 15:50:49
【问题描述】:

以下是未使用最近创建的复合索引的更新查询/查询计划。解释表明它没有使用名为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 &lt;table&gt;输出

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

【问题讨论】:

    标签: mysql explain mysql-cluster


    【解决方案1】:

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

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

    【讨论】:

    • MySQL 忽略 UPDATE 语句中的索引提示
    猜你喜欢
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2013-06-13
    • 2019-10-07
    • 1970-01-01
    • 2018-12-18
    相关资源
    最近更新 更多