These issues seems to be getting hotter and hotter, especially since MODx 2.6 is using InnoDB as it's default storage engine and more and more people are complaining about the same issues:
- Change new installs to create tables with InnoDB engine on mysql [#13462]
The team didn't make any changes in the table/session design which should be considered when switching from MyISAM to InnoDB. (
https://mariadb.com/kb/en/library/converting-tables-from-myisam-to-innodb/)
I am now going back and forth for more than 4 weeks with these issues, debugging, switching my.cnf configuration files (playing with InnoDB buffer size), analyzing queries, explain queries, checking my InnoDB Engine status but in the end it all breaks down to session related Inserts and Updates of my MODx session tables. The queries get stuck in query_end state as you can see in this example:
MariaDB [modx]> SHOW PROFILE FOR QUERY 216;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000086 |
| checking permissions | 0.000010 |
| Opening tables | 0.000027 |
| After opening tables | 0.000013 |
| System lock | 0.000006 |
| Table lock | 0.000006 |
| init | 0.000071 |
| updating | 0.000099 |
| end | 0.000008 |
| query end | 4.061019 |
| closing tables | 0.000032 |
| Unlocking tables | 0.000022 |
| freeing items | 0.000012 |
| updating status | 0.000028 |
| logging slow query | 0.000155 |
| cleaning up | 0.000026 |
+----------------------+----------+
Digging in deeper this happens when the db server stalls/flushes/re-organizes the index. When we take a look at the MODx sessions table with SHOW CREATE TABLE modx_sessions:
CREATE TABLE `modx_session` (
`id` varchar(191) NOT NULL DEFAULT '',
`access` int(20) unsigned NOT NULL,
`data` mediumtext DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `access` (`access`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
We can see that id is not using `auto_increment` and `DEFAULT ''` which brings me back to the InnoDB and clustered indexex issue from my opening post. This sessions PK structure seems to be keeping InnoDB busy, especially when a lot of INSERTS and UPDATES are going on, either with a high-traffic installation or multiple low to mid-sized MODx installations. Obviously MyISAM was more solid/faster for this workflow...
From my dba stack thread:
For every row added to modx_session table, you can expect it to take more time for any INSERT, UPDATE or DELETE as long as the ID is not UNIQUE (which it isn't because auto-increment is not used). Give this a try on just one table that is listed in slow log and then use EXPLAIN to confirm you went from RANGE type to EQ_REF type and you will get away from 100% filtering [..]
Of course not all MODx default tables should be re-structured, but when MODx now is using InnoDB as their default engine this should be taken care of. Maybe set session driver to file by default or re-structure sessions table...
Also people that are just hosting one MODx installation on their server/webspace won't see any significant slowdowns, but in my case I am running about 10 MODx installations an my slowlogs exploded with slow queries related to sessions after switching from MyISAM to InnoDB. Here's what I did to bypass this issue (starting from easiest to most complicated solutions):
- disabled session for the whole installation (if not needed)
- disabled anonymous_session (if not needed)
- converted session related tables back to MyISAM
- switched session from database to file driver (definitely faster than InnoDB)
- switched session from database to redis driver (best and fastest solution, but complicated to setup)
Regarding your case: anonymous slow, authenticated fast => Maybe MODx recovered already existing sessions for authenticated users which processed faster then INSERTING new sessions...I am not really sure.
PS: If you see slowlogs for other tables than session, check the PK/Index and the create statements. InnoDB is also slow with text type fields and try to analyze the table if the next auto_increment (e.g. 560) and the cardinality (e.g. 230) have a huge difference.
It is not an ideal solution but my slowlog is empty again and Redis Session Storage has the best performance so far.