diff options
author | Vicențiu Ciorbaru <vicentiu@mariadb.org> | 2019-02-10 01:43:15 +0200 |
---|---|---|
committer | Vicențiu Ciorbaru <vicentiu@mariadb.org> | 2019-02-10 01:43:15 +0200 |
commit | 25947c60d54b969359521b9ca55da3054c600745 (patch) | |
tree | d28924673e6f4ddab059251395d60b9f189ed7c2 /sql/sql_statistics.cc | |
parent | 30a18eed822c207880a52b31c566525dfef8fb55 (diff) | |
download | mariadb-git-10.4-vicentiu-histograms.tar.gz |
Default Bernoulli Sampling implementationbb-10.4-vicentiu-histograms10.4-vicentiu-histograms
Diffstat (limited to 'sql/sql_statistics.cc')
-rw-r--r-- | sql/sql_statistics.cc | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc index daebc5d0b38..b8b99015745 100644 --- a/sql/sql_statistics.cc +++ b/sql/sql_statistics.cc @@ -2727,12 +2727,15 @@ int collect_statistics_for_table(THD *thd, TABLE *table) Field *table_field; ha_rows rows= 0; handler *file=table->file; + double sample_fraction; DBUG_ENTER("collect_statistics_for_table"); table->collected_stats->cardinality_is_null= TRUE; table->collected_stats->cardinality= 0; + table->file->info(HA_STATUS_VARIABLE); + for (field_ptr= table->field; *field_ptr; field_ptr++) { table_field= *field_ptr; @@ -2743,19 +2746,27 @@ int collect_statistics_for_table(THD *thd, TABLE *table) restore_record(table, s->default_values); - rc= file->ha_random_sample_init(thd, 100); - rc= file->ha_random_sample(table->record[0]); - table_field->collected_stats->add(0); - rc= file->ha_random_sample_end(); + if (file->records() < 30000) + { + sample_fraction= 1; + } + else + { + sample_fraction= std::fmin( + (30000 + 4096 * log(200 * file->records())) / + (file->records() + 1), 1); + } + + + /* Fetch samples from the table to collect statistics on table's columns */ - /* Perform a full table scan to collect statistics on 'table's columns */ - /* - if (!(rc= file->ha_rnd_init(TRUE))) - { + if (!(rc= file->ha_random_sample_init(thd, HA_SAMPLE_BERNOULLI, + sample_fraction))) + { DEBUG_SYNC(table->in_use, "statistics_collection_start"); - while ((rc= file->ha_rnd_next(table->record[0])) != HA_ERR_END_OF_FILE) + while ((rc= file->ha_random_sample(table->record[0])) != HA_ERR_END_OF_FILE) { if (thd->killed) break; @@ -2775,10 +2786,9 @@ int collect_statistics_for_table(THD *thd, TABLE *table) break; rows++; } - file->ha_rnd_end(); + file->ha_random_sample_end(); } rc= (rc == HA_ERR_END_OF_FILE && !thd->killed) ? 0 : 1; - */ /* Calculate values for all statistical characteristics on columns and and for each field f of 'table' save them in the write_stat structure @@ -2787,7 +2797,7 @@ int collect_statistics_for_table(THD *thd, TABLE *table) if (!rc) { table->collected_stats->cardinality_is_null= FALSE; - table->collected_stats->cardinality= rows; + table->collected_stats->cardinality= rows / sample_fraction; } bitmap_clear_all(table->write_set); |