[英]Slow *initial* mysql query
運行Mysql V5.6.22,在正確索引的數據庫上運行復雜的查詢,最初的響應時間非常慢(10秒鍾)。 隨后的查詢(針對不同的項目)響應速度非常快(50毫秒)。 所以我想查詢緩存正在完成他們的工作-但是如何最小化初始緩慢的響應呢?
該數據庫是一個外部維護的Medical數據庫(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`
哪里:
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;
在沒有所有難題的情況下診斷其他人的服務器很困難,因此我將做出以下假設(如果我輸入錯了,請更正我):
對於您顯示的示例,表看起來過度索引,並且由於數據集很大,我可以想象最大的索引文件太大而無法容納MySQL的索引緩存。
如果沒有有關環境的更多信息,似乎您描述的行為取決於已配置的緩沖區。 此時,操作系統介入並在內存中緩沖數據庫。
當您重新啟動MySQL時,它查詢的速度很快,因為OS仍將文件保存在其緩沖區中。 當您停止訪問數據庫時,操作系統最終將取消緩沖文件,並且您將返回到緩慢的初始查詢。
我在超過已配置的緩沖區大小的大型索引上看到了相同的行為,但這是一組更大的數據。 將數據庫配置為具有更大的索引和表數據緩沖區可以解決我的特定問題。 以我為例,查詢速度從10-15秒降低到毫秒。
http://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_buffer_pool_size
如果您有一些可用的RAM,請嘗試增加緩沖池大小。 默認大小為128Mb,最大的索引文件約為279Mb(64字節* 4,582,286行)。 首先,嘗試將配置中的值設置為512Mb。 重新啟動,重新測試。 如果仍然不是很好,請再添加一個128Mb,然后重復直到它起作用為止。 如果此數據庫在專用計算機上,則根據您的設置,將值設置得很高(總RAM占50-75%)應該是安全的。
一個快速的Google提出了一個不錯的指南,說明了要弄弄哪些配置值。 http://www.tocker.ca/2013/09/17/what-to-tune-in-mysql-56-after-installation.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.