Skip to content

Commit 9207a83

Browse files
author
Varun Gupta
committed
MDEV-17255: New optimizer defaults and ANALYZE TABLE
Added to new values to the server variable use_stat_tables. The values are COMPLEMENTARY_FOR_QUERIES and PREFERABLY_FOR_QUERIES. Both these values don't allow to collect EITS for queries like analyze table t1; To collect EITS we would need to use the syntax with persistent like analyze table t1 persistent for columns (col1,col2...) index (idx1, idx2...) / ALL Changing the default value from NEVER to PREFERABLY_FOR_QUERIES.
1 parent 93c360e commit 9207a83

12 files changed

+198
-15
lines changed

mysql-test/include/default_mysqld.cnf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ loose-performance-schema-consumer-thread-instrumentation=ON
107107
binlog-direct-non-transactional-updates
108108

109109
default-storage-engine=myisam
110+
use_stat_tables=preferably
110111

111112
loose-ssl-ca=@ENV.MYSQL_TEST_DIR/std_data/cacert.pem
112113
loose-ssl-cert=@ENV.MYSQL_TEST_DIR/std_data/server-cert.pem

mysql-test/main/mysqld--help.result

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,8 @@ The following specify which files/extra groups are read (specified before remain
13581358
(usually get from GUI tools)
13591359
--use-stat-tables=name
13601360
Specifies how to use system statistics tables. One of:
1361-
NEVER, COMPLEMENTARY, PREFERABLY
1361+
NEVER, COMPLEMENTARY, PREFERABLY,
1362+
COMPLEMENTARY_FOR_QUERIES, PREFERABLY_FOR_QUERIES
13621363
-u, --user=name Run mysqld daemon as user.
13631364
--userstat Enables statistics gathering for USER_STATISTICS,
13641365
CLIENT_STATISTICS, INDEX_STATISTICS and TABLE_STATISTICS
@@ -1723,7 +1724,7 @@ transaction-isolation REPEATABLE-READ
17231724
transaction-prealloc-size 4096
17241725
transaction-read-only FALSE
17251726
updatable-views-with-limit YES
1726-
use-stat-tables PREFERABLY
1727+
use-stat-tables PREFERABLY_FOR_QUERIES
17271728
userstat FALSE
17281729
verbose TRUE
17291730
wait-timeout 28800

mysql-test/main/stat_tables.result

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,59 @@ MAX(pk)
625625
NULL
626626
DROP TABLE t1;
627627
set use_stat_tables=@save_use_stat_tables;
628+
#
629+
# MDEV-17255: New optimizer defaults and ANALYZE TABLE
630+
#
631+
create table t1 (a int, b int);
632+
insert into t1(a,b) values (1,2),(1,3),(1,4),(1,5),(2,6),(2,7),(3,8),(3,9),(3,9),(4,10);
633+
set use_stat_tables= preferably_for_queries;
634+
#
635+
# with use_stat_tables= PREFERABLY_FOR_QUERIES
636+
# analyze table t1 will not collect statistics
637+
#
638+
analyze table t1;
639+
Table Op Msg_type Msg_text
640+
test.t1 analyze status OK
641+
select * from mysql.column_stats;
642+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
643+
analyze
644+
select * from t1 where a = 1 and b=3;
645+
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
646+
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 100.00 10.00 Using where
647+
#
648+
# with use_stat_tables= PREFERABLY_FOR_QUERIES
649+
# analyze table t1 will collect statistics if we use PERSISTENT
650+
# for columns, indexes or everything
651+
#
652+
analyze table t1 persistent for columns (a) indexes ();
653+
Table Op Msg_type Msg_text
654+
test.t1 analyze status Engine-independent statistics collected
655+
test.t1 analyze status Table is already up to date
656+
select * from mysql.column_stats;
657+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
658+
test t1 a 1 4 0.0000 4.0000 2.5000 0 NULL NULL
659+
# filtered shows that we used the data from stat tables
660+
analyze
661+
select * from t1 where a = 1 and b=3;
662+
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
663+
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 25.00 10.00 Using where
664+
#
665+
# with use_stat_tables= PREFERABLY
666+
# analyze table t1 will collect statistics
667+
#
668+
set use_stat_tables=PREFERABLY;
669+
analyze table t1;
670+
Table Op Msg_type Msg_text
671+
test.t1 analyze status Engine-independent statistics collected
672+
test.t1 analyze status Table is already up to date
673+
select * from mysql.column_stats;
674+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
675+
test t1 a 1 4 0.0000 4.0000 2.5000 0 NULL NULL
676+
test t1 b 2 10 0.0000 4.0000 1.1111 0 NULL NULL
677+
# filtered shows that we used the data from stat tables
678+
analyze
679+
select * from t1 where a=1 and b=3;
680+
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
681+
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 2.78 10.00 Using where
682+
drop table t1;
683+
set use_stat_tables=@save_use_stat_tables;

mysql-test/main/stat_tables.test

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,5 +400,47 @@ CREATE OR REPLACE TABLE t1 (pk INT PRIMARY KEY, t TEXT);
400400
SELECT MAX(pk) FROM t1;
401401

402402
DROP TABLE t1;
403+
set use_stat_tables=@save_use_stat_tables;
404+
405+
--echo #
406+
--echo # MDEV-17255: New optimizer defaults and ANALYZE TABLE
407+
--echo #
408+
409+
create table t1 (a int, b int);
410+
insert into t1(a,b) values (1,2),(1,3),(1,4),(1,5),(2,6),(2,7),(3,8),(3,9),(3,9),(4,10);
411+
set use_stat_tables= preferably_for_queries;
412+
--echo #
413+
--echo # with use_stat_tables= PREFERABLY_FOR_QUERIES
414+
--echo # analyze table t1 will not collect statistics
415+
--echo #
416+
417+
analyze table t1;
418+
select * from mysql.column_stats;
419+
analyze
420+
select * from t1 where a = 1 and b=3;
403421

422+
--echo #
423+
--echo # with use_stat_tables= PREFERABLY_FOR_QUERIES
424+
--echo # analyze table t1 will collect statistics if we use PERSISTENT
425+
--echo # for columns, indexes or everything
426+
--echo #
427+
428+
analyze table t1 persistent for columns (a) indexes ();
429+
select * from mysql.column_stats;
430+
--echo # filtered shows that we used the data from stat tables
431+
analyze
432+
select * from t1 where a = 1 and b=3;
433+
434+
--echo #
435+
--echo # with use_stat_tables= PREFERABLY
436+
--echo # analyze table t1 will collect statistics
437+
--echo #
438+
439+
set use_stat_tables=PREFERABLY;
440+
analyze table t1;
441+
select * from mysql.column_stats;
442+
--echo # filtered shows that we used the data from stat tables
443+
analyze
444+
select * from t1 where a=1 and b=3;
445+
drop table t1;
404446
set use_stat_tables=@save_use_stat_tables;

mysql-test/main/stat_tables_innodb.result

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,5 +652,61 @@ MAX(pk)
652652
NULL
653653
DROP TABLE t1;
654654
set use_stat_tables=@save_use_stat_tables;
655+
#
656+
# MDEV-17255: New optimizer defaults and ANALYZE TABLE
657+
#
658+
create table t1 (a int, b int);
659+
insert into t1(a,b) values (1,2),(1,3),(1,4),(1,5),(2,6),(2,7),(3,8),(3,9),(3,9),(4,10);
660+
set use_stat_tables= preferably_for_queries;
661+
#
662+
# with use_stat_tables= PREFERABLY_FOR_QUERIES
663+
# analyze table t1 will not collect statistics
664+
#
665+
analyze table t1;
666+
Table Op Msg_type Msg_text
667+
test.t1 analyze status OK
668+
select * from mysql.column_stats;
669+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
670+
analyze
671+
select * from t1 where a = 1 and b=3;
672+
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
673+
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 100.00 10.00 Using where
674+
#
675+
# with use_stat_tables= PREFERABLY_FOR_QUERIES
676+
# analyze table t1 will collect statistics if we use PERSISTENT
677+
# for columns, indexes or everything
678+
#
679+
analyze table t1 persistent for columns (a) indexes ();
680+
Table Op Msg_type Msg_text
681+
test.t1 analyze status Engine-independent statistics collected
682+
test.t1 analyze status OK
683+
select * from mysql.column_stats;
684+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
685+
test t1 a 1 4 0.0000 4.0000 2.5000 0 NULL NULL
686+
# filtered shows that we used the data from stat tables
687+
analyze
688+
select * from t1 where a = 1 and b=3;
689+
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
690+
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 25.00 10.00 Using where
691+
#
692+
# with use_stat_tables= PREFERABLY
693+
# analyze table t1 will collect statistics
694+
#
695+
set use_stat_tables=PREFERABLY;
696+
analyze table t1;
697+
Table Op Msg_type Msg_text
698+
test.t1 analyze status Engine-independent statistics collected
699+
test.t1 analyze status OK
700+
select * from mysql.column_stats;
701+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
702+
test t1 a 1 4 0.0000 4.0000 2.5000 0 NULL NULL
703+
test t1 b 2 10 0.0000 4.0000 1.1111 0 NULL NULL
704+
# filtered shows that we used the data from stat tables
705+
analyze
706+
select * from t1 where a=1 and b=3;
707+
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
708+
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 2.78 10.00 Using where
709+
drop table t1;
710+
set use_stat_tables=@save_use_stat_tables;
655711
set optimizer_switch=@save_optimizer_switch_for_stat_tables_test;
656712
SET SESSION STORAGE_ENGINE=DEFAULT;

mysql-test/suite/sys_vars/r/sysvars_server_embedded.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4509,15 +4509,15 @@ COMMAND_LINE_ARGUMENT OPTIONAL
45094509
VARIABLE_NAME USE_STAT_TABLES
45104510
SESSION_VALUE PREFERABLY
45114511
GLOBAL_VALUE PREFERABLY
4512-
GLOBAL_VALUE_ORIGIN COMPILE-TIME
4513-
DEFAULT_VALUE PREFERABLY
4512+
GLOBAL_VALUE_ORIGIN CONFIG
4513+
DEFAULT_VALUE PREFERABLY_FOR_QUERIES
45144514
VARIABLE_SCOPE SESSION
45154515
VARIABLE_TYPE ENUM
45164516
VARIABLE_COMMENT Specifies how to use system statistics tables
45174517
NUMERIC_MIN_VALUE NULL
45184518
NUMERIC_MAX_VALUE NULL
45194519
NUMERIC_BLOCK_SIZE NULL
4520-
ENUM_VALUE_LIST NEVER,COMPLEMENTARY,PREFERABLY
4520+
ENUM_VALUE_LIST NEVER,COMPLEMENTARY,PREFERABLY,COMPLEMENTARY_FOR_QUERIES,PREFERABLY_FOR_QUERIES
45214521
READ_ONLY NO
45224522
COMMAND_LINE_ARGUMENT REQUIRED
45234523
VARIABLE_NAME WAIT_TIMEOUT

mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5573,15 +5573,15 @@ COMMAND_LINE_ARGUMENT OPTIONAL
55735573
VARIABLE_NAME USE_STAT_TABLES
55745574
SESSION_VALUE PREFERABLY
55755575
GLOBAL_VALUE PREFERABLY
5576-
GLOBAL_VALUE_ORIGIN COMPILE-TIME
5577-
DEFAULT_VALUE PREFERABLY
5576+
GLOBAL_VALUE_ORIGIN CONFIG
5577+
DEFAULT_VALUE PREFERABLY_FOR_QUERIES
55785578
VARIABLE_SCOPE SESSION
55795579
VARIABLE_TYPE ENUM
55805580
VARIABLE_COMMENT Specifies how to use system statistics tables
55815581
NUMERIC_MIN_VALUE NULL
55825582
NUMERIC_MAX_VALUE NULL
55835583
NUMERIC_BLOCK_SIZE NULL
5584-
ENUM_VALUE_LIST NEVER,COMPLEMENTARY,PREFERABLY
5584+
ENUM_VALUE_LIST NEVER,COMPLEMENTARY,PREFERABLY,COMPLEMENTARY_FOR_QUERIES,PREFERABLY_FOR_QUERIES
55855585
READ_ONLY NO
55865586
COMMAND_LINE_ARGUMENT REQUIRED
55875587
VARIABLE_NAME WAIT_TIMEOUT

mysql-test/suite/sys_vars/r/use_stat_tables_basic.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SET @@global.use_stat_tables = 2;
1010
SET @@global.use_stat_tables = DEFAULT;
1111
SELECT @@global.use_stat_tables;
1212
@@global.use_stat_tables
13-
PREFERABLY
13+
PREFERABLY_FOR_QUERIES
1414
SET @@global.use_stat_tables = 0;
1515
SELECT @@global.use_stat_tables;
1616
@@global.use_stat_tables

sql/sql_admin.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
767767
}
768768
collect_eis=
769769
(table->table->s->table_category == TABLE_CATEGORY_USER &&
770-
(get_use_stat_tables_mode(thd) > NEVER ||
770+
(check_eits_collection_allowed(thd) ||
771771
lex->with_persistent_for_clause));
772772

773773

sql/sql_statistics.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3720,17 +3720,16 @@ void set_statistics_for_table(THD *thd, TABLE *table)
37203720
{
37213721
TABLE_STATISTICS_CB *stats_cb= &table->s->stats_cb;
37223722
Table_statistics *read_stats= stats_cb->table_stats;
3723-
Use_stat_tables_mode use_stat_table_mode= get_use_stat_tables_mode(thd);
37243723
table->used_stat_records=
3725-
(use_stat_table_mode <= COMPLEMENTARY ||
3724+
(!check_eits_preferred(thd) ||
37263725
!table->stats_is_read || read_stats->cardinality_is_null) ?
37273726
table->file->stats.records : read_stats->cardinality;
37283727
KEY *key_info, *key_info_end;
37293728
for (key_info= table->key_info, key_info_end= key_info+table->s->keys;
37303729
key_info < key_info_end; key_info++)
37313730
{
37323731
key_info->is_statistics_from_stat_tables=
3733-
(use_stat_table_mode > COMPLEMENTARY &&
3732+
(check_eits_preferred(thd) &&
37343733
table->stats_is_read &&
37353734
key_info->read_stats->avg_frequency_is_inited() &&
37363735
key_info->read_stats->get_avg_frequency(0) > 0.5);

0 commit comments

Comments
 (0)