mysql 复合索引

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  * 所有的列,根据计算全表扫描成本低的话,会使用全表扫描 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值