Description:
A constant obtained through a join to a one-row const table is not used to recognize the physical ordering provided by an InnoDB secondary index's primary-key suffix.
The tested query and the faster comparison query both return 5000000. The comparison only adds t.pk1 = 3, which is already implied by c.id = 1, c.v = 3, and t.pk1 = c.v.
With 5,000,000 rows, three alternating measurements were:
baseline: 14.4 ms, slow: 5012.0 ms (347.3x)
baseline: 21.1 ms, slow: 5012.4 ms (237.1x)
baseline: 13.8 ms, slow: 5011.9 ms (363.7x)
The slow plan sorts all matching rows:
-> Limit: 1 row(s) (cost=261411 rows=1)
-> Sort: t.pk2 DESC, limit input to 1 row(s) per chunk (cost=261411 rows=2.59e+6)
-> Covering index lookup on t using k1 (f1 = 7, pk1 = '3') (cost=261411 rows=2.59e+6)
With the redundant explicit equality, the plan instead uses a reverse covering index lookup and does not sort:
-> Limit: 1 row(s) (cost=261411 rows=1)
-> Covering index lookup on t using k1 (f1 = 7, pk1 = 3) (reverse) (cost=261411 rows=2.59e+6)
The same issue affects GROUP BY. Both forms return (5000000,1), but without the redundant equality MySQL aggregates 5,000,000 groups into a temporary table and sorts them before LIMIT. Measurements for this variant were 5013.1/14.7 ms (339.9x), 5013.6/15.8 ms (318.3x), and 5014.6/14.6 ms (343.9x). Its slow plan is:
-> Limit: 1 row(s)
-> Sort: t.pk2 DESC, limit input to 1 row(s) per chunk
-> Table scan on <temporary>
-> Aggregate using temporary table
-> Covering index lookup on t using k1 (f1 = 7, pk1 = '3') (cost=315342 rows=3.12e+6)
How to repeat:
Run as root:
DROP DATABASE IF EXISTS db1;
CREATE DATABASE db1;
USE db1;
CREATE TABLE c (
id INT PRIMARY KEY,
v INT NOT NULL
) ENGINE=InnoDB;
CREATE TABLE t (
pk1 INT NOT NULL,
pk2 INT NOT NULL,
f1 INT NOT NULL,
PRIMARY KEY (pk1, pk2),
KEY k1 (f1)
) ENGINE=InnoDB;
CREATE TABLE digits (d TINYINT UNSIGNED PRIMARY KEY);
INSERT INTO digits VALUES
(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
INSERT INTO c VALUES (1, 3);
INSERT INTO t (pk1, pk2, f1)
SELECT 3,
1 + d0.d + 10*d1.d + 100*d2.d + 1000*d3.d +
10000*d4.d + 100000*d5.d + 1000000*d6.d,
7
FROM digits AS d0
CROSS JOIN digits AS d1
CROSS JOIN digits AS d2
CROSS JOIN digits AS d3
CROSS JOIN digits AS d4
CROSS JOIN digits AS d5
CROSS JOIN digits AS d6
WHERE d6.d < 5;
ANALYZE TABLE c, t;
-- Slow; returns 5000000.
SELECT t.pk2
FROM c
JOIN t FORCE INDEX (k1) ON t.pk1 = c.v
WHERE c.id = 1 AND t.f1 = 7
ORDER BY t.pk2 DESC
LIMIT 1;
-- Fast; t.pk1 = 3 is logically implied; also returns 5000000.
SELECT t.pk2
FROM c
JOIN t FORCE INDEX (k1) ON t.pk1 = c.v
WHERE c.id = 1 AND t.f1 = 7 AND t.pk1 = 3
ORDER BY t.pk2 DESC
LIMIT 1;
EXPLAIN FORMAT=TREE
SELECT t.pk2
FROM c
JOIN t FORCE INDEX (k1) ON t.pk1 = c.v
WHERE c.id = 1 AND t.f1 = 7
ORDER BY t.pk2 DESC
LIMIT 1;
EXPLAIN FORMAT=TREE
SELECT t.pk2
FROM c
JOIN t FORCE INDEX (k1) ON t.pk1 = c.v
WHERE c.id = 1 AND t.f1 = 7 AND t.pk1 = 3
ORDER BY t.pk2 DESC
LIMIT 1;
-- GROUP BY variant: slow; returns 5000000, 1.
SELECT t.pk2, COUNT(*) AS n
FROM c
JOIN t FORCE INDEX (k1) ON t.pk1 = c.v
WHERE c.id = 1 AND t.f1 = 7
GROUP BY t.pk2
ORDER BY t.pk2 DESC
LIMIT 1;
-- GROUP BY variant: fast; also returns 5000000, 1.
SELECT t.pk2, COUNT(*) AS n
FROM c
JOIN t FORCE INDEX (k1) ON t.pk1 = c.v
WHERE c.id = 1 AND t.f1 = 7 AND t.pk1 = 3
GROUP BY t.pk2
ORDER BY t.pk2 DESC
LIMIT 1;
Suggested fix:
When constants are discovered after reading const tables, preserve the primary-key ordering metadata needed to recognize fixed leading parts of an InnoDB secondary index's physical primary-key suffix.
Description: A constant obtained through a join to a one-row const table is not used to recognize the physical ordering provided by an InnoDB secondary index's primary-key suffix. The tested query and the faster comparison query both return 5000000. The comparison only adds t.pk1 = 3, which is already implied by c.id = 1, c.v = 3, and t.pk1 = c.v. With 5,000,000 rows, three alternating measurements were: baseline: 14.4 ms, slow: 5012.0 ms (347.3x) baseline: 21.1 ms, slow: 5012.4 ms (237.1x) baseline: 13.8 ms, slow: 5011.9 ms (363.7x) The slow plan sorts all matching rows: -> Limit: 1 row(s) (cost=261411 rows=1) -> Sort: t.pk2 DESC, limit input to 1 row(s) per chunk (cost=261411 rows=2.59e+6) -> Covering index lookup on t using k1 (f1 = 7, pk1 = '3') (cost=261411 rows=2.59e+6) With the redundant explicit equality, the plan instead uses a reverse covering index lookup and does not sort: -> Limit: 1 row(s) (cost=261411 rows=1) -> Covering index lookup on t using k1 (f1 = 7, pk1 = 3) (reverse) (cost=261411 rows=2.59e+6) The same issue affects GROUP BY. Both forms return (5000000,1), but without the redundant equality MySQL aggregates 5,000,000 groups into a temporary table and sorts them before LIMIT. Measurements for this variant were 5013.1/14.7 ms (339.9x), 5013.6/15.8 ms (318.3x), and 5014.6/14.6 ms (343.9x). Its slow plan is: -> Limit: 1 row(s) -> Sort: t.pk2 DESC, limit input to 1 row(s) per chunk -> Table scan on <temporary> -> Aggregate using temporary table -> Covering index lookup on t using k1 (f1 = 7, pk1 = '3') (cost=315342 rows=3.12e+6) How to repeat: Run as root: DROP DATABASE IF EXISTS db1; CREATE DATABASE db1; USE db1; CREATE TABLE c ( id INT PRIMARY KEY, v INT NOT NULL ) ENGINE=InnoDB; CREATE TABLE t ( pk1 INT NOT NULL, pk2 INT NOT NULL, f1 INT NOT NULL, PRIMARY KEY (pk1, pk2), KEY k1 (f1) ) ENGINE=InnoDB; CREATE TABLE digits (d TINYINT UNSIGNED PRIMARY KEY); INSERT INTO digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); INSERT INTO c VALUES (1, 3); INSERT INTO t (pk1, pk2, f1) SELECT 3, 1 + d0.d + 10*d1.d + 100*d2.d + 1000*d3.d + 10000*d4.d + 100000*d5.d + 1000000*d6.d, 7 FROM digits AS d0 CROSS JOIN digits AS d1 CROSS JOIN digits AS d2 CROSS JOIN digits AS d3 CROSS JOIN digits AS d4 CROSS JOIN digits AS d5 CROSS JOIN digits AS d6 WHERE d6.d < 5; ANALYZE TABLE c, t; -- Slow; returns 5000000. SELECT t.pk2 FROM c JOIN t FORCE INDEX (k1) ON t.pk1 = c.v WHERE c.id = 1 AND t.f1 = 7 ORDER BY t.pk2 DESC LIMIT 1; -- Fast; t.pk1 = 3 is logically implied; also returns 5000000. SELECT t.pk2 FROM c JOIN t FORCE INDEX (k1) ON t.pk1 = c.v WHERE c.id = 1 AND t.f1 = 7 AND t.pk1 = 3 ORDER BY t.pk2 DESC LIMIT 1; EXPLAIN FORMAT=TREE SELECT t.pk2 FROM c JOIN t FORCE INDEX (k1) ON t.pk1 = c.v WHERE c.id = 1 AND t.f1 = 7 ORDER BY t.pk2 DESC LIMIT 1; EXPLAIN FORMAT=TREE SELECT t.pk2 FROM c JOIN t FORCE INDEX (k1) ON t.pk1 = c.v WHERE c.id = 1 AND t.f1 = 7 AND t.pk1 = 3 ORDER BY t.pk2 DESC LIMIT 1; -- GROUP BY variant: slow; returns 5000000, 1. SELECT t.pk2, COUNT(*) AS n FROM c JOIN t FORCE INDEX (k1) ON t.pk1 = c.v WHERE c.id = 1 AND t.f1 = 7 GROUP BY t.pk2 ORDER BY t.pk2 DESC LIMIT 1; -- GROUP BY variant: fast; also returns 5000000, 1. SELECT t.pk2, COUNT(*) AS n FROM c JOIN t FORCE INDEX (k1) ON t.pk1 = c.v WHERE c.id = 1 AND t.f1 = 7 AND t.pk1 = 3 GROUP BY t.pk2 ORDER BY t.pk2 DESC LIMIT 1; Suggested fix: When constants are discovered after reading const tables, preserve the primary-key ordering metadata needed to recognize fixed leading parts of an InnoDB secondary index's physical primary-key suffix.