MySQL does not always make a right decision about indexes
usage.
Condsider a simple table:
PLAIN TEXT SQL:
- CREATE TABLE `t2` (
- `ID` int(11) DEFAULT NULL,
- `ID1` int(11) DEFAULT NULL,
- `SUBNAME` varchar(32) DEFAULT NULL,
- KEY `ID1` (`ID1`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1
SELECT COUNT(*) FROM t2;
250001 (V1)
SELECT COUNT(*) FROM t2 WHERE ID1=1;
83036 (V2)
(execution time = 110 ms)
That is index selectivity by condition (ID1=1) is V2/V1 = 0.3321 or 33.21%
It is said (e.g. book "SQL
Tuning") if selectivity over 20% then a full table scan is
preferable than an index access.
As far as I know Oracle alway chooses a full table scan if
selectivity over 25%.
…
[Read more]