我有下表:
名称:示例
Fields
Id: Int(10)
Quantity: Int(10)
ProductId: Int(10)
SaleTimestamp: Int(10)
Indexes
ID, Primary, Unique
ProductId, Not Unique
SaleTimestamp, Not Unique
如何优化以下查询?
SELECT Quantity FROM Example WHERE Product_id = 'x' ORDER BY SaleTimestamp DESC LIMIT 1
发布于 2014-08-26 04:25:30
MySQL只使用一个索引,因此为了查询,它将只查询ProductId索引,因此ORDER无法使用索引。
解决方案是向这两列添加一个索引,例如,
ALTER TABLE example ADD INDEX comb( ProductId, SaleTimestamp );
https://stackoverflow.com/questions/25505718
复制