作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下查询,它在销售表中的 60,000 条记录上花费了大约 20 秒。我知道 ORDER BY 和 LIMIT 导致了这个问题,因为当 ORDER BY 被删除时,它会在 0.10 秒内返回。
我不确定如何优化这个查询,有什么想法吗?
解释输出在这里 https://gist.github.com/anonymous/1b92fa64261559de32da
SELECT sale.saleID as id,
node.title as location,
sale.saleTotal as total,
sale.saleStatus as status,
payment.paymentMethod,
field_data_field_band_name.field_band_name_value as band,
invoice.invoiceID,
field_data_field_first_name.field_first_name_value as firstName,
field_data_field_last_name.field_last_name_value as lastName,
sale.created as date
FROM sale
LEFT JOIN payment
ON payment.saleID = sale.saleID
LEFT JOIN field_data_field_location
ON field_data_field_location.entity_id = sale.registerSessionID
LEFT JOIN node
ON node.nid = field_data_field_location.field_location_target_id
LEFT JOIN invoice
ON invoice.saleID = sale.saleID
LEFT JOIN profile
ON profile.uid = sale.clientID
LEFT JOIN field_data_field_band_name
ON field_data_field_band_name.entity_id = profile.pid
LEFT JOIN field_data_field_first_name
ON field_data_field_first_name.entity_id = profile.pid
LEFT JOIN field_data_field_last_name
ON field_data_field_last_name.entity_id = profile.pid
ORDER BY sale.created DESC
LIMIT 0,50
最佳答案
可能,您无能为力。例如,当您衡量性能时,您是在查看返回第一个 记录的时间还是整个结果集?如果没有 order by
,查询可以很快返回第一行,但您可能仍需要稍等片刻才能获取所有行。
假设比较有效,以下索引可能会有所帮助:sale(created, saleId, clientId, SaleTotal, SaleStatus
。这是查询的覆盖索引,精心构造,以便可以按正确的顺序读取。如果这避免了最终排序,那么它应该会加快查询速度,即使是获取第一行也是如此。
关于Mysql ORDER BY 和 LIMIT 慢查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31964528/
我是一名优秀的程序员,十分优秀!