【发布时间】:2021-07-18 13:48:12
【问题描述】:
我有一种情况,我在 where 子句中指定了最多 100 个主键。
一个示例查询:
select product_id, max(date_created) AS last_order_date
from orders
where product_id=28906
or product_id=28903
or product_id=28897
or product_id=28848
or product_id=28841
or product_id=28839
or product_id=28838
or product_id=28837
or product_id=28833
or product_id=28832
or product_id=28831
or product_id=28821
or product_id=28819
or product_id=28816
or product_id=28814
or product_id=28813
or product_id=28802
or product_id=28800
or product_id=28775
or product_id=28773
group by product_id
order by date_created desc
解释显示Using index condition; Using temporary; Using filesort
我知道我应该避免使用Using temporary; Using filesort 进行查询,但是即使对于大型数据集,查询执行时间也很快,我是否必须避免它?我已经给出了一个 ID 列表,所以我能做的就是最好的查询。
如果我决定继续使用该查询,我应该预料到哪些副作用或缺点?
解释输出:
1 SIMPLE wc_order_product_lookup range product_id product_id 8 NULL 3 Using index condition; Using temporary; Using filesort
【问题讨论】:
-
您使用的是什么版本的 MySQL?较新的版本现在包括运算符 Index Skip Scan(从 Oracle 导入),对于此类情况可以非常快。
product_id和date_created列是否参与索引? -
@TheImpaler 我正在 MySQL 5.6 和 MariaDB 10.3.3 上测试它。我对这两个字段都有索引。用 EXPLAIN 更新了帖子。
-
啊...这些都没有 new 运算符。我认为它是 MySQL 8 的最新版本之一。
标签: mysql sql optimization