MySQL efficient query left join and group by (plus index)

MySQL efficient query left join and group by (plus index)

mysql efficient query

MySQL 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 MySQL

For 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:

  • If the driving table is t1, the connection order is t1->t2->t3, and an index must be created on the driven table field, that is, create an index on t2.a and t3.b
  • If the driving table is t3, the join order is t3->t2->t1, and indexes need to be created on t2.b and t1.a.

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.

Summarize

This 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:
  • Why MySQL does not recommend using subqueries and joins
  • Mysql join query syntax and examples
  • MySQL joint table query basic operation left-join common pitfalls
  • Summary of various common join table query examples in MySQL
  • Query process and optimization method of (JOIN/ORDER BY) statement in MySQL
  • The principle of MySQL join query

<<:  Interviewer asked how to achieve a fixed aspect ratio in CSS

>>:  Tips for creating two-dimensional arrays in JavaScript

Recommend

js code that associates the button with the enter key

Copy code The code is as follows: <html> &l...

Modify the maximum number of mysql connections and configuration files in docker

1. Find the mysql image docker ps 2. Enter the mi...

Detailed explanation of MySQL injection without knowing the column name

Preface I feel like my mind is empty lately, as I...

Docker container log analysis

View container logs First, use docker run -it --r...

Solve the cross-domain problem of get and post requests of vue $http

Vue $http get and post request cross-domain probl...

MySQL uses init-connect to increase the implementation of access audit function

The mysql connection must first be initialized th...

Two ways to prohibit clearing the input text input cache in html

Most browsers will cache input values ​​by defaul...

IE6 space bug fix method

Look at the code: Copy code The code is as follows...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

Three principles of efficient navigation design that web designers must know

Designing navigation for a website is like laying...

How to display the border when td is empty

Previously, I summarized how to use CSS to achieve...

Best Practices for Sharing React Code

When any project develops to a certain complexity...