0
我們試圖根據某些條件檢索數據,但問題在於給列指定索引。 已經有一些索引,但沒有被使用。如何在索引中選擇正確的列以獲得更好的性能
查詢:
select `UK_Generic_Demand_Details`.`genericDemandId` AS `genericDemandId`,
`UK_Generic_Demand_Details`.`customerReference` AS `customerReference`,
`UK_Generic_Demand_Details`.`policyNumber` AS `policyNumber`,
`UK_Generic_Demand_Details`.`demandTypeId` AS `demandTypeId`,
`UK_Generic_Demand_Details`.`initiator` AS `initiator`,
`UK_Generic_Demand_Details`.`demandCreatedDateTime` AS `demandCreatedDateTime`,
`UK_Generic_Demand_Details`.`demandEndDateTime` AS `demandEndDateTime`,
`UK_Generic_Demand_Details`.`startDateTime` AS `startDateTime`,
`UK_Generic_Demand_Details`.`endDateTime` AS `endDateTime`,
`UK_Generic_Demand_Details`.`status` AS `status`,
`UK_Generic_Demand_Details`.`isUrgent` AS `isUrgent`,
`UK_Generic_Demand_Details`.`modifiedCreatedDateTime` AS `modifiedCreatedDateTime`,
`UK_Generic_Demand_Details`.`demandSource` AS `demandSource`,
`UK_Generic_Demand_Details`.`id` AS `Id`,
`UK_Generic_Demand_Details`.`schemeReference` AS `schemeReference`,
`UK_Generic_Demand_Details`.`categoryReference` AS `categoryReference`,
`UK_Generic_Demand_Details`.`aggregationKey` AS `aggregationKey`
from `UK_Generic_Demand_Details`
where (((`UK_Generic_Demand_Details`.`customerReference` is not null)
and (`UK_Generic_Demand_Details`.`customerReference` <> '')
and (`UK_Generic_Demand_Details`.`policyNumber` is not null)
and (`UK_Generic_Demand_Details`.`policyNumber` <> '')
and (`UK_Generic_Demand_Details`.`genericDemandId` is not null))
or ((`UK_Generic_Demand_Details`.`schemeReference` is not null)
and (`UK_Generic_Demand_Details`.`schemeReference` <> '')
and (`UK_Generic_Demand_Details`.`genericDemandId` is not null)))
所存在的指標是:
PRIMARY KEY (`id`),
KEY `IX7_UK_Generic_Demand_Details` (`genericDemandId`),
KEY `IX8_UK_Generic_Demand_Details` (`initiator`,`status`) USING BTREE,
KEY `IX9_UK_Generic_Demand_Details` (`aggregationKey`,`status`,`genericDemandId`) USING BTREE,
KEY `IX10_UK_Generic_Demand_Details` (`categoryReference`),
KEY `IX11_UK_Generic_Demand_Details` (`aggregationKey`),
KEY `IX1_UK_Generic_Demand_Details` (`status`,`aggregationKey`) USING BTREE,
KEY `IX2_UK_Generic_Demand_Details` (`customerReference`),
KEY `IX4_UK_Generic_Demand_Details` (`schemeReference`) USING BTREE,
KEY `IX12_UK_Generic_Demand_Details` (`demandTypeId`,`contractSystem`,`productGroup`,`isBusinessException`) USING BTREE,
KEY `IX3_UK_Generic_Demand_Details` (`policyNumber`,`demandTypeId`,`status`) USING BTREE,
KEY `IX5_UK_Generic_Demand_Details` (`demandTypeId`,`demandId`) USING BTREE
查詢的解釋是:
有人請建議我在索引正確的列獲得更好的表現。
完整的表結構是:
CREATE TABLE `UK_Generic_Demand_Details` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`policyNumber` varchar(255) DEFAULT NULL,
`customerReference` varchar(255) DEFAULT NULL,
`demandTypeId` int(11) DEFAULT NULL,
`demandId` int(11) DEFAULT NULL,
`initiator` varchar(255) DEFAULT NULL,
`startDateTime` datetime DEFAULT NULL,
`endDateTime` datetime DEFAULT NULL,
`isActive` tinyint(1) DEFAULT NULL,
`demandCreatedDateTime` datetime DEFAULT NULL,
`demandEndDateTime` datetime DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`documentUniqueReference` varchar(20) DEFAULT NULL,
`isUrgent` bit(1) DEFAULT NULL,
`modifiedCreatedDateTime` datetime DEFAULT NULL,
`demandSource` varchar(20) DEFAULT NULL,
`genericDemandId` int(11) DEFAULT NULL,
`schemeReference` varchar(8) NOT NULL,
`categoryReference` varchar(8) DEFAULT NULL,
`aggregationKey` varchar(255) DEFAULT NULL,
`contractSystem` varchar(50) NOT NULL,
`productGroup` varchar(50) NOT NULL,
`isBusinessException` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IX7_UK_Generic_Demand_Details` (`genericDemandId`),
KEY `IX8_UK_Generic_Demand_Details` (`initiator`,`status`) USING BTREE,
KEY `IX9_UK_Generic_Demand_Details` (`aggregationKey`,`status`,`genericDemandId`) USING BTREE,
KEY `IX10_UK_Generic_Demand_Details` (`categoryReference`),
KEY `IX11_UK_Generic_Demand_Details` (`aggregationKey`),
KEY `IX1_UK_Generic_Demand_Details` (`status`,`aggregationKey`) USING BTREE,
KEY `IX2_UK_Generic_Demand_Details` (`customerReference`),
KEY `IX4_UK_Generic_Demand_Details` (`schemeReference`) USING BTREE,
KEY `IX12_UK_Generic_Demand_Details` (`demandTypeId`,`contractSystem`,`productGroup`,`isBusinessException`) USING BTREE,
KEY `IX3_UK_Generic_Demand_Details` (`policyNumber`,`demandTypeId`,`status`) USING BTREE,
KEY `IX5_UK_Generic_Demand_Details` (`demandTypeId`,`demandId`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=35350 DEFAULT CHARSET=utf8
或者如果我的查詢中有任何錯誤需要更改,請告訴我。 – user7761587
請包括表定義 – bc004346
此外,'KEY'與'INDEX'不同,因此需要包含**所有索引定義的表定義**以提出任何建議 – bc004346