【发布时间】:2016-11-21 00:33:51
【问题描述】:
SELECT SQL_CALC_FOUND_ROWS null as `rows` ,
(SELECT count(*) FROM `ci_user_smiley` where post_id = P.post_id) as smiley,
(SELECT count(*) FROM `ci_user_tag` where post_id = P.post_id) as tag,
(SELECT count(*) FROM `ci_comments` where post_id = P.post_id) as comment_count,
P.*, U.point_1 + U.point_2 + U.point_3 as `total`, F.*, Fi.*, U.*
FROM `ci_posts` P
LEFT JOIN `ci_users` U
ON P.`user_id` = U.`user_id`
LEFT JOIN `ci_filters` F
ON P.`filter` = F.filter_id
LEFT JOIN `ci_files` Fi
ON P.file = Fi.file_id
WHERE P.`approve` = 1 AND U.active = 1 AND P.post_type = 'post-color'
AND 1 ORDER BY P.post_date DESC LIMIT 0,20
这个查询需要 5 分钟才能执行,我该如何改进它!?那是因为左连接还是选择? 请注意,某些表的行数在 10K 到 100k 之间 我很感激任何建议!
【问题讨论】:
-
我建议您学习如何格式化问题。在此之后,我建议您向连接列添加索引。
-
SQL_CALC_FOUND_ROWS null as rows很奇怪。它只是在您的结果集中提供一个名为rows的 NULL 列。SQL_CALC_FOUND_ROWS项是整个查询的修饰符,而不是行。除非您知道自己需要它,否则您可能希望省略它。 -
@tim-biegeleisen 感谢您的提示
-
@OllieJones 这是必需的,将用于代码的其他部分。顺便说一句,感谢编辑
标签: mysql select indexing left-join query-optimization