简体   繁体   中英

Codeigniter session query in mysql slow query

I'm maintaining a heavy traffic web app providing RESTFul API using codeigniter and mysql. After turning mysql slow query log with 1 second long query time following query is login repeatedly with execution time of 100+ seconds.

SELECT GET_LOCK('fhn1enbb1u9hgu4m1tq32fbsds4c8ohj', 300) AS ci_session_lock;

It seems something with codeigniter configuration please help.

Sample slow query log:

SET timestamp=1554912062;
SELECT GET_LOCK('fhn1enbb1u9hgu4m1tq32fbsds4c8ohj', 300) AS ci_session_lock;
Time: 2019-04-10T16:01:02.640796Z
Query_time: 99.819745  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0

SET timestamp=1554912062;
SELECT GET_LOCK('taff1quqs4gv2g33740t41cdplnpkof8', 300) AS ci_session_lock;
Time: 2019-04-10T16:01:03.082128Z
Query_time: 94.859307  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0

SET timestamp=1554912061;
SELECT GET_LOCK('fhn1enbb1u9hgu4m1tq32fbsds4c8ohj', 300) AS ci_session_lock;
Time: 2019-04-10T16:01:01.979967Z
Query_time: 106.766586  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0

By looking at this archive from the old CodeIgniter forum , it looks like normal behavior.

You can just ignore this.

It's caused by the same user attempting to simultaneously load multiple pages (or one page with asynchronous AJAX calls) on your website. And it's exactly what is supposed to happen - wait for the lock to be freed (session closed on one page) before being able to acquire a lock for the second page load.

Concurrent users and the database engine are irrelevant. You didn't see this on MyISAM just because you didn't have a user with the same behavior during that temporary switch.

This is basically because CodeIgniter uses persistent database connection by default. This is set by the pconnect option :

$db['default'] = array(
    ...
    'pconnect' => TRUE,
    ...
);

Since the lock is not released by the connection, another page trying to load will wait for the lock to be freed...

You could try to disable the persistent connection. This SO answer gives a pretty good explanation on when and why enabling persistent connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM