LIMIT little about MySQL syntax optimization!

Today a friend asked optimization of such statements, I generally introduced him down from doing simple SQL optimization point of view, as we consider the application side not to consider.
Let me give a simple example.
Consider the following table structure:
/ * DDL Information the For - t_girl.t_page_sample * /
---------------------------------- ------------

the Table the Create the Table                                                    
------------- ---------------------- ------------------------------------------
t_page_sample the CREATE TABLE `t_page_sample` (                                  
                 ` int id` (10) unsigned the NOT NULL,                               
                 `v_state` tinyint (. 1) the NOT NULL the DEFAULT '. 1',                    
                 ` log_time` timestamp the NOT NULL the DEFAULT '0000-00-00 00:00:00',  
                 update_time` timestamp the NOT NULL the DEFAULT `CURRENT_TIMESTAMP,   
                 PRIMARY KEY (` id`)                                            
               ) ENGINE = MyISAM the DEFAULT CHARSET = utf8

my test system as standard DELL D630, XP system.
Example of a table number of records:
SELECT COUNT (*) from t_page_sample;


query result(1 records)

count(*)
993098

Let's take a look at the following step by step, this statement:
EXPLAIN SQL_NO_CACHE the SELECT * from t_page_sample the Order by the above mentioned id asc limit 900001,20;

query result(1 records)

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t_page_sample ALL (NULL) (NULL) (NULL) (NULL) 993098 Using filesort

As can be seen from the above, she did not use any indexes, the number of scanning lines is 993,098, and used the sort!

SQL_NO_CACHE * from the Order t_page_sample the SELECT by the above mentioned id asc limit 900001,20;

(20 Row (S) returned)

(4688 MS taken)

then how do we optimize this statement do?
First of all, we think of the index. In this statement, only the ID may use the index, then we add a hint to the optimizer conditions and let him use the index.


SQL_NO_CACHE * t_page_sample from the SELECT Force index (Primary) the Order by the above mentioned id asc limit 900001,20;

(20 Row (S) returned)
(9239 MS taken)

did not think the time is used even longer than that without an index. It seems like a dead end road.
We try to change the next statement is as follows:
the SELECT * from t_page_sample
the WHERE the above mentioned id the BETWEEN
         (the SELECT SQL_NO_CACHE the above mentioned id from t_page_sample the Order by the above mentioned id asc limit 900001,1)
         and
         (SQL_NO_CACHE the above mentioned id from the SELECT t_page_sample the Order by the above mentioned id asc limit 900020,1);

(20 Row (S) returned)

(625 MS taken)
wow, this is very good, full shorten nearly 15 times!
Then there is the optimization of space?
Once again, we change the statement:
the SELECT * from t_page_sample
the WHERE the above mentioned id> = (from the SELECT SQL_NO_CACHE the above mentioned id t_page_sample the Order by the above mentioned id asc limit 900001,1)

limit 20;

(20 Row (S) returned)
(406 MS taken)

on time and last time the sentence shortened by a third. congratulations.

This article comes from " God, let there or be square! " Blog, be sure to keep this source http://yueliangdao0608.blog.51cto.com/397025/304996

Reproduced in: https: //my.oschina.net/u/585111/blog/219460

Guess you like

Origin blog.csdn.net/weixin_34347651/article/details/92008351