
MySQL
索引状况: show index in demo; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+ | demo | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+
查询计划: explAIn select * from demo where name like '李%'; +----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+ | 1 | SIMPLE | demo | ALL | NULL | NULL | NULL | NULL | 4 | Using where | +----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+ 注意到没有使用任何索引.

AI
现在的索引状况: show index in demo; +-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | demo | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | | | demo | 1 | name_index_demo | 1 | name | A | NULL | NULL | NULL | YES | BTREE | | +-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
新的查询的查询计划: explAIn select * from demo where name like '李%'; +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+ | 1 | SIMPLE | demo | range | name_index_demo | name_index_demo | 13 | NULL | 1 | Using where | +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+ 1 row in set (0.03 sec)
注意到查询已经使用了我们创建的索引.
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号