pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.2k stars 5.84k forks source link

Selectivity() can't correctly recognize an index can cover which expressions #23456

Open time-and-fate opened 3 years ago

time-and-fate commented 3 years ago

Development Task

CREATE TABLE `t` (
  `a` int(11) DEFAULT NULL,
  `b` int(11) DEFAULT NULL,
  `c` int(11) DEFAULT NULL,
  KEY `iab` (`a`,`b`),
  KEY `ic` (`c`)
);

Add a breakpoint in getMaskAndRanges() and run this SQL:

select * from t where a >= 2 and a <= 2 and b >= 20 and b <= 20 and c > 10;

When handling index iab, you'll find the mask is 0, which means this index doesn't cover any expressions. However, this is wrong, in fact, index iab covers the first 4 expressions. Eventually, this makes GetUsableSetsByGreedy() chooses statistics of column a, column b, and index ic to calculate selectivity, which should have been index iab and index ic by design.

time-and-fate commented 3 years ago

/sig planner

time-and-fate commented 3 years ago

/assign time-and-fate