Scene
I use the database is mysql5.6, the following simple introduction of the next scenario
Curriculum
CREATE TABLE Course (c_id int PRIMARY key,name varchar (10))
Data 100 article
Student table:
CREATE TABLE Student (ID int PRIMARY key,name varchar (10))
Data 70,000 article
Student Score Table SC
CREATE table SC ( sc_id int PRIMARY KEY, s_id int, c_id int, score int)
Data 70w Bar
Purpose of Enquiry:
Find candidates with 100 points in Chinese test
Query statement:
Select s.* from Student s where s.s_id in (select s_id from SC sc where sc.c_id = 0 and sc.score = 100)
Execution Time: 30248.271s
Dizzy, why so slow, first to see the following query plan:
EXPLAIN Select s.* from Student s where s.s_id in (select s_id from SC sc where sc.c_id = 0 and sc.score = 100)
Found no index, type is all, then the first thought is to build an index, the indexed field is of course the field in the Where condition.
Index the SC table c_id and score first
CREATE index Sc_c_id_index on SC (c_id);
CREATE index Sc_score_index on SC (score);
Execute the above query statement again, time is: 1.054s
Faster than 3w times, greatly shorten the query time, it seems that the index can greatly improve the query efficiency, it seems necessary to build index, many times have forgotten to build
Index, the amount of data is not felt at all, this optimization feeling very cool.
But the 1s time is still too long, can also be optimized, look closely at the execution plan:
To view the optimized SQL:
SELECT ' YSB '. ' s ', ' s_id ' as ' s_id ', ' YSB '. ' ' name ' as ' name ' from ' YSB '. ' Student ' s ' WHERE < In_ Optimizer > ( ' YSB '. ' s '. ' s_id ',< EXISTS > ( SELECT 1 from ' YSB '. ' sc ' SC ' WHERE (' YSB '. ' SC '. ' c_id ' = 0) and (' YSB '. ' SC '. ' Score ' =-) and ( < CACHE > (' YSB '. ' s ') ' S_ ID ') = ' YSB '. ' SC '. ' s_id ')))
Add: Here are some netizens asked how to view the optimized statement
Here's how:
Executing in the command window
Have Type=all
According to my previous idea, the order of execution of the SQL should be to execute the subquery first
Select s_id from SC sc where sc.c_id = 0 and Sc.score = 100
Time: 0.001s
The following results are obtained:
And then execute
Select s.* from Student s where s.s_id in (7,29,5000)
Time: 0.001s
This is quite fast, ah, MySQL is not the first to execute the inner query, but the SQL was optimized into a exists clause, and appeared ependent subquery,
MySQL is to execute the outer query first, then execute the query in the inner layer, so that the loop 70007*11=770077 times.
Then use a connection query instead?
SELECT s.* from Student sinner JOIN SC Scon sc.s_id = S.s_idwhere sc.c_id=0 and sc.score=100
In order to re-analyze the situation of the connection query, we will first temporarily delete the index Sc_c_id_index,sc_score_index
Execution time is: 0.057s
Improve efficiency and look at the execution plan:
There are even tables here, and I guess I'm going to set an index on the SC table s_id.
CREATE index Sc_s_id_index on SC (s_id);
Show index from SC
When you perform a connection query
Time: 1.076s, even the time has become longer, what reason? To view the execution plan:
The optimized query statements are:
SELECT ' YSB '. ' s ', ' s_id ' as ' s_id ', ' YSB '. ' Name ' as ' name ' from ' YSB '. ' Student ' ' JOIN ' YSB '. ' SC ' SC ' WHERE ( ' YSB '. ' SC '. ' s_id ' = ' YSB '. ' s '. ' s_id ') and (' YSB ' . ' sc '. ' Score ' = +) and (' YSB '. ') SC '. ' c_id ' = 0) )
Seems to be the first to do the connection query, and then execute the where filter
Go back to the previous execution plan:
Here is the first to do where filter, and then make a table, the execution plan is not fixed, then we first look at the standard SQL execution order:
Normally it is the first join and then the where filter, but here we are, if we join first, there will be 70w data to send the join exercises, so first execute where
Filtering is a smart solution, and now to rule out MySQL query optimization, I write an optimized SQL
Select s.*from ( Select * from SC sc WHERE sc.c_id = 0 and Sc.score = Tinner JOIN Student s on t.s_id = s.s_id
That is, the SC table filtering, then table connection, the execution time is: 0.054s
It's about the same time you didn't build s_id index before.
To view the execution plan:
First, the SC re-list is extracted, so the efficiency is much higher, now the problem is to extract SC when the scan table appears, then you can now clearly need to establish the relevant index
CREATE index Sc_c_id_index on SC (c_id);
CREATE index Sc_score_index on SC (score);
To execute the query again:
Select s.*from ( Select * from SC sc WHERE sc.c_id = 0 and Sc.score = + ) Tinner JOIN Student s on t.s_id = s.s_id
Execution time: 0.001s, this time is quite reliable, 50 times times faster
Execution Plan:
We'll see that the index is used to extract the SC first and then the table.
Then execute the next SQL.
SELECT s.* from Student sinner JOIN SC Scon sc.s_id = S.s_idwhere sc.c_id=0 and sc.score=100
Execution Time 0.001s
Execution Plan:
Here is MySQL query statement optimization, the first to perform a where filter, and then perform the connection operation, and all use the index.
Summarize:
1.mysql nested subqueries are actually less efficient
2. You can optimize it for a connection query
3. Create an appropriate index
4. Learning to analyze the SQL execution plan, MySQL optimizes SQL, so it's important to analyze the execution plan
Due to the time issue, this article is written here, followed by other SQL optimization experience.
A very interesting SQL optimization experience: from 30248.271s to 0.001s