[MDEV-28171] Query regression from 10.4.22 to 10.4.24 Created: 2022-03-25  Updated: 2022-07-09

Status: Open
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.4.24
Fix Version/s: 10.4

Type: Bug Priority: Critical
Reporter: Manuel Arostegui Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 1
Labels: optimizer-watch
Environment:

debian bullseye



 Description   

We have noticed the optimizer preferring a full scan rather than the proper index on a query when upgrading from 10.4.22 to 10.4.24

root@db1123.eqiad.wmnet[mediawikiwiki]> select @@version ; nopager; explain SELECT  rc_id,rc_timestamp,rc_namespace,rc_title,rc_minor,rc_bot,rc_new,rc_cur_id,rc_this_oldid,rc_last_oldid,rc_type,rc_source,rc_patrolled,rc_ip,rc_old_len,rc_new_len,rc_deleted,rc_logid,rc_log_type,rc_log_action,rc_params,rc_actor,recentchanges_actor.actor_user AS `rc_user`,recentchanges_actor.actor_name AS `rc_user_text`,comment_rc_comment.comment_text AS `rc_comment_text`,comment_rc_comment.comment_data AS `rc_comment_data`,comment_rc_comment.comment_id AS `rc_comment_cid`,rc_title,rc_namespace,wl_user,wl_notificationtimestamp,we_expiry,page_latest,(SELECT  GROUP_CONCAT(ctd_name SEPARATOR ',')  FROM `change_tag` JOIN `change_tag_def` ON ((ct_tag_id=ctd_id))   WHERE ct_rc_id=rc_id  ) AS `ts_tags`  FROM `recentchanges` JOIN `actor` `recentchanges_actor` ON ((actor_id=rc_actor)) JOIN `comment` `comment_rc_comment` ON ((comment_rc_comment.comment_id = rc_comment_id)) LEFT JOIN `watchlist` ON (wl_user = 14096723 AND (wl_title=rc_title) AND (wl_namespace=rc_namespace)) LEFT JOIN `watchlist_expiry` ON ((wl_id = we_item)) LEFT JOIN `page` ON ((rc_cur_id=page_id))   WHERE rc_bot = 0 AND (rc_type != 6) AND (rc_source != 'wb') AND (rc_namespace NOT IN (1198,1199)) AND (rc_timestamp >= '20220316090254') AND rc_new IN (0,1)   ORDER BY rc_timestamp DESC LIMIT 100;
+---------------------+
| @@version           |
+---------------------+
| 10.4.22-MariaDB-log |
+---------------------+
1 row in set (0.001 sec)
 
PAGER set to stdout
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+-------+-------------+
| id   | select_type        | table               | type   | possible_keys                                                                                                         | key          | key_len | ref                                                                                 | rows  | Extra       |
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+-------+-------------+
|    1 | PRIMARY            | recentchanges       | range  | rc_timestamp,rc_name_type_patrolled_timestamp,rc_ns_actor,rc_actor,rc_namespace_title_timestamp,rc_new_name_timestamp | rc_timestamp | 14      | NULL                                                                                | 33098 | Using where |
|    1 | PRIMARY            | watchlist           | eq_ref | wl_user,wl_user_notificationtimestamp,wl_namespace_title                                                              | wl_user      | 265     | const,mediawikiwiki.recentchanges.rc_namespace,mediawikiwiki.recentchanges.rc_title | 1     |             |
|    1 | PRIMARY            | watchlist_expiry    | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.watchlist.wl_id                                                       | 1     | Using where |
|    1 | PRIMARY            | page                | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.recentchanges.rc_cur_id                                               | 1     |             |
|    1 | PRIMARY            | comment_rc_comment  | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 8       | mediawikiwiki.recentchanges.rc_comment_id                                           | 1     |             |
|    1 | PRIMARY            | recentchanges_actor | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 8       | mediawikiwiki.recentchanges.rc_actor                                                | 1     |             |
|    2 | DEPENDENT SUBQUERY | change_tag          | ref    | ct_rc_tag_id,ct_tag_id_id                                                                                             | ct_rc_tag_id | 5       | mediawikiwiki.recentchanges.rc_id                                                   | 1     | Using index |
|    2 | DEPENDENT SUBQUERY | change_tag_def      | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.change_tag.ct_tag_id                                                  | 1     |             |
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+-------+-------------+
8 rows in set (0.002 sec)

The query on 10.4.22 takes 0.03 and on 10.4.24 the optimizer picks no index and the query takes more than 60 seconds.

root@db1112.eqiad.wmnet[mediawikiwiki]> select @@version ; nopager; explain SELECT  rc_id,rc_timestamp,rc_namespace,rc_title,rc_minor,rc_bot,rc_new,rc_cur_id,rc_this_oldid,rc_last_oldid,rc_type,rc_source,rc_patrolled,rc_ip,rc_old_len,rc_new_len,rc_deleted,rc_logid,rc_log_type,rc_log_action,rc_params,rc_actor,recentchanges_actor.actor_user AS `rc_user`,recentchanges_actor.actor_name AS `rc_user_text`,comment_rc_comment.comment_text AS `rc_comment_text`,comment_rc_comment.comment_data AS `rc_comment_data`,comment_rc_comment.comment_id AS `rc_comment_cid`,rc_title,rc_namespace,wl_user,wl_notificationtimestamp,we_expiry,page_latest,(SELECT  GROUP_CONCAT(ctd_name SEPARATOR ',')  FROM `change_tag` JOIN `change_tag_def` ON ((ct_tag_id=ctd_id))   WHERE ct_rc_id=rc_id  ) AS `ts_tags`  FROM `recentchanges` JOIN `actor` `recentchanges_actor` ON ((actor_id=rc_actor)) JOIN `comment` `comment_rc_comment` ON ((comment_rc_comment.comment_id = rc_comment_id)) LEFT JOIN `watchlist` ON (wl_user = 14096723 AND (wl_title=rc_title) AND (wl_namespace=rc_namespace)) LEFT JOIN `watchlist_expiry` ON ((wl_id = we_item)) LEFT JOIN `page` ON ((rc_cur_id=page_id))   WHERE rc_bot = 0 AND (rc_type != 6) AND (rc_source != 'wb') AND (rc_namespace NOT IN (1198,1199)) AND (rc_timestamp >= '20220316090254') AND rc_new IN (0,1)   ORDER BY rc_timestamp DESC LIMIT 100;
+---------------------+
| @@version           |
+---------------------+
| 10.4.24-MariaDB-log |
+---------------------+
1 row in set (0.000 sec)
 
PAGER set to stdout
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+----------+------------------------------------+
| id   | select_type        | table               | type   | possible_keys                                                                                                         | key          | key_len | ref                                                                                 | rows     | Extra                              |
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+----------+------------------------------------+
|    1 | PRIMARY            | recentchanges_actor | ALL    | PRIMARY                                                                                                               | NULL         | NULL    | NULL                                                                                | 15842120 | Using temporary; Using filesort    |
|    1 | PRIMARY            | recentchanges       | ref    | rc_timestamp,rc_name_type_patrolled_timestamp,rc_ns_actor,rc_actor,rc_namespace_title_timestamp,rc_new_name_timestamp | rc_actor     | 8       | mediawikiwiki.recentchanges_actor.actor_id                                          | 4        | Using index condition; Using where |
|    1 | PRIMARY            | comment_rc_comment  | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 8       | mediawikiwiki.recentchanges.rc_comment_id                                           | 1        |                                    |
|    1 | PRIMARY            | watchlist           | eq_ref | wl_user,wl_user_notificationtimestamp,wl_namespace_title                                                              | wl_user      | 265     | const,mediawikiwiki.recentchanges.rc_namespace,mediawikiwiki.recentchanges.rc_title | 1        |                                    |
|    1 | PRIMARY            | watchlist_expiry    | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.watchlist.wl_id                                                       | 1        | Using where                        |
|    1 | PRIMARY            | page                | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.recentchanges.rc_cur_id                                               | 1        |                                    |
|    2 | DEPENDENT SUBQUERY | change_tag          | ref    | ct_rc_tag_id,ct_tag_id_id                                                                                             | ct_rc_tag_id | 5       | mediawikiwiki.recentchanges.rc_id                                                   | 1        | Using index                        |
|    2 | DEPENDENT SUBQUERY | change_tag_def      | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.change_tag.ct_tag_id                                                  | 1        |                                    |
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+----------+------------------------------------+
8 rows in set (0.003 sec)

We've optimized, analyzed and fully rebuilt the table but that made no difference for the optimizer (or the stats). It keeps choosing to do a full scan.

This is the analyze:

| {
  "query_block": {
    "select_id": 1,
    "r_loops": 1,
    "r_total_time_ms": 24957,
    "filesort": {
      "sort_key": "recentchanges.rc_timestamp desc",
      "r_loops": 1,
      "r_total_time_ms": 5.0222,
      "r_limit": 100,
      "r_used_priority_queue": true,
      "r_output_rows": 101,
      "temporary_table": {
        "table": {
          "table_name": "recentchanges_actor",
          "access_type": "ALL",
          "possible_keys": ["PRIMARY"],
          "r_loops": 1,
          "rows": 15842121,
          "r_rows": 1.78e7,
          "r_total_time_ms": 3924.8,
          "filtered": 100,
          "r_filtered": 100
        },
        "table": {
          "table_name": "recentchanges",
          "access_type": "ref",
          "possible_keys": [
            "rc_timestamp",
            "rc_name_type_patrolled_timestamp",
            "rc_ns_actor",
            "rc_actor",
            "rc_namespace_title_timestamp",
            "rc_new_name_timestamp"
          ],
          "key": "rc_actor",
          "key_length": "8",
          "used_key_parts": ["rc_actor"],
          "ref": ["mediawikiwiki.recentchanges_actor.actor_id"],
          "r_loops": 17788620,
          "rows": 9,
          "r_rows": 9.8e-4,
          "r_total_time_ms": 19329,
          "filtered": 100,
          "r_filtered": 47.244,
          "index_condition": "recentchanges.rc_timestamp >= '20220316090254'",
          "attached_condition": "recentchanges.rc_bot = 0 and recentchanges.rc_type <> 6 and recentchanges.rc_source <> 'wb' and recentchanges.rc_namespace not in (1198,1199) and recentchanges.rc_new in (0,1)"
        },
        "table": {
          "table_name": "comment_rc_comment",
          "access_type": "eq_ref",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "8",
          "used_key_parts": ["comment_id"],
          "ref": ["mediawikiwiki.recentchanges.rc_comment_id"],
          "r_loops": 8270,
          "rows": 1,
          "r_rows": 1,
          "r_total_time_ms": 12.884,
          "filtered": 100,
          "r_filtered": 100
        },
        "table": {
          "table_name": "watchlist",
          "access_type": "eq_ref",
          "possible_keys": [
            "wl_user",
            "wl_user_notificationtimestamp",
            "wl_namespace_title"
          ],
          "key": "wl_user",
          "key_length": "265",
          "used_key_parts": ["wl_user", "wl_namespace", "wl_title"],
          "ref": [
            "const",
            "mediawikiwiki.recentchanges.rc_namespace",
            "mediawikiwiki.recentchanges.rc_title"
          ],
          "r_loops": 8270,
          "rows": 1,
          "r_rows": 0.017,
          "r_total_time_ms": 16.65,
          "filtered": 100,
          "r_filtered": 100
        },
        "table": {
          "table_name": "watchlist_expiry",
          "access_type": "eq_ref",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "4",
          "used_key_parts": ["we_item"],
          "ref": ["mediawikiwiki.watchlist.wl_id"],
          "r_loops": 8270,
          "rows": 1,
          "r_rows": 0,
          "r_total_time_ms": 0.1871,
          "filtered": 100,
          "r_filtered": 100,
          "attached_condition": "trigcond(trigcond(watchlist.wl_id is not null))"
        },
        "table": {
          "table_name": "page",
          "access_type": "eq_ref",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "4",
          "used_key_parts": ["page_id"],
          "ref": ["mediawikiwiki.recentchanges.rc_cur_id"],
          "r_loops": 8270,
          "rows": 1,
          "r_rows": 0.6671,
          "r_total_time_ms": 10.732,
          "filtered": 100,
          "r_filtered": 100
        },
        "subqueries": [
          {
            "expression_cache": {
              "state": "disabled",
              "r_loops": 0,
              "query_block": {
                "select_id": 2,
                "r_loops": 8270,
                "r_total_time_ms": 27.913,
                "table": {
                  "table_name": "change_tag",
                  "access_type": "ref",
                  "possible_keys": ["ct_rc_tag_id", "ct_tag_id_id"],
                  "key": "ct_rc_tag_id",
                  "key_length": "5",
                  "used_key_parts": ["ct_rc_id"],
                  "ref": ["mediawikiwiki.recentchanges.rc_id"],
                  "r_loops": 8270,
                  "rows": 1,
                  "r_rows": 0.3435,
                  "r_total_time_ms": 15.135,
                  "filtered": 100,
                  "r_filtered": 100,
                  "using_index": true
                },
                "table": {
                  "table_name": "change_tag_def",
                  "access_type": "eq_ref",
                  "possible_keys": ["PRIMARY"],
                  "key": "PRIMARY",
                  "key_length": "4",
                  "used_key_parts": ["ctd_id"],
                  "ref": ["mediawikiwiki.change_tag.ct_tag_id"],
                  "r_loops": 2841,
                  "rows": 1,
                  "r_rows": 1,
                  "r_total_time_ms": 2.492,
                  "filtered": 100,
                  "r_filtered": 100
                }
              }
            }
          }
        ]
      }
    }
  }
} |

Table:

CREATE TABLE `recentchanges` (
  `rc_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `rc_timestamp` binary(14) NOT NULL,
  `rc_actor` bigint(20) unsigned NOT NULL,
  `rc_namespace` int(11) NOT NULL DEFAULT 0,
  `rc_title` varbinary(255) NOT NULL DEFAULT '',
  `rc_comment_id` bigint(20) unsigned NOT NULL,
  `rc_minor` tinyint(3) unsigned NOT NULL DEFAULT 0,
  `rc_bot` tinyint(3) unsigned NOT NULL DEFAULT 0,
  `rc_new` tinyint(3) unsigned NOT NULL DEFAULT 0,
  `rc_cur_id` int(10) unsigned NOT NULL DEFAULT 0,
  `rc_this_oldid` int(10) unsigned NOT NULL DEFAULT 0,
  `rc_last_oldid` int(10) unsigned NOT NULL DEFAULT 0,
  `rc_type` tinyint(3) unsigned NOT NULL DEFAULT 0,
  `rc_source` varbinary(16) NOT NULL DEFAULT '',
  `rc_patrolled` tinyint(3) unsigned NOT NULL DEFAULT 0,
  `rc_ip` varbinary(40) NOT NULL DEFAULT '',
  `rc_old_len` int(10) DEFAULT NULL,
  `rc_new_len` int(10) DEFAULT NULL,
  `rc_deleted` tinyint(1) unsigned NOT NULL DEFAULT 0,
  `rc_logid` int(10) unsigned NOT NULL DEFAULT 0,
  `rc_log_type` varbinary(255) DEFAULT NULL,
  `rc_log_action` varbinary(255) DEFAULT NULL,
  `rc_params` blob DEFAULT NULL,
  PRIMARY KEY (`rc_id`),
  KEY `rc_timestamp` (`rc_timestamp`),
  KEY `rc_cur_id` (`rc_cur_id`),
  KEY `rc_ip` (`rc_ip`),
  KEY `rc_name_type_patrolled_timestamp` (`rc_namespace`,`rc_type`,`rc_patrolled`,`rc_timestamp`),
  KEY `rc_ns_actor` (`rc_namespace`,`rc_actor`),
  KEY `rc_actor` (`rc_actor`,`rc_timestamp`),
  KEY `rc_namespace_title_timestamp` (`rc_namespace`,`rc_title`,`rc_timestamp`),
  KEY `rc_this_oldid` (`rc_this_oldid`),
  KEY `rc_new_name_timestamp` (`rc_new`,`rc_namespace`,`rc_timestamp`)
) ENGINE=InnoDB AUTO_INCREMENT=6896299 DEFAULT CHARSET=binary



 Comments   
Comment by Manuel Arostegui [ 2022-03-25 ]

I have changed the priority for this bug as this is probably making us downgrading to 10.4.22 in production

Comment by Sergei Petrunia [ 2022-03-25 ]

Formatted query:

explain 
SELECT
  rc_id,rc_timestamp,rc_namespace,rc_title,rc_minor,
  rc_bot,rc_new,rc_cur_id,rc_this_oldid,rc_last_oldid,
  rc_type,rc_source,rc_patrolled,rc_ip,rc_old_len,
  rc_new_len,rc_deleted,rc_logid,rc_log_type,
  rc_log_action,rc_params,rc_actor,
  recentchanges_actor.actor_user AS `rc_user`,
  recentchanges_actor.actor_name AS `rc_user_text`,
  comment_rc_comment.comment_text AS `rc_comment_text`,
  comment_rc_comment.comment_data AS `rc_comment_data`,
  comment_rc_comment.comment_id AS `rc_comment_cid`,
  rc_title,rc_namespace,wl_user,wl_notificationtimestamp,
  we_expiry,page_latest,
  (SELECT  
     GROUP_CONCAT(ctd_name SEPARATOR ',')  
     FROM `change_tag` JOIN `change_tag_def` ON ((ct_tag_id=ctd_id))   
     WHERE ct_rc_id=rc_id  
  ) AS `ts_tags`  
FROM 
  `recentchanges` 
  JOIN `actor` `recentchanges_actor` ON ((actor_id=rc_actor)) 
  JOIN `comment` `comment_rc_comment` ON ((comment_rc_comment.comment_id = rc_comment_id)) 
  LEFT JOIN `watchlist` ON (wl_user = 14096723 AND (wl_title=rc_title) AND (wl_namespace=rc_namespace)) 
  LEFT JOIN `watchlist_expiry` ON ((wl_id = we_item)) 
  LEFT JOIN `page` ON ((rc_cur_id=page_id))   
WHERE 
  rc_bot = 0 AND 
  (rc_type != 6) AND 
  (rc_source != 'wb') AND 
  (rc_namespace NOT IN (1198,1199)) AND 
  (rc_timestamp >= '20220316090254') AND 
  rc_new IN (0,1)   
ORDER BY 
  rc_timestamp DESC LIMIT 100;

Comment by Sergei Petrunia [ 2022-03-25 ]

Looking at the differences in EXPLAINs:

10.4.22 (good plan)

+------+---------+---------------------+--------+--------------+------------------------+-------+-------------+
| id   | select_t| table               | type   | key          | ref                    | rows  | Extra       |
+------+---------+---------------------+--------+--------------+------------------------+-------+-------------+
|    1 | PRIMARY | recentchanges       | range  | rc_timestamp | NULL                   | 33098 | Using where |
 
...
|    1 | PRIMARY | recentchanges_actor | eq_ref | PRIMARY      | recentchanges.rc_actor | 1     |             |
...
+------+---------+---------------------+--------+--------------+------------------------+-------+-------------+

10.4.24 (bad plan):

+------+---------+---------------------+------+----------+---------+------------------------------+----------+------------------------------------+
| id   | select_t| table               | type | key      | key_len | ref                          | rows     | Extra                              |
+------+---------+---------------------+------+----------+---------+------------------------------+----------+------------------------------------+
|    1 | PRIMARY | recentchanges_actor | ALL  | NULL     | NULL    | NULL                         | 15842120 | Using temporary; Using filesort    |
|    1 | PRIMARY | recentchanges       | ref  | rc_actor | 8       | recentchanges_actor.actor_id | 4        | Using index condition; Using where |
...
+------+---------+---------------------+------+----------+---------+------------------------------+----------+------------------------------------+

Note that they use different join orders.
10.4.22 benefits from ability to short-cut the execution for ORDER BY ... LIMIT...
10.4.24 has to do sorting and so computes the whole query plan.

Comment by Sergei Petrunia [ 2022-03-25 ]

Looked through the commits between 10.4.22 and 10.4.24... I didn't see anything that would cause this behavior...

Comment by Sergei Petrunia [ 2022-03-25 ]

marostegui, I have two requests:

  • could you please run EXPLAIN for the query without the ORDER BY ... LIMIT clause? (The output from 10.4.24 is of most interest, but the output of 10.4.22 is interesting, too).
  • Could you please get the optimizer trace for the EXPLAIN for both versions? This would help alot with the analysis...
Comment by Manuel Arostegui [ 2022-03-25 ]

Absolutely!
10.4.24 version:

root@db1112.eqiad.wmnet[mediawikiwiki]> select @@version;explain  SELECT   rc_id,rc_timestamp,rc_namespace,rc_title,rc_minor,   rc_bot,rc_new,rc_cur_id,rc_this_oldid,rc_last_oldid,   rc_type,rc_source,rc_patrolled,rc_ip,rc_old_len,   rc_new_len,rc_deleted,rc_logid,rc_log_type,   rc_log_action,rc_params,rc_actor,   recentchanges_actor.actor_user AS `rc_user`,   recentchanges_actor.actor_name AS `rc_user_text`,   comment_rc_comment.comment_text AS `rc_comment_text`,   comment_rc_comment.comment_data AS `rc_comment_data`,   comment_rc_comment.comment_id AS `rc_comment_cid`,   rc_title,rc_namespace,wl_user,wl_notificationtimestamp,   we_expiry,page_latest,   (SELECT        GROUP_CONCAT(ctd_name SEPARATOR ',')        FROM `change_tag` JOIN `change_tag_def` ON ((ct_tag_id=ctd_id))         WHERE ct_rc_id=rc_id     ) AS `ts_tags`   FROM    `recentchanges`    JOIN `actor` `recentchanges_actor` ON ((actor_id=rc_actor))    JOIN `comment` `comment_rc_comment` ON ((comment_rc_comment.comment_id = rc_comment_id))    LEFT JOIN `watchlist` ON (wl_user = 14096723 AND (wl_title=rc_title) AND (wl_namespace=rc_namespace))    LEFT JOIN `watchlist_expiry` ON ((wl_id = we_item))    LEFT JOIN `page` ON ((rc_cur_id=page_id))    WHERE    rc_bot = 0 AND    (rc_type != 6) AND    (rc_source != 'wb') AND    (rc_namespace NOT IN (1198,1199)) AND    (rc_timestamp >= '20220316090254') AND    rc_new IN (0,1);
+---------------------+
| @@version           |
+---------------------+
| 10.4.24-MariaDB-log |
+---------------------+
1 row in set (0.001 sec)
 
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+----------+------------------------------------+
| id   | select_type        | table               | type   | possible_keys                                                                                                         | key          | key_len | ref                                                                                 | rows     | Extra                              |
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+----------+------------------------------------+
|    1 | PRIMARY            | recentchanges_actor | ALL    | PRIMARY                                                                                                               | NULL         | NULL    | NULL                                                                                | 15842278 |                                    |
|    1 | PRIMARY            | recentchanges       | ref    | rc_timestamp,rc_name_type_patrolled_timestamp,rc_ns_actor,rc_actor,rc_namespace_title_timestamp,rc_new_name_timestamp | rc_actor     | 8       | mediawikiwiki.recentchanges_actor.actor_id                                          | 9        | Using index condition; Using where |
|    1 | PRIMARY            | comment_rc_comment  | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 8       | mediawikiwiki.recentchanges.rc_comment_id                                           | 1        |                                    |
|    1 | PRIMARY            | watchlist           | eq_ref | wl_user,wl_user_notificationtimestamp,wl_namespace_title                                                              | wl_user      | 265     | const,mediawikiwiki.recentchanges.rc_namespace,mediawikiwiki.recentchanges.rc_title | 1        |                                    |
|    1 | PRIMARY            | watchlist_expiry    | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.watchlist.wl_id                                                       | 1        | Using where                        |
|    1 | PRIMARY            | page                | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.recentchanges.rc_cur_id                                               | 1        |                                    |
|    2 | DEPENDENT SUBQUERY | change_tag          | ref    | ct_rc_tag_id,ct_tag_id_id                                                                                             | ct_rc_tag_id | 5       | mediawikiwiki.recentchanges.rc_id                                                   | 1        | Using index                        |
|    2 | DEPENDENT SUBQUERY | change_tag_def      | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.change_tag.ct_tag_id                                                  | 1        |                                    |
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+----------+------------------------------------+
8 rows in set (0.002 sec)

Optimizer trace WITHOUT ORDER BY: https://phabricator.wikimedia.org/P23106
Optimizer trace with ORDER BY: https://phabricator.wikimedia.org/P23105

10.4.22 version:

root@db1123.eqiad.wmnet[mediawikiwiki]> select @@version;explain  SELECT   rc_id,rc_timestamp,rc_namespace,rc_title,rc_minor,   rc_bot,rc_new,rc_cur_id,rc_this_oldid,rc_last_oldid,   rc_type,rc_source,rc_patrolled,rc_ip,rc_old_len,   rc_new_len,rc_deleted,rc_logid,rc_log_type,   rc_log_action,rc_params,rc_actor,   recentchanges_actor.actor_user AS `rc_user`,   recentchanges_actor.actor_name AS `rc_user_text`,   comment_rc_comment.comment_text AS `rc_comment_text`,   comment_rc_comment.comment_data AS `rc_comment_data`,   comment_rc_comment.comment_id AS `rc_comment_cid`,   rc_title,rc_namespace,wl_user,wl_notificationtimestamp,   we_expiry,page_latest,   (SELECT        GROUP_CONCAT(ctd_name SEPARATOR ',')        FROM `change_tag` JOIN `change_tag_def` ON ((ct_tag_id=ctd_id))         WHERE ct_rc_id=rc_id     ) AS `ts_tags`   FROM    `recentchanges`    JOIN `actor` `recentchanges_actor` ON ((actor_id=rc_actor))    JOIN `comment` `comment_rc_comment` ON ((comment_rc_comment.comment_id = rc_comment_id))    LEFT JOIN `watchlist` ON (wl_user = 14096723 AND (wl_title=rc_title) AND (wl_namespace=rc_namespace))    LEFT JOIN `watchlist_expiry` ON ((wl_id = we_item))    LEFT JOIN `page` ON ((rc_cur_id=page_id))    WHERE    rc_bot = 0 AND    (rc_type != 6) AND    (rc_source != 'wb') AND    (rc_namespace NOT IN (1198,1199)) AND    (rc_timestamp >= '20220316090254') AND    rc_new IN (0,1);
+---------------------+
| @@version           |
+---------------------+
| 10.4.22-MariaDB-log |
+---------------------+
1 row in set (0.000 sec)
 
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+-------+-------------+
| id   | select_type        | table               | type   | possible_keys                                                                                                         | key          | key_len | ref                                                                                 | rows  | Extra       |
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+-------+-------------+
|    1 | PRIMARY            | recentchanges       | ALL    | rc_timestamp,rc_name_type_patrolled_timestamp,rc_ns_actor,rc_actor,rc_namespace_title_timestamp,rc_new_name_timestamp | NULL         | NULL    | NULL                                                                                | 63512 | Using where |
|    1 | PRIMARY            | watchlist           | eq_ref | wl_user,wl_user_notificationtimestamp,wl_namespace_title                                                              | wl_user      | 265     | const,mediawikiwiki.recentchanges.rc_namespace,mediawikiwiki.recentchanges.rc_title | 1     |             |
|    1 | PRIMARY            | watchlist_expiry    | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.watchlist.wl_id                                                       | 1     | Using where |
|    1 | PRIMARY            | page                | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.recentchanges.rc_cur_id                                               | 1     |             |
|    1 | PRIMARY            | comment_rc_comment  | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 8       | mediawikiwiki.recentchanges.rc_comment_id                                           | 1     |             |
|    1 | PRIMARY            | recentchanges_actor | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 8       | mediawikiwiki.recentchanges.rc_actor                                                | 1     |             |
|    2 | DEPENDENT SUBQUERY | change_tag          | ref    | ct_rc_tag_id,ct_tag_id_id                                                                                             | ct_rc_tag_id | 5       | mediawikiwiki.recentchanges.rc_id                                                   | 1     | Using index |
|    2 | DEPENDENT SUBQUERY | change_tag_def      | eq_ref | PRIMARY                                                                                                               | PRIMARY      | 4       | mediawikiwiki.change_tag.ct_tag_id                                                  | 1     |             |
+------+--------------------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------------+--------------+---------+-------------------------------------------------------------------------------------+-------+-------------+
8 rows in set (0.002 sec)

Optimizer trace WITHOUT ORDER BY: https://phabricator.wikimedia.org/P23103
Optimizer trace with ORDER BY: https://phabricator.wikimedia.org/P23104

Comment by Sergei Petrunia [ 2022-03-29 ]

marostegui, thanks for the data!

Notes: without ORDER BY, the join orders were the same as with.
10.4.22 has difference in the query plan for the first table, recentchanges:

  • no order by: type=ALL, rows=64K
  • with order by: type=range, rows=33K
    the range access is used to produce rows in order.
Comment by Manuel Arostegui [ 2022-03-29 ]

If you need anything else, let me know. Happy to provide as much info as I can from my side. Thanks for looking into this!

Comment by Sergei Petrunia [ 2022-03-29 ]

10.4.22_with_order_by.txt has

            "rows_estimation": [
              {
                "table": "recentchanges",
...
                 "selectivity_for_indexes": [
                  {
                    "index_name": "rc_new_name_timestamp",
                    "selectivity_from_index": 0.5679
                  },
                  {
                    "index_name": "rc_timestamp",
                    "selectivity_from_index": 0.5
                  }
                ],
                "selectivity_for_columns": [],
                "cond_selectivity": 0.2839
              },

while in 10.4.24_with_order_by.txt has:

            "rows_estimation": [
              {
                "table": "recentchanges",
...
                "selectivity_for_indexes": [
                  {
                    "index_name": "rc_timestamp",
                    "selectivity_from_index": 0.5179
                  },
                  {
                    "index_name": "rc_name_type_patrolled_timestamp",
                    "selectivity_from_index": 0.5643
                  },
                  {
                    "index_name": "rc_new_name_timestamp",
                    "selectivity_from_index": 0.5853,
                    "selectivity_multiplier": 20.5
                  }
                ],
                "selectivity_for_columns": [
                  {
                    "column_name": "rc_bot",
                    "ranges": ["0 <= rc_bot <= 0"],
                    "selectivity_from_histogram": 0.8125
                  },
                  {
                    "column_name": "rc_type",
                    "ranges": ["rc_type < 6", "6 < rc_type"],
                    "selectivity_from_histogram": 1.1042
                  },
                  {
                    "column_name": "rc_source",
                    "ranges": ["rc_source < wb", "wb < rc_source"],
                    "selectivity_from_histogram": 1.1589
                  }
                ],
                "cond_selectivity": 2.849
              },

and then eventually:

                "plan_prefix": [],
                "table": "recentchanges",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "scan",
                      "resulting_rows": 182215,
                      "cost": 2e308,

Comment by Sergei Petrunia [ 2022-03-29 ]

The issue may be related to fix for MDEV-25830 ( which was pushed into 10.4.23).

Comment by Sergei Petrunia [ 2022-03-29 ]

The code in calculate_cond_selectivity_for_table():

In 10.4.22: computes index-based selectivity by starting from indexes using bigger # of key parts and then proceeding to smaller # of key parts.

In 10.4.24, uses the logic in cmp_quick_ranges():

Compare quick select ranges according to number of found rows
If there is equal amounts of rows, use the long key part.

Comment by Manuel Arostegui [ 2022-04-26 ]

Hi psergei, were you able to find a fix for this issue? Thanks a lot!

Generated at Thu Feb 08 09:58:38 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.