MySQL 5.6 is coming soon, and 5.6 has made many optimizations to the optimizer. This time I mainly explain MRR (MULTI-RANGE-READ ).
I used stored procedures to explain the changes in this process. Let's take a look.
For statements:
- SelectLog_timeFromPersonWhereNick_name ='Lucy';
Table Structure:
- CREATE TABLE'Person '(
- 'Id'Int(10) unsignedNOT NULLAUTO_INCREMENT,
- 'Nick _ name'Varchar(40)NOT NULL,
- 'Log _ time'Timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY('Id '),
- KEY'Idx _ nick_name '('Nick _ name ')
- ) ENGINE = InnoDB AUTO_INCREMENT = 5DEFAULTCHARSET = latin1
The first is MySQL 5.5.
- DELIMITER $
- USE 'TT' $
- DROP PROCEDUREIf exists 'SP _ range_scan5_5 '$
- CREATEDEFINER = 'admin' @ '%'PROCEDURE'SP _ range_scan5_5 '()
- BEGIN
- -- Sample SQL statement is below.
- -- Select log_time from person where nick_name = 'Lucy ';
- DECLAREIINTUNSIGNEDDEFAULT0;
- DECLARECntINTUNSIGNEDDEFAULT0;
- SET@ Result ='';
- SELECT COUNT(1)INTOCntFROMPersonWHERENick_name ='Lucy';
- Loop1: WHILE I <cnt
- DO
- SET@ Stmt = CONCAT ('Select id into @ v_id from person where nick_name =''Lucy''Order by nick_name asc limit', I,', 1');
- PREPARES1FROM@ Stmt;
- EXECUTES1;
- SET@ Result = CONCAT (@ result,'Select log_time from person where id = @ v_id');
- SET@ Result = CONCAT (@ result,'Union all');
- SETI = I + 1;
- ENDWHILE loop1;
- SET@ Result = SUBSTR (@ result, 1, CHAR_LENGTH (@ result)-CHAR_LENGTH ('Union all'));
- PREPARES1FROM@ Result;
- EXECUTES1;
- DROP PREPARES1;
- SET@ Result =NULL;
- END$
- DELIMITER;
MySQL 5.6.
- DELIMITER $
- USE 'TT' $
- DROP PROCEDUREIf exists 'SP _ range_scan5_6 '$
- CREATEDEFINER = 'admin' @ '%'PROCEDURE'SP _ range_scan5_6 '()
- BEGIN
- -- Sample SQL statement is below.
- -- Select log_time from person where nick_name = 'Lucy ';
- DECLAREIINTUNSIGNEDDEFAULT0;
- DECLARECntINTUNSIGNEDDEFAULT0;
- DECLAREIds TEXT;
- SETIds ='';
- SELECT COUNT(1)INTOCntFROMPersonWHERENick_name ='Lucy';
- Loop1: WHILE I <cnt
- DO
- SET@ Stmt = CONCAT ('Select id into @ v_id from person where nick_name =''Lucy''
- Order ByNick_nameAscLimit', I ,', 1 ');
- PREPARES1FROM@ Stmt;
- EXECUTES1;
- SETIds = CONCAT (ids, @ v_id,',');
- SETI = I + 1;
- ENDWHILE loop1;
- SETIds = CONCAT ('(', SUBSTR (ids, 1, CHAR_LENGTH (ids)-1 ),')');
- SET@ Result = CONCAT ('Select log_time from person where id in', Ids );
- PREPARES1FROM@ Result;
- EXECUTES1;
- DROP PREPARES1;
- SET@ Result =NULL;
- END$
- DELIMITER;