A perfect interpretation of the stored procedure of MySQL 5.6 MRR

Source: Internet
Author: User

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:

  1. SelectLog_timeFromPersonWhereNick_name ='Lucy';

Table Structure:

  1. CREATE TABLE'Person '(
  2. 'Id'Int(10) unsignedNOT NULLAUTO_INCREMENT,
  3. 'Nick _ name'Varchar(40)NOT NULL,
  4. 'Log _ time'Timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  5. PRIMARY KEY('Id '),
  6. KEY'Idx _ nick_name '('Nick _ name ')
  7. ) ENGINE = InnoDB AUTO_INCREMENT = 5DEFAULTCHARSET = latin1

The first is MySQL 5.5.

  1. DELIMITER $
  2. USE 'TT' $
  3. DROP PROCEDUREIf exists 'SP _ range_scan5_5 '$
  4. CREATEDEFINER = 'admin' @ '%'PROCEDURE'SP _ range_scan5_5 '()
  5. BEGIN
  6. -- Sample SQL statement is below.
  7. -- Select log_time from person where nick_name = 'Lucy ';
  8. DECLAREIINTUNSIGNEDDEFAULT0;
  9. DECLARECntINTUNSIGNEDDEFAULT0;
  10. SET@ Result ='';
  11. SELECT COUNT(1)INTOCntFROMPersonWHERENick_name ='Lucy';
  12. Loop1: WHILE I <cnt
  13. DO
  14. SET@ Stmt = CONCAT ('Select id into @ v_id from person where nick_name =''Lucy''Order by nick_name asc limit', I,', 1');
  15. PREPARES1FROM@ Stmt;
  16. EXECUTES1;
  17. SET@ Result = CONCAT (@ result,'Select log_time from person where id = @ v_id');
  18. SET@ Result = CONCAT (@ result,'Union all');
  19. SETI = I + 1;
  20. ENDWHILE loop1;
  21. SET@ Result = SUBSTR (@ result, 1, CHAR_LENGTH (@ result)-CHAR_LENGTH ('Union all'));
  22. PREPARES1FROM@ Result;
  23. EXECUTES1;
  24. DROP PREPARES1;
  25. SET@ Result =NULL;
  26. END$
  27. DELIMITER;

MySQL 5.6.

  1. DELIMITER $
  2. USE 'TT' $
  3. DROP PROCEDUREIf exists 'SP _ range_scan5_6 '$
  4. CREATEDEFINER = 'admin' @ '%'PROCEDURE'SP _ range_scan5_6 '()
  5. BEGIN
  6. -- Sample SQL statement is below.
  7. -- Select log_time from person where nick_name = 'Lucy ';
  8. DECLAREIINTUNSIGNEDDEFAULT0;
  9. DECLARECntINTUNSIGNEDDEFAULT0;
  10. DECLAREIds TEXT;
  11. SETIds ='';
  12. SELECT COUNT(1)INTOCntFROMPersonWHERENick_name ='Lucy';
  13. Loop1: WHILE I <cnt
  14. DO
  15. SET@ Stmt = CONCAT ('Select id into @ v_id from person where nick_name =''Lucy''
  16. Order ByNick_nameAscLimit', I ,', 1 ');
  17. PREPARES1FROM@ Stmt;
  18. EXECUTES1;
  19. SETIds = CONCAT (ids, @ v_id,',');
  20. SETI = I + 1;
  21. ENDWHILE loop1;
  22. SETIds = CONCAT ('(', SUBSTR (ids, 1, CHAR_LENGTH (ids)-1 ),')');
  23. SET@ Result = CONCAT ('Select log_time from person where id in', Ids );
  24. PREPARES1FROM@ Result;
  25. EXECUTES1;
  26. DROP PREPARES1;
  27. SET@ Result =NULL;
  28. END$
  29. DELIMITER;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.