Click here to Skip to main content
15,887,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a database of millions of rows. each rows contain one transaction.
I have to search how many rows are added/updated in last 24 hours. Table always contains timestamps of modification/addition.

Now data beyond 1 days is not useful anyway. If a search is made for records of last 24 hour. I have 2 ways

1. I can write a search query, returning count of rows between current time and 24 hour ago.

2. I delete all rows beyond 24 hours, and just return the no. of rows via Count method (It may delete some few thousand at a given time).

Which one will be better. Please provide a insight.

Note- I have a job in place deletes records every few minutes (30 min) anyways.
Posted

1 solution

I have to say that for your purpose deletion is not the right option, Deletion is a costly activity as it has to do so many tasks behind the scene which you probably not aware of (for example index correction, locking, logging, etc).

in your case an index over your timespan field will increase search performance significantly. This should be the correct way of getting count of rows added in last 24 hours.

Finally in your problem deletion will never be faster then search operation
 
Share this answer
 
v2
Comments
Anant Beriwal 7-Jan-16 2:51am    
Asif, here I have updations too on existing row, in any case , job is deleting old items from database. but job cant run every minute(It will be a very operation), so it is schedule for half hour. now the only problem is this half hour time stamp which can be left out of calculation via calculating records of last 24 hours. Can delete and then simply count will not be a good idea?
_Asif_ 7-Jan-16 3:09am    
Are you purging records or archiving ? for every 30 mins
Anant Beriwal 7-Jan-16 3:10am    
purging
_Asif_ 7-Jan-16 3:40am    
I don't know your business case but usually Transactions do not get purge because of strict regulatory archiving policy. why don't you archive transactions for a day and get the count from archived table/repository.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900