mysql 的复合索引,如何查询语句用到第二个列,没有使用首个字段,是否会用到索引?
create index idx_01 on job_log(job_id,job_desc,executor_address) ;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
explain select job_id from job_log where job_desc like 'cnj%'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: job_log
partitions: p0,p1,p20250227,p20250228,p20250301,p20250302,p20250303,p20250304,p20250305,p20250306,p20250307,p20250308,p20250309,p20250310,p20250311,p20250312,p20250313,p20250314,p20250315,p20250316,p20250317,p20250318,p20250319,p20250320,p20250321,p20250322,p20250323,p20250324,p20250325,p20250326,p20250327,p20250328,p20250329,p20250330,p20250331,p20250401
type: index
possible_keys: idx_01
key: idx_01
key_len: 2050
ref: NULL
rows: 404
filtered: 11.11
Extra: Using where; Using index
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
mysql> show index from job_log;
+---------+------------+----------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+---------+------------+----------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| job_log | 0 | PRIMARY | 1 | id | A | 404 | NULL | NULL | | BTREE | | | YES | NULL |
| job_log | 0 | PRIMARY | 2 | trigger_time | A | 404 | NULL | NULL | | BTREE | | | YES | NULL |
| job_log | 1 | I_trigger_time | 1 | trigger_time | A | 132 | NULL | NULL | | BTREE | | | YES | NULL |
| job_log | 1 | I_handle_code | 1 | handle_code | A | 2 | NULL | NULL | | BTREE | | | YES | NULL |
| job_log | 1 | idx_01 | 1 | job_id | A | 34 | NULL | NULL | | BTREE | | | YES | NULL |
| job_log | 1 | idx_01 | 2 | job_desc | A | 34 | NULL | NULL | YES | BTREE | | | YES | NULL |
| job_log | 1 | idx_01 | 3 | executor_address | A | 34 | NULL | NULL | YES | BTREE | | | YES | NULL |
+---------+------------+----------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
7 rows in set (0.01 sec)
mysql> explain select job_DESC from job_log where job_desc like 't01%' AND executor_address like 'A%'\G;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 10439
Current database: dataxweb
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: job_log
partitions: p0,p1,p20250227,p20250228,p20250301,p20250302,p20250303,p20250304,p20250305,p20250306,p20250307,p20250308,p20250309,p20250310,p20250311,p20250312,p20250313,p20250314,p20250315,p20250316,p20250317,p20250318,p20250319,p20250320,p20250321,p20250322,p20250323,p20250324,p20250325,p20250326,p20250327,p20250328,p20250329,p20250330,p20250331,p20250401
type: index
possible_keys: idx_01
key: idx_01
key_len: 2050
ref: NULL
rows: 404
filtered: 1.23
Extra: Using where; Using index
1 row in set, 1 warning (0.03 sec)
ERROR:
No query specified
mysql> explain select * from job_log where job_desc like 't01%' AND executor_address like 'A%'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: job_log
partitions: p0,p1,p20250227,p20250228,p20250301,p20250302,p20250303,p20250304,p20250305,p20250306,p20250307,p20250308,p20250309,p20250310,p20250311,p20250312,p20250313,p20250314,p20250315,p20250316,p20250317,p20250318,p20250319,p20250320,p20250321,p20250322,p20250323,p20250324,p20250325,p20250326,p20250327,p20250328,p20250329,p20250330,p20250331,p20250401
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 404
filtered: 1.23
Extra: Using where
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
通过explain
输出的type这一列可以看出,复合索引使用第二个列进行查询是可以用到到该索引的,如果select * 所有的列,根据计算全表扫描成本低的话,会使用全表扫描 。