- b2evolution CMS User Manual
- Developer Reference
- Scheduled Tasks Reference
- Prune old hits & sessions (includes OPTIMIZE)
Prune old hits & sessions (includes OPTIMIZE)
This task will avoid that your sessions and hitlog tables grow indefinitely. It should typically run once a day (preferably at a low traffic time for your site).
The parameter Auto pruning must be set to "With a scheduled job"; otherwise the task will do nothing. For more details, please read this page: Hit & Session Logging Panel.
Settings
- Keep detailed logs for: (default 15 days) . The number of days of hitlog and sessions to keep. Note: this setting is also duplicated in Analytics > Settings.
- In Users > Settings > Profiles there is another setting: Session timeout: (default 7days). This value defines when a session expires and becomes available to be pruned.
This means that:
- Anonymous sessions will be pruned after "Keep detailed logs for:" (default: 15 days)
- Logged-in sessions will be pruned after the longest period of "Keep detailed logs for:" AND "Session timeout:".
- The hitlog (individual hits) will be pruned after "Keep detailed logs for:" (default: 15 days)
Performance
This task can take a very long time to execute, depending on the size of your session, goal hits and hitlog tables.
- If you have not run that task for a long time, your tables may be so large that MySQL will choke itself when trying to prune them. In such a case, it may be a good idea to TRUNCATE the tables using phpMyAdmin and then set this task to run on a daily basis.
- If your site is receiving a lot of daily traffic and MySQL chokes every night when trying to prune, your options are to either upgrade your web server or to log less data by lowering the settings above.
Technical details
This code performs 4 main actions:
Pruning the Hitlog
Prune T_hitlog
based on the number of days to keep.
Before pruning, the data is aggregated on a daily basis and kept in T_hits__aggregate
.
Pruning the Goal Hits
Prune T_track__goalhit
based on the number of days to keep.
Before pruning, the data is aggregated on a daily basis and kept in T_track__goalhit_aggregate
.
Pruning Expired Sessions
Prune timed-out T_sessions
older than number of days to keep.
Before pruning, the data is aggregated on a daily basis and kept in T_hits__aggregate_sessions
.
Note: old sessions that have not timed out yet will be kept in the DB.
The decision about which sessions are timed out or not is based on the value of this field: T_sessions.sess_lastseen_ts
; it stores the last timestamp for any activity on this session. All the sessions where lastseen_ts
exceeds the sessions expiration time and also reach the hitlog keep number of days described above, will be pruned.
Note: if a custom session expiration delay is set for a user, it cannot be longer than the pruning expiration time, because at that point the session will be deleted from the DB.
Before pruning sessions, a BeforeSessionsDelete
event is triggered to all the plugins in the site, and the behavior of the task could be affected if at least one of those plugins takes some action on the sessions data. This could be your first target when detecting an issue with this task.
The section of the code that manages the sessions data deletion performs three tasks:
- Calculates the cutoff date. All sessions inactive beyond that date will be deleted.
- Triggers the event "BeforeSessionsDelete" to all plugins.
- Delete all the sessions beyond the date obtained in 1.
Pruning Basedomains
Prune T_basedomains
that are not used by any hit/session
Optimizing the database
MYSQL OPTIMIZE the tables:
- T_hitlog
- T_track__goalhit
- T_sessions
- T_basedomains