使用 InnoDB 运行 MySQL:
我有一个SELECT wide_table.*要优化的查询,SELECT wide_table.id因为这就是调用代码所需的全部内容。对其进行测试,我发现 with 的执行时间*比 with 快id(尽管“精制”版本通过网络传输结果的时间更快)。
为什么会这样?
查询(更改名称以保护无辜者)是:
SELECT
`things`.*
FROM
`things`
WHERE
`things`.`active` = 1
AND (owner_id IS NOT NULL
AND owner_id > 0)
AND ((`things`.`status` IN (0 , 1)
OR `things`.`status` IS NULL))
AND (date < '2015-07-11 00:00:00');
Run Code Online (Sandbox Code Playgroud)
有一个复合索引active并且date,这是在两个版本中使用。
作为参考,输出SHOW CREATE TABLE(省略了不相关的列):
CREATE TABLE `things` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`active` tinyint(1) DEFAULT '0',
`date` datetime DEFAULT NULL, …Run Code Online (Sandbox Code Playgroud)