MariaDB / mariadb-docker

Docker Official Image packaging for MariaDB
https://mariadb.org
GNU General Public License v2.0
759 stars 438 forks source link

MariaDB performance in Docker is too fast #478

Closed dgsiegel closed 1 year ago

dgsiegel commented 1 year ago

I am currently debugging a very strange issue with Craft CMS and MariaDB. Inside Craft I've got a collection with over 20k entries and running a search query on those entries. Craft produces a quite complicated and convoluted SQL query that takes around 180-200 seconds to complete on my local system (MariaDB 10.5). It takes the same time on other other boxes such as several VMs (Debian and Ubuntu, freshly installed) as well as a few cloud instances I've spun up (DigitalOcean, Uberspace). The database was exported using mysqldump and then imported on the individual MariaDB instances.

Running the same query on the same database inside Docker (e.g docker run -it mariadb:10.5, works with 10.4 and 10.6 too) reduces the query time to about 2 seconds consistently. I've tried to compare the MariaDB settings between that Docker image and my local system but can't find any big difference. Even the following things didn't change anything:

It seems like MariaDB on Docker is mostly stock anyways. Do you have any idea what other factors could play part in this? Unfortunately I am not able to edit or improve the query at this point, so it's mostly about finding out why Docker MariaDB is so much faster... Thanks!

The long query: https://github.com/craftcms/cms/files/9811193/long-query.txt Output of SHOW VARIABLES: https://github.com/craftcms/cms/files/9811023/mysql-variables.txt

EXPLAIN of the query inside Docker:

+------+--------------------+-------------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------+---------+---------------------------------------------------+-------+-----------------------------------------------------------+
| id   | select_type        | table             | type   | possible_keys                                                                                                                                                                                                                                                                                        | key                                      | key_len | ref                                               | rows  | Extra                                                     |
+------+--------------------+-------------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------+---------+---------------------------------------------------+-------+-----------------------------------------------------------+
|    1 | PRIMARY            | elements          | ref    | PRIMARY,idx_meipholhpnizcpmxvbcrbmevwnsnebawnltn,idx_iktcbxftnhyutgzsugumsdsmhzlaolkykdlc,idx_ypauezzubkcibagjgearovprycixuxckjuzr,fk_qhlmsdqdhxwyzhtchtdttjhvipcxedmrvgoc,fk_bcrgdsmcruzgdrlltpmtludqqmstpywtnjuf,idx_zjvmoxsqtycxitwbnuqczrpqkuubzorlimhz,idx_kdfsxejckoixbhyzvjekvjxzkzkbpglrfwwp | idx_kdfsxejckoixbhyzvjekvjxzkzkbpglrfwwp | 17      | const,const,const,const                           | 19389 | Using where; Using index; Using temporary; Using filesort |
|    1 | PRIMARY            | entries           | eq_ref | PRIMARY,idx_pliwvstcbedtxmlgysmllcdsrhlyljttoiid,idx_ldvcibanbubfakyjeipskkesbeshvhppzmvs                                                                                                                                                                                                            | PRIMARY                                  | 4       | bnm.elements.id                                   | 1     | Using where                                               |
|    1 | PRIMARY            | entries           | eq_ref | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                  | 4       | bnm.elements.id                                   | 1     |                                                           |
|    1 | PRIMARY            | elements          | eq_ref | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                  | 4       | bnm.elements.id                                   | 1     |                                                           |
|    1 | PRIMARY            | elements_sites    | eq_ref | PRIMARY,idx_yheraloujqdszecohbrfqnphataqmqqiqpom,idx_wfvbpajifbqjobqvgabeammaiyjndvbowmsy,idx_xcvtdfmcvfgqtynaiclsnlnclhecfcylvjkh                                                                                                                                                                   | idx_yheraloujqdszecohbrfqnphataqmqqiqpom | 8       | bnm.elements.id,const                             | 1     | Using where                                               |
|    1 | PRIMARY            | elements_sites    | eq_ref | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                  | 4       | bnm.elements_sites.id                             | 1     |                                                           |
|    1 | PRIMARY            | content           | eq_ref | PRIMARY,idx_nsvbqtorsofqydjhbgmzwcetxfyxoohgnvua,idx_omhipfiowphtoweivjawvpiwerhyjkyovfuk                                                                                                                                                                                                            | idx_nsvbqtorsofqydjhbgmzwcetxfyxoohgnvua | 8       | bnm.elements.id,const                             | 1     | Using index                                               |
|    1 | PRIMARY            | structureelements | ref    | idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg                                                                                                                                                                                                                                                             | idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg | 5       | bnm.elements.id                                   | 1     | Using where                                               |
|    1 | PRIMARY            | structureelements | eq_ref | idx_egibobxqkbyqfrjejrmdddzwcrcgiuegydfr,idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg                                                                                                                                                                                                                    | idx_egibobxqkbyqfrjejrmdddzwcrcgiuegydfr | 9       | bnm.structureelements.structureId,bnm.elements.id | 1     | Using where                                               |
|    1 | PRIMARY            | content           | eq_ref | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                  | 4       | bnm.content.id                                    | 1     |                                                           |
|    3 | DEPENDENT SUBQUERY | structures        | eq_ref | PRIMARY,idx_okivpybovoibhjdkncizpovelmveaeevosog                                                                                                                                                                                                                                                     | PRIMARY                                  | 4       | bnm.structureelements.structureId                 | 1     | Using where                                               |
+------+--------------------+-------------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------+---------+---------------------------------------------------+-------+-----------------------------------------------------------+
11 rows in set (0.613 sec)

EXPLAIN of the query on my local system:

+------+--------------------+-------------------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+---------+----------------------------------------------------+---------+----------------------------------------------+
| id   | select_type        | table             | type          | possible_keys                                                                                                                                                                                                                                                                                        | key                                                                               | key_len | ref                                                | rows    | Extra                                        |
+------+--------------------+-------------------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+---------+----------------------------------------------------+---------+----------------------------------------------+
|    1 | PRIMARY            | entries           | ALL           | PRIMARY,idx_pliwvstcbedtxmlgysmllcdsrhlyljttoiid,idx_ldvcibanbubfakyjeipskkesbeshvhppzmvs                                                                                                                                                                                                            | NULL                                                                              | NULL    | NULL                                               | 23913   | Using where; Using temporary; Using filesort |
|    1 | PRIMARY            | elements          | eq_ref|filter | PRIMARY,idx_meipholhpnizcpmxvbcrbmevwnsnebawnltn,idx_iktcbxftnhyutgzsugumsdsmhzlaolkykdlc,idx_ypauezzubkcibagjgearovprycixuxckjuzr,fk_qhlmsdqdhxwyzhtchtdttjhvipcxedmrvgoc,fk_bcrgdsmcruzgdrlltpmtludqqmstpywtnjuf,idx_zjvmoxsqtycxitwbnuqczrpqkuubzorlimhz,idx_kdfsxejckoixbhyzvjekvjxzkzkbpglrfwwp | idx_iktcbxftnhyutgzsugumsdsmhzlaolkykdlc|idx_ypauezzubkcibagjgearovprycixuxckjuzr | 5|1     | const,bnm2.entries.id                              | 1 (50%) | Using where; Using rowid filter              |
|    1 | PRIMARY            | structureelements | ref           | idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg                                                                                                                                                                                                                                                             | idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg                                          | 5       | bnm2.entries.id                                    | 1       | Using where                                  |
|    1 | PRIMARY            | structureelements | eq_ref        | idx_egibobxqkbyqfrjejrmdddzwcrcgiuegydfr,idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg                                                                                                                                                                                                                    | idx_egibobxqkbyqfrjejrmdddzwcrcgiuegydfr                                          | 9       | bnm2.structureelements.structureId,bnm2.entries.id | 1       | Using where                                  |
|    1 | PRIMARY            | entries           | eq_ref        | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                                                           | 4       | bnm2.entries.id                                    | 1       |                                              |
|    1 | PRIMARY            | elements          | eq_ref        | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                                                           | 4       | bnm2.entries.id                                    | 1       |                                              |
|    1 | PRIMARY            | content           | eq_ref        | PRIMARY,idx_nsvbqtorsofqydjhbgmzwcetxfyxoohgnvua,idx_omhipfiowphtoweivjawvpiwerhyjkyovfuk                                                                                                                                                                                                            | idx_nsvbqtorsofqydjhbgmzwcetxfyxoohgnvua                                          | 8       | bnm2.entries.id,const                              | 1       | Using index                                  |
|    1 | PRIMARY            | content           | eq_ref        | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                                                           | 4       | bnm2.content.id                                    | 1       |                                              |
|    1 | PRIMARY            | elements_sites    | eq_ref        | PRIMARY,idx_yheraloujqdszecohbrfqnphataqmqqiqpom,idx_wfvbpajifbqjobqvgabeammaiyjndvbowmsy,idx_xcvtdfmcvfgqtynaiclsnlnclhecfcylvjkh                                                                                                                                                                   | idx_yheraloujqdszecohbrfqnphataqmqqiqpom                                          | 8       | bnm2.entries.id,const                              | 1       | Using where                                  |
|    1 | PRIMARY            | elements_sites    | eq_ref        | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                                                           | 4       | bnm2.elements_sites.id                             | 1       |                                              |
|    3 | DEPENDENT SUBQUERY | structures        | eq_ref        | PRIMARY,idx_okivpybovoibhjdkncizpovelmveaeevosog                                                                                                                                                                                                                                                     | PRIMARY                                                                           | 4       | bnm2.structureelements.structureId                 | 1       | Using where                                  |
+------+--------------------+-------------------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+---------+----------------------------------------------------+---------+----------------------------------------------+
11 rows in set (3 min 5.657 sec)

CREATE TABLE instructions of the relevant tables:

DROP TABLE IF EXISTS `elements`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `elements` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `canonicalId` int(11) DEFAULT NULL,
  `draftId` int(11) DEFAULT NULL,
  `revisionId` int(11) DEFAULT NULL,
  `fieldLayoutId` int(11) DEFAULT NULL,
  `type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `enabled` tinyint(1) NOT NULL DEFAULT 1,
  `archived` tinyint(1) NOT NULL DEFAULT 0,
  `dateCreated` datetime NOT NULL,
  `dateUpdated` datetime NOT NULL,
  `dateLastMerged` datetime DEFAULT NULL,
  `dateDeleted` datetime DEFAULT NULL,
  `uid` char(36) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `idx_meipholhpnizcpmxvbcrbmevwnsnebawnltn` (`dateDeleted`),
  KEY `idx_xnyoacrfqwptzuqgouqnfxjyptotsdmswjcv` (`fieldLayoutId`),
  KEY `idx_xdoussqeruutjeisvovrnydkinbwciflauhm` (`type`),
  KEY `idx_iktcbxftnhyutgzsugumsdsmhzlaolkykdlc` (`enabled`),
  KEY `idx_ypauezzubkcibagjgearovprycixuxckjuzr` (`archived`,`dateCreated`),
  KEY `fk_qhlmsdqdhxwyzhtchtdttjhvipcxedmrvgoc` (`draftId`),
  KEY `fk_bcrgdsmcruzgdrlltpmtludqqmstpywtnjuf` (`revisionId`),
  KEY `fk_scflauydurdkdhnyiuhusdkuyajnqvabyklr` (`canonicalId`),
  KEY `idx_zjvmoxsqtycxitwbnuqczrpqkuubzorlimhz` (`archived`,`dateDeleted`,`draftId`,`revisionId`,`canonicalId`),
  KEY `idx_kdfsxejckoixbhyzvjekvjxzkzkbpglrfwwp` (`archived`,`dateDeleted`,`draftId`,`revisionId`,`canonicalId`,`enabled`),
  CONSTRAINT `fk_angnfpaspjgfknkovbofiwhtxwyrsdaqpqnw` FOREIGN KEY (`fieldLayoutId`) REFERENCES `fieldlayouts` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_bcrgdsmcruzgdrlltpmtludqqmstpywtnjuf` FOREIGN KEY (`revisionId`) REFERENCES `revisions` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_qhlmsdqdhxwyzhtchtdttjhvipcxedmrvgoc` FOREIGN KEY (`draftId`) REFERENCES `drafts` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_scflauydurdkdhnyiuhusdkuyajnqvabyklr` FOREIGN KEY (`canonicalId`) REFERENCES `elements` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=145188 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `entries`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `entries` (
  `id` int(11) NOT NULL,
  `sectionId` int(11) NOT NULL,
  `parentId` int(11) DEFAULT NULL,
  `typeId` int(11) NOT NULL,
  `authorId` int(11) DEFAULT NULL,
  `postDate` datetime DEFAULT NULL,
  `expiryDate` datetime DEFAULT NULL,
  `deletedWithEntryType` tinyint(1) DEFAULT NULL,
  `dateCreated` datetime NOT NULL,
  `dateUpdated` datetime NOT NULL,
  `uid` char(36) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `idx_pliwvstcbedtxmlgysmllcdsrhlyljttoiid` (`postDate`),
  KEY `idx_ldvcibanbubfakyjeipskkesbeshvhppzmvs` (`expiryDate`),
  KEY `idx_ifrhedelyvhdtnffwxzsinfdwlwqczkvpgrp` (`authorId`),
  KEY `idx_qyrboztqlmxpoqfqdatmoagchobiveqcqioh` (`sectionId`),
  KEY `idx_qdyfyymdbrjunvaxxxvrgyzwmshzpgiyemen` (`typeId`),
  KEY `fk_ovgfyzqjltfdkgdsalfeetxcjibkbajtasfa` (`parentId`),
  CONSTRAINT `fk_cwdbysfpkswchtotjhfmmlnzpxmzkspbitxw` FOREIGN KEY (`sectionId`) REFERENCES `sections` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_emqtcfzytdhhstjvqbniqirxvnjgnmeiazbb` FOREIGN KEY (`authorId`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_nlycygttqukzwkpeudpsoaihklzofcpmtqro` FOREIGN KEY (`id`) REFERENCES `elements` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_ovgfyzqjltfdkgdsalfeetxcjibkbajtasfa` FOREIGN KEY (`parentId`) REFERENCES `entries` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_wzxxgyyfciflotmydbtbojrpduywduppqyey` FOREIGN KEY (`typeId`) REFERENCES `entrytypes` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `elements_sites`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `elements_sites` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `elementId` int(11) NOT NULL,
  `siteId` int(11) NOT NULL,
  `slug` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `uri` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `enabled` tinyint(1) NOT NULL DEFAULT 1,
  `dateCreated` datetime NOT NULL,
  `dateUpdated` datetime NOT NULL,
  `uid` char(36) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_yheraloujqdszecohbrfqnphataqmqqiqpom` (`elementId`,`siteId`),
  KEY `idx_wfvbpajifbqjobqvgabeammaiyjndvbowmsy` (`siteId`),
  KEY `idx_xljqrwpjrsomwjmzrtiatxttjpoefizlabbq` (`slug`,`siteId`),
  KEY `idx_xcvtdfmcvfgqtynaiclsnlnclhecfcylvjkh` (`enabled`),
  KEY `idx_bspogjjjqswuhonbxbtyllbfvrgnpefqfbky` (`uri`,`siteId`),
  CONSTRAINT `fk_azypvzqdyzxdotkdqcjptcmgknxykztbjqwr` FOREIGN KEY (`elementId`) REFERENCES `elements` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_funlkmitzweokfztronebgwluwsyarfrfhym` FOREIGN KEY (`siteId`) REFERENCES `sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=290369 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `content`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `content` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `elementId` int(11) NOT NULL,
  `siteId` int(11) NOT NULL, 
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `dateCreated` datetime NOT NULL,
  `dateUpdated` datetime NOT NULL,
  `uid` char(36) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `field_copyright` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_description` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_dateStart` datetime DEFAULT NULL,
  `field_dateEnd` datetime DEFAULT NULL,
  `field_objectId` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectLocation` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectDate` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectDescription` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectArtist` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectAcquisition` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectMaterial` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectDimensions` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectLocality` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_pageTitle` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_pageDescription` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectInventory` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_linkField` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_address_abttpidp` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_searchSuggestions_gvvuqshj` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_birthdayTheme_ipabhtqy` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_membershipFee_nnywsiyu` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_rentalLocation_gduyfthw` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_tourLanguage_rilorfsc` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_tourTopic_jyrpfxuu` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_eventAttendees_ntavzfpd` int(11) DEFAULT NULL,
  `field_eventConsent_lnlooeoi` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_newsletterPrivacy_zdagmqed` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectDateStart_lsjrrhtt` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectDateEnd_jlknjmtt` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectLiterature_hiobyknr` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_newsletterPrivacyPress_dzqhbude` text COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_objectProvenance_kivrsjot` text COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_nsvbqtorsofqydjhbgmzwcetxfyxoohgnvua` (`elementId`,`siteId`),
  KEY `idx_omhipfiowphtoweivjawvpiwerhyjkyovfuk` (`siteId`),
  KEY `idx_ixqeuiohxfnjthyydyiauegogxqwehnclbre` (`title`),
  CONSTRAINT `fk_fiwuxwtdevdmzyvivtruowlxkhogdxhkyyht` FOREIGN KEY (`elementId`) REFERENCES `elements` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_szczzgjaxziipjxhxmzabmealykieehwhmdy` FOREIGN KEY (`siteId`) REFERENCES `sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=63400 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `structureelements`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `structureelements` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `structureId` int(11) NOT NULL, 
  `elementId` int(11) DEFAULT NULL,
  `root` int(10) unsigned DEFAULT NULL,
  `lft` int(10) unsigned NOT NULL,
  `rgt` int(10) unsigned NOT NULL,
  `level` smallint(5) unsigned NOT NULL,
  `dateCreated` datetime NOT NULL,
  `dateUpdated` datetime NOT NULL,
  `uid` char(36) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_egibobxqkbyqfrjejrmdddzwcrcgiuegydfr` (`structureId`,`elementId`),
  KEY `idx_kwpswngmxcxenffhxnxxrbwpamjlfcrmugyq` (`root`),
  KEY `idx_cappnjqpwsytjichbslxnzenxrsmsfamaajv` (`lft`),
  KEY `idx_jubqdghtvhqghgfzeqailesnalzghzkmcjzd` (`rgt`),
  KEY `idx_tgpbmvoemdcnkffbukddnshrphspzpqpctwu` (`level`),
  KEY `idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg` (`elementId`),
  CONSTRAINT `fk_bameomxbegbdvqwsuvptsrwhlyeerwumheyf` FOREIGN KEY (`structureId`) REFERENCES `structures` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_cmlbiidqacpfnyjrieztnnujujswnwjjwhtj` FOREIGN KEY (`elementId`) REFERENCES `elements` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2091 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `structures`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `structures` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `maxLevels` smallint(5) unsigned DEFAULT NULL,
  `dateCreated` datetime NOT NULL,
  `dateUpdated` datetime NOT NULL,
  `dateDeleted` datetime DEFAULT NULL,
  `uid` char(36) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `idx_okivpybovoibhjdkncizpovelmveaeevosog` (`dateDeleted`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
grooverdan commented 1 year ago

I'm not sure either. my_print_defaults --mysqld will show non-default configuration quickly. Are other forms distro package or MariaDB packages?

Try optimizer trace to see differences.

If this doesn't help I'll look a little later.

dgsiegel commented 1 year ago

I'm not sure either. my_print_defaults --mysqld will show non-default configuration quickly. Are other forms distro package or MariaDB packages?

No I am using the default distro packages. Nothing custom installed.

Local:

# my_print_defaults --mysqld
--datadir=/var/lib/mysql
--socket=/var/lib/mysql/mysql.sock
--skip-host-cache
--skip-name-resolve
--log-error=/var/log/mariadb/mariadb.log
--pid-file=/run/mariadb/mariadb.pid

Docker:

# my_print_defaults --mysqld
--socket=/run/mysqld/mysqld.sock
--skip-host-cache
--skip-name-resolve
--pid-file=/run/mysqld/mysqld.pid
--basedir=/usr
--expire_logs_days=10
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci

Try optimizer trace to see differences.

Local: optimizer.txt

Docker: optimizer-docker.txt

grooverdan commented 1 year ago

It looks like statistical samples caused the container instance to use elements before entries while the local didn't.

Could try analyze table on the local instance of elements and entries to see if that affects the query plan.

With your IN list appearing in order, the order by FIELD(element.id, ...) could just be order by element.id right?

dgsiegel commented 1 year ago

It looks like statistical samples caused the container instance to use elements before entries while the local didn't.

Right, but I haven't found a way to change that. Or the reason why it does that on all other instances, except Docker.

Could try analyze table on the local instance of elements and entries to see if that affects the query plan.

Sure, this is the output:

> ANALYZE TABLE elements PERSISTENT FOR ALL;
+---------------+---------+----------+-----------------------------------------+
| Table         | Op      | Msg_type | Msg_text                                |
+---------------+---------+----------+-----------------------------------------+
| bnm2.elements | analyze | status   | Engine-independent statistics collected |
| bnm2.elements | analyze | status   | OK                                      |
+---------------+---------+----------+-----------------------------------------+
2 rows in set (0.925 sec)

> ANALYZE TABLE entries PERSISTENT FOR ALL;
+--------------+---------+----------+-----------------------------------------+
| Table        | Op      | Msg_type | Msg_text                                |
+--------------+---------+----------+-----------------------------------------+
| bnm2.entries | analyze | status   | Engine-independent statistics collected |
| bnm2.entries | analyze | status   | OK                                      |
+--------------+---------+----------+-----------------------------------------+
2 rows in set (0.326 sec)

Unfortunately this didn't change anything. I have then analyzed all tables in the DB, but this didn't have an impact either.

With your IN list appearing in order, the order by FIELD(element.id, ...) could just be order by element.id right?

In this case yes, as the specific search query ny the CMS shows all entries. But normally, not all elements should appear there, but only the matching ones.

RitterKnightCreative commented 1 year ago

Your character-set-server=utf8mb4 and collation-server=utf8mb4_general_ci looks like are being set on docker but not local?

dgsiegel commented 1 year ago

Your character-set-server=utf8mb4 and collation-server=utf8mb4_general_ci looks like are being set on docker but not local?

That seems to be it! character-set-server was set to latin1 and collation-server was set to latin1_swedish_ci on my local system. By setting both to utf8mb4 and utf8mb4_general_ci I get this:

22004 rows in set (1,787 sec)

Looking at EXPLAIN it follows a slightly different path than the Docker one, but at least it doesn't have to iterate over the entries table:

+------+--------------------+-------------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------+---------+------------------------------------------------------------------+-------+----------------------------------------------+
| id   | select_type        | table             | type   | possible_keys                                                                                                                                                                                                                                                                                        | key                                      | key_len | ref                                                              | rows  | Extra                                        |
+------+--------------------+-------------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------+---------+------------------------------------------------------------------+-------+----------------------------------------------+
|    1 | PRIMARY            | elements_sites    | ALL    | PRIMARY,idx_yheraloujqdszecohbrfqnphataqmqqiqpom,idx_wfvbpajifbqjobqvgabeammaiyjndvbowmsy,idx_xcvtdfmcvfgqtynaiclsnlnclhecfcylvjkh                                                                                                                                                                   | NULL                                     | NULL    | NULL                                                             | 77343 | Using where; Using temporary; Using filesort |
|    1 | PRIMARY            | entries           | eq_ref | PRIMARY,idx_pliwvstcbedtxmlgysmllcdsrhlyljttoiid,idx_ldvcibanbubfakyjeipskkesbeshvhppzmvs                                                                                                                                                                                                            | PRIMARY                                  | 4       | bnm2.elements_sites.elementId                                    | 1     | Using where                                  |
|    1 | PRIMARY            | entries           | eq_ref | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                  | 4       | bnm2.elements_sites.elementId                                    | 1     |                                              |
|    1 | PRIMARY            | elements          | eq_ref | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                  | 4       | bnm2.elements_sites.elementId                                    | 1     |                                              |
|    1 | PRIMARY            | content           | eq_ref | PRIMARY,idx_nsvbqtorsofqydjhbgmzwcetxfyxoohgnvua,idx_omhipfiowphtoweivjawvpiwerhyjkyovfuk                                                                                                                                                                                                            | idx_nsvbqtorsofqydjhbgmzwcetxfyxoohgnvua | 8       | bnm2.elements_sites.elementId,const                              | 1     | Using index                                  |
|    1 | PRIMARY            | content           | eq_ref | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                  | 4       | bnm2.content.id                                                  | 1     |                                              |
|    1 | PRIMARY            | elements          | eq_ref | PRIMARY,idx_meipholhpnizcpmxvbcrbmevwnsnebawnltn,idx_iktcbxftnhyutgzsugumsdsmhzlaolkykdlc,idx_ypauezzubkcibagjgearovprycixuxckjuzr,fk_qhlmsdqdhxwyzhtchtdttjhvipcxedmrvgoc,fk_bcrgdsmcruzgdrlltpmtludqqmstpywtnjuf,idx_zjvmoxsqtycxitwbnuqczrpqkuubzorlimhz,idx_kdfsxejckoixbhyzvjekvjxzkzkbpglrfwwp | PRIMARY                                  | 4       | bnm2.elements_sites.elementId                                    | 1     | Using where                                  |
|    1 | PRIMARY            | structureelements | ref    | idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg                                                                                                                                                                                                                                                             | idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg | 5       | bnm2.elements_sites.elementId                                    | 1     | Using where                                  |
|    1 | PRIMARY            | structureelements | eq_ref | idx_egibobxqkbyqfrjejrmdddzwcrcgiuegydfr,idx_hbtojdjueqltcgmauyzlqoxwkptqcwjxamfg                                                                                                                                                                                                                    | idx_egibobxqkbyqfrjejrmdddzwcrcgiuegydfr | 9       | bnm2.structureelements.structureId,bnm2.elements_sites.elementId | 1     | Using where                                  |
|    1 | PRIMARY            | elements_sites    | eq_ref | PRIMARY                                                                                                                                                                                                                                                                                              | PRIMARY                                  | 4       | bnm2.elements_sites.id                                           | 1     |                                              |
|    3 | DEPENDENT SUBQUERY | structures        | eq_ref | PRIMARY,idx_okivpybovoibhjdkncizpovelmveaeevosog                                                                                                                                                                                                                                                     | PRIMARY                                  | 4       | bnm2.structureelements.structureId                               | 1     | Using where                                  |
+------+--------------------+-------------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------+---------+------------------------------------------------------------------+-------+----------------------------------------------+
11 rows in set (0,170 sec)

Thanks @RitterKnightCreative for spotting this!!