Skip to content

Commit d8453a0

Browse files
committed
Bug#30766181 I_S.KEY_COLUMN_USAGE IS VERY SLOW
Bug#30216864 INFORMATION_SCHEMA.KEY_COLUMN_USAGE PERFORMANCE IS SLOW Problem: The system views INFORMATION_SCHEMA.KEY_COLUMN_USAGE and INFORMATION_SCHEMA.TABLE_CONSTRAINTS uses UNION. This causes the conditions to be evaluated once per each SELECT statement which are part of UNION. This causes dictionary metadata to be traversed multiple times. Mainly the metadata tables mysql.schema and mysql.tables would be traversed multiple times. And this intern causes the access checks for SQL objects to be done multiple times. Fix: Rewrite the system views for INFORMATION_SCHEMA.KEY_COLUMN_USAGE and INFORMATION_SCHEMA.TABLE_CONSTRAINTS to move UNION into a LATERAL table. That way, it is be possible to merge the view and the conditions on schema name and table name is applied before the lateral table is executed, enabling indexes of the specified table to be accessed. There is following change in behavior with this patch. The column names of two INFORMATION_SCHEMA tables would be presented as capitalized column names and not the case provided by user in the query. E.g., SELECT Table_Name, Constraint_name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS (or KEY_COLUMN_USAGE) LIMIT 1; TABLE_NAME CONSTRAINT_NAME ... Unlike, the behavior before patch is that we get user provided case, SELECT Table_Name, Constraint_name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS (or KEY_COLUMN_USAGE) LIMIT 1; Table_Name Constraint_name ... The new behavior is same as seen in other INFORMATION_SCHEMA tables based on system view. E.g., I_S.TABLES, I_S.SCHEMATA. See bug Bug#31195099 for more details. Change-Id: Ie8d309024316b694c210fbb660d55d4eb7afa05e
1 parent a2ceb6e commit d8453a0

21 files changed

+341
-490
lines changed

mysql-test/r/foreign_key.result

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.

mysql-test/r/information_schema_ci.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ INDEX_NAME
733733
f1_key
734734
select constraint_name from information_schema.table_constraints
735735
where table_schema='test' order by constraint_name;
736-
constraint_name
736+
CONSTRAINT_NAME
737737
show create view v2;
738738
View Create View character_set_client collation_connection
739739
v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` (`c`) AS select `t1`.`f1` AS `f1` from `t1` latin1 latin1_swedish_ci

mysql-test/r/information_schema_cs.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ INDEX_NAME
733733
f1_key
734734
select constraint_name from information_schema.table_constraints
735735
where table_schema='test' order by constraint_name;
736-
constraint_name
736+
CONSTRAINT_NAME
737737
show create view v2;
738738
View Create View character_set_client collation_connection
739739
v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` (`c`) AS select `t1`.`f1` AS `f1` from `t1` latin1 latin1_swedish_ci

mysql-test/r/information_schema_inno.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ use test;
7979
select referenced_table_schema, referenced_table_name
8080
from information_schema.key_column_usage
8181
where constraint_schema = 'db-1' and table_schema != 'PERFORMANCE_SCHEMA' order by referenced_table_schema, referenced_table_name;
82-
referenced_table_schema referenced_table_name
82+
REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME
8383
NULL NULL
8484
NULL NULL
8585
db-1 t-2

mysql-test/r/lowercase_table2.result

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ CONSTRAINT_NAME
312312
t2_ibfk_1
313313
SELECT constraint_name FROM information_schema.table_constraints
314314
WHERE table_name = 'T2' ORDER BY constraint_name;
315-
constraint_name
315+
CONSTRAINT_NAME
316316
t2_ibfk_1
317317
RENAME TABLE T2 TO T3;
318318
SELECT constraint_name FROM information_schema.referential_constraints
@@ -321,7 +321,7 @@ CONSTRAINT_NAME
321321
t3_ibfk_1
322322
SELECT constraint_name FROM information_schema.table_constraints
323323
WHERE table_name = 'T3' ORDER BY constraint_name;
324-
constraint_name
324+
CONSTRAINT_NAME
325325
t3_ibfk_1
326326
RENAME TABLE t3 TO T4;
327327
SELECT constraint_name FROM information_schema.referential_constraints
@@ -330,7 +330,7 @@ CONSTRAINT_NAME
330330
t4_ibfk_1
331331
SELECT constraint_name FROM information_schema.table_constraints
332332
WHERE table_name = 'T4' ORDER BY constraint_name;
333-
constraint_name
333+
CONSTRAINT_NAME
334334
t4_ibfk_1
335335
DROP TABLE T4;
336336
CREATE TABLE T2(a INT, CONSTRAINT T2_ibfk_1 FOREIGN KEY(a) REFERENCES t1(a));
@@ -340,7 +340,7 @@ CONSTRAINT_NAME
340340
T2_ibfk_1
341341
SELECT constraint_name FROM information_schema.table_constraints
342342
WHERE table_name = 'T2' ORDER BY constraint_name;
343-
constraint_name
343+
CONSTRAINT_NAME
344344
T2_ibfk_1
345345
RENAME TABLE T2 TO T3;
346346
SELECT constraint_name FROM information_schema.referential_constraints
@@ -349,7 +349,7 @@ CONSTRAINT_NAME
349349
T2_ibfk_1
350350
SELECT constraint_name FROM information_schema.table_constraints
351351
WHERE table_name = 'T3' ORDER BY constraint_name;
352-
constraint_name
352+
CONSTRAINT_NAME
353353
T2_ibfk_1
354354
ALTER TABLE T3 DROP FOREIGN KEY t2_ibfk_1;
355355
DROP TABLE T3, t1;

mysql-test/r/show_check_ci.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CON
14891489
def test PRIMARY test t1 PRIMARY KEY YES
14901490
SELECT constraint_schema, constraint_name, table_name, column_name FROM
14911491
INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE table_name='t1';
1492-
constraint_schema constraint_name table_name column_name
1492+
CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_NAME COLUMN_NAME
14931493
test PRIMARY t1 c1
14941494
# EXTENDED keyword is introduced to list the hidden index and index
14951495
# columns.

mysql-test/r/show_check_cs.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CON
14871487
def test PRIMARY test t1 PRIMARY KEY YES
14881488
SELECT constraint_schema, constraint_name, table_name, column_name FROM
14891489
INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE table_name='t1';
1490-
constraint_schema constraint_name table_name column_name
1490+
CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_NAME COLUMN_NAME
14911491
test PRIMARY t1 c1
14921492
# EXTENDED keyword is introduced to list the hidden index and index
14931493
# columns.

mysql-test/suite/funcs_1/r/is_basics_mixed.result

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ table_schema
441441
SELECT DISTINCT constraint_schema,table_schema
442442
FROM information_schema.key_column_usage
443443
WHERE constraint_schema LIKE 'db_data%' OR table_schema LIKE 'db_data%';
444-
constraint_schema table_schema
444+
CONSTRAINT_SCHEMA TABLE_SCHEMA
445445
SELECT DISTINCT routine_schema FROM information_schema.routines
446446
WHERE routine_schema LIKE 'db_data%';
447447
ROUTINE_SCHEMA
@@ -460,7 +460,7 @@ TABLE_SCHEMA
460460
SELECT DISTINCT constraint_schema,table_schema
461461
FROM information_schema.table_constraints
462462
WHERE constraint_schema LIKE 'db_data%' OR table_schema LIKE 'db_data%';
463-
constraint_schema table_schema
463+
CONSTRAINT_SCHEMA TABLE_SCHEMA
464464
SELECT DISTINCT table_schema FROM information_schema.table_privileges
465465
WHERE table_schema LIKE 'db_data%';
466466
table_schema
@@ -493,7 +493,7 @@ table_schema
493493
SELECT DISTINCT constraint_schema,table_schema
494494
FROM information_schema.key_column_usage
495495
WHERE constraint_schema LIKE 'db_data%' OR table_schema LIKE 'db_data%';
496-
constraint_schema table_schema
496+
CONSTRAINT_SCHEMA TABLE_SCHEMA
497497
db_datadict db_datadict
498498
SELECT DISTINCT routine_schema FROM information_schema.routines
499499
WHERE routine_schema LIKE 'db_data%';
@@ -517,7 +517,7 @@ db_datadict
517517
SELECT DISTINCT constraint_schema,table_schema
518518
FROM information_schema.table_constraints
519519
WHERE constraint_schema LIKE 'db_data%' OR table_schema LIKE 'db_data%';
520-
constraint_schema table_schema
520+
CONSTRAINT_SCHEMA TABLE_SCHEMA
521521
db_datadict db_datadict
522522
SELECT DISTINCT table_schema FROM information_schema.table_privileges
523523
WHERE table_schema LIKE 'db_data%';
@@ -544,7 +544,7 @@ table_schema
544544
SELECT DISTINCT constraint_schema,table_schema
545545
FROM information_schema.key_column_usage
546546
WHERE constraint_schema LIKE 'db_data%' OR table_schema LIKE 'db_data%';
547-
constraint_schema table_schema
547+
CONSTRAINT_SCHEMA TABLE_SCHEMA
548548
SELECT DISTINCT routine_schema FROM information_schema.routines
549549
WHERE routine_schema LIKE 'db_data%';
550550
ROUTINE_SCHEMA
@@ -563,7 +563,7 @@ TABLE_SCHEMA
563563
SELECT DISTINCT constraint_schema,table_schema
564564
FROM information_schema.table_constraints
565565
WHERE constraint_schema LIKE 'db_data%' OR table_schema LIKE 'db_data%';
566-
constraint_schema table_schema
566+
CONSTRAINT_SCHEMA TABLE_SCHEMA
567567
SELECT DISTINCT table_schema FROM information_schema.table_privileges
568568
WHERE table_schema LIKE 'db_data%';
569569
table_schema

mysql-test/suite/funcs_1/r/is_columns_is_cs.result

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ def information_schema FILES STATUS 37 NULL YES varchar 256 768 NULL NULL NULL u
154154
def information_schema FILES EXTRA 38 NULL YES varchar 256 768 NULL NULL NULL utf8 utf8_general_ci varchar(256) select NULL
155155
def information_schema KEYWORDS WORD 1 NULL YES varchar 31 124 NULL NULL NULL utf8mb4 utf8mb4_0900_ai_ci varchar(31) select NULL
156156
def information_schema KEYWORDS RESERVED 2 NULL YES int NULL NULL 10 0 NULL NULL NULL int select NULL
157-
def information_schema KEY_COLUMN_USAGE CONSTRAINT_CATALOG 1 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
158-
def information_schema KEY_COLUMN_USAGE CONSTRAINT_SCHEMA 2 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
157+
def information_schema KEY_COLUMN_USAGE CONSTRAINT_CATALOG 1 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
158+
def information_schema KEY_COLUMN_USAGE CONSTRAINT_SCHEMA 2 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
159159
def information_schema KEY_COLUMN_USAGE CONSTRAINT_NAME 3 NULL YES varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
160-
def information_schema KEY_COLUMN_USAGE TABLE_CATALOG 4 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
161-
def information_schema KEY_COLUMN_USAGE TABLE_SCHEMA 5 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
162-
def information_schema KEY_COLUMN_USAGE TABLE_NAME 6 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
160+
def information_schema KEY_COLUMN_USAGE TABLE_CATALOG 4 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
161+
def information_schema KEY_COLUMN_USAGE TABLE_SCHEMA 5 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
162+
def information_schema KEY_COLUMN_USAGE TABLE_NAME 6 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
163163
def information_schema KEY_COLUMN_USAGE COLUMN_NAME 7 NULL YES varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
164164
def information_schema KEY_COLUMN_USAGE ORDINAL_POSITION 8 0 NO int NULL NULL 10 0 NULL NULL NULL int unsigned select NULL
165165
def information_schema KEY_COLUMN_USAGE POSITION_IN_UNIQUE_CONSTRAINT 9 NULL YES int NULL NULL 10 0 NULL NULL NULL int unsigned select NULL
@@ -391,11 +391,11 @@ def information_schema TABLES_EXTENSIONS TABLE_SCHEMA 2 NULL NO varchar 64 192 N
391391
def information_schema TABLES_EXTENSIONS TABLE_NAME 3 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
392392
def information_schema TABLES_EXTENSIONS ENGINE_ATTRIBUTE 4 NULL YES json NULL NULL NULL NULL NULL NULL NULL json select NULL
393393
def information_schema TABLES_EXTENSIONS SECONDARY_ENGINE_ATTRIBUTE 5 NULL YES json NULL NULL NULL NULL NULL NULL NULL json select NULL
394-
def information_schema TABLE_CONSTRAINTS CONSTRAINT_CATALOG 1 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
395-
def information_schema TABLE_CONSTRAINTS CONSTRAINT_SCHEMA 2 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
394+
def information_schema TABLE_CONSTRAINTS CONSTRAINT_CATALOG 1 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
395+
def information_schema TABLE_CONSTRAINTS CONSTRAINT_SCHEMA 2 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
396396
def information_schema TABLE_CONSTRAINTS CONSTRAINT_NAME 3 NULL YES varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
397-
def information_schema TABLE_CONSTRAINTS TABLE_SCHEMA 4 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
398-
def information_schema TABLE_CONSTRAINTS TABLE_NAME 5 NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
397+
def information_schema TABLE_CONSTRAINTS TABLE_SCHEMA 4 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
398+
def information_schema TABLE_CONSTRAINTS TABLE_NAME 5 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL
399399
def information_schema TABLE_CONSTRAINTS CONSTRAINT_TYPE 6 NO varchar 11 33 NULL NULL NULL utf8 utf8_bin varchar(11) select NULL
400400
def information_schema TABLE_CONSTRAINTS ENFORCED 7 NO varchar 3 9 NULL NULL NULL utf8 utf8_bin varchar(3) select NULL
401401
def information_schema TABLE_CONSTRAINTS_EXTENSIONS CONSTRAINT_CATALOG 1 NULL NO varchar 64 192 NULL NULL NULL utf8 utf8_bin varchar(64) select NULL

0 commit comments

Comments
 (0)