2015-02-06 318 views
1

运行Mysql V5.6.22,在正确索引的数据库上运行复杂查询时,响应时间最初非常缓慢(10秒)。随后的查询(针对不同的项目)非常敏感(50毫秒)。所以我猜查询缓存正在做他们的工作 - 但我如何最小化最初的慢响应?缓慢*初始* mysql查询

该数据库是一个外部维护的医疗数据库(SNOMED),我使用推荐视图为当前快照 - 我认为这些视图是对速度的限制。

奇怪地重新启动mysql并没有什么区别 - 它似乎是时间事情 - 如果数据库没有被使用一段时间,它只需要很长时间就可以启动。

所以我的问题是,是否有一个MySQL设置用于多久这些缓存保留或应该我使用不同的方法,而不是使用视图(SNOMED的数据更新2年,并在另一个类似的数据库对于药品有每月发布。?

你们有些人希望看到这里的查询。警告它确实有点棘手,基本查询的行数在评论中给出... ;-)

SELECT DISTINCT concat(c.id, '::', c.effectiveTime) as id, `d1`.`term` as label, `d2`.`term` 
FROM (`snomed`.`rf2_ss_refset` as refset) 
JOIN `snomed`.`rf2_ss_concepts` as c ON `c`.`id` = `refset`.`referencedCOmponentId` 
JOIN `snomed`.`rf2_ss_descriptions` as d1 ON `d1`.`conceptId` = `refset`.`referencedComponentId` 
JOIN `snomed`.`rf2_ss_descriptions` as d2 ON `d2`.`conceptId` = `d1`.`conceptId` 
JOIN `snomed`.`rf2_ss_language_refset` as lang ON `lang`.`referencedComponentId` = `d1`.`id` 
WHERE `refset`.`refSetId` = 32570071000036102 
AND `refset`.`active` = 1 
AND `d2`.`typeId` = 900000000000013009 
AND `d1`.`active` = 1 
AND `d2`.`active` = 1 
AND `d1`.`moduleId` = 900000000000207008 
AND `d2`.`moduleId` = 900000000000207008 
AND `lang`.`active` = 1 
AND `lang`.`acceptabilityId` = 900000000000548007 
AND `d2`.`term` like "hypertension%" 
ORDER BY `d1`.`term`, `d2`.`term` 

WHERE:

CREATE TABLE `rf2_ss_refset` (
    `id` char(36) COLLATE utf8_unicode_ci NOT NULL, 
    `effectiveTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `active` smallint(1) NOT NULL, 
    `moduleId` bigint(20) unsigned NOT NULL, 
    `refSetId` bigint(20) unsigned NOT NULL, 
    `referencedComponentId` bigint(20) unsigned NOT NULL, 
    PRIMARY KEY (`id`,`effectiveTime`), 
    KEY `moduleId_idx` (`moduleId`), 
    KEY `refSetId_idx` (`refSetId`), 
    KEY `referencedComponentId_idx` (`referencedComponentId`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

CREATE ALGORITHM=UNDEFINED DEFINER=`snomed`@`localhost` SQL SECURITY DEFINER VIEW `rf2_ss_concepts` 
AS SELECT 
    `t1`.`id` AS `id`, 
    `t1`.`effectiveTime` AS `effectiveTime`, 
    `t1`.`active` AS `active`, 
    `t1`.`moduleId` AS `moduleId`, 
    `t1`.`definitionStatusId` AS `definitionStatusId` 
FROM `rf2_full_concepts` `t1` where (`t1`.`effectiveTime` = (select max(`t2`.`effectiveTime`) from `rf2_full_concepts` `t2` where (`t1`.`id` = `t2`.`id`))); 

CREATE ALGORITHM=UNDEFINED DEFINER=`snomed`@`localhost` SQL SECURITY DEFINER VIEW `rf2_ss_descriptions` 
AS SELECT 
    `t1`.`id` AS `id`, 
    `t1`.`effectiveTime` AS `effectiveTime`, 
    `t1`.`active` AS `active`, 
    `t1`.`moduleID` AS `moduleID`, 
    `t1`.`conceptId` AS `conceptId`, 
    `t1`.`languageCode` AS `languageCode`, 
    `t1`.`typeID` AS `typeID`, 
    `t1`.`term` AS `term`, 
    `t1`.`caseSignificanceId` AS `caseSignificanceId` 
FROM `rf2_full_descriptions` `t1` where (`t1`.`effectiveTime` = (select max(`t2`.`effectiveTime`) from `rf2_full_descriptions` `t2` where (`t1`.`id` = `t2`.`id`))); 

CREATE ALGORITHM=UNDEFINED DEFINER=`snomed`@`localhost` SQL SECURITY DEFINER VIEW `rf2_ss_language_refset` 
AS SELECT 
    `t1`.`id` AS `id`, 
    `t1`.`effectiveTime` AS `effectiveTime`, 
    `t1`.`active` AS `active`, 
    `t1`.`moduleId` AS `moduleId`, 
    `t1`.`refSetId` AS `refSetId`, 
    `t1`.`referencedComponentId` AS `referencedComponentId`, 
    `t1`.`acceptabilityId` AS `acceptabilityId` 
FROM `rf2_full_language_refset` `t1` where (`t1`.`effectiveTime` = (select max(`t2`.`effectiveTime`) from `rf2_full_language_refset` `t2` where (`t1`.`id` = `t2`.`id`))); 

CREATE ALGORITHM=UNDEFINED DEFINER=`snomed`@`localhost` SQL SECURITY DEFINER VIEW `rf2_ss_relationships` 
AS SELECT 
    `t1`.`id` AS `id`, 
    `t1`.`effectiveTime` AS `effectiveTime`, 
    `t1`.`active` AS `active`, 
    `t1`.`moduleId` AS `moduleId`, 
    `t1`.`sourceId` AS `sourceId`, 
    `t1`.`destinationId` AS `destinationId`, 
    `t1`.`relationshipGroup` AS `relationshipGroup`, 
    `t1`.`typeId` AS `typeId`, 
    `t1`.`characteristicTypeId` AS `characteristicTypeId`, 
    `t1`.`modifierId` AS `modifierId` 
FROM `rf2_full_relationships` `t1` where (`t1`.`effectiveTime` = (select max(`t2`.`effectiveTime`) from `rf2_full_relationships` `t2` where (`t1`.`id` = `t2`.`id`))); 

#select count(*) from rf2_full_concepts # 507046 
CREATE TABLE `rf2_full_concepts` (
    `id` bigint(20) unsigned NOT NULL, 
    `effectiveTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `active` tinyint(4) DEFAULT NULL, 
    `moduleId` bigint(20) unsigned NOT NULL, 
    `definitionStatusId` bigint(20) unsigned NOT NULL, 
    PRIMARY KEY (`id`,`effectiveTime`), 
    KEY `moduleId_idx` (`moduleId`), 
    KEY `definitionStatusId_idx` (`definitionStatusId`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

#select count(*) from rf2_full_descriptions # 1486373 
CREATE TABLE `rf2_full_descriptions` (
    `id` bigint(20) unsigned NOT NULL, 
    `effectiveTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `active` tinyint(4) DEFAULT NULL, 
    `moduleID` bigint(20) unsigned NOT NULL, 
    `conceptId` bigint(20) unsigned NOT NULL, 
    `languageCode` char(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'en', 
    `typeID` bigint(20) unsigned NOT NULL, 
    `term` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `caseSignificanceId` bigint(20) unsigned NOT NULL, 
    PRIMARY KEY (`id`,`effectiveTime`), 
    KEY `moduleID_idx` (`moduleID`), 
    KEY `conceptId_idx` (`conceptId`), 
    KEY `typeID_idx` (`typeID`), 
    KEY `caseSignificanceId_idx` (`caseSignificanceId`), 
    KEY `term_idx` (`term`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

#select count(*) from rf2_full_relationships = 4582286 
CREATE TABLE `rf2_full_relationships` (
    `id` bigint(20) unsigned NOT NULL, 
    `effectiveTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `active` tinyint(4) DEFAULT '1', 
    `moduleId` bigint(20) unsigned NOT NULL, 
    `sourceId` bigint(20) unsigned NOT NULL, 
    `destinationId` bigint(20) unsigned NOT NULL, 
    `relationshipGroup` bigint(20) unsigned NOT NULL, 
    `typeId` bigint(20) unsigned NOT NULL, 
    `characteristicTypeId` bigint(20) unsigned NOT NULL, 
    `modifierId` bigint(20) unsigned NOT NULL, 
    PRIMARY KEY (`id`,`effectiveTime`), 
    KEY `moduleId_idx` (`moduleId`), 
    KEY `sourceId_idx` (`sourceId`), 
    KEY `destinationId_idx` (`destinationId`), 
    KEY `relationshipGroup_idx` (`relationshipGroup`), 
    KEY `typeId_idx` (`typeId`), 
    KEY `characteristicTypeId_idx` (`characteristicTypeId`), 
    KEY `modifierId_idx` (`modifierId`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

#select count(*) from rf2_full_language_refset # 624467 
CREATE TABLE `rf2_full_language_refset` (
    `id` char(36) COLLATE utf8_unicode_ci NOT NULL, 
    `effectiveTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `active` smallint(1) NOT NULL, 
    `moduleId` bigint(20) unsigned NOT NULL, 
    `refSetId` bigint(20) unsigned NOT NULL, 
    `referencedComponentId` bigint(20) unsigned NOT NULL, 
    `acceptabilityId` bigint(20) unsigned NOT NULL, 
    PRIMARY KEY (`id`,`effectiveTime`), 
    KEY `moduleId` (`moduleId`), 
    KEY `refSetId` (`refSetId`), 
    KEY `referencedComponentId` (`referencedComponentId`), 
    KEY `acceptabilityId` (`acceptabilityId`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 
+0

通过将视图转换成表我可以确认它是视图放慢了它。现在这个问题真的是为了利益而起作用 - 我认为我将不得不忍受与更新相关的更长的停机时间 - 视图太昂贵了! – pgee70 2015-02-06 12:04:08

+0

你可以发布查询的查询执行计划吗?只是为了看看发生了什么? – Olli 2015-02-06 12:51:58

回答

2

诊断别人的服务器,而所有谜题的部分是困难的,所以我会做如下假设(纠正我,如果我错了):

  1. 这不是一个DB-级运行服务器
  2. MySQL的配置还没有被特别地被配置为例子,你表现出你的表看起来超指数的这个任务

,并且由于大型数据集,我可以想像,最大的索引文件太大而不适合MySQL的索引缓存。

没有关于环境的更多信息,看起来您所描述的行为取决于配置的缓冲区。此时,操作系统进入并将数据库缓存在内存中。

当您重新启动MySQL时,它查询的很好,而且速度很快,因为操作系统仍将文件保存在缓冲区中。当您停止访问数据库时,操作系统将最终取消缓冲文件,并且您将回到最初的慢查询。

我已经看到大于超过配置的缓冲区大小的大型索引上的相同行为,但是这是一个更大的一组数据。配置数据库具有更大的索引和表数据缓冲区解决了我的特殊问题。在我的情况下,查询速度从10-15秒降到毫秒。

http://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_buffer_pool_size

如果你有一些空闲内存,尝试有所加大了缓冲池大小。 默认大小为128Mb,并且您的最大索引文件大约为279Mb(64字节* 4,582,286行)。 要开始,请尝试将配置中的值设置为512Mb。重新启动,重新测试。如果它还不是很好,请再添加一个128Mb并重复,直到它工作。 如果此DB位于专用机器上,请根据您的设置将该值设置得相当高(总RAM的50-75%)应该是安全的。

一个快速的谷歌提出了一个很好的指导什么配置值来摆弄。 http://www.tocker.ca/2013/09/17/what-to-tune-in-mysql-56-after-installation.html

+0

谢谢。我已经接受你的答案。我将重新编码数据库以使用表格而不是视图 - 因为我不会使用表格来获得滞后,因为您建议使用更多的内存将会很昂贵! – pgee70 2015-02-07 01:36:36