[英]sql server - Using Datepart in where clause taking a longer time
For example : Say We have a staging transaction table which has 30 million rows, when joining with a master table which has unique (24 rows) ...each hours(1 to 24) 例如:说我们有一个临时交易表,其中包含3000万行,与具有唯一性(24行)的主表联接时……每个小时(1到24)
SELECT F.*
FROM STAGING_TRANSACTION F
JOIN DIM_TIME DT ON DATEPART(HOUR,F.RECORDED_TIME) = DATEPART(HOUR,DT.ON_TIME)
Datatypes are RECORDED TIME is DATETIME ON_TIME is decimal 数据类型为RECORDED TIME为DATETIME ON_TIME为十进制
There is no index on staging a, i have created index on dim_time since it was slow. 没有关于暂存a的索引,我已经在dim_time上创建了索引,因为它很慢。 When staging tabe is joined with other tables, it is faster but when joined dim time, it takes a longer time.
将暂存Tabe与其他表连接时,速度更快,但在连接昏暗时间时,则需要更长的时间。 Index doesn't work since a function is used in where clause.
索引不起作用,因为在where子句中使用了函数。
is there any other way to make it faster 还有其他方法可以使其更快
Leo 狮子座
This is your query: 这是您的查询:
SELECT F.*
FROM STAGING_TRANSACTION F JOIN
DIM_TIME DT
ON DATEPART(HOUR, F.RECORDED_TIME) = DATEPART(HOUR, DT.ON_TIME) ;
If you have DIM_TIME
, you should have a column with the appropriate hour in the dimension. 如果您有
DIM_TIME
,则应在维中有一列带有适当小时数的列。 After all, it is a utility table. 毕竟,它是一个实用程序表。 This is easily fixed:
这很容易解决:
alter table dim_time add dt_hour as (datepart(hour, on_time) );
Then, an index on this column will probably help the query: 然后,此列上的索引可能会帮助查询:
create index idx_dim_time_hour on dim_time(dt_hour);
This should help the query: 这应该有助于查询:
SELECT F.*
FROM STAGING_TRANSACTION F JOIN
DIM_TIME DT
ON DATEPART(HOUR, F.RECORDED_TIME) = dt.dp_time;
You can also add a computed column to staging_transaction
: 您还可以将计算列添加到
staging_transaction
:
alter table staging_transaction add st_hour as (datepart(hour, recored_time) );
create index idx_st_hour on staging_transaction(st_hour);
Then the query looks like: 然后查询看起来像:
SELECT F.*
FROM STAGING_TRANSACTION F JOIN
DIM_TIME DT
ON f.st_hour = dt.dp_time;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.