简体   繁体   English

两个数据库/表之间的持续时间更快

[英]Faster Duration between two db's/tables

The duration to the following query is around 8 mins. 以下查询的持续时间约为8分钟。 I am looking for the average business days between two dates and am using two db's and tables with one being a calendar table. 我正在寻找两个日期之间的平均工作日,并且正在使用两个数据库和一个表,其中一个是日历表。 Is there a more efficient way of making the duration for the following query faster? 有没有更有效的方法来使以下查询的持续时间更快?

select avg(datediff(e.column1, e.column2)) as 'diff' 
from DB1 e
inner join db2.table1 pd on e.column3 = pd.column1
where pd.calendar_day_in_week not in (1,7) 

Is e.column3 and pd.column1 indexed? e.column3和pd.column1是否已建立索引? I think is the join that makes query go slow. 我认为是使查询运行缓慢的联接。 Try this 尝试这个

select avg(datediff(e.column1, e.column2)) as 'diff' 
from DB1 e
inner join (SELECT column1 FROM db2.table1 WHERE calendar_day_in_week not in (1,7)) AS pd on e.column3 = pd.column1

Do the where on the table1 first, so you get smaller chunk table for the join. 首先在table1上执行where,这样您就可以得到较小的块表进行连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM