表中,有一个日期字段WorkDate(Date YYYY-MM-DD格式),现在我把它建成了索引,在检索条件时,WorkDate='YYYY-MM-DD' 时,用EXPLAIN分析,能看到使用了索引,但是当我想按月查询时,
写成 WorkDate like 'YYYY-MM%'
或者 SUBSTR(WorkDate,1,7) = 'YYYY-MM'
或者 DATE_FORMAT(WorkDate,'%Y%m') = 'YYYYMM'
用EXPLAIN分析的结果是都没有使用索引。。现在我想实现按月查询也能使用上索引该怎么办呀。。高手指点!!
写成 WorkDate like 'YYYY-MM%'
或者 SUBSTR(WorkDate,1,7) = 'YYYY-MM'
或者 DATE_FORMAT(WorkDate,'%Y%m') = 'YYYYMM'
用EXPLAIN分析的结果是都没有使用索引。。现在我想实现按月查询也能使用上索引该怎么办呀。。高手指点!!
这个方法我也试过了,还是不走索引,EXPLAIN分析的结果是Type是All, possible_key 是WorkDate 。
show INDEX FROM TAppointMst
table Non_unique Key_name Seq_in_index Column_name collation Cardinality NULL Index_type
TAppointMst 0 PRIMARY 1 AppId A 84568 BTREE
TAppointMst 1 AcceptId 1 AcceptId A 84568 YES BTREE
TAppointMst 1 DayRootId 1 DayRootId A 28189 YES BTREE
TAppointMst 1 AppDate 1 AppDate A 355 YES BTREE
TAppointMst 1 WorkUserId 1 WorkUserId A 93 YES BTREE
TAppointMst 1 BookId 1 BookId A 84568 YES BTREEEXPLAIN
SELECT * from
TAppointMst
where
AppDate BETWEEN '2012-01-01' and '2012-01-31'
id select_type table type possible_keys rows Extra
1 SIMPLE TAppointMst ALL AppDate 85315 Using whereAppDate就是我说的WorkDate
望大家指点。。
TAppointMst
where
AppDate BETWEEN '2012-01-01' and '2012-01-31'
这个查出来有多少记录,如果占了表中所有数据的大部分,全表扫描效率更高,Mysql优化器会自动选择不走索引
莫非我的索引有问题??下面是建表SQl文。。CREATE TABLE `TAppointMst` (
`AppId` int(11) NOT NULL AUTO_INCREMENT,
`AcceptId` int(11) DEFAULT NULL,
`DayRootId` int(11) DEFAULT NULL,
`BookId` int(11) DEFAULT '0',
`WorkUserId` int(11) DEFAULT '0',
`AppDate` date DEFAULT NULL,
`FromTime` varchar(4) DEFAULT NULL,
`ToTime` varchar(4) DEFAULT NULL,
`FreeTime` int(11) DEFAULT '0',
`DisPlayIndex` int(11) DEFAULT NULL,
`DisPlayEndIndex` int(11) DEFAULT NULL,
`DisPlayLineSpan` varchar(50) DEFAULT NULL,
`MemberCnt` int(11) DEFAULT '0',
`CarType` int(1) DEFAULT '0',
`AppIntentionId` int(3) DEFAULT '0',
`AppResutId` int(3) DEFAULT '0',
`Sales` decimal(10,0) DEFAULT NULL,
`CardSales` decimal(10,0) DEFAULT NULL,
`FirstMenoy` decimal(10,0) DEFAULT NULL,
`LeavMenoy` decimal(10,0) DEFAULT NULL,
`LeavMenoyDate` date DEFAULT NULL,
`Comment` text,
`Comment2` text,
`Comment3` text,
`ReportUser` text,
`OverHour` varchar(2) DEFAULT NULL,
`OverMinute` varchar(2) DEFAULT NULL,
`Status` int(1) DEFAULT '0',
`HelpAppId` int(11) DEFAULT '0',
`ConfirmUserId` int(11) DEFAULT NULL,
`ParentAppId` int(11) DEFAULT '0',
`ChildAppId` int(11) DEFAULT '0',
`ParentPlanDate` date DEFAULT NULL,
`ChildPlanDate` date DEFAULT NULL,
`Yobi1` int(11) DEFAULT '0',
`Yobi2` int(11) DEFAULT '0',
`Yobi3` int(11) DEFAULT '0',
`Yobi4` int(11) DEFAULT '0',
`Yobi5` int(11) DEFAULT '0',
`Yobi6` varchar(50) DEFAULT NULL,
`Yobi7` varchar(50) DEFAULT NULL,
`Yobi8` varchar(50) DEFAULT NULL,
`Yobi9` varchar(50) DEFAULT NULL,
`Yobi10` varchar(50) DEFAULT NULL,
`Yobi11` date DEFAULT NULL,
`Yobi12` date DEFAULT NULL,
`Yobi13` datetime DEFAULT NULL,
`InsertDate` datetime NOT NULL,
`SakuseiSyaId` int(11) NOT NULL,
`UpdateDate` datetime NOT NULL,
`UpdateUserId` int(11) NOT NULL,
PRIMARY KEY (`AppId`)
) ENGINE=InnoDB AUTO_INCREMENT=308 DEFAULT CHARSET=utf8;
`AppId` int(11) NOT NULL AUTO_INCREMENT,
`AcceptId` int(11) DEFAULT NULL,
`DayRootId` int(11) DEFAULT NULL,
`BookId` int(11) DEFAULT '0',
`WorkUserId` int(11) DEFAULT '0',
`AppDate` date DEFAULT NULL,
`FromTime` varchar(4) DEFAULT NULL,
`ToTime` varchar(4) DEFAULT NULL,
`FreeTime` int(11) DEFAULT '0',
`DisPlayIndex` int(11) DEFAULT NULL,
`DisPlayEndIndex` int(11) DEFAULT NULL,
`DisPlayLineSpan` varchar(50) DEFAULT NULL,
`MemberCnt` int(11) DEFAULT '0',
`CarType` int(1) DEFAULT '0',
`AppIntentionId` int(3) DEFAULT '0',
`AppResutId` int(3) DEFAULT '0',
`Sales` decimal(10,0) DEFAULT NULL,
`CardSales` decimal(10,0) DEFAULT NULL,
`FirstMenoy` decimal(10,0) DEFAULT NULL,
`LeavMenoy` decimal(10,0) DEFAULT NULL,
`LeavMenoyDate` date DEFAULT NULL,
`Comment` text,
`Comment2` text,
`Comment3` text,
`ReportUser` text,
`OverHour` varchar(2) DEFAULT NULL,
`OverMinute` varchar(2) DEFAULT NULL,
`Status` int(1) DEFAULT '0',
`HelpAppId` int(11) DEFAULT '0',
`ConfirmUserId` int(11) DEFAULT NULL,
`ParentAppId` int(11) DEFAULT '0',
`ChildAppId` int(11) DEFAULT '0',
`ParentPlanDate` date DEFAULT NULL,
`ChildPlanDate` date DEFAULT NULL,
`Yobi1` int(11) DEFAULT '0',
`Yobi2` int(11) DEFAULT '0',
`Yobi3` int(11) DEFAULT '0',
`Yobi4` int(11) DEFAULT '0',
`Yobi5` int(11) DEFAULT '0',
`Yobi6` varchar(50) DEFAULT NULL,
`Yobi7` varchar(50) DEFAULT NULL,
`Yobi8` varchar(50) DEFAULT NULL,
`Yobi9` varchar(50) DEFAULT NULL,
`Yobi10` varchar(50) DEFAULT NULL,
`Yobi11` date DEFAULT NULL,
`Yobi12` date DEFAULT NULL,
`Yobi13` datetime DEFAULT NULL,
`InsertDate` datetime NOT NULL,
`SakuseiSyaId` int(11) NOT NULL,
`UpdateDate` datetime NOT NULL,
`UpdateUserId` int(11) NOT NULL,
PRIMARY KEY (`AppId`),
KEY `AcceptId` (`AcceptId`),
KEY `DayRootId` (`DayRootId`),
KEY `AppDate` (`AppDate`),
KEY `WorkUserId` (`WorkUserId`),
KEY `BookId` (`BookId`)
) ENGINE=InnoDB AUTO_INCREMENT=84191 DEFAULT CHARSET=utf8;
SELECT * FROM table USE INDEX(index_name) WHERE ...
具体资料可以网上查查
或者 SUBSTR(WorkDate,1,7) = 'YYYY-MM'
或者 DATE_FORMAT(WorkDate,'%Y%m') = 'YYYYMM' sql语句中出现like字段后 不走索引;
=左边不能出现表达式,否则也不走索引
详细情况请看http://www.cnblogs.com/luxf/archive/2012/02/08/2343345.html