1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
| CREATE TABLE `table_test` (
`FId` bigint(20) NOT NULL AUTO_INCREMENT,
`FName` varchar(128) DEFAULT NULL,
`FStatus` int(10) UNSIGNED DEFAULT NULL,
`FPreviewCondition` int(10) UNSIGNED DEFAULT NULL,
`FLastModTime` bigint(20) DEFAULT NULL,
PRIMARY KEY (`FId`)
) ENGINE=InnoDB AUTO_INCREMENT=10000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test1', 1, 0, 1633737600);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test2', 2, 1, 1633737660);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test3', 3, 2, 1633737720);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test4', 4, 0, 1633737780);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test5', 5, 1, 1633737840);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test6', 6, 2, 1633737900);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test7', 7, 0, 1633737960);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test8', 8, 1, 1633738020);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test9', 9, 2, 1633738080);
INSERT INTO `table_test` (`FName`, `FStatus`, `FPreviewCondition`, `FLastModTime`) VALUES ('test10', 10, 0, 1633738140);
mysql> explain select FId,FName from table_test WHERE FStatus = 1 AND FPreviewCondition = 1 AND FLastModTime >= 1695794400 LIMIT 0,10;
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | table_test | NULL | ALL | NULL | NULL | NULL | NULL | 10 | 10.00 | Using where |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
查看并添加索引
mysql> SHOW INDEX FROM table_test;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| table_test | 0 | PRIMARY | 1 | FId | A | 10 | NULL | NULL | | BTREE | | | YES | NULL |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.01 sec)
CREATE INDEX idx_FLastModTime ON table_test (FLastModTime);
mysql> SHOW INDEX FROM table_test;
+------------+------------+------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+------------+------------+------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| table_test | 0 | PRIMARY | 1 | FId | A | 10 | NULL | NULL | | BTREE | | | YES | NULL |
| table_test | 1 | idx_FLastModTime | 1 | FLastModTime | A | 10 | NULL | NULL | YES | BTREE | | | YES | NULL |
+------------+------------+------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.00 sec)
重新检查索引使用条件
mysql> explain select FId,FName from table_test WHERE FStatus = 1 AND FPreviewCondition = 1 AND FLastModTime >= 1695794400 LIMIT 0,10;
+----+-------------+------------+------------+-------+------------------+------------------+---------+------+------+----------+------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+------------------+------------------+---------+------+------+----------+------------------------------------+
| 1 | SIMPLE | table_test | NULL | range | idx_FLastModTime | idx_FLastModTime | 9 | NULL | 1 | 10.00 | Using index condition; Using where |
+----+-------------+------------+------------+-------+------------------+------------------+---------+------+------+----------+------------------------------------+
1 row in set, 1 warning (0.00 sec)
|