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
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. create_template_postgis-ubuntu_lucid by clawlor 13 years, 6 months ago
  2. PostgreSQL fulltext with language translations by diverman 13 years, 7 months ago
  3. Drop all tables in MySQL database by mpasternacki 14 years, 1 month ago
  4. grep and delete sqlite tables by kifkif 14 years, 3 months ago
  5. Substitute hyphens with spaces to enale URLs to reference to multi-word tags by miernik 14 years, 4 months ago

Comments

Please login first before commenting.