i create a table lc178(emp_no int,salary int) from salaries (THAT salaries in mysql example database employees) for test rank score.
same sql ,different result after i created an index. i knew that index i created is the 'KILLER',but how does it work?
mysql> select salary, (select count(distinct salary) from lc178 where salary >= s.salary) rank from lc178 s order by s.salary desc;
+--------+------+
| salary | rank |
+--------+------+
| 46950 | 1 |
| 46950 | 1 |
| 44789 | 2 |
| 44789 | 2 |
| 44500 | 3 |
| 44500 | 3 |
| 44500 | 3 |
| 41797 | 4 |
| 40000 | 5 |
+--------+------+
9 rows in set (0.00 sec)
explain select salary, (select count(distinct salary) from lc178 where salary >= s.salary) rank from lc178 s order by s.salary desc;
+----+--------------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | PRIMARY | s | ALL | NULL | NULL | NULL | NULL | 9 | Using filesort |
| 2 | DEPENDENT SUBQUERY | lc178 | ALL | NULL | NULL | NULL | NULL | 9 | Using where |
+----+--------------------+-------+------+---------------+------+---------+------+------+----------------+
mysql> create index salary_lc178 on lc178(salary);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select salary, (select count(distinct salary) from lc178 where salary >= s.salary) rank from lc178 s order by s.salary desc;
+--------+------+
| salary | rank |
+--------+------+
| 46950 | 1 |
| 46950 | 0 |
| 44789 | 0 |
| 44789 | 0 |
| 44500 | 0 |
| 44500 | 0 |
| 44500 | 0 |
| 41797 | 0 |
| 40000 | 0 |
+--------+------+
9 rows in set (0.00 sec)
mysql> explain select salary, (select count(distinct salary) from lc178 where salary >= s.salary) rank from lc178 s order by s.salary desc;
+----+--------------------+-------+-------+---------------+--------------+---------+------+------+--------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-------+---------------+--------------+---------+------+------+--------------------------------------------------+
| 1 | PRIMARY | s | index | NULL | salary_lc178 | 5 | NULL | 9 | Using index |
| 2 | DEPENDENT SUBQUERY | lc178 | range | salary_lc178 | salary_lc178 | 5 | NULL | 10 | Using where; Using index for group-by (scanning) |
+----+--------------------+-------+-------+---------------+--------------+---------+------+------+--------------------------------------------------+
table declares and some sample data below:
CREATE TABLE `lc178` (`salary` int(11) DEFAULT NULL);
insert into lc178 values(40000);
insert into lc178 values(44500);
insert into lc178 values(41797);
insert into lc178 values(44500);
insert into lc178 values(44789);
insert into lc178 values(46950);
insert into lc178 values(46950);
insert into lc178 values(44500);
insert into lc178 values(44789);