Delete session from database after it expired?

后端 未结 3 679
野趣味
野趣味 2021-01-15 11:54

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

3条回答
  •  臣服心动
    2021-01-15 12:37

    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

提交回复
热议问题