简体   繁体   English

外连接中的这个条件会比 where 子句更快吗?

[英]Will be this on condition in outer join faster than where clause?

Here is my table这是我的桌子

CREATE TABLE log_table (
  `user_id` VARCHAR(5),
  `date_time` DATETIME,
  `event_name` VARCHAR(10),
  `trivial` int
);

INSERT INTO log_table
  (`user_id`, `date_time`, `event_name`, `trivial`)
VALUES
  ('001', '2020-12-10 10:00:02', 'c', 3),
  ('001', '2020-12-10 10:00:01', 'b', 9),
  ('001', '2020-12-10 10:00:40', 'e', 2),
  ('001', '2020-12-10 10:00:20', 'd', 6),
  ('001', '2020-12-10 10:00:00', 'a', 1),
  ('002', '2020-12-09 10:00:10', 'C', 9),
  ('002', '2020-12-10 10:00:50', 'D', 0),
  ('002', '2020-12-10 10:00:02', 'A', 2),
  ('002', '2020-12-10 10:00:09', 'B', 4);

(created at DB Fiddle ) (在DB Fiddle创建)

I want to find one person(anyone) that triggered an event name and retrieve all the records of that user on that day.我想找到一个触发事件名称的人(任何人)并检索该用户当天的所有记录。

user_id用户身份 date_time约会时间 event_name事件名称 trivial琐碎的 trivial_new琐碎的新
001 001 2020-12-10 10:00:00 2020-12-10 10:00:00 a一个 1 1 13 13
001 001 2020-12-10 10:00:01 2020-12-10 10:00:01 b b 9 9 19 19
001 001 2020-12-10 10:00:02 2020-12-10 10:00:02 c c 3 3 21 21
001 001 2020-12-10 10:00:20 2020-12-10 10:00:20 d d 6 6 20 20
001 001 2020-12-10 10:00:40 2020-12-10 10:00:40 e e 2 2 11 11
002 002 2020-12-09 10:00:02 2020-12-09 10:00:02 A一个 2 2 15 15
002 002 2020-12-10 10:00:09 2020-12-10 10:00:09 B 4 4 15 15
002 002 2020-12-10 10:00:10 2020-12-10 10:00:10 C C 9 9 15 15
002 002 2020-12-10 10:00:50 2020-12-10 10:00:50 D D 0 0 13 13

Here is my code:这是我的代码:

SELECT t_left.*
FROM   log_table AS t_left
       RIGHT JOIN (SELECT user_id,
                          date_time
                   FROM   log_table
                   WHERE  BINARY event_name = 'B'
                   LIMIT  1) AS t_right
               ON t_left.user_id = t_right.user_id
                  AND Substring_index(t_left.date_time, ' ', 1) =
                      Substring_index(t_right.date_time, ' ', 1)
ORDER  BY date_time 

In the right table, there would be only one record that satisfies the conditions that the event_name is B , that is the user with the id 002. And then I join it with the left table on conditions that their user_ids are equal and the date is 2020-12-10 , removing other users with different ids, 001 , and the records of the same person whose events occurred not on 2020-12-10 .在右表中,只有一条记录满足event_nameB的条件,即 id 为 002 的用户。然后我将其与左表连接,条件是他们的 user_id 相等且日期为2020-12-10 ,删除其他 id 不同的用户001 ,以及同一个人在2020-12-10的事件记录。

It works all well.它运作良好。

Then I modified my code to check if it would go as I expected to(it did, see here ):然后我修改了我的代码以检查它是否会像我预期的那样 go (确实如此,请参见此处):

SELECT t_left.*
FROM   log_table AS t_left
       RIGHT JOIN (SELECT user_id,
                          date_time
                   FROM   log_table
                   WHERE  BINARY event_name = 'B'
                   LIMIT  1) AS t_right
               ON t_left.user_id = t_right.user_id
WHERE  Substring_index(t_left.date_time, ' ', 1) =
       Substring_index(t_right.date_time, ' ', 1)
ORDER  BY date_time 

In this case, I just join the tables by one condition and filter the dates of that user to get the right records.在这种情况下,我只需按一个条件连接表并过滤该用户的日期以获得正确的记录。

I read some answers here and here and here where most of the examples are conditioning on a constant and some people say join would be faster while others state that the compiler will optimize the clauses and hence the speed would be the same.在此处此处以及此处阅读了一些答案,其中大多数示例都以常量为条件,有些人说加入会更快,而另一些人则说编译器将优化子句,因此速度将是相同的。

I wonder if in my case the first one would be faster?我想知道在我的情况下,第一个是否会更快?

Any online platform to compare the speed?有什么在线平台可以比较速度吗?

ON should be used to say how the tables are related. ON应该用来说明表格之间的关系。

WHERE should be used for filtering. WHERE应该用于过滤。

For plain JOIN ( INNER JOIN ), they are implemented identically.对于普通JOIN ( INNER JOIN ),它们的实现方式相同。

For LEFT/RIGHT, they may make a big difference.对于左/右,它们可能会产生很大的不同。

If the second query is suppose to be the final query structure, you don't need to use RIGHT JOIN anymore, that's especially true since you're matching the dates in WHERE instead in ON .. effectively cancelling the right join (or left join).如果假设第二个查询是最终查询结构,则不再需要使用RIGHT JOIN ,尤其如此,因为您匹配WHERE中的日期而不是ON .. 有效地取消了右连接(或左连接) )。 Actually, since you're doing right join to a sub-query that only extract a certain data for the table on the right, there's hardly any reason to right join, you'll got more differences in results if you do left join instead.实际上,由于您正在对仅提取右侧表的特定数据的子查询进行右连接,因此几乎没有任何理由进行右连接,如果您使用左连接,您会得到更多的结果差异。 Besides, In my experience, JOIN ( INNER JOIN ) is usually much faster than left/right join.此外,根据我的经验, JOININNER JOIN )通常比左/右连接快得多。

Another thing is, indexes make a lot of differences when it comes to performance.另一件事是,索引在性能方面有很大的不同。 On your current example, there were no indexes so I recommend that you do that.在您当前的示例中,没有索引,因此我建议您这样做。 I learned a lot about indexing from @Rick James' page here .我从@Rick James 的页面here学到了很多关于索引的知识。

Here is a fiddle based on your example date consisting of your original attempt and few more:这是一个基于您的示例日期的小提琴,其中包括您的原始尝试和更多:

  1. RIGHT JOIN.. ON... AND
  2. RIGHT JOIN.. ON... WHERE
  3. LEFT JOIN.. ON... AND
  4. LEFT JOIN.. ON... WHERE
  5. adding index添加索引
  6. EXPLAIN before and after index.EXPLAIN之前和之后解释。

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1b244deb6fad0dda0bf4aaf701e6bac2 https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1b244deb6fad0dda0bf4aaf701e6bac2

PS: In the fiddle, I changed the date extraction to use DATE() . PS:在小提琴中,我将日期提取更改为使用DATE()

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

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