Here is the table definition
CREATE TABLE `dt_prdtime` (
`TCompany` varchar(3) NOT NULL DEFAULT '',
`TPerCode` varchar(8) NOT NULL,
`TBegDateTime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '??????',
`TQPay` int(1) NOT NULL DEFAULT '2',
`TYear` int(4) NOT NULL,
`TMonth` int(2) NOT NULL,
PRIMARY KEY (`TCompany`,`TPerCode`,`TBegDateTime`),
KEY `TMonth` (`TMonth`) USING BTREE,
KEY `TPerCode` (`TPerCode`,`TYear`,`TMonth`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
And this is data sample. This table has 10000+ records and value in TMonth
field varies
+----------+----------+---------------------+-------+-------+--------+
| TCompany | TPerCode | TBegDateTime | TQPay | TYear | TMonth |
+----------+----------+---------------------+-------+-------+--------+
| S10 | 000001 | 2016-01-02 17:33:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-02 07:48:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-03 17:39:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-03 07:30:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-04 17:49:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-04 07:54:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-05 17:50:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-05 07:36:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-06 17:37:00 | 1 | 2016 | 1 |
| S10 | 000001 | 2016-01-06 07:35:00 | 1 | 2016 | 1 |
+----------+----------+---------------------+-------+-------+--------+
With EXPLAIN
, This query uses TMonth
index:
SELECT * FROM dt_prdtime WHERE TMonth = 5
while this one refuses to use the index:
SELECT * FROM dt_prdtime WHERE TMonth IN (5,6)
I tested with another simple table,
CREATE TABLE `table1` (
`id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
SELECT * FROM table2 WHERE id IN (5,6)
and the index for this table was used
Can anybody explain this? Is there something wrong with dt_prdtime
table?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…