Sql-server – Update an entry in table when time expires

sql servertimetrigger

I need to modify an entry in table, based on time.

i.e. table contains customer-info & subscription-info. I need to modify/delete subscription when subscription expires for each user.

There can be upto 2-3 million entries in the table.

each user will have different expiration date so what is the best means to achieve this?

Is there some sort of trigger mechanism which gets triggered when time expires?

or is it safe to create an event that poles per second or create separate event for each subscriber?

or is it safe to have a third party timer tool that maintains expiration times and trigger an action when timer expires?

or is there a better approach to address this requirement?

btw: database is sql server.

Thanks & Regards,
Prasun

Best Answer

A trigger would not be the most appropriate solution for you particularly for a large table which could have lots of hits.

Right a simple stored procedure and just have a SQL Server Agent Job that runs it periodically (as often as is required for your business purposes).

Something simple like:

UPDATE 
    custSubsTable 
SET 
    subscritionActive = 0
WHERE 
    subscritionActive = 1
        AND
    expiryDate < GETDATE()

Would work. I don't think the first half of the WHERE clause is strictly necessary, but it doesn't hurt. You could just stick the code straight into the Job, but it'll be more easily maintained as a stored procedure and can be manually run that way

This could easily be modified to delete instead of update, or even possible delete/update a different row in the same table (by joining the table with itself), although if that is the case then best practise would dictate denormalising your db and splitting the tables up.

Even with a large table, that should be a fairly quick statement to run if it's done frequently enough - obviously the more changes it has to make at once the longer it could be. Make sure you run it manually yourself the first time and add/update any indexes if necessary.

You can do this across multiple tables - I won't get into it here and it's beyond the scope of your question (and I know nothing about your DB), but my first impression, purely from what you say in your question, is that holding customer and subscription data in a single table could lead to lots of denormalisation, might be something to bear in mind.