2017-04-05 91 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也许洒在主体的光线报价:

在数据库中搜索索引是象印刷电话 目录搜索。关键概念是所有条目都按照明确定义的顺序排列。在有序数据集中查找数据非常简单,因为排序顺序决定了每个条目的位置。

你明白了吗?空值/空值超出范围,因此引擎必须对表进行全面扫描。

如果这将是一个多模型数据库引擎,它可能(!),至少,集群空闲在一起,从而避免获取这些字段 - 但行模型让你没有机会。

相关问题