gpt4 book ai didi

mysql - 为什么这个查询的 MySQL 是 "using_where"?

转载 作者:行者123 更新时间:2023-11-29 06:37:22 26 4
gpt4 key购买 nike

我有这个问题:

SELECT *
FROM items i
WHERE i.user_id = 1 AND i.source_id = 34
ORDER BY i.time DESC LIMIT 0, 30;

这个复合索引:

idx_user_src_time (user_id, source_id, time)

Explain表明MySQL确实在使用索引:

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, filtered, Extra
'1', 'SIMPLE', 'ui', 'ref', 'PRIMARY,idx_source_id,idx_user_src_time', 'idx_user_src_time', '8', 'const,const', '329', '100.00', 'Using where'

但在额外的列中说他需要执行一些where

有人能解释一下为什么 MySQL 需要对这个查询执行额外的操作而索引不够用吗???

编辑

如果我删除 order by 子句,EXPLAIN 保持不变(使用相同的索引)但 using_where 消失了。

编辑

表格看起来像:

CREATE TABLE `items` (`user_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
`source_id` int(11) NOT NULL,
`time` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`,`item_id`)
KEY `idx_iid_user_src_time` (`item_id`,`user_id`,`source_id`,`time`) USING BTREE,
KEY `idx_user_time` (`user_id`,`time_order`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

最佳答案

来自 Mysql doc :

A WHERE clause is used to restrict which rows to match against the next table or send to the client. Unless you specifically intend to fetch or examine all rows from the table, you may have something wrong in your query if the Extra value is not Using where and the table join type is ALL or index. Even if you are using an index for all parts of a WHERE clause, you may see Using where if the column can be NULL.

关于mysql - 为什么这个查询的 MySQL 是 "using_where"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23695410/

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