001
001

Reputation: 65165

Delete session from database after it expired?

This might be a silly question but i was wondering if it would be a good idea to delete all expired "sessions" from my database, every 15 minutes?

Or just leave it there? Session expires after X minutes, its no longer useful anymore, seems like it just taking up space?

Upvotes: 2

Views: 6596

Answers (3)

RickNZ
RickNZ

Reputation: 18654

If you're using the standard SQL Server Session State, old sessions are automatically deleted for you. Until the old entries are deleted, sessions do not expire, since the provider does not actively monitor the Expires column in the session table.

The install / configuration utility creates a SQL Agent job that calls the DeleteExpiredSessions stored procedure every 60 seconds. This means that SQL Agent needs to be running for session expiration to work and for the table to get cleaned up.

Upvotes: 2

Tim M.
Tim M.

Reputation: 54377

When my team deploys SQL Server session state in a .NET application, we simply create a job with SQL Server Agent to clean up the expired sessions nightly (or at some appropriate interval).

Unless you have massive amounts of traffic, you can do this whenever it is convenient without worrying about instantly deleting unused sessions, but it's definitely a good idea to clean up.

Upvotes: 2

Oleg Dok
Oleg Dok

Reputation: 21766

If your table looks like that

SessionId datatype,
SessionLastTouch datetime

then when registering the new session do some cleanup after writing(session issue or touch) to the table, like this:

delete from YourSessionsRegister WHERE SessionLastTouch < DATEADD(min, -15, GETDATE())

BUT

May be better way is to cleanup the session register based on some schedule - to reduce DB load in peak hours - simply create the job, which cleans up the register every night or smth. like this

Upvotes: 0

Related Questions