Bug #121955 ROLES_GRAPHML() is recalculated for every row in an aggregate

Submitted: 27 Nov 3:24
Reporter: QiFan Liu Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Security: Roles Severity:S5 (Performance)
Version:26.10.0 OS:Linux
Assigned to: CPU Architecture:x86
Tags: aggregate, roles, ROLES_GRAPHML, statement cache

[27 Nov 3:24] QiFan Liu
Description:
ROLES_GRAPHML() is statement-constant, but evaluating it inside SUM() appears to serialize the complete role graph once per input row.

With 100,000 rows and a chain of 32 roles, these expressions are equivalent; in the original observed setup, the captured baseline returned 666100000:

SELECT SUM(LENGTH(ROLES_GRAPHML())) FROM t1;
SELECT COUNT(*) * LENGTH(ROLES_GRAPHML()) FROM t1;

In three alternating measurements, the first statement ran for 5013.0, 5012.1, and 5011.9 ms before being stopped at the 5-second limit. The second completed in 42.4, 58.6, and 44.0 ms. The measured slowdowns were 118.3x, 85.5x, and 113.9x.

The slow statement's plan was:

-> Aggregate: sum(length(roles_graphml()))  (cost=32777 rows=1)
    -> Table scan on t1  (cost=9959 rows=99029)

The equivalent faster statement's plan was:

-> Aggregate: count(0)  (cost=32777 rows=1)
    -> Table scan on t1  (cost=9959 rows=99029)

The implementation has a per-statement GraphML value cache, but the cached value does not appear to be consulted before each evaluation of ROLES_GRAPHML().

How to repeat:
Run as root:

CREATE DATABASE db1;
USE db1;

CREATE TABLE t1 (id INT NOT NULL) ENGINE=InnoDB;
INSERT INTO t1
SELECT a.n + 10*b.n + 100*c.n + 1000*d.n + 10000*e.n
FROM
  (SELECT 0 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) a
CROSS JOIN
  (SELECT 0 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) b
CROSS JOIN
  (SELECT 0 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) c
CROSS JOIN
  (SELECT 0 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) d
CROSS JOIN
  (SELECT 0 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) e;

CREATE ROLE
  example_roles_chain_r01, example_roles_chain_r02,
  example_roles_chain_r03, example_roles_chain_r04,
  example_roles_chain_r05, example_roles_chain_r06,
  example_roles_chain_r07, example_roles_chain_r08,
  example_roles_chain_r09, example_roles_chain_r10,
  example_roles_chain_r11, example_roles_chain_r12,
  example_roles_chain_r13, example_roles_chain_r14,
  example_roles_chain_r15, example_roles_chain_r16,
  example_roles_chain_r17, example_roles_chain_r18,
  example_roles_chain_r19, example_roles_chain_r20,
  example_roles_chain_r21, example_roles_chain_r22,
  example_roles_chain_r23, example_roles_chain_r24,
  example_roles_chain_r25, example_roles_chain_r26,
  example_roles_chain_r27, example_roles_chain_r28,
  example_roles_chain_r29, example_roles_chain_r30,
  example_roles_chain_r31, example_roles_chain_r32;

GRANT example_roles_chain_r01 TO example_roles_chain_r02;
GRANT example_roles_chain_r02 TO example_roles_chain_r03;
GRANT example_roles_chain_r03 TO example_roles_chain_r04;
GRANT example_roles_chain_r04 TO example_roles_chain_r05;
GRANT example_roles_chain_r05 TO example_roles_chain_r06;
GRANT example_roles_chain_r06 TO example_roles_chain_r07;
GRANT example_roles_chain_r07 TO example_roles_chain_r08;
GRANT example_roles_chain_r08 TO example_roles_chain_r09;
GRANT example_roles_chain_r09 TO example_roles_chain_r10;
GRANT example_roles_chain_r10 TO example_roles_chain_r11;
GRANT example_roles_chain_r11 TO example_roles_chain_r12;
GRANT example_roles_chain_r12 TO example_roles_chain_r13;
GRANT example_roles_chain_r13 TO example_roles_chain_r14;
GRANT example_roles_chain_r14 TO example_roles_chain_r15;
GRANT example_roles_chain_r15 TO example_roles_chain_r16;
GRANT example_roles_chain_r16 TO example_roles_chain_r17;
GRANT example_roles_chain_r17 TO example_roles_chain_r18;
GRANT example_roles_chain_r18 TO example_roles_chain_r19;
GRANT example_roles_chain_r19 TO example_roles_chain_r20;
GRANT example_roles_chain_r20 TO example_roles_chain_r21;
GRANT example_roles_chain_r21 TO example_roles_chain_r22;
GRANT example_roles_chain_r22 TO example_roles_chain_r23;
GRANT example_roles_chain_r23 TO example_roles_chain_r24;
GRANT example_roles_chain_r24 TO example_roles_chain_r25;
GRANT example_roles_chain_r25 TO example_roles_chain_r26;
GRANT example_roles_chain_r26 TO example_roles_chain_r27;
GRANT example_roles_chain_r27 TO example_roles_chain_r28;
GRANT example_roles_chain_r28 TO example_roles_chain_r29;
GRANT example_roles_chain_r29 TO example_roles_chain_r30;
GRANT example_roles_chain_r30 TO example_roles_chain_r31;
GRANT example_roles_chain_r31 TO example_roles_chain_r32;

SELECT SUM(LENGTH(ROLES_GRAPHML())) FROM t1;

SELECT COUNT(*) * LENGTH(ROLES_GRAPHML()) FROM t1;
-- Both statements should return the same scalar; compare their execution times.

EXPLAIN FORMAT=TREE
SELECT SUM(LENGTH(ROLES_GRAPHML())) FROM t1;

EXPLAIN FORMAT=TREE
SELECT COUNT(*) * LENGTH(ROLES_GRAPHML()) FROM t1;

Suggested fix:
Consult the ROLES_GRAPHML() statement-cache-valid flag before traversing and serializing the role graph. Continue invalidating the cached value during item cleanup so it is recalculated on the next execution.

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

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