SQL Querying - Matching datetimes vs Matching integers

Viewed 52

I have a bunch of data in my database and I want to filter out data that has been stored that for longer than a week. I'm using SQLServer and I found that I could use the 'DATEDIFF' function.

At the moment it works great and fast but I don't have a lot of records at the moment therefore anything runs quite smoothly.

After some research online I found out that the comparison of integers in databases is faster than the comparison of strings, I assume at this point that the comparison of datetimes (using the given function) is even slower at a major scale.

Let's say my database table content looks like this: enter image description here

Currently I would filter out records that are older like a week like so:

SELECT * FROM onlineMainTable WHERE DATEDIFF(wk, Date, GETDATE()) > 1

I assume that this query would be quite slow if there were a thousand rows tin the table.

The status column represents a calculation status, I wondered if I would speed up the process if I were to look for a specific status instead of matching datetimes, for me in order to set that status to the one that represents 'old records' I need to update those rows before I select them, it would look something like this:

UPDATE table SET Status = -1 WHERE NOT Status = -1 AND DATEDIFF(wk, Date, GETDATE()) > 1;
SELECT * FROM table WHERE Status = -1;

I used the '-1' as an example.

So obviously I could be wrong but I think updating in this case would be fast enough since there won't be that many records to update since older ones have already been updated with its status. The selection would be faster aswell since I would be matching integers instead of datetimes.

The downside to my (possible) solution is that I would query twice every time I fetch data, even when it might not be needed (if every row is newer than 1 week).

It comes down to this: Should I compare datetimes or should I update an integer column based on that datetime and then select using the comparison of those ints?

If there is a different/better way of doing this i'm all ears.

Context I am making a webapp for quotation requests. Requests should expire after a week since they won't be valid at that point. I need to both display valid requests and expired requests (so costumers have an overview). All these requests are stored in a database table.

1 Answers
Related