Bug #121931 CONVERT_TZ repeats execution-constant time-zone lookups for every row

Submitted: 26 Nov 2:19
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: const_for_execution, convert_tz, stored procedure, time zone

[26 Nov 2:19] QiFan Liu
Description:
CONVERT_TZ() does not cache time-zone lookups when the zone arguments are stored-procedure parameters that remain constant for the execution. It repeats both lookups for every input row.

On a table containing 1,000,000 rows, COUNT(CONVERT_TZ(...)) with two invalid 64-character zone names returned 0 in both forms, as expected. With literal zone names, three runs took 151.3, 149.5, and 153.2 ms. Passing the identical names as stored-procedure parameters took 19565.1, 19316.7, and 18971.7 ms: approximately 129.3x, 129.2x, and 123.8x slower.

The literal form demonstrates that failed time-zone lookups can be cached. Stored-procedure parameters are constant for one execution, so repeating their failed lookups for every row is unnecessary.

The relevant CONVERT_TZ evaluation code appears to cache lookups only for const_item() operands, although stored-routine parameters are const_for_execution(). Its cache flags are already reset at the execution boundary.

How to repeat:
Run as root:

CREATE DATABASE db1;
USE db1;

CREATE TABLE digits (d TINYINT NOT NULL PRIMARY KEY);
INSERT INTO digits VALUES
  (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

CREATE TABLE t1 (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  dt DATETIME NOT NULL
) ENGINE=InnoDB;

INSERT INTO t1 (dt)
SELECT TIMESTAMP('2001-01-01 00:00:00') + INTERVAL
       (a.d + 10*b.d + 100*c.d + 1000*d.d + 10000*e.d + 100000*f.d)
       SECOND
FROM digits AS a
CROSS JOIN digits AS b
CROSS JOIN digits AS c
CROSS JOIN digits AS d
CROSS JOIN digits AS e
CROSS JOIN digits AS f;

CREATE PROCEDURE convert_tz_slow(
  IN from_zone VARCHAR(64), IN to_zone VARCHAR(64))
SELECT COUNT(CONVERT_TZ(dt, from_zone, to_zone)) AS converted_rows
FROM t1;

-- Baseline: returns 0; measured at 151.3, 149.5, and 153.2 ms.
SELECT COUNT(CONVERT_TZ(
  dt,
  'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'))
  AS converted_rows
FROM t1;

-- Slow form: returns 0; measured at 19565.1, 19316.7, and 18971.7 ms.
CALL convert_tz_slow(
  'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy');

Suggested fix:
Cache each time-zone lookup once per execution when its operand is const_for_execution(), using the existing execution-boundary cache reset.

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

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