Display data from the past week (a billion-row database)
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?
One Solution collect form web for “Display data from the past week (a billion-row database)”
2 things you need:
SELECT ID ,TIMESTAMP ,USERNAME ,CLIENTNAME ,COMMAND ,PATH FROM P4D (NOLOCK) WHERE TIMESTAMP BETWEEN @CurrentDate AND @PastWeek;
(Add a nolock to your query)
And add a non clustustred index with
TIMESTAMP and add the selected columns
(ID,USERNAME,CLIENTNAME,COMMAND,PATH) to this index.