有人可以帮我理解如何加快Order By条款的速度吗?
我做了很多研究,但似乎无法找到答案。
我有一个包含100万行测试数据的表,以下查询需要20-30秒才能加载:
SELECT f.f_id, f.type, f.s_id, f.story_name, f.new_story, f.share_id, f.photo_template, f.num_media, f.num_products, f.original_language, f.original_language_name,
f.feed_photo, f.feed_photo_type, f.feed_photo_length, f.feed_photo_video,
f.feed_photo2, f.feed_photo2_type, f.feed_photo2_length, f.feed_photo2_video,
f.feed_photo3, f.feed_photo3_type, f.feed_photo3_length, f.feed_photo3_video,
f.feed_photo4, f.feed_photo4_type, f.feed_photo4_length, f.feed_photo4_video,
f.feed_photo5, f.feed_photo5_type, f.feed_photo5_length, f.feed_photo5_video,
f.feed_photo6, f.feed_photo6_type, f.feed_photo6_length, f.feed_photo6_video,
f.background_photo, f.font_colour, f.hashtag_colour, f.at_name_colour,
f.title, f.content, f.num_likes, f.num_comments, f.hashtag, f.shared_with, f.created_by, f.date_order, f.date_published,
u.u_id, u.forename, u.surname, u.profile_photo, u.username, u.staff_member, u.brand_ambassador, u.last_signed_in, u.deleted,
s.s_id AS seller_id, s.name AS seller_name, s.logo AS seller_logo,
g.g_id AS group_id, g.name AS group_name, g.type AS group_type
FROM feed AS f
INNER JOIN users AS u ON u.u_id = f.created_by
LEFT JOIN sellers AS s ON s.s_id = f.seller_id
LEFT JOIN groups AS g ON g.g_id = f.group_id
WHERE f.published = 1 AND f.deleted = 0 AND f.shared_with = 1
OR f.published = 1 AND f.deleted = 0 AND f.shared_with = 2 AND g.type = 0
OR f.published = 1 AND f.deleted = 0 AND f.shared_with = 2 AND f.group_id IN (5, 4)
OR f.published = 1 AND f.deleted = 0 AND f.shared_with = 3 AND f.created_by = 1
GROUP BY f.f_id
ORDER BY f.date_order DESC
LIMIT 0, 20
但是,如果我删除了ORDER BY f.date_order DESC
,它会立即加载。
我尝试为date_order添加索引。
为什么用date_order列加载需要20-30秒,但没有它就立即加载?
谢谢
更新: 我已经改变了我的查询以使用上一页加载的最新ID:
SELECT f.f_id, f.type, f.s_id, f.story_name, f.new_story, f.share_id, f.photo_template, f.num_media, f.num_products, f.original_language, f.original_language_name,
f.feed_photo, f.feed_photo_type, f.feed_photo_length, f.feed_photo_video,
f.feed_photo2, f.feed_photo2_type, f.feed_photo2_length, f.feed_photo2_video,
f.feed_photo3, f.feed_photo3_type, f.feed_photo3_length, f.feed_photo3_video,
f.feed_photo4, f.feed_photo4_type, f.feed_photo4_length, f.feed_photo4_video,
f.feed_photo5, f.feed_photo5_type, f.feed_photo5_length, f.feed_photo5_video,
f.feed_photo6, f.feed_photo6_type, f.feed_photo6_length, f.feed_photo6_video,
f.background_photo, f.font_colour, f.hashtag_colour, f.at_name_colour,
f.title, f.content, f.num_likes, f.num_comments, f.hashtag, f.shared_with, f.created_by, f.date_order, f.date_published,
f.poll_date_end, f.poll_choice1_val, f.poll_choice1_score, f.poll_choice2_val, f.poll_choice2_score, f.poll_choice3_val, f.poll_choice3_score, f.poll_choice4_val, f.poll_choice4_score, f.poll_choice5_val, f.poll_choice5_score, f.poll_choice6_val, f.poll_choice6_score, f.poll_choice7_val, f.poll_choice7_score, f.poll_choice8_val, f.poll_choice8_score, f.poll_choice9_val, f.poll_choice9_score, f.poll_choice10_val, f.poll_choice10_score,
u.u_id, u.forename, u.surname, u.profile_photo, u.username, u.staff_member, u.brand_ambassador, u.last_signed_in, u.deleted,
s.s_id AS seller_id, s.name AS seller_name, s.logo AS seller_logo,
g.g_id AS group_id, g.name AS group_name, g.type AS group_type
FROM feed AS f
INNER JOIN users AS u ON u.u_id = f.created_by
LEFT JOIN sellers AS s ON s.s_id = f.seller_id
LEFT JOIN groups AS g ON g.g_id = f.group_id
WHERE f.published = 1 AND f.deleted = 0 AND f.shared_with = 1 AND f.f_id >= 1802859 AND f.f_id <= 1802869
OR f.published = 1 AND f.deleted = 0 AND f.shared_with = 2 AND g.type = 0 AND f.f_id >= 1802859 AND f.f_id <= 1802869
OR f.published = 1 AND f.deleted = 0 AND f.shared_with = 2 AND f.group_id IN (5, 4) AND f.f_id >= 1802859 AND f.f_id <= 1802869
OR f.published = 1 AND f.deleted = 0 AND f.shared_with = 3 AND f.created_by = 1 AND f.f_id >= 1802859 AND f.f_id <= 1802869
ORDER BY f.date_order DESC, f.f_id DESC
LIMIT 11
我按照建议删除了该组。
现在加载0.3秒但是只要我将AND u.brand_ambassador = 1
添加到where子句(根据用户选择的视图,这是我的要求),它会跳转到5-6秒。
此查询需要0.3秒:
SELECT f.f_id
FROM feed AS f
INNER JOIN users AS u ON u.u_id = f.created_by
LEFT JOIN sellers AS s ON s.s_id = f.seller_id
LEFT JOIN groups AS g ON g.g_id = f.group_id
WHERE f.date_order < '2018-03-21 15:41:03' AND f.published = 1 AND f.deleted = 0 AND f.shared_with = 1 AND u.brand_ambassador = 1
OR f.date_order < '2018-03-21 15:41:03' AND f.published = 1 AND f.deleted = 0
AND f.shared_with = 2 AND g.type = 0 AND u.brand_ambassador = 1
OR f.date_order < '2018-03-21 15:41:03' AND f.published = 1 AND f.deleted = 0
AND f.shared_with = 2 AND f.group_id IN (5, 4) AND u.brand_ambassador = 1
ORDER BY f.date_order DESC, f.f_id DESC
LIMIT 10, 1
以下查询需要5-6秒:
SELECT f.f_id
FROM feed AS f
INNER JOIN users AS u ON u.u_id = f.created_by
LEFT JOIN sellers AS s ON s.s_id = f.seller_id
LEFT JOIN groups AS g ON g.g_id = f.group_id
WHERE f.date_order < '2018-03-21 15:41:03' AND f.published = 1 AND f.deleted = 0 AND f.shared_with = 1 AND u.brand_ambassador = 1
OR f.date_order < '2018-03-21 15:41:03' AND f.published = 1 AND f.deleted = 0 AND f.shared_with = 2 AND g.type = 0 AND u.brand_ambassador = 1
OR f.date_order < '2018-03-21 15:41:03' AND f.published = 1 AND f.deleted = 0 AND f.shared_with = 2 AND f.group_id IN (5, 4) AND u.brand_ambassador = 1
ORDER BY f.date_order DESC, f.f_id DESC
LIMIT 10, 1
我认为用户表没有使用brand_ambassador列上的索引吗?
我尝试过使用FORCE INDEX,但似乎无法使用连接表。
更新: 正如Solarflare所建议的那样,从原始查询中删除GROUP BY就可以了。
我现在面临一个新问题。
当我将where子句的最后一行从f.created_by = 1
更改为f.created_by = 2
时,它会恢复为45秒加载。
请参阅以下说明:
我添加的数百万条测试记录几乎都是由用户1创建的,可能与它有关。