Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.01 KB | None | 0 0
  1. USE employees;
  2.  
  3. /* Creating an index for an index-covered query */
  4.     CREATE INDEX dept_no_from_date_idx ON dept_emp (dept_no, from_date);
  5.  
  6. /* Show `dept_emp` table structure, indexes and generic data */
  7.     SHOW TABLE STATUS LIKE "dept_emp";
  8.     DESCRIBE dept_emp;
  9.     SHOW KEYS IN dept_emp;
  10.  
  11. /* The EXPLAIN shows that the subquery doesn't use a covering-index */
  12. EXPLAIN SELECT SQL_NO_CACHE * FROM dept_emp INNER JOIN (
  13.         /* The subquery should use a covering index, but isn't */
  14.         SELECT SQL_NO_CACHE emp_no, dept_no FROM dept_emp WHERE dept_no="d001" ORDER BY from_date DESC LIMIT 20000,50
  15.     ) AS `der` USING (`emp_no`, `dept_no`);
  16.  
  17. /* The EXPLAIN shows that the subquery DOES use a covering-index,
  18.         thanks to the FORCE INDEX clause */
  19. EXPLAIN SELECT SQL_NO_CACHE * FROM dept_emp INNER JOIN (
  20.         /* The subquery use a covering index */
  21.         SELECT SQL_NO_CACHE emp_no, dept_no FROM dept_emp FORCE INDEX(dept_no_from_date_idx) WHERE dept_no="d001" ORDER BY from_date DESC LIMIT 20000,50
  22.     ) AS `der` USING (`emp_no`, `dept_no`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement