CodeIgniter Forums

Full Version: ci_sessions table is getting big - like 6M records and 90meg big
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
ci_sessions table is getting big - like 6M records and 90meg big

Is there any problem with me just truncating the table?

These are my settings in config

PHP Code:
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE

I also note that my table did not have the primary key set:

PHP Code:
// When sess_match_ip = FALSE
ALTER TABLE ci_sessions ADD PRIMARY KEY (id); 

So my plan is to truncate the table (knock it to 0 records) and then add the primary key.

Buuuuut - this is a production DB so I want to get the advice of the learned folks here before I commit such an act.

Also why would my sessions in ci_sessions not be being cleaned up by the garbage collector?
Maybe a cron like this would help:

PHP Code:
/**
 * Garbage collection on the ci_sessions table.
 *
 * crontab entry for once a day at 12:59am (59 3 * * *):
 * /usr/local/bin/php /path/to/index.php crons ci_sessions_gc > /dev/null 2>&1
 */
public function ci_sessions_gc()
{
 
   $this->load->database();
 
   $this->config->load('db_tables');
 
   
    $sess_expiration 
config_item('sess_expiration');
 
   $sess_save_path  config_item('sess_save_path');

 
   if$sess_expiration )
 
   {
 
       $epoch_expired time() - $sess_expiration;
 
       $this->db->where('timestamp <'$epoch_expired)
 
           ->delete$sess_save_path );
 
   }
}

// ----------------------------------------------------------------------- 
Thank you.

CI doesnt do auto garbage collection of the sessions table?
By the way - thanks for that code - it works a treat!
(07-02-2018, 05:50 PM)zurtri Wrote: [ -> ]Thank you.

CI doesnt do auto garbage collection of the sessions table?

Yes, it is supposed to.

In php.ini what are the values you have for
session.gc_probability and session.gc_divisor?
hmmm. both those values are 0 in my php.ini

I guess that's the problem.
These are the defaults for php.ini

Code:
session.gc_probability = 1

session.gc_divisor = 1000

session.gc_maxlifetime = 1440

Try that.
(07-02-2018, 08:12 PM)zurtri Wrote: [ -> ]hmmm. both those values are 0 in my php.ini

I guess that's the problem.

Yup. That is a problem.

InsiteFX offers a typical setting with session.gc_divisor = 1000 being the recommended "Production Value:. You could make it happen more often with

Code:
session.gc_probability = 1
session.gc_divisor = 500

The ratio of session.gc_probability to session.gc_divisor determines the chance that GC will occur not a hard and fast "once every 500 times".

You might find this article interesting.

The article suggests one reason why the values would be set to zero - so a cron job could periodically clean up. Looks like you don't' have that cron setup.
Thank you dave