mysql 执行计划走索引

<pre name="code" class="html">mysql> desc AssignClientManager;
+-----------------+----------+------+-----+---------+----------------+
| Field           | Type     | Null | Key | Default | Extra          |
+-----------------+----------+------+-----+---------+----------------+
| sn              | int(11)  | NO   | PRI | NULL    | auto_increment |
| clientSn        | int(11)  | NO   | MUL | NULL    |                |
| clientManagerSn | int(11)  | NO   |     | NULL    |                |
| status          | char(1)  | NO   |     | 1       |                |
| createdTime     | datetime | YES  |     | NULL    |                |
| updatedTime     | datetime | YES  |     | NULL    |                |
+-----------------+----------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> show index from AssignClientManager;
+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table               | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| AssignClientManager |          0 | PRIMARY  |            1 | sn          | A         |        4934 |     NULL | NULL   |      | BTREE      |         |               |
| AssignClientManager |          1 | ACM_IDX1 |            1 | clientSn    | A         |        4458 |     NULL | NULL   |      | BTREE      |         |               |
+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> explian select sn,clientSn from AssignClientManager;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'explian select sn,clientSn from AssignClientManager' at line 1
mysql> explain select sn,clientSn from AssignClientManager;
+----+-------------+---------------------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| id | select_type | table               | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------------------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | AssignClientManager | NULL       | index | NULL          | ACM_IDX1 | 4       | NULL | 4934 |   100.00 | Using index |
+----+-------------+---------------------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain SELECT COUNT(sn) FROM AssignClientManager where clientSn>5;
+----+-------------+---------------------+------------+-------+---------------+----------+---------+------+------+----------+--------------------------+
| id | select_type | table               | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+---------------------+------------+-------+---------------+----------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | AssignClientManager | NULL       | range | ACM_IDX1      | ACM_IDX1 | 4       | NULL | 4928 |   100.00 | Using where; Using index |
+----+-------------+---------------------+------------+-------+---------------+----------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from AssignClientManager where sn <100 and clientSn <200;
+----+-------------+---------------------+------------+-------+------------------+---------+---------+------+------+----------+-------------+
| id | select_type | table               | partitions | type  | possible_keys    | key     | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------------------+------------+-------+------------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | AssignClientManager | NULL       | range | PRIMARY,ACM_IDX1 | PRIMARY | 4       | NULL |   99 |     4.95 | Using where |
+----+-------------+---------------------+------------+-------+------------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)



   

原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199932.html