mysql efficient queryMySQL sacrifices group by to increase the speed of left join (assuming an index is added). User table: 100,000 data Example 1: About 200 seconds SELECT U.id, A.favorite_count FROM (SELECT id from user) U LEFT JOIN ( -- Number of likes SELECT favorite_by AS user_id, SUM(favorite_count) AS favorite_count FROM favorite GROUP BY favorite_by ) A ON U.id=A.user_id LEFT JOIN ( -- Number of comments SELECT user_id, COUNT(*) AS comment_count FROM photo_comment GROUP BY user_id ) B ON U.id=B.user_id Example 2: More than 1 second select uf.user_id , uf.favorite_count, COUNT(pc.id) as comment_count from ( select u.id as user_id , SUM(f.favorite_count) as favorite_count from (SELECT id from user) u LEFT JOIN favorite f on f.favorite_by = u.id GROUP BY u.id ) LEFT JOIN photo_comment pc on pc.user_id = uf.user_id GROUP BY uf.user_id Appendix: How to efficiently join 3 tables in MySQLFor the following three tables join statement select * from t1 join t2 on(t1.a=t2.a) join t3 on (t2.b=t3.b) where t1.c>=X and t2.c>=Y and t3.c>=Z; If rewritten as straight_join, how to specify the connection order and how to create indexes for the three tables? Try to use the BKA algorithm When using BKA, instead of "calculating the result of joining two tables first and then joining with the third table", the query is directly nested. Specific implementation: Among the three conditions of t1.c>=X, t2.c>=Y, and t3.c>=Z, select the table with the least data after filtering as the first driving table. At this point, the following two situations may occur. If table t1 or t3 is selected, the rest is fixed:
At the same time, we also need to create an index on field c of the first driving table. In the second case, if the first driving table selected is table t2, the filtering effects of the other two conditions need to be evaluated. The idea is to try to make the data set of the driving table participating in each join as small as possible, because this will make our driving table smaller. SummarizeThis is the end of this article about MySQL efficient query left join and group by. For more relevant MySQL efficient query content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Interviewer asked how to achieve a fixed aspect ratio in CSS
>>: Tips for creating two-dimensional arrays in JavaScript
This article example shares the specific code of ...
Table of contents Technology Stack Effect analyze...
Front-end technology layer (The picture is a bit e...
Table of contents 01 sql_slave_skip_counter param...
Table of contents 1. Introduction 2. Use 1. Diffe...
Table of contents Preface What is Deno? Compariso...
Table of contents Configure node.js+nvm+npm npm s...
1. Query speed of two query engines (myIsam engin...
1.1 Data Type Overview The data type is a field c...
Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...
Preface Today I encountered a very strange proble...
pt-heartbeat When the database is replicated betw...
Preface Before we start explaining the principle ...
Some people say that IE9 is Microsoft's secon...
SVN service backup steps 1. Prepare the source se...