gpt4 book ai didi

Mysql ORDER BY 和 LIMIT 慢查询

转载 作者:可可西里 更新时间:2023-11-01 07:58:30 37 4
gpt4 key购买 nike

我有以下查询,它在销售表中的 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/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com