tidb
tidb copied to clipboard
Optimize coprocessor SelectIndex
Point Get is much faster than range query on TiKV, we should take index unique info and index type into account, use Point Get if possible.
I played around with this a little bit. If I understand correctly, the FR is to logically transform a range such as range:[2,3] to 2 point gets?
I know for example that MySQL will optimize IN (2,3) into 2 fetches, but not BETWEEN 2 AND 3.
PTAL @zz-jason
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (
id INT NOT NULL PRIMARY KEY auto_increment,
b INT NOT NULL,
UNIQUE (b)
);
INSERT INTO t1 (b) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
mysql> EXPLAIN SELECT * FROM t1 WHERE b = 2;
+-------------+-------+------+-------------------+
| id | count | task | operator info |
+-------------+-------+------+-------------------+
| Point_Get_1 | 1.00 | root | table:t1, index:b |
+-------------+-------+------+-------------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT * FROM t1 WHERE b = 2 or b = 3;
+-------------------+-------+------+----------------------------------------------------------------+
| id | count | task | operator info |
+-------------------+-------+------+----------------------------------------------------------------+
| IndexReader_6 | 0.25 | root | index:IndexScan_5 |
| └─IndexScan_5 | 0.25 | cop | table:t1, index:b, range:[2,3], keep order:false, stats:pseudo |
+-------------------+-------+------+----------------------------------------------------------------+
2 rows in set (0.01 sec)
mysql> EXPLAIN SELECT * FROM t1 WHERE b IN (2,3);
+-------------------+-------+------+-----------------------------------------------------------------------+
| id | count | task | operator info |
+-------------------+-------+------+-----------------------------------------------------------------------+
| IndexReader_6 | 2.00 | root | index:IndexScan_5 |
| └─IndexScan_5 | 2.00 | cop | table:t1, index:b, range:[2,2], [3,3], keep order:false, stats:pseudo |
+-------------------+-------+------+-----------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> EXPLAIN SELECT * FROM t1 WHERE b BETWEEN 2 AND 3;
+-------------------+-------+------+----------------------------------------------------------------+
| id | count | task | operator info |
+-------------------+-------+------+----------------------------------------------------------------+
| IndexReader_6 | 0.25 | root | index:IndexScan_5 |
| └─IndexScan_5 | 0.25 | cop | table:t1, index:b, range:[2,3], keep order:false, stats:pseudo |
+-------------------+-------+------+----------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> EXPLAIN SELECT * FROM t1 WHERE b = 2 UNION SELECT * FROM t1 WHERE b = 3;
+--------------------------+-------+------+----------------------------------------------------------------+
| id | count | task | operator info |
+--------------------------+-------+------+----------------------------------------------------------------+
| HashAgg_13 | 2.00 | root | group by:b, id, funcs:firstrow(id), firstrow(b) |
| └─Union_14 | 2.00 | root | |
| ├─IndexReader_17 | 1.00 | root | index:IndexScan_16 |
| │ └─IndexScan_16 | 1.00 | cop | table:t1, index:b, range:[2,2], keep order:false, stats:pseudo |
| └─IndexReader_20 | 1.00 | root | index:IndexScan_19 |
| └─IndexScan_19 | 1.00 | cop | table:t1, index:b, range:[3,3], keep order:false, stats:pseudo |
+--------------------------+-------+------+----------------------------------------------------------------+
6 rows in set (0.00 sec)
I guess this FR is to logically transform ranges like [2,2] to point get plan, like your first example. This have been finished and should be closed IMH, @coocood please confirm.