Login

Clean up expired django.contrib.session's in a huge MySQL InnoDB table

Author:
patsplat
Posted:
December 31, 2008
Language:
SQL
Version:
Not specified
Tags:
mysql django.contrib.session expire innodb
Score:
3 (after 3 ratings)

While django provides the django_admin.py cleanup script, if sessions get out of control sometimes you have to go lower level to get everything cleaned up. If the problem gets out of hand and you hit the resource limits of the machine, it is very difficult to get anything done in the database.

Attached is SQL code which was used to cleanup 27GB of expired session data in 3h. Run it like this to make sure it runs to completion:

nohup mysql --user=username --password=password --host=hostname database < delete_expired_sessions.sql

nohup causes the script to run detached from a terminal, so if your session gets disconnected it will keep running.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
DROP TABLE IF EXISTS `django_session_cleaned`;
CREATE TABLE `django_session_cleaned` (
  `session_key` varchar(40) NOT NULL,
  `session_data` longtext NOT NULL,
  `expire_date` datetime NOT NULL,
  PRIMARY KEY  (`session_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `django_session_cleaned` SELECT * FROM `django_session` WHERE `expire_date` > CURRENT_TIMESTAMP;

RENAME TABLE `django_session` TO `django_session_old_master`, `django_session_cleaned` TO `django_session`;

DROP TABLE `django_session_old_master`;

More like this

  1. MySQL django password function by mcosta 6 years, 2 months ago
  2. Database cleanup by skyjur 5 years ago
  3. Creating MySQL Alter table commands for Foreign Keys by vidyanand 7 years, 1 month ago
  4. Test Django against many Pythons and databases by jacobian 8 years, 4 months ago
  5. Load customized SQL by roppert 6 years, 5 months ago

Comments

Please login first before commenting.