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

标签 mysql sql optimization

我有这个问题:

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/

相关文章:

javascript - 用于 Javascript/JSON 的 ORM

sql - 是否有一种算法可以对两个文本进行编码,即使改变它们的位置,结果也会相同?

css - 未捕获的语法错误 : Unexpected token exception after bundle two CSS file in MVC4

MySQL AUTO DECREMENT 而不是 AUTO INCREMENT,这可能吗?

python - 如何一次性执行多条SQL语句?

sql - 我怎样才能摆脱这个查询中的 'ORA-01489: result of string concatenation is too long' ?

MYSQL查询优化连接4张表

mysql - 如何使用 MySQL 在两列中填充名称?

mysql - 这怎么可能 - 较旧的记录具有较新的created_at日期?

python - 如何在MySQL中一次原子操作更新后选择值