简体   繁体   English

MySQL内部连接与in子句性能

[英]Mysql inner join vs in clause performance

I have a query to get data of friends of user. 我有一个查询来获取用户朋友的数据。 I have 3 tables, one is user table, second is a user_friend table which has user_id and friend_id (both are foreign key to user table) and 3rd table is feed table which has user_id and feed content. 我有3个表,一个是用户表,第二个是具有user_id和friend_id(都是用户表的外键)的user_friend表,第三个表是具有user_id和feed内容的feed表。 Feed can be shown to friends. 提要可以显示给朋友。 I can query in two ways either by join or by using IN clause (I can get all the friends' ids by graph database which I am using for networking). 我可以通过联接或使用IN子句两种方式进行查询(我可以通过用于网络的图数据库获取所有朋友的ID)。

Here are two queries: 这是两个查询:

SELECT
  a.*
FROM feed a
INNER JOIN user_friend b ON a.user_id = b.friend_id
WHERE b.user_id = 1;

In this query I get friend ids from graph database and will pass to this query: 在此查询中,我从图形数据库获取朋友ID,并将其传递给此查询:

SELECT
 a.*
FROM feed a
WHERE a.user_id IN (2,3,4,5)

Which query runs faster and good for performance when I have millions of records? 当我有数百万条记录时,哪个查询运行得更快并且性能更好?

This depends on your desired result when you have a compared big data in your subquery their always a join is much preferred for such conditions. 当您在子查询中具有比较的大数据时,这取决于您想要的结果,在这种情况下,始终选择连接是首选。 Because subqueries can be slower than LEFT [OUTER] JOINS / INNER JOIN [LEft JOIN is faster than INNER JOIN] , but in my opinion, their strength is slightly higher readability. 因为子查询的速度可能比LEFT [OUTER] JOINS / INNER JOIN [LEFT JOIN快于INNER JOIN]慢 ,但是在我看来,它们的优势是可读性更高。

So if your data have fewer data to compare then why you chose a complete table join so that depends on how much data you have. 因此,如果您的数据中要比较的数据较少,那么为什么选择完整的表联接,这取决于您拥有多少数据。

In my opinion, if you have a less number of compared data in IN than it's good but if you have a subquery or big data then you must go for a join ... 我认为,如果您在IN中比较的数据数量少于正常情况,但是如果您有子查询或大数据,则必须进行join ...

With suitable indexes, a one-query JOIN (Choice 1) will almost always run faster than a 2-query (Choice 2) algorithm. 使用合适的索引,单查询JOIN (选择1)的运行几乎总是比2查询(选择2)的算法快。

To optimize Choice 1, b needs this composite index: INDEX(user_id, friend_id) . 为了优化选择1, b需要以下综合索引: INDEX(user_id, friend_id) Also, a needs an index (presumably the PRIMARY KEY ?) starting with user_id . 另外, a需要一个以user_id开头的索引(大概是PRIMARY KEY ?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM