2017-04-05 56 views
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 

查詢的解釋是: enter image description here

有人請建議我在索引正確的列獲得更好的表現。

完整的表結構是:

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 
+0

或者如果我的查詢中有任何錯誤需要更改,請告訴我。 – user7761587

+0

請包括表定義 – bc004346

+0

此外,'KEY'與'INDEX'不同,因此需要包含**所有索引定義的表定義**以提出任何建議 – bc004346

回答

0

指數不會幫你的那種類型的過濾條件。

讓我introdice也許灑在主體的光線報價:

在數據庫中搜索索引是象印刷電話 目錄搜索。關鍵概念是所有條目都按照明確定義的順序排列。在有序數據集中查找數據非常簡單,因爲排序順序決定了每個條目的位置。

你明白了嗎?空值/空值超出範圍,因此引擎必須對錶進行全面掃描。

如果這將是一個多模型數據庫引擎,它可能(!),至少,集羣空閒在一起,從而避免獲取這些字段 - 但行模型讓你沒有機會。

相關問題