summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorapostle <vladkakurin007@gmail.com>2022-05-14 14:17:57 +0000
committerapostle <vladkakurin007@gmail.com>2022-05-14 14:17:57 +0000
commit06338602ed5a9f77f84d976276012599f137d624 (patch)
tree90280a200cfa47e49cd5a8453902001ad7b8909a
parent46a38452ac14770053b3a3575ccb9a8a5b17a915 (diff)
downloadmariadb-git-06338602ed5a9f77f84d976276012599f137d624.tar.gz
add new HA_NATIVE_SAMPLING flag
-rw-r--r--sql/handler.h5
-rw-r--r--sql/sql_statistics.cc54
-rw-r--r--storage/innobase/handler/ha_innodb.cc3
3 files changed, 35 insertions, 27 deletions
diff --git a/sql/handler.h b/sql/handler.h
index 305b2afdf6a..58ad863277a 100644
--- a/sql/handler.h
+++ b/sql/handler.h
@@ -366,7 +366,10 @@ enum chf_create_flags {
/* Implements SELECT ... FOR UPDATE SKIP LOCKED */
#define HA_CAN_SKIP_LOCKED (1ULL << 61)
-#define HA_LAST_TABLE_FLAG HA_CAN_SKIP_LOCKED
+/* Implements native sampling (MDEV-19556) */
+#define HA_NATIVE_SAMPLING (1ULL << 62)
+
+#define HA_LAST_TABLE_FLAG HA_NATIVE_SAMPLING
/* bits in index_flags(index_number) for what you can do with index */
diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc
index ce856bc5204..99c8bbd26b7 100644
--- a/sql/sql_statistics.cc
+++ b/sql/sql_statistics.cc
@@ -2634,11 +2634,6 @@ int collect_statistics_for_table(THD *thd, TABLE *table)
table->collected_stats->cardinality_is_null= TRUE;
table->collected_stats->cardinality= 0;
- int b = file->sample_next(table->record[0]);
-
- b += 1;
-
-
if (thd->variables.sample_percentage == 0)
{
if (file->records() < MIN_THRESHOLD_FOR_SAMPLING)
@@ -2663,39 +2658,48 @@ int collect_statistics_for_table(THD *thd, TABLE *table)
restore_record(table, s->default_values);
+ if(file->ha_table_flags() & HA_NATIVE_SAMPLING)
+ {
+ rc = -1;
+ int b = file->sample_next(table->record[0]);
+ b += 1;
-
- /* Perform a full table scan to collect statistics on 'table's columns */
- if (!(rc= file->ha_rnd_init(TRUE)))
+ }
+ else
{
- DEBUG_SYNC(table->in_use, "statistics_collection_start");
- while ((rc= file->ha_rnd_next(table->record[0])) != HA_ERR_END_OF_FILE)
+ /* Perform a full table scan to collect statistics on 'table's columns */
+ if (!(rc= file->ha_rnd_init(TRUE)))
{
- if (thd->killed)
- break;
-
- if (rc)
- break;
+ DEBUG_SYNC(table->in_use, "statistics_collection_start");
- if (thd_rnd(thd) <= sample_fraction)
+ while ((rc= file->ha_rnd_next(table->record[0])) != HA_ERR_END_OF_FILE)
{
- for (field_ptr= table->field; *field_ptr; field_ptr++)
+ if (thd->killed)
+ break;
+
+ if (rc)
+ break;
+
+ if (thd_rnd(thd) <= sample_fraction)
{
- table_field= *field_ptr;
- if (!table_field->collected_stats)
- continue;
- if ((rc= table_field->collected_stats->add()))
+ for (field_ptr= table->field; *field_ptr; field_ptr++)
+ {
+ table_field= *field_ptr;
+ if (!table_field->collected_stats)
+ continue;
+ if ((rc= table_field->collected_stats->add()))
+ break;
+ }
+ if (rc)
break;
+ rows++;
}
- if (rc)
- break;
- rows++;
}
+ file->ha_rnd_end();
}
- file->ha_rnd_end();
}
rc= (rc == HA_ERR_END_OF_FILE && !thd->killed) ? 0 : 1;
diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
index 9d9a4453588..b1d68485a96 100644
--- a/storage/innobase/handler/ha_innodb.cc
+++ b/storage/innobase/handler/ha_innodb.cc
@@ -3020,7 +3020,8 @@ ha_innobase::ha_innobase(
| HA_CAN_ONLINE_BACKUPS
| HA_CONCURRENT_OPTIMIZE
| HA_CAN_SKIP_LOCKED
- | (srv_force_primary_key ? HA_REQUIRE_PRIMARY_KEY : 0)
+ | (srv_force_primary_key ? HA_REQUIRE_PRIMARY_KEY : 0
+ | HA_NATIVE_SAMPLING)
),
m_start_of_scan(),
m_mysql_has_locked()