Bug #121891 JSON_TABLE parses constant input despite outer LIMIT 0

Submitted: 24 Nov 4:30 Modified: 24 Nov 8:46
Reporter: QiFan Liu Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S5 (Performance)
Version:26.10.0 OS:Linux
Assigned to: CPU Architecture:x86
Tags: constant expression, explain, json_table, LIMIT 0

[24 Nov 4:30] QiFan Liu
Description:
A JSON_TABLE query with an outer LIMIT 0 parses a large constant JSON document even though the query returns no rows. The execution plan already recognizes the zero limit:

-> Zero rows (Zero limit)  (cost=0..0 rows=0)

The query containing 12,000,001 JSON elements and the baseline both returned identical empty results. In three alternating measurements, the query took 2743.6, 1848.3, and 2098.3 ms, while the baseline took 11.1, 11.2, and 11.5 ms. The query was 247.2, 164.8, and 182.8 times slower, respectively.

The same behavior occurs with EXPLAIN FORMAT=TREE. EXPLAIN for an 8,000,001-element document took 1933.8, 1143.0, and 1114.8 ms, compared with 12.3, 11.3, and 11.2 ms for the equivalent one-element input. Both statements produced the same plan shown above.

How to repeat:
Run in the mysql client as root.

Run the following statements and measure their elapsed times:

SELECT CAST(NULL AS SIGNED) AS x WHERE FALSE;

SELECT jt.x AS x
FROM JSON_TABLE(
  CONCAT('[', REPEAT('0,', 12000000), '0]'),
  '$[*]' COLUMNS (x INT PATH '$')
) AS jt
LIMIT 0;

Both statements return an empty result, but the second statement takes over one second because the constant JSON input is processed despite LIMIT 0.

The EXPLAIN variant is:

EXPLAIN FORMAT=TREE
SELECT jt.x AS x
FROM JSON_TABLE(
  '[0]',
  '$[*]' COLUMNS (x INT PATH '$')
) AS jt
LIMIT 0;

EXPLAIN FORMAT=TREE
SELECT jt.x AS x
FROM JSON_TABLE(
  CONCAT('[', REPEAT('0,', 8000000), '0]'),
  '$[*]' COLUMNS (x INT PATH '$')
) AS jt
LIMIT 0;

Both EXPLAIN statements return:

-> Zero rows (Zero limit)  (cost=0..0 rows=0)

The second EXPLAIN nevertheless takes over one second.
[24 Nov 8:46] Roy Lyseng
Thank you for the bug report.

However, this is not a bug, but a deliberate optimization pattern.

A const JSON expression may be evaluated during resolving, so that it can be cached and does not have to be evaluated later.
At this point, no iterators have been set up, thus the resolver does not know about the outer LIMIT 0 clause (which, by itself, is quite unnatural and nothing that we should optimize for).

OTOH, the JSON expression evaluation is very slow, but that might be a different performance related bug.

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

Page generated in 0.050 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.