Bug #121952 Mixed-order multi-valued index uses filesort instead of reverse range scan

Submitted: 27 Nov 3:12
Reporter: QiFan Liu Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S5 (Performance)
Version:26.10.0 OS:Linux
Assigned to: CPU Architecture:x86
Tags: filesort, MEMBER OF, multi-valued index, reverse range scan

[27 Nov 3:12] QiFan Liu
Description:
The two queries below return the same result, id=1, from a table containing 3,000,000 rows. They differ only in the forced index.

Using mvi_id_asc took 11.5 ms, 12.2 ms, and 11.7 ms. Using mvi_id_desc took 5012.4 ms, 5090.7 ms, and 5012.9 ms. The respective slowdowns were 434.4x, 418.1x, and 428.6x.

The mvi_id_desc plan scans the qualifying range and sorts it before applying LIMIT:

-> Limit: 1 row(s)  (cost=1.87 rows=1)
    -> Sort: t1.id, limit input to 1 row(s) per chunk  (cost=1.87 rows=1)
        -> Filter: (json'50' member of (cast(json_extract(ids,_latin1'$[*]') as signed array)) and (t1.id <= 3000000))  (cost=1.87 rows=1)
            -> Index range scan on t1 using mvi_id_desc over (50 MEMBER OF (json_extract(ids,_latin1'$[*]')) AND 3000000 <= id)  (cost=1.87 rows=1)

The equivalent mvi_id_asc plan has no sort and stops after its first qualifying entry:

-> Limit: 1 row(s)  (cost=1.87 rows=1)
    -> Filter: (json'50' member of (cast(json_extract(ids,_latin1'$[*]') as signed array)) and (t1.id <= 3000000))  (cost=1.87 rows=1)
        -> Index range scan on t1 using mvi_id_asc over (50 MEMBER OF (json_extract(ids,_latin1'$[*]')) AND id <= 3000000)  (cost=1.87 rows=1)

A reverse range scan of the single range on mvi_id_desc can provide the requested ascending id order, so sorting the full qualifying range is avoidable.

How to repeat:
Run as root in a fresh mysql session:

CREATE DATABASE db1;
USE db1;

CREATE TABLE t1 (
  id INT NOT NULL PRIMARY KEY,
  ids JSON NOT NULL
) ENGINE=InnoDB;

INSERT INTO t1 (id, ids)
WITH d AS (
  SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7
  UNION ALL SELECT 8 UNION ALL SELECT 9
)
SELECT 1 + d0.n + 10*d1.n + 100*d2.n + 1000*d3.n +
       10000*d4.n + 100000*d5.n + 1000000*d6.n,
       JSON_ARRAY(50)
FROM d d0 CROSS JOIN d d1 CROSS JOIN d d2 CROSS JOIN d d3
CROSS JOIN d d4 CROSS JOIN d d5 CROSS JOIN d d6
WHERE 1 + d0.n + 10*d1.n + 100*d2.n + 1000*d3.n +
          10000*d4.n + 100000*d5.n + 1000000*d6.n <= 3000000;

ALTER TABLE t1
  ADD KEY mvi_id_asc ((CAST(ids->'$[*]' AS SIGNED ARRAY)), id ASC),
  ADD KEY mvi_id_desc ((CAST(ids->'$[*]' AS SIGNED ARRAY)), id DESC);

ANALYZE TABLE t1;

EXPLAIN FORMAT=TREE
SELECT id
FROM t1 FORCE INDEX (mvi_id_desc)
WHERE 50 MEMBER OF (ids->'$[*]') AND id <= 3000000
ORDER BY id ASC
LIMIT 1;

SELECT id
FROM t1 FORCE INDEX (mvi_id_desc)
WHERE 50 MEMBER OF (ids->'$[*]') AND id <= 3000000
ORDER BY id ASC
LIMIT 1;
-- returns 1; measured at 5012.4, 5090.7, and 5012.9 ms

EXPLAIN FORMAT=TREE
SELECT id
FROM t1 FORCE INDEX (mvi_id_asc)
WHERE 50 MEMBER OF (ids->'$[*]') AND id <= 3000000
ORDER BY id ASC
LIMIT 1;

SELECT id
FROM t1 FORCE INDEX (mvi_id_asc)
WHERE 50 MEMBER OF (ids->'$[*]') AND id <= 3000000
ORDER BY id ASC
LIMIT 1;
-- returns 1; measured at 11.5, 12.2, and 11.7 ms

Suggested fix:
Permit reverse range access for a single range on a mixed-direction multi-valued index when it supplies the requested order, instead of forcing a filesort.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.049 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.