Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way in ASP.NET of configuring rows in a database to delete after a certain time

Suppose I have a table in my database like

                           Registrants
=======================================================================
  id |    name    |            email        |    registration_date
=======================================================================
  1  |   "Sam"    | "[email protected]"  |    "2016-03-26T14:25:10"
-----------------------------------------------------------------------
  2  |   "Bob"    | "[email protected]"   |    "2015-12-01T10:09:30"
-----------------------------------------------------------------------
  .  |     .      |           .             |              .
-----------------------------------------------------------------------
  .  |     .      |           .             |              .
-----------------------------------------------------------------------
  .  |     .      |           .             |              .
-----------------------------------------------------------------------
  .  |     .      |           .             |              .

and I want every registrant to automatically be deleted, say, 100 days after registering. What is a proper way to do this, and what is the best way?

The shoddy way I was planning on doing it was going to be to create a sproc

CREATE PROCEDURE FlushOldRegistrants
AS
BEGIN
   DELETE FROM Registrants WHERE DATEADD(day,100,registration_date) < GETDATE()
END

and in my server-side code invoke that sproc every once in a while like

Script.SetInterval(delegate {
    using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("FlushOldRegistrants", conn) { 
                           CommandType = CommandType.StoredProcedure }) {
   conn.Open();
   command.ExecuteNonQuery();
   conn.Close();
}
 }, 60000); // flush old registrants every hour

But, can I do that all at the database level? Is it possible to create a T-SQL TRIGGER that does this? Or is there an even better way?

like image 898
user6048670 Avatar asked Mar 19 '16 17:03

user6048670


People also ask

How do I purge old data in SQL Server?

To delete rows in a SQL Server table, use the DELETE statement: delete from sessions where id = 10; The WHERE clause is optional, but you'll usually want it, unless you really want to delete every row from the table.

How display millions of records in Gridview in ASP NET MVC?

You can store the record index in viewstate or in hidden fields run at = server. So, while you first load, set the pageindex to 0 and say you display 20 records. When the next is clicked, update the page index (pageindex++). Say when you click 8th page, set the page index to 8 and so on.


1 Answers

The most obvious method is to use SQL Server Agent. Set up a job that runs every day or once a week, and have it call your stored procedure.

Another option is to leave all the data in the table and create a view:

create view v_Registrants as
   select r.*
   from Registrants r
   where registration_date < DATEADD(day, -100, getdate()) ;

When you access the data through the view, you will only see the most recent data. One advantage of this approach is that it is precise -- you will never get data that is 102 days old or 100 days and 8 hours old -- which is a danger using a scheduled job. In fact, if the exact cut-off is important, you might want this view as well as the job.

In both cases, you are safer using this where statement, because it can make use of indexes (and hence should be faster).

like image 95
Gordon Linoff Avatar answered Sep 22 '22 14:09

Gordon Linoff