我对此查询存在性能问题:
SELECT *
FROM post
JOIN post_plus
ON post_plus.news_id = post.id
WHERE category in(130,3)
AND approve=1
AND allow_main=1
ORDER BY kp_votes DESC,
kp_rating DESC LIMIT 30;
我想尝试的是将上述查询转换为“子查询”而不是使用JOIN
我尝试了什么:
SELECT *
FROM post
WHERE category in(130,3)
(SELECT *
FROM post_plus
WHERE post_plus.news_id = post.id)
AND approve=1
AND allow_main=1
ORDER BY fixed DESC,
kp_votes DESC,
kp_rating DESC LIMIT 10;
错误1064(42000):您的SQL语法有错误;检查 手册,对应右边的MySQL服务器版本 在'附近使用的语法'(从post_plus中选择kp_votes kp_rating WHERE post_plus.news_id ='第1行
答案 0 :(得分:1)
修复语法错误是这样的:
SELECT *
FROM post,
(SELECT *
FROM post_plus
WHERE post_plus.news_id = post.id) tmp
WHERE category in(130,3)
AND approve=1
AND allow_main=1
ORDER BY fixed DESC,
kp_votes DESC,
kp_rating DESC LIMIT 10;
但是我坚信这不如原来的加入更好。
我想这是索引的问题。你能从post
表中显示索引吗?
答案 1 :(得分:0)
答案 2 :(得分:-1)
如果优化不是问题,您只想要替代JOIN
,那么您可以使用WHERE EXISTS
在你的情况下,它可能是:
SELECT *
FROM post
WHERE EXISTS
(SELECT *
FROM post_plus
WHERE post_plus.news_id = post.id)
AND category in(130,3)
AND approve=1
AND allow_main=1
ORDER BY fixed DESC,
kp_votes DESC,
kp_rating DESC LIMIT 10;