简体   繁体   中英

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)

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

There is no index on staging a, i have created index on dim_time since it was slow. When staging tabe is joined with other tables, it is faster but when joined dim time, it takes a longer time. Index doesn't work since a function is used in where clause.

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. 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 :

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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