我有一个包含200,000条记录的表,在某些条件下我试图对其进行过滤和排序。
ORDER BY p.date_time desc
,查询耗时0.078秒。 ORDER BY p.date_time desc
花费2.225秒查询:
SELECT p.*
, s.*
, (SELECT COUNT(1)
FROM gbs_comments
WHERE product_short_url = p.product_short_url
) AS comment_count
,( EXISTS
( SELECT *
FROM `gbs_likes`
WHERE `product_short_url` = p.product_short_url
AND `ip_address` = '1'
)
) AS like_status
FROM gbs_products p, gbs_store s
WHERE p.deal_type = 1
AND p.product_store = s.sid
ORDER BY p.date_time DESC
表索引:
说明计划:
表索引:
GBS_COMMENTS
GBS_LIKES索引
GBS产品索引
答案 0 :(得分:0)
gbs_comments
需要INDEX(product_short_url)
gbs_likes
需要使用INDEX(ip_address, product_short_url)
的任何一种顺序。
gbs_products
需要INDEX(deal_type, date_time)
(按此顺序)。