好的,下面是一个包含联合索引 (A, B, C) 的建表语句和示例数据,以及如何使用 EXPLAIN 查看查询执行计划的例子。

建表语句

CREATE TABLE my_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    A INT NOT NULL,
    B INT NOT NULL,
    C INT NOT NULL,
    data VARCHAR(100),
    INDEX idx_abc (A, B, C)
);

插入示例数据

INSERT INTO my_table (A, B, C, data) VALUES
(1, 1, 10, 'data1'),
(1, 2, 20, 'data2'),
(2, 1, 30, 'data3'),
(2, 2, 40, 'data4'),
(3, 1, 50, 'data5'),
(3, 2, 60, 'data6');

查询语句和执行计划

假设我们要执行以下查询:

SELECT * FROM my_table WHERE A = 1 AND C < 20;

使用 EXPLAIN 查看执行计划:

EXPLAIN SELECT * FROM my_table WHERE A = 1 AND C < 20;

解释执行计划

执行上述 EXPLAIN 查询后,结果可能会类似于以下内容:

+----+-------------+----------+------------+-------+---------------+--------+---------+------+------+----------+--------------------------+
| id | select_type | table    | partitions | type  | possible_keys | key    | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+----------+------------+-------+---------------+--------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | my_table | NULL       | range | idx_abc       | idx_abc| 5       | NULL |    2 |   100.00 | Using index condition; Using where |
+----+-------------+----------+------------+-------+---------------+--------+---------+------+------+----------+--------------------------+

解读执行计划