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
There are few and inadequate installation tutoria...
The specific code is as follows: <style> #t...
<br /> This article is translated from allwe...
Take MySQL 5.7.19 installation as an example, fir...
Table of contents Vue3 encapsulation message prom...
Recently, during the development process, I encou...
Table of contents 1. WordPress deployment 1. Prep...
To implement the "Enter != Submit" probl...
Generally, learning Java and deploying projects a...
Closure implementation of private variables Priva...
<br />I have summarized the annotation writi...
Putting aside databases, what is dialect in life?...
environment Hostname IP address Serve Prometheus ...
Recently I used MySQL to export table data to an ...
1. Event bubbling : In the process of JavaScript ...