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

 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. dumpdata/loaddata with MySQL and ForeignKeys, as django command by brondsem 4 years ago
  2. Debug middleware for displaying sql queries and template loading info when ?debug=true by SEJeff 2 years, 1 month ago
  3. Test Django against many Pythons and databases by jacobian 6 years, 2 months ago
  4. MySQL django password function by mcosta 3 years, 11 months ago
  5. TestSettingsManager: temporarily change settings for tests by carljm 4 years, 8 months ago

Comments

(Forgotten your password?)