清理过期django.contrib.会议&#在一个巨大的MySQL InnoDB表中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了清理过期django.contrib.会议&#在一个巨大的MySQL InnoDB表中相关的知识,希望对你有一定的参考价值。

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.

p.s.
An alternative approach is to launch this command from the shell:

DELETE FROM django_session WHERE expire_date < NOW();

or

DELETE FROM django_session WHERE expire_date < "2011-12-10 00:00:00";
  1. DROP TABLE IF EXISTS `django_session_cleaned`;
  2. CREATE TABLE `django_session_cleaned` (
  3. `session_key` varchar(40) NOT NULL,
  4. `session_data` longtext NOT NULL,
  5. `expire_date` datetime NOT NULL,
  6. PRIMARY KEY (`session_key`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  8.  
  9. INSERT INTO `django_session_cleaned` SELECT * FROM `django_session` WHERE `expire_date` > CURRENT_TIMESTAMP;
  10.  
  11. RENAME TABLE `django_session` TO `django_session_old_master`, `django_session_cleaned` TO `django_session`;
  12.  
  13. DROP TABLE `django_session_old_master`;

以上是关于清理过期django.contrib.会议&#在一个巨大的MySQL InnoDB表中的主要内容,如果未能解决你的问题,请参考以下文章

redis过期key的清理策略

Django的标准库django.contrib包介绍

Django的标准库django.contrib包介绍

django--开发博客

使用 django.contrib.messages 重定向 Django 注册激活

django的零碎注意点