sql - Optimize select query on huge database -
i developing sql query using join based on 2 tables, table 1 , table 2. rdbms sql server. both tables have common id
column, based on join formed. there datetime column in both columns.
objective: want retrieve rows table2 table2.datetime
within range of 1 minute of table1.datetime
.
note: not have write permission database indexing not option me.
i got query right. working correctly; however, database huge. if want retrieve data last 15 days, takes forever.
is there better way it?
here query
select a.column1, a.column2, a.column3, a.column4, a.column5, a.column6, a.column7, a.column8, a.column9, a.column10, a.column11, b.column1, b.column2, b.column3, b.column4, b.column5 table1 a, table2 b a.commoncolumn = b.commoncolumn , b.datetime between dateadd(minute, -1, a.datetime) , dateadd(minute, 1, a.datetime) , a.datetime between getdate() - 15 , getdate()
try filter data first reduce number of records before applying minute condition define variable startdate , enddate , use them in query below.(hope helps, or give idea) (i'm afraid cte performance , lets try)
declare @startdate datetime =getdate()-15; declare @enddate datetime=getdate(); ;with filtereddataa ( select a.commoncolumn,a.datetime, a.column1, a.column2, a.column3, a.column4, a.column5, a.column6, a.column7, a.column8, a.column9, a.column10, a.column11 table1 a.datetime between @startdate , @enddate ), filtereddatab ( select b.commoncolumn,b.datetime, b.column1, b.column2, b.column3, b.column4, b.column5 table2 b b.datetime between @startdate , @enddate ) select a.column1, a.column2, a.column3, a.column4, a.column5, a.column6, a.column7, a.column8, a.column9, a.column10, a.column11, b.column1, b.column2, b.column3, b.column4, b.column5 filtereddataa left join filtereddatab b on a.commoncolumn=b.commoncolumn , b.datetime between dateadd(minute, -1, a.datetime) , dateadd(minute, 1, a.datetime)
wiki
Comments
Post a Comment