gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:34:14 25 4
gpt4 key购买 nike

下面是未使用最近创建的复合索引的更新查询/查询计划。解释表明它没有使用名为 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 | | |
+---------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

最佳答案

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

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 - 如何强制 mysql UPDATE 查询使用索引?如何让mysql引擎自动使用索引而不是强制使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41530165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com