[英]MySql inner join takes more than 10 seconds
I have two tables posts and followings 我有两个表帖子和以下
posts (id,userid,post,timestamp) 30 000 rows
and 和
followings(id_me,userid) 90 000 rows
I want to get lattest 10 posts form posts table based on the people i follow and my posts 我想根据我关注的人和我的帖子获得最新的10个帖子表格
SELECT p.*
FROM posts as p INNER JOIN
followings as f
ON (f.id_me=(my user id) AND p.userid=f.userid )
OR
p.userid=(my user id)
ORDER BY id DESC LIMIT 10
But it takes about 10-15 seconds to return. 但是返回大约需要10-15秒。 Thanks in advance!
提前致谢!
Using an OR in SQL is a performance killer, try this: 在SQL中使用OR是性能杀手,请尝试以下操作:
SELEC p.*
FROM posts as p INNER JOIN
followings as f
ON (f.id_me=(my user id) AND p.userid IN (f.userid,(my user id)))
ORDER BY id DESC LIMIT 10
Do this query using union
: 使用
union
以下查询:
(SELECT p.*
FROM posts p INNER JOIN
followings f
ON (f.id_me=(my user id) AND p.userid=f.userid
)
union
(select p.*
from posts p
where p.userid=(my user id)
)
ORDER BY id DESC
LIMIT 10
If the two conditions never overlap, then use union all
instead. 如果两个条件永不重叠,则改用
union all
。
An OR
condition like that prevents the query optimizer from making use of indexes. 诸如此类的
OR
条件会阻止查询优化器使用索引。 Use a UNION
instead: 改用
UNION
:
SELECT *
FROM (SELECT p.*
FROM posts as p
INNER JOIN followings as f
ON f.id_me=(my user id) AND p.userid=f.userid
UNION
SELECT *
FROM posts
WHERE userid = (my user id)) u
ORDER BY id DESC
LIMIT 10
It might be just me but I think your WHERE
clause is in an inefficient location: 可能只有我一个人,但我认为您的
WHERE
子句位于效率低下的位置:
SELECT
p.*
FROM
posts p
INNER JOIN
followings f
ON p.userid=f.userid
WHERE
MyUserID IN (p.userid, f.id_me)
ORDER BY
id DESC
LIMIT
10
First, remove the filter from the join clause, let the join just correlate the joining tables. 首先,从join子句中删除过滤器,让联接只关联联接表。
(
SELECT p.*
FROM posts as p
INNER JOIN followings as f ON p.userid=f.userid
where f.id_me=(my user id)
UNION
SELECT p.*
FROM posts as p
where p.userid=(my user id)
)
ORDER BY id DESC LIMIT 10
second, verify your indexes if that ids got no indexes it ill perform a full table scan for each cartesian product of both tables (30k x 90k =~ 3700k pairs being compared) 其次,如果您的id没有索引,请验证您的索引是否会对两个表的每个笛卡尔积执行全表扫描(比较30k x 90k =〜3700k对)
third, if you don't follow yourself you need a union from post you are following and your posts 第三,如果您不跟随自己,则需要从您正在关注的职位和您的职位合并
I read in comments that you have the required indexes. 我在评论中读到您具有必需的索引。 The problem is the query.
问题是查询。 Combining
OR
with a JOIN
confuses the poor and (often) dumb optimizer. 将
OR
JOIN
结合使用会混淆较差的(通常是)笨拙的优化器。 The LIMIT 10
should be helpful but the optimizer is not (yet) smart enough to make the best plan. LIMIT 10
应该会有所帮助,但是优化器还不够聪明,无法制定最佳计划。
Try this query: 试试这个查询:
( SELECT p.*
FROM posts AS p
JOIN followings AS f
ON f.id_me = (my_user_id)
AND p.userid = f.userid
ORDER BY p.id DESC
LIMIT 10
)
UNION ALL
( SELECT p.*
FROM posts AS p
WHERE p.userid = (my_user_id)
ORDER BY p.id DESC
LIMIT 10
) AS x
ORDER BY id DESC
LIMIT 10 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.