考虑一下这个表:
CREATE TABLE `Alarms` (
`AlarmId` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`DeviceId` BINARY(16) NOT NULL,
`Code` BIGINT(20) UNSIGNED NOT NULL,
`Ended` TINYINT(1) NOT NULL DEFAULT '0',
`NaturalEnd` TINYINT(1) NOT NULL DEFAULT '0',
`Pinned` TINYINT(1) NOT NULL DEFAULT '0',
`Acknowledged` TINYINT(1) NOT NULL DEFAULT '0',
`StartedAt` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`EndedAt` TIMESTAMP NULL DEFAULT NULL,
`MarkedForDeletion` TINYINT(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`AlarmId`),
KEY `Key1` (`Ended`,`Acknowledged`),
KEY `Key2` (`Pinned`),
KEY `Key3` (`DeviceId`,`Pinned`),
KEY `Key4` (`DeviceId`,`StartedAt`,`EndedAt`),
KEY `Key5` (`DeviceId`,`Ended`,`EndedAt`),
KEY `Key6` (`MarkedForDeletion`)
) ENGINE=INNODB;
而且,对于此测试,请将其填充如下:
-- Populate some dummy data; 500 alarms for each
-- of 1000 one-second periods
SET @testDevice = UNHEX('00030000000000000000000000000000');
DROP PROCEDURE IF EXISTS `injectAlarms`;
DELIMITER ;;
CREATE PROCEDURE injectAlarms()
BEGIN
SET @fromdate = '2018-02-18 00:00:00';
SET @numdates = 1000;
SET @todate = DATE_ADD(@fromdate, INTERVAL @numdates SECOND);
-- Create table of alarm codes to join on
DROP TABLE IF EXISTS `__codes`;
CREATE TEMPORARY TABLE `__codes` (
`Code` BIGINT NOT NULL PRIMARY KEY
);
SET @startcode = 0;
SET @endcode = 499;
REPEAT
INSERT INTO `__codes` VALUES(@startcode);
SET @startcode = @startcode + 1;
UNTIL @startcode > @endcode END REPEAT;
-- Add an alarm for each code, for each second in range
REPEAT
INSERT INTO `Alarms`
(`DeviceId`, `Code`, `Ended`, `NaturalEnd`, `Pinned`, `Acknowledged`, `StartedAt`, `EndedAt`)
SELECT
@testDevice,
`Code`,
TRUE, FALSE, FALSE, FALSE,
@fromdate, @fromdate
FROM `__codes`;
SET @fromdate = DATE_ADD(@fromdate, INTERVAL 1 SECOND);
UNTIL @fromdate > @todate END REPEAT;
END;;
DELIMITER ;
CALL injectAlarms();
现在,对于某些数据集,以下查询非常有效:
SELECT * FROM `Alarms`
WHERE
((`Alarms`.`Ended` = FALSE AND `Alarms`.`Acknowledged` = FALSE) OR `Alarms`.`Pinned` = TRUE) AND
`MarkedForDeletion` = FALSE AND
`DeviceId` = @testDevice
;
这是因为MariaDB足够聪明,可以使用索引合并,例如:
id select_type table type possible_keys
1 SIMPLE Alarms index_merge Key1,Key2,Key3,Key4,Key5,Key6
key key_len ref rows Extra
Key1,Key2,Key3 2,1,17 (NULL) 2 Using union(Key1,intersect(Key2,Key3)); Using where
但是,如果我使用上面的过程填充的数据集,并翻转查询(这是我需要的另一个视图,但在这种情况下将返回更多行):
SELECT * FROM `Alarms`
WHERE
((`Alarms`.`Ended` = TRUE OR `Alarms`.`Acknowledged` = TRUE) AND `Alarms`.`Pinned` = FALSE) AND
`MarkedForDeletion` = FALSE AND
`DeviceId` = @testDevice
;
……它没有:
id select_type table type possible_keys
1 SIMPLE Alarms ref Key1,Key2,Key3,Key4,Key5,Key6
key key_len ref rows Extra
Key2 1 const 144706 Using where
我宁愿让索引合并更频繁地发生.实际上,给定ref = const,这个查询计划看起来并不太可怕……但是,查询需要几秒钟才能运行.这本身并不是世界末日,但是我的设计表现不佳,在尝试更具异国情调的查询时显示,这需要很长时间:
-- Create a temporary table that we'll join against in a mo
DROP TABLE IF EXISTS `_ranges`;
CREATE TEMPORARY TABLE `_ranges` (
`Start` TIMESTAMP NOT NULL DEFAULT 0,
`End` TIMESTAMP NOT NULL DEFAULT 0,
PRIMARY KEY(`Start`, `End`)
);
-- Populate it (in reality this is performed by my application layer)
SET @endtime = 1518992216;
SET @starttime = @endtime - 86400;
SET @inter = 900;
DROP PROCEDURE IF EXISTS `populateRanges`;
DELIMITER ;;
CREATE PROCEDURE populateRanges()
BEGIN
REPEAT
INSERT IGNORE INTO `_ranges` VALUES(FROM_UNIXTIME(@starttime),FROM_UNIXTIME(@starttime + @inter));
SET @starttime = @starttime + @inter;
UNTIL @starttime > @endtime END REPEAT;
END;;
DELIMITER ;
CALL populateRanges();
-- Actual query
SELECT UNIX_TIMESTAMP(`_ranges`.`Start`) AS `Start_TS`,
COUNT(`Alarms`.`AlarmId`) AS `n`
FROM `_ranges`
LEFT JOIN `Alarms`
ON `Alarms`.`StartedAt` < `_ranges`.`End`
AND (`Alarms`.`EndedAt` IS NULL OR `Alarms`.`EndedAt` >= `_ranges`.`Start`)
AND ((`Alarms`.`EndedAt` IS NULL AND `Alarms`.`Acknowledged` = FALSE) OR `Alarms`.`Pinned` = TRUE)
-- Again, the above condition is sometimes replaced by:
-- AND ((`Alarms`.`EndedAt` IS NOT NULL OR `Alarms`.`Acknowledged` = TRUE) AND `Alarms`.`Pinned` = FALSE)
AND `DeviceId` = @testDevice
AND `MarkedForDeletion` = FALSE
GROUP BY `_ranges`.`Start`
(此查询应该收集每个时间片的计数列表,每个计数表示[StartedAt,EndedAt]范围与该时间片相交的警报数量.结果填充了一个折线图.)
同样,当我设计这些表并且它们中没有很多行时,索引合并似乎使一切都成为现实.但现在不是这样的:使用injectAlarms()中给出的数据集,这需要40秒才能完成!
我在添加MarkedForDeletion列并执行我的第一个大型数据集比例测试时注意到了这一点.这就是为什么我选择索引并不会因为MarkedForDeletion的存在而造成太大影响,尽管如果我从查询中删除AND MarkedForDeletion = FALSE,上面描述的结果是相同的;但是,我保持了这个条件,最终我需要它在那里.
我已经尝试了一些USE INDEX / FORCE INDEX组合,但它似乎从未使用索引合并作为结果.
我可以定义哪些索引使这个表在给定的情况下表现得很快?或者我如何重构我的查询以实现相同的目标?
(以上在MariaDB 5.5.56 / CentOS 7上获得的查询计划,但解决方案也必须适用于MySQL 5.1.73 / CentOS 6.)
解决方法:
哇!这是我见过的最复杂的“索引合并”.
通常(可能总是),您可以创建一个’复合’索引来替换索引合并相交,并且执行得更好.将key2从just(pinned)更改为(pinned,DeviceId).这可能会摆脱“相交”并加快速度.
通常,优化程序仅在绝望时使用索引合并. (我认为这是标题问题的答案.)对查询或所涉及的值进行任何细微更改,优化程序将执行查询而不进行索引合并.
临时表__codes的改进是构建具有大范围值的永久表,然后使用Proc中该表的一系列值.如果您使用的是MariaDB,则使用动态构建的“序列”表.例如,’table’seq_1_to_100实际上是一列的表,数字为1..100.无需声明或填充它.
您可以通过从Code计算时间来摆脱其他REPEAT循环.
避免LOOP将是最大的性能优势.
完成所有这些,然后我可能有其他提示.