Skip to content

Commit 5a2f150

Browse files
author
Erik Froseth
committed
Bug#27637968 PROVIDE A DEFAULT NUMBER OF BUCKETS FOR HISTOGRAM STATISTICS
Problem: If a user wants to create histogram statistics, he/she MUST specify the maximum number of buckets to be created. However, the server should provide a default value since it's difficult for users to know what a reasonable number of buckets is. Fix: Make the clause [WITH n BUCKETS] optional. If it isn't specified, a default value of 100 will be used. Change-Id: I6e00adbb37ebd412b2908ad731daa27713171116
1 parent 5d99362 commit 5a2f150

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

mysql-test/r/histograms.result

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,3 +3112,18 @@ should_be_true
31123112
SET cte_max_recursion_depth = DEFAULT;
31133113
SET histogram_generation_max_mem_size = DEFAULT;
31143114
DROP TABLE t1;
3115+
#
3116+
# Bug#27637968 PROVIDE A DEFAULT NUMBER OF BUCKETS FOR HISTOGRAM
3117+
# STATISTICS
3118+
#
3119+
CREATE TABLE t1 (col1 INT);
3120+
INSERT INTO t1 VALUES (10), (20), (30);
3121+
ANALYZE TABLE t1 UPDATE HISTOGRAM ON col1;
3122+
Table Op Msg_type Msg_text
3123+
test.t1 histogram status Histogram statistics created for column 'col1'.
3124+
SELECT JSON_EXTRACT(histogram, '$."number-of-buckets-specified"')
3125+
FROM INFORMATION_SCHEMA.column_statistics
3126+
WHERE table_name = "t1" AND column_name = "col1";
3127+
JSON_EXTRACT(histogram, '$."number-of-buckets-specified"')
3128+
100
3129+
DROP TABLE t1;

mysql-test/t/histograms.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,3 +1932,16 @@ SET cte_max_recursion_depth = DEFAULT;
19321932
SET histogram_generation_max_mem_size = DEFAULT;
19331933

19341934
DROP TABLE t1;
1935+
1936+
--echo #
1937+
--echo # Bug#27637968 PROVIDE A DEFAULT NUMBER OF BUCKETS FOR HISTOGRAM
1938+
--echo # STATISTICS
1939+
--echo #
1940+
CREATE TABLE t1 (col1 INT);
1941+
INSERT INTO t1 VALUES (10), (20), (30);
1942+
ANALYZE TABLE t1 UPDATE HISTOGRAM ON col1;
1943+
1944+
SELECT JSON_EXTRACT(histogram, '$."number-of-buckets-specified"')
1945+
FROM INFORMATION_SCHEMA.column_statistics
1946+
WHERE table_name = "t1" AND column_name = "col1";
1947+
DROP TABLE t1;

sql/sql_yacc.yy

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ using std::max;
120120
/// The maximum number of histogram buckets.
121121
static const int MAX_NUMBER_OF_HISTOGRAM_BUCKETS= 1024;
122122

123+
/// The default number of histogram buckets when the user does not specify it
124+
/// explicitly. A value of 100 is chosen because the gain in accuracy above this
125+
/// point seems to be generally low.
126+
static const int DEFAULT_NUMBER_OF_HISTOGRAM_BUCKETS= 100;
127+
123128
int yylex(void *yylval, void *yythd);
124129

125130
#define yyoverflow(A,B,C,D,E,F,G,H) \
@@ -1294,6 +1299,7 @@ void warn_about_deprecated_national(THD *thd)
12941299
trg_action_time trg_event
12951300
view_check_option
12961301
signed_num
1302+
opt_num_buckets
12971303

12981304

12991305
%type <order_direction> order_dir
@@ -8262,21 +8268,12 @@ analyze_table_stmt:
82628268
}
82638269
;
82648270

8265-
opt_histogram:
8266-
/* empty */
8271+
opt_num_buckets:
8272+
/* empty */ { $$= DEFAULT_NUMBER_OF_HISTOGRAM_BUCKETS; }
8273+
| WITH NUM BUCKETS_SYM
82678274
{
8268-
$$.command= Sql_cmd_analyze_table::Histogram_command::NONE;
8269-
$$.columns= nullptr;
8270-
$$.num_buckets= 0;
8271-
}
8272-
| UPDATE_SYM HISTOGRAM_SYM ON_SYM ident_string_list WITH NUM BUCKETS_SYM
8273-
{
8274-
$$.command=
8275-
Sql_cmd_analyze_table::Histogram_command::UPDATE_HISTOGRAM;
8276-
$$.columns= $4;
8277-
82788275
int error;
8279-
longlong num= my_strtoll10($6.str, nullptr, &error);
8276+
longlong num= my_strtoll10($2.str, nullptr, &error);
82808277
MYSQL_YYABORT_UNLESS(error <= 0);
82818278

82828279
if (num < 1 || num > MAX_NUMBER_OF_HISTOGRAM_BUCKETS)
@@ -8286,7 +8283,23 @@ opt_histogram:
82868283
MYSQL_YYABORT;
82878284
}
82888285

8289-
$$.num_buckets= static_cast<int>(num);
8286+
$$= num;
8287+
}
8288+
;
8289+
8290+
opt_histogram:
8291+
/* empty */
8292+
{
8293+
$$.command= Sql_cmd_analyze_table::Histogram_command::NONE;
8294+
$$.columns= nullptr;
8295+
$$.num_buckets= 0;
8296+
}
8297+
| UPDATE_SYM HISTOGRAM_SYM ON_SYM ident_string_list opt_num_buckets
8298+
{
8299+
$$.command=
8300+
Sql_cmd_analyze_table::Histogram_command::UPDATE_HISTOGRAM;
8301+
$$.columns= $4;
8302+
$$.num_buckets= $5;
82908303
}
82918304
| DROP HISTOGRAM_SYM ON_SYM ident_string_list
82928305
{

0 commit comments

Comments
 (0)