From a00e99accaf98ed92cd1a5aaa1dcee95bdb6daf5 Mon Sep 17 00:00:00 2001 From: Monty Date: Tue, 2 Jan 2024 17:11:14 +0200 Subject: [PATCH] MDEV-33152 Add QUERIES to INDEX_STATISTICS Other changes: - Do not collect index statistics for system tables like index_stats table_stats, performance_schema, information_schema etc as the user has no control of these and the generate noise in the statistics. --- .../main/information_schema_stats.result | 28 ++++++++++--------- mysql-test/main/information_schema_stats.test | 3 +- mysql-test/main/lowercase_table5.result | 6 ++-- mysql-test/main/subselect.result | 4 +-- .../main/subselect_no_exists_to_in.result | 4 +-- mysql-test/main/subselect_no_mat.result | 4 +-- mysql-test/main/subselect_no_opts.result | 4 +-- mysql-test/main/subselect_no_scache.result | 4 +-- mysql-test/main/subselect_no_semijoin.result | 4 +-- mysql-test/main/userstat.result | 28 +++++++++++++++++-- mysql-test/main/userstat.test | 18 ++++++++++++ .../suite/funcs_1/r/is_columns_is.result | 2 ++ .../funcs_1/r/is_columns_is_embedded.result | 2 ++ plugin/userstat/index_stats.cc | 2 ++ sql/handler.cc | 12 ++++++++ sql/structs.h | 3 ++ 16 files changed, 96 insertions(+), 32 deletions(-) diff --git a/mysql-test/main/information_schema_stats.result b/mysql-test/main/information_schema_stats.result index 352bcbab823..dae6b8c8972 100644 --- a/mysql-test/main/information_schema_stats.result +++ b/mysql-test/main/information_schema_stats.result @@ -16,30 +16,32 @@ select count(*) from just_a_test where state = 'California'; count(*) 2 select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ -test just_a_test IND_just_a_test_first_name_last_name 1 -test just_a_test IND_just_a_test_state 2 +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES +test just_a_test IND_just_a_test_first_name_last_name 1 1 +test just_a_test IND_just_a_test_state 2 1 select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test'; TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES test just_a_test 18 5 5 alter table just_a_test drop key IND_just_a_test_first_name_last_name; select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ -test just_a_test IND_just_a_test_state 2 +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES +test just_a_test IND_just_a_test_state 2 1 select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test'; TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES test just_a_test 23 5 5 alter table just_a_test drop column state; select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test'; TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES test just_a_test 28 5 5 -drop table just_a_test; select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES +drop table just_a_test; select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test'; TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES +select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES create table just_a_test(id int not null primary key,first_name varchar(10),last_name varchar(10),address varchar(100),phone bigint,email varchar(30), state varchar(30),key(first_name,last_name),key(state)); insert into just_a_test values(1,'fa','la','china_a',11111111,'fa_la@163.com','California'), (2,'fb','lb','china_b',22222222,'fb_lb@163.com','Arizona'), @@ -56,16 +58,16 @@ select count(*) from just_a_test where id between 2 and 4; count(*) 3 select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ -test just_a_test PRIMARY 4 -test just_a_test first_name 1 -test just_a_test state 2 +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES +test just_a_test PRIMARY 4 1 +test just_a_test first_name 1 1 +test just_a_test state 2 1 select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test'; TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES test just_a_test 7 5 15 drop table just_a_test; select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test'; TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES set global userstat=@save_userstat; diff --git a/mysql-test/main/information_schema_stats.test b/mysql-test/main/information_schema_stats.test index fd5171c3fb4..5e6a949d98d 100644 --- a/mysql-test/main/information_schema_stats.test +++ b/mysql-test/main/information_schema_stats.test @@ -24,9 +24,10 @@ select * from information_schema.table_statistics where table_schema='test' and alter table just_a_test drop column state; select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test'; -drop table just_a_test; select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; +drop table just_a_test; select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test'; +select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test'; # # Test direct drop table # diff --git a/mysql-test/main/lowercase_table5.result b/mysql-test/main/lowercase_table5.result index b284de8e996..c4193a8d822 100644 --- a/mysql-test/main/lowercase_table5.result +++ b/mysql-test/main/lowercase_table5.result @@ -130,9 +130,9 @@ TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES test T1 4 4 4 test t1 4 4 4 SELECT * FROM INFORMATION_SCHEMA.INDEX_STATISTICS ORDER BY BINARY TABLE_NAME; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ -test T1 a 4 -test t1 a 4 +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES +test T1 a 4 1 +test t1 a 4 1 DROP TABLE t1; DROP TABLE T1; SET GLOBAL userstat=DEFAULT; diff --git a/mysql-test/main/subselect.result b/mysql-test/main/subselect.result index 627090703b4..08a0f240a69 100644 --- a/mysql-test/main/subselect.result +++ b/mysql-test/main/subselect.result @@ -7128,8 +7128,8 @@ Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes test t1 2 0 0 test t2 3 0 0 show index_statistics; -Table_schema Table_name Index_name Rows_read -test t2 b 1 +Table_schema Table_name Index_name Rows_read Queries +test t2 b 1 1 set global userstat=@tmp_mdev410; DROP TABLE t1,t2,t3,t4; # diff --git a/mysql-test/main/subselect_no_exists_to_in.result b/mysql-test/main/subselect_no_exists_to_in.result index d3f5e6d6ce4..d04b05e77a7 100644 --- a/mysql-test/main/subselect_no_exists_to_in.result +++ b/mysql-test/main/subselect_no_exists_to_in.result @@ -7130,8 +7130,8 @@ Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes test t1 2 0 0 test t2 3 0 0 show index_statistics; -Table_schema Table_name Index_name Rows_read -test t2 b 1 +Table_schema Table_name Index_name Rows_read Queries +test t2 b 1 1 set global userstat=@tmp_mdev410; DROP TABLE t1,t2,t3,t4; # diff --git a/mysql-test/main/subselect_no_mat.result b/mysql-test/main/subselect_no_mat.result index 25924f3d247..70d1a01224b 100644 --- a/mysql-test/main/subselect_no_mat.result +++ b/mysql-test/main/subselect_no_mat.result @@ -7126,8 +7126,8 @@ Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes test t1 2 0 0 test t2 3 0 0 show index_statistics; -Table_schema Table_name Index_name Rows_read -test t2 b 1 +Table_schema Table_name Index_name Rows_read Queries +test t2 b 1 1 set global userstat=@tmp_mdev410; DROP TABLE t1,t2,t3,t4; # diff --git a/mysql-test/main/subselect_no_opts.result b/mysql-test/main/subselect_no_opts.result index 191dfc828aa..77d0d73756a 100644 --- a/mysql-test/main/subselect_no_opts.result +++ b/mysql-test/main/subselect_no_opts.result @@ -7123,8 +7123,8 @@ Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes test t1 2 0 0 test t2 3 0 0 show index_statistics; -Table_schema Table_name Index_name Rows_read -test t2 b 1 +Table_schema Table_name Index_name Rows_read Queries +test t2 b 1 1 set global userstat=@tmp_mdev410; DROP TABLE t1,t2,t3,t4; # diff --git a/mysql-test/main/subselect_no_scache.result b/mysql-test/main/subselect_no_scache.result index 0d18c5cc5eb..8103c1ec5e0 100644 --- a/mysql-test/main/subselect_no_scache.result +++ b/mysql-test/main/subselect_no_scache.result @@ -7134,8 +7134,8 @@ Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes test t1 2 0 0 test t2 3 0 0 show index_statistics; -Table_schema Table_name Index_name Rows_read -test t2 b 1 +Table_schema Table_name Index_name Rows_read Queries +test t2 b 1 1 set global userstat=@tmp_mdev410; DROP TABLE t1,t2,t3,t4; # diff --git a/mysql-test/main/subselect_no_semijoin.result b/mysql-test/main/subselect_no_semijoin.result index 0e2b6a9d9d7..308b0fa31d9 100644 --- a/mysql-test/main/subselect_no_semijoin.result +++ b/mysql-test/main/subselect_no_semijoin.result @@ -7123,8 +7123,8 @@ Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes test t1 2 0 0 test t2 3 0 0 show index_statistics; -Table_schema Table_name Index_name Rows_read -test t2 b 1 +Table_schema Table_name Index_name Rows_read Queries +test t2 b 1 1 set global userstat=@tmp_mdev410; DROP TABLE t1,t2,t3,t4; # diff --git a/mysql-test/main/userstat.result b/mysql-test/main/userstat.result index eec7910aae8..a0e8cf1fcf0 100644 --- a/mysql-test/main/userstat.result +++ b/mysql-test/main/userstat.result @@ -59,6 +59,7 @@ TABLE_SCHEMA varchar(192) NO NULL TABLE_NAME varchar(192) NO NULL INDEX_NAME varchar(192) NO NULL ROWS_READ bigint(21) NO NULL +QUERIES bigint(21) NO NULL show columns from information_schema.table_statistics; Field Type Null Key Default Extra TABLE_SCHEMA varchar(192) NO NULL @@ -138,13 +139,13 @@ handler_read_key disconnect ssl_con; set @@global.userstat=0; select * from information_schema.index_statistics; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES select * from information_schema.table_statistics; TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES show table_statistics; Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes show index_statistics; -Table_schema Table_name Index_name Rows_read +Table_schema Table_name Index_name Rows_read Queries select TOTAL_CONNECTIONS, TOTAL_SSL_CONNECTIONS, CONCURRENT_CONNECTIONS, ROWS_READ, ROWS_SENT, ROWS_DELETED, ROWS_INSERTED, ROWS_UPDATED, SELECT_COMMANDS, UPDATE_COMMANDS, COMMIT_TRANSACTIONS, ROLLBACK_TRANSACTIONS, DENIED_CONNECTIONS, LOST_CONNECTIONS, ACCESS_DENIED, EMPTY_QUERIES from information_schema.client_statistics;; TOTAL_CONNECTIONS 2 TOTAL_SSL_CONNECTIONS 1 @@ -188,7 +189,7 @@ OTHER_COMMANDS IN (7,8) flush table_statistics; flush index_statistics; select * from information_schema.index_statistics; -TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ +TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES select * from information_schema.table_statistics; TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES show status like "%generic%"; @@ -248,3 +249,24 @@ drop function f; # # End of 10.2 tests # +# +# MDEV-33901 INDEX_STATISTICS.QUERIES is incremented additionally for +# subqueries +# +SET @save_userstat= @@userstat; +set global userstat= 1; +create or replace table t1 (a int, key(a)) engine=MyISAM; +insert into t1 values (1),(2),(3),(4); +flush index_statistics; +select a from t1 where a in ( select a from t1 ); +a +1 +2 +3 +4 +show index_statistics; +Table_schema Table_name Index_name Rows_read Queries +test t1 a 8 1 +drop table t1; +set global userstat=@save_userstat; +# End of 11.5 tests diff --git a/mysql-test/main/userstat.test b/mysql-test/main/userstat.test index a452bd9e679..36a128b25f1 100644 --- a/mysql-test/main/userstat.test +++ b/mysql-test/main/userstat.test @@ -133,3 +133,21 @@ drop function f; --echo # --echo # End of 10.2 tests --echo # + +--echo # +--echo # MDEV-33901 INDEX_STATISTICS.QUERIES is incremented additionally for +--echo # subqueries +--echo # + +SET @save_userstat= @@userstat; +set global userstat= 1; + +create or replace table t1 (a int, key(a)) engine=MyISAM; +insert into t1 values (1),(2),(3),(4); +flush index_statistics; +select a from t1 where a in ( select a from t1 ); +show index_statistics; +drop table t1; +set global userstat=@save_userstat; + +--echo # End of 11.5 tests diff --git a/mysql-test/suite/funcs_1/r/is_columns_is.result b/mysql-test/suite/funcs_1/r/is_columns_is.result index c136485965c..d0ff156e7a8 100644 --- a/mysql-test/suite/funcs_1/r/is_columns_is.result +++ b/mysql-test/suite/funcs_1/r/is_columns_is.result @@ -184,6 +184,7 @@ def information_schema GLOBAL_STATUS VARIABLE_VALUE 2 NULL NO varchar 4096 12288 def information_schema GLOBAL_VARIABLES VARIABLE_NAME 1 NULL NO varchar 64 192 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(64) select NEVER NULL NO NO def information_schema GLOBAL_VARIABLES VARIABLE_VALUE 2 NULL NO varchar 4096 12288 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(4096) select NEVER NULL NO NO def information_schema INDEX_STATISTICS INDEX_NAME 3 NULL NO varchar 192 576 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(192) select NEVER NULL NO NO +def information_schema INDEX_STATISTICS QUERIES 5 NULL NO bigint NULL NULL 19 0 NULL NULL NULL bigint(21) select NEVER NULL NO NO def information_schema INDEX_STATISTICS ROWS_READ 4 NULL NO bigint NULL NULL 19 0 NULL NULL NULL bigint(21) select NEVER NULL NO NO def information_schema INDEX_STATISTICS TABLE_NAME 2 NULL NO varchar 192 576 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(192) select NEVER NULL NO NO def information_schema INDEX_STATISTICS TABLE_SCHEMA 1 NULL NO varchar 192 576 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(192) select NEVER NULL NO NO @@ -779,6 +780,7 @@ NULL information_schema GEOMETRY_COLUMNS SRID smallint NULL NULL NULL NULL small 3.0000 information_schema INDEX_STATISTICS TABLE_NAME varchar 192 576 utf8mb3 utf8mb3_general_ci varchar(192) 3.0000 information_schema INDEX_STATISTICS INDEX_NAME varchar 192 576 utf8mb3 utf8mb3_general_ci varchar(192) NULL information_schema INDEX_STATISTICS ROWS_READ bigint NULL NULL NULL NULL bigint(21) +NULL information_schema INDEX_STATISTICS QUERIES bigint NULL NULL NULL NULL bigint(21) 3.0000 information_schema KEYWORDS WORD varchar 64 192 utf8mb3 utf8mb3_general_ci varchar(64) 3.0000 information_schema KEY_CACHES KEY_CACHE_NAME varchar 192 576 utf8mb3 utf8mb3_general_ci varchar(192) NULL information_schema KEY_CACHES SEGMENTS int NULL NULL NULL NULL int(3) unsigned diff --git a/mysql-test/suite/funcs_1/r/is_columns_is_embedded.result b/mysql-test/suite/funcs_1/r/is_columns_is_embedded.result index 915d179bbb2..d927fdee2b7 100644 --- a/mysql-test/suite/funcs_1/r/is_columns_is_embedded.result +++ b/mysql-test/suite/funcs_1/r/is_columns_is_embedded.result @@ -184,6 +184,7 @@ def information_schema GLOBAL_STATUS VARIABLE_VALUE 2 NULL NO varchar 4096 12288 def information_schema GLOBAL_VARIABLES VARIABLE_NAME 1 NULL NO varchar 64 192 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(64) NEVER NULL NO NO def information_schema GLOBAL_VARIABLES VARIABLE_VALUE 2 NULL NO varchar 4096 12288 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(4096) NEVER NULL NO NO def information_schema INDEX_STATISTICS INDEX_NAME 3 NULL NO varchar 192 576 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(192) NEVER NULL NO NO +def information_schema INDEX_STATISTICS QUERIES 5 NULL NO bigint NULL NULL 19 0 NULL NULL NULL bigint(21) NEVER NULL NO NO def information_schema INDEX_STATISTICS ROWS_READ 4 NULL NO bigint NULL NULL 19 0 NULL NULL NULL bigint(21) NEVER NULL NO NO def information_schema INDEX_STATISTICS TABLE_NAME 2 NULL NO varchar 192 576 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(192) NEVER NULL NO NO def information_schema INDEX_STATISTICS TABLE_SCHEMA 1 NULL NO varchar 192 576 NULL NULL NULL utf8mb3 utf8mb3_general_ci varchar(192) NEVER NULL NO NO @@ -779,6 +780,7 @@ NULL information_schema GEOMETRY_COLUMNS SRID smallint NULL NULL NULL NULL small 3.0000 information_schema INDEX_STATISTICS TABLE_NAME varchar 192 576 utf8mb3 utf8mb3_general_ci varchar(192) 3.0000 information_schema INDEX_STATISTICS INDEX_NAME varchar 192 576 utf8mb3 utf8mb3_general_ci varchar(192) NULL information_schema INDEX_STATISTICS ROWS_READ bigint NULL NULL NULL NULL bigint(21) +NULL information_schema INDEX_STATISTICS QUERIES bigint NULL NULL NULL NULL bigint(21) 3.0000 information_schema KEYWORDS WORD varchar 64 192 utf8mb3 utf8mb3_general_ci varchar(64) 3.0000 information_schema KEY_CACHES KEY_CACHE_NAME varchar 192 576 utf8mb3 utf8mb3_general_ci varchar(192) NULL information_schema KEY_CACHES SEGMENTS int NULL NULL NULL NULL int(3) unsigned diff --git a/plugin/userstat/index_stats.cc b/plugin/userstat/index_stats.cc index 97305e894bb..8dcb69c5225 100644 --- a/plugin/userstat/index_stats.cc +++ b/plugin/userstat/index_stats.cc @@ -6,6 +6,7 @@ static ST_FIELD_INFO index_stats_fields[]= Column("TABLE_NAME", Varchar(NAME_LEN), NOT_NULL, "Table_name"), Column("INDEX_NAME", Varchar(NAME_LEN), NOT_NULL, "Index_name"), Column("ROWS_READ", SLonglong(), NOT_NULL, "Rows_read"), + Column("QUERIES", SLonglong(), NOT_NULL, "Queries"), CEnd() }; @@ -44,6 +45,7 @@ static int index_stats_fill(THD *thd, TABLE_LIST *tables, COND *cond) system_charset_info); table->field[2]->store(index_name, (uint) index_name_length, system_charset_info); table->field[3]->store((longlong)index_stats->rows_read, TRUE); + table->field[4]->store((longlong)index_stats->queries, TRUE); if (schema_table_store_record(thd, table)) { diff --git a/sql/handler.cc b/sql/handler.cc index c0211accb2a..0d770b90c5a 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -6033,6 +6033,9 @@ void handler::update_global_index_stats() { DBUG_ASSERT(table->s); + if (table->s->table_category != TABLE_CATEGORY_USER) + return; // Ignore stat tables, performance_schema, information_schema etc. + if (!table->in_use->userstat_running) { /* Reset all index read values */ @@ -6073,6 +6076,15 @@ void handler::update_global_index_stats() } /* Updates the global index stats. */ index_stats->rows_read+= index_rows_read[index]; + /* + Ensure we do not update queries if the table is used + twice in the same statement. + */ + if (index_stats->query_id != table->in_use->query_id) + { + index_stats->query_id= table->in_use->query_id; + index_stats->queries++; + } index_rows_read[index]= 0; end: mysql_mutex_unlock(&LOCK_global_index_stats); diff --git a/sql/structs.h b/sql/structs.h index 4f196de6254..27e71f83883 100644 --- a/sql/structs.h +++ b/sql/structs.h @@ -30,6 +30,7 @@ #include "sql_bitmap.h" #include "lex_charset.h" #include "lex_ident.h" +#include "sql_basic_types.h" /* query_id_t */ struct TABLE; class Type_handler; @@ -381,6 +382,8 @@ typedef struct st_index_stats char index[NAME_LEN * 3 + 3]; size_t index_name_length; /* Length of 'index' */ ulonglong rows_read; + ulonglong queries; + query_id_t query_id; } INDEX_STATS;