gpt4 book ai didi

php - MySql 查询优化以避免文件排序

转载 作者:行者123 更新时间:2023-11-29 00:48:34 24 4
gpt4 key购买 nike

我希望有人帮助我优化并避免在以下查询中进行文件排序。附上其说明截图

SELECT DISTINCT(storages.id), 
IF(admin_approve = 1
OR storage_free_auctions.user_id = 167
OR ISNULL(storage_free_auctions.user_id),
auction_date, NULL) AS auction_date1
FROM user_list_storage
JOIN storages ON storages.id = user_list_storage.storage_id
JOIN states ON states.id = storages.state
LEFT JOIN storage_auctions ON storage_auctions.id = (
SELECT storage_auctions.id FROM storage_auctions
LEFT JOIN storage_free_auctions
ON storage_free_auctions.storage_auctions_id=storage_auctions.id
WHERE storage_auctions.storage_id = storages.id
AND storage_auctions.status = 'Active'
AND ( ISNULL(storage_free_auctions.user_id)
OR admin_approve = 1
OR storage_free_auctions.user_id = 167)
AND CONCAT(auction_date,' ',start_time) >= CURDATE()
ORDER BY auction_date ASC LIMIT 1)
LEFT JOIN storage_free_auctions
ON storage_free_auctions.storage_auctions_id=storage_auctions.id
LEFT JOIN storage_auction_units
ON storage_auction_units.storage_auction_id = storage_auctions.id
AND storage_auction_units.status='Active'
WHERE storages.storage_status = 'Active'
AND user_list_storage.user_id = 167
AND user_list_storage.user_list_id = 3
AND (storage_free_auctions.user_id = 167
OR admin_approve = 1
OR ISNULL(storage_free_auctions.user_id)
OR user_list_storage.user_id = 167)
GROUP BY storages.id
order by auction_date1

解释结果的第一行如下。

编号: 1选择类型:主要表: user_list_storage类型: refpossible_keys : idx_user_list_id,idx_storage_id,idx_user_id键: idx_user_id key 长度: 5ref : 常量
行: 64
额外的:使用哪里;使用临时的;使用文件排序

最佳答案

替换

 LEFT JOIN storage_auctions 
ON storage_auctions.id= (SELECT storage_auctions.id
FROM storage_auctions
LEFT JOIN storage_free_auctions
ON storage_free_auctions.storage_auctions_id = storage_auctions.id
WHERE storage_auctions.storage_id = storages.id
AND storage_auctions.status = 'Active'
AND (ISNULL(storage_free_auctions.user_id)
OR admin_approve = 1
OR storage_free_auctions.user_id = 167)
AND CONCAT(auction_date,' ',start_time) >= CURDATE() ORDER BY auction_date ASC LIMIT 1)

AND EXISTS (SELECT 1 
FROM storage_auctions
WHERE storage_auctions.id = storages.id
AND CONCAT(auction_date,' ',start_time) >= CURDATE())

因为您在左联接中的子句是多余的并且之前已经完成,所以您只希望将来存在具有相同 id 的记录,对吗?

您的其余连接都可以。它们都基于整数列,如果它们不是主键和运行状态,请确保对所有这些 ID 建立索引。

关于php - MySql 查询优化以避免文件排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9480090/

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