【发布时间】:2016-05-01 02:52:34
【问题描述】:
我有一个非常小、简单的 MYSQL 表,用于保存预先计算的财务数据。该表如下所示: 参考日期 |仪器| rate|startDate |maturityDate|carry1|carry2|carry3
3 个索引定义为:
唯一的 unique_ID(refDate,instrument)
参考日期(参考日期)
仪器(仪器)
目前的行数约为 1000 万,但对于每个 refDate,目前只有大约 5000 个不同的工具
我有一个查询,它在此表上自连接以生成如下输出: refDate|利率工具=X |利率工具 = Y|评级工具=Z|....
基本上返回时间序列数据,然后我可以在其中进行自己的分析。
问题出在:我原来的查询是这样的:
Select distinct AUDSpot1yFq.refDate,AUDSpot1yFq.rate as 'AUDSpot1yFq',
AUD1y1yFq.rate as AUD1y1yFq
from audratedb AUDSpot1yFq inner join audratedb AUD1y1yFq on
AUDSpot1yFq.refDate=AUD1y1yFq.refDate
where AUDSpot1yFq.instrument = 'AUDSpot1yFq' and
AUD1y1yFq.instrument = 'AUD1y1yFq'
order by AUDSpot1yFq.refDate
注意,在下面这个特定的时间查询中,我实际上得到了 10 种不同的工具,这意味着查询要长得多,但遵循相同的命名模式、内部连接和 where 语句。
这很慢,在工作台中我将其计时为 7-8 秒的持续时间(但接近 0 获取时间,因为我在运行服务器的机器上有工作台)。当我剥离不同的,持续时间下降到 0.25-0.5 秒(更易于管理),当我剥离“order by”时,它变得更快(
当我对缩减的查询(具有可怕的获取时间)运行解释时,我得到:
1 SIMPLE AUDSpot1yFq ref unique_ID,refDate,instrument instrument 39 const 1432 100.00 Using where
1 SIMPLE AUD1y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
1 SIMPLE AUD2y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
1 SIMPLE AUD3y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
1 SIMPLE AUD4y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
1 SIMPLE AUD5y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
1 SIMPLE AUD6y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
1 SIMPLE AUD7y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
1 SIMPLE AUD8y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
1 SIMPLE AUD9y1yFq ref unique_ID,refDate,instrument unique_ID 42 historicalratesdb.AUDSpot1yFq.refDate,const 1 100.00 Using where
我现在意识到不需要 distinct,当我将输出输出到数据框时,我可以丢弃并在 pandas 中排序。这太棒了。但我不知道如何缩短 Fetch 时间。我不会在这个网站上赢得任何能力竞赛,但我已经尽可能多地搜索并且找不到这个问题的解决方案。非常感谢任何帮助。
~可可
【问题讨论】:
-
嗨社区,我不确定为什么这个解决方案有效(考虑到我没有认为我在获取时间方面以非常有意义的方式更改了查询)但似乎我的时间基本上崩溃了在我更改 innodb_buffer_pool_size(从 1G 到 2G)后,我希望尽可能快。我想更改请求,当您从 select 语句中删除“distinct”时,任何人都可以解释这种行为发生如此巨大的变化吗?数据是否变得更难拼接在一起?如果我改为使用子选择查询,它的性能会更好吗?
标签: mysql optimization distinct fetch duration