I am making a web app, using ASP.NET. Basically, I need to display data from a SQL Server, which has a billion-row database. When I query something, it often takes a lot of time. For example, I used this codes to query data created from the past week:
DECLARE @CurrentDate as DateTime,
@PastWeek as DateTime;
SET @CurrentDate = GETDATE();
SET @PastWeek = DATEADD(ww, -1, @CurrentDate);
SELECT [ID]
,[TIMESTAMP]
,[USERNAME]
,[CLIENTNAME]
,[COMMAND]
,[PATH]
FROM [P4D] WHERE TIMESTAMP BETWEEN @CurrentDate AND @PastWeek;
It is still querying after 20 min. I think it takes long because it tests every single row whether it is in the time range or not.
Is it the wrong way to pull data created from pastweek? Or are there any ways making the query statements more efficient?
Thanks!
2 things you need:
(Add a nolock to your query)
And add a non clustustred index with
TIMESTAMPand add the selected columns(ID,USERNAME,CLIENTNAME,COMMAND,PATH)to this index.