我认为 TiDB 和 MySQL 在这里有相同的行为。如果您将 VARCHAR 列与字符串进行比较,它会将其作为字符串进行比较,并将使用索引。将其与整数进行比较时,它将尝试将每一行的值转换为整数,然后进行比较。然后它不能使用索引。请注意,您需要在表中添加几行以获得更现实的解释计划。另请注意,根据类型,结果可能会有所不同。
mysql 8.0.22 > CREATE TABLE Employee (
-> ID BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> EmpID VARCHAR(200) NOT NULL
-> );
Query OK, 0 rows affected (0.14 sec)
mysql 8.0.22 > CREATE INDEX idx_EmpID ON Employee(EmpID);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql 8.0.22 > EXPLAIN SELECT * FROM Employee WHERE EmpID= 123;
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | Employee | NULL | index | idx_EmpID | idx_EmpID | 802 | NULL | 1 | 100.00 | Using where; Using index |
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
1 row in set, 3 warnings (0.00 sec)
mysql 8.0.22 > EXPLAIN SELECT * FROM Employee WHERE EmpID= '123';
+----+-------------+----------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | Employee | NULL | ref | idx_EmpID | idx_EmpID | 802 | const | 1 | 100.00 | Using index |
+----+-------------+----------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
mysql 8.0.22 > INSERT INTO Employee(EmpID) VALUES ('010'),('10');
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql 8.0.22 > SELECT * FROM Employee;
+----+-------+
| ID | EmpID |
+----+-------+
| 1 | 010 |
| 2 | 10 |
+----+-------+
2 rows in set (0.00 sec)
mysql 8.0.22 > SELECT * FROM Employee WHERE EmpID='10';
+----+-------+
| ID | EmpID |
+----+-------+
| 2 | 10 |
+----+-------+
1 row in set (0.00 sec)
mysql 8.0.22 > SELECT * FROM Employee WHERE EmpID=10;
+----+-------+
| ID | EmpID |
+----+-------+
| 1 | 010 |
| 2 | 10 |
+----+-------+
2 rows in set (0.00 sec)
tidb 5.7.25-TiDB-v5.0.0 > CREATE TABLE Employee (
-> ID BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> EmpID VARCHAR(200) NOT NULL
-> );
Query OK, 0 rows affected (0.15 sec)
tidb 5.7.25-TiDB-v5.0.0 > CREATE INDEX idx_EmpID ON Employee(EmpID);
Query OK, 0 rows affected (2.87 sec)
tidb 5.7.25-TiDB-v5.0.0 > EXPLAIN SELECT * FROM Employee WHERE EmpID= 123;
+-------------------------+----------+-----------+----------------+------------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------+----------+-----------+----------------+------------------------------------+
| TableReader_7 | 8000.00 | root | | data:Selection_6 |
| └─Selection_6 | 8000.00 | cop[tikv] | | eq(cast(test.employee.empid), 123) |
| └─TableFullScan_5 | 10000.00 | cop[tikv] | table:Employee | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+----------------+------------------------------------+
3 rows in set (0.01 sec)
tidb 5.7.25-TiDB-v5.0.0 > EXPLAIN SELECT * FROM Employee WHERE EmpID= '123';
+------------------------+---------+-----------+----------------------------------------+-----------------------------------------------------+
| id | estRows | task | access object | operator info |
+------------------------+---------+-----------+----------------------------------------+-----------------------------------------------------+
| IndexReader_6 | 10.00 | root | | index:IndexRangeScan_5 |
| └─IndexRangeScan_5 | 10.00 | cop[tikv] | table:Employee, index:idx_EmpID(EmpID) | range:["123","123"], keep order:false, stats:pseudo |
+------------------------+---------+-----------+----------------------------------------+-----------------------------------------------------+
2 rows in set (0.00 sec)
tidb 5.7.25-TiDB-v5.0.0 > INSERT INTO Employee(EmpID) VALUES ('010'),('10');
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
tidb 5.7.25-TiDB-v5.0.0 > SELECT * FROM Employee;
+----+-------+
| ID | EmpID |
+----+-------+
| 1 | 010 |
| 2 | 10 |
+----+-------+
2 rows in set (0.00 sec)
tidb 5.7.25-TiDB-v5.0.0 > SELECT * FROM Employee WHERE EmpID='10';
+----+-------+
| ID | EmpID |
+----+-------+
| 2 | 10 |
+----+-------+
1 row in set (0.00 sec)
tidb 5.7.25-TiDB-v5.0.0 > SELECT * FROM Employee WHERE EmpID=10;
+----+-------+
| ID | EmpID |
+----+-------+
| 1 | 010 |
| 2 | 10 |
+----+-------+
2 rows in set (0.00 sec)