| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2009, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
#include "my_dbug.h" |
| 29 |
|
| 30 |
/** @file dict/dict0stats.cc |
| 31 |
Code used for calculating and manipulating table statistics. |
| 32 |
|
| 33 |
Created Jan 06, 2010 Vasil Dimov |
| 34 |
*******************************************************/ |
| 35 |
|
| 36 |
#include <mysql_com.h> |
| 37 |
#include <algorithm> |
| 38 |
#include <map> |
| 39 |
#include <vector> |
| 40 |
|
| 41 |
#include "dict0stats.h" |
| 42 |
#include "dyn0buf.h" |
| 43 |
#include "ha_prototypes.h" |
| 44 |
#include "lob0lob.h" |
| 45 |
#include "log0chkp.h" |
| 46 |
#include "pars0pars.h" |
| 47 |
#include "row0sel.h" |
| 48 |
#include "trx0trx.h" |
| 49 |
#include "univ.i" |
| 50 |
#include "ut0new.h" |
| 51 |
#include "ut0rnd.h" |
| 52 |
#include "ut0ut.h" |
| 53 |
|
| 54 |
/* Sampling algorithm description @{ |
| 55 |
|
| 56 |
The algorithm is controlled by one number - N_SAMPLE_PAGES(index), |
| 57 |
let it be A, which is the number of leaf pages to analyze for a given index |
| 58 |
for each n-prefix (if the index is on 3 columns, then 3*A leaf pages will be |
| 59 |
analyzed). |
| 60 |
|
| 61 |
Let the total number of leaf pages in the table be T. |
| 62 |
Level 0 - leaf pages, level H - root. |
| 63 |
|
| 64 |
Definition: N-prefix-boring record is a record on a non-leaf page that equals |
| 65 |
the next (to the right, cross page boundaries, skipping the supremum and |
| 66 |
infimum) record on the same level when looking at the fist n-prefix columns. |
| 67 |
The last (user) record on a level is not boring (it does not match the |
| 68 |
non-existent user record to the right). We call the records boring because all |
| 69 |
the records on the page below a boring record are equal to that boring record. |
| 70 |
|
| 71 |
We avoid diving below boring records when searching for a leaf page to |
| 72 |
estimate the number of distinct records because we know that such a leaf |
| 73 |
page will have number of distinct records == 1. |
| 74 |
|
| 75 |
For each n-prefix: start from the root level and full scan subsequent lower |
| 76 |
levels until a level that contains at least A*10 distinct records is found. |
| 77 |
Lets call this level LA. |
| 78 |
As an optimization the search is canceled if it has reached level 1 (never |
| 79 |
descend to the level 0 (leaf)) and also if the next level to be scanned |
| 80 |
would contain more than A pages. The latter is because the user has asked |
| 81 |
to analyze A leaf pages and it does not make sense to scan much more than |
| 82 |
A non-leaf pages with the sole purpose of finding a good sample of A leaf |
| 83 |
pages. |
| 84 |
|
| 85 |
After finding the appropriate level LA with >A*10 distinct records (or less in |
| 86 |
the exceptions described above), divide it into groups of equal records and |
| 87 |
pick A such groups. Then pick the last record from each group. For example, |
| 88 |
let the level be: |
| 89 |
|
| 90 |
index: 0,1,2,3,4,5,6,7,8,9,10 |
| 91 |
record: 1,1,1,2,2,7,7,7,7,7,9 |
| 92 |
|
| 93 |
There are 4 groups of distinct records and if A=2 random ones are selected, |
| 94 |
e.g. 1,1,1 and 7,7,7,7,7, then records with indexes 2 and 9 will be selected. |
| 95 |
|
| 96 |
After selecting A records as described above, dive below them to find A leaf |
| 97 |
pages and analyze them, finding the total number of distinct records. The |
| 98 |
dive to the leaf level is performed by selecting a non-boring record from |
| 99 |
each page and diving below it. |
| 100 |
|
| 101 |
This way, a total of A leaf pages are analyzed for the given n-prefix. |
| 102 |
|
| 103 |
Let the number of different key values found in each leaf page i be Pi (i=1..A). |
| 104 |
Let N_DIFF_AVG_LEAF be (P1 + P2 + ... + PA) / A. |
| 105 |
Let the number of different key values on level LA be N_DIFF_LA. |
| 106 |
Let the total number of records on level LA be TOTAL_LA. |
| 107 |
Let R be N_DIFF_LA / TOTAL_LA, we assume this ratio is the same on the |
| 108 |
leaf level. |
| 109 |
Let the number of leaf pages be N. |
| 110 |
Then the total number of different key values on the leaf level is: |
| 111 |
N * R * N_DIFF_AVG_LEAF. |
| 112 |
See REF01 for the implementation. |
| 113 |
|
| 114 |
The above describes how to calculate the cardinality of an index. |
| 115 |
This algorithm is executed for each n-prefix of a multi-column index |
| 116 |
where n=1..n_uniq. |
| 117 |
@} */ |
| 118 |
|
| 119 |
/* names of the tables from the persistent statistics storage */ |
| 120 |
#define TABLE_STATS_NAME "mysql/innodb_table_stats" |
| 121 |
#define TABLE_STATS_NAME_PRINT "mysql.innodb_table_stats" |
| 122 |
#define INDEX_STATS_NAME "mysql/innodb_index_stats" |
| 123 |
#define INDEX_STATS_NAME_PRINT "mysql.innodb_index_stats" |
| 124 |
|
| 125 |
#ifdef UNIV_STATS_DEBUG |
| 126 |
#define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) |
| 127 |
#else /* UNIV_STATS_DEBUG */ |
| 128 |
#define DEBUG_PRINTF(fmt, ...) /* noop */ |
| 129 |
#endif /* UNIV_STATS_DEBUG */ |
| 130 |
|
| 131 |
/* Gets the number of leaf pages to sample in persistent stats estimation */ |
| 132 |
#define N_SAMPLE_PAGES(index) \ |
| 133 |
static_cast<uint64_t>((index)->table->stats_sample_pages != 0 \ |
| 134 |
? (index)->table->stats_sample_pages \ |
| 135 |
: srv_stats_persistent_sample_pages) |
| 136 |
|
| 137 |
/* number of distinct records on a given level that are required to stop |
| 138 |
descending to lower levels and fetch N_SAMPLE_PAGES(index) records |
| 139 |
from that level */ |
| 140 |
#define N_DIFF_REQUIRED(index) (N_SAMPLE_PAGES(index) * 10) |
| 141 |
|
| 142 |
/* A dynamic array where we store the boundaries of each distinct group |
| 143 |
of keys. For example if a btree level is: |
| 144 |
index: 0,1,2,3,4,5,6,7,8,9,10,11,12 |
| 145 |
data: b,b,b,b,b,b,g,g,j,j,j, x, y |
| 146 |
then we would store 5,7,10,11,12 in the array. */ |
| 147 |
typedef std::vector<uint64_t, ut::allocator<uint64_t>> boundaries_t; |
| 148 |
|
| 149 |
/** Allocator type used for index_map_t. */ |
| 150 |
typedef ut::allocator<std::pair<const char *const, dict_index_t *>> |
| 151 |
index_map_t_allocator; |
| 152 |
|
| 153 |
/** Auxiliary map used for sorting indexes by name in dict_stats_save(). */ |
| 154 |
typedef std::map<const char *, dict_index_t *, ut_strcmp_functor, |
| 155 |
index_map_t_allocator> |
| 156 |
index_map_t; |
| 157 |
|
| 158 |
/** Checks whether an index should be ignored in stats manipulations: |
| 159 |
- stats fetch |
| 160 |
- stats recalc |
| 161 |
- stats save |
| 162 |
@return true if exists and all tables are ok */ |
| 163 |
static inline bool dict_stats_should_ignore_index( |
| 164 |
const dict_index_t *index) /*!< in: index */ |
| 165 |
{ |
| 166 |
return ((index->type & DICT_FTS) || index->is_corrupted() || |
| 167 |
dict_index_is_spatial(index) || index->to_be_dropped || |
| 168 |
!index->is_committed()); |
| 169 |
} |
| 170 |
|
| 171 |
/** Executes a given SQL statement using the InnoDB internal SQL parser. |
| 172 |
This function will free the pinfo object. |
| 173 |
@param[in,out] pinfo pinfo to pass to que_eval_sql() must already |
| 174 |
have any literals bound to it |
| 175 |
@param[in] sql SQL string to execute |
| 176 |
@param[in,out] trx in case of NULL the function will allocate and |
| 177 |
free the trx object. If it is not NULL then it will be rolled back |
| 178 |
only in the case of error, but not freed. |
| 179 |
@return DB_SUCCESS or error code */ |
| 180 |
static dberr_t dict_stats_exec_sql(pars_info_t *pinfo, const char *sql, |
| 181 |
trx_t *trx) { |
| 182 |
dberr_t err; |
| 183 |
bool trx_started = false; |
| 184 |
|
| 185 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 186 |
ut_ad(!dict_sys_mutex_own()); |
| 187 |
|
| 188 |
if (trx == nullptr) { |
| 189 |
trx = trx_allocate_for_background(); |
| 190 |
trx_started = true; |
| 191 |
|
| 192 |
if (srv_read_only_mode) { |
| 193 |
trx_start_internal_read_only(trx, UT_LOCATION_HERE); |
| 194 |
} else { |
| 195 |
trx_start_internal(trx, UT_LOCATION_HERE); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
err = que_eval_sql(pinfo, sql, trx); /* pinfo is freed here */ |
| 200 |
|
| 201 |
DBUG_EXECUTE_IF( |
| 202 |
"stats_index_error", if (!trx_started) { |
| 203 |
err = DB_STATS_DO_NOT_EXIST; |
| 204 |
trx->error_state = DB_STATS_DO_NOT_EXIST; |
| 205 |
}); |
| 206 |
|
| 207 |
if (!trx_started && err == DB_SUCCESS) { |
| 208 |
return (DB_SUCCESS); |
| 209 |
} |
| 210 |
|
| 211 |
if (err == DB_SUCCESS) { |
| 212 |
trx_commit_for_mysql(trx); |
| 213 |
} else { |
| 214 |
trx->op_info = "rollback of internal trx on stats tables"; |
| 215 |
trx->dict_operation_lock_mode = RW_X_LATCH; |
| 216 |
trx_rollback_to_savepoint(trx, nullptr); |
| 217 |
trx->dict_operation_lock_mode = 0; |
| 218 |
trx->op_info = ""; |
| 219 |
ut_a(trx->error_state == DB_SUCCESS); |
| 220 |
} |
| 221 |
|
| 222 |
if (trx_started) { |
| 223 |
trx_free_for_background(trx); |
| 224 |
} |
| 225 |
|
| 226 |
return (err); |
| 227 |
} |
| 228 |
|
| 229 |
/** Duplicate a table object and its indexes. |
| 230 |
This function creates a dummy dict_table_t object and initializes the |
| 231 |
following table and index members: |
| 232 |
dict_table_t::id (copied) |
| 233 |
dict_table_t::heap (newly created) |
| 234 |
dict_table_t::name (copied) |
| 235 |
dict_table_t::corrupted (copied) |
| 236 |
dict_table_t::indexes<> (newly created) |
| 237 |
dict_table_t::magic_n |
| 238 |
for each entry in dict_table_t::indexes, the following are initialized: |
| 239 |
(indexes that have DICT_FTS set in index->type are skipped) |
| 240 |
dict_index_t::id (copied) |
| 241 |
dict_index_t::name (copied) |
| 242 |
dict_index_t::table_name (points to the copied table name) |
| 243 |
dict_index_t::table (points to the above semi-initialized object) |
| 244 |
dict_index_t::type (copied) |
| 245 |
dict_index_t::to_be_dropped (copied) |
| 246 |
dict_index_t::online_status (copied) |
| 247 |
dict_index_t::n_uniq (copied) |
| 248 |
dict_index_t::fields[] (newly created, only first n_uniq, only fields[i].name) |
| 249 |
dict_index_t::indexes<> (newly created) |
| 250 |
dict_index_t::stat_n_diff_key_vals[] (only allocated, left uninitialized) |
| 251 |
dict_index_t::stat_n_sample_sizes[] (only allocated, left uninitialized) |
| 252 |
dict_index_t::stat_n_non_null_key_vals[] (only allocated, left uninitialized) |
| 253 |
dict_index_t::magic_n |
| 254 |
The returned object should be freed with dict_stats_table_clone_free() |
| 255 |
when no longer needed. |
| 256 |
@return incomplete table object */ |
| 257 |
static dict_table_t *dict_stats_table_clone_create( |
| 258 |
const dict_table_t *table) /*!< in: table whose stats to copy */ |
| 259 |
{ |
| 260 |
size_t heap_size; |
| 261 |
const dict_index_t *index; |
| 262 |
|
| 263 |
/* Estimate the size needed for the table and all of its indexes */ |
| 264 |
|
| 265 |
heap_size = 0; |
| 266 |
heap_size += sizeof(dict_table_t); |
| 267 |
heap_size += strlen(table->name.m_name) + 1; |
| 268 |
|
| 269 |
for (index = table->first_index(); index != nullptr; index = index->next()) { |
| 270 |
if (dict_stats_should_ignore_index(index)) { |
| 271 |
continue; |
| 272 |
} |
| 273 |
|
| 274 |
ut_ad(!dict_index_is_ibuf(index)); |
| 275 |
|
| 276 |
ulint n_uniq = dict_index_get_n_unique(index); |
| 277 |
|
| 278 |
heap_size += sizeof(dict_index_t); |
| 279 |
heap_size += strlen(index->name) + 1; |
| 280 |
heap_size += n_uniq * sizeof(index->fields[0]); |
| 281 |
for (ulint i = 0; i < n_uniq; i++) { |
| 282 |
heap_size += strlen(index->fields[i].name) + 1; |
| 283 |
} |
| 284 |
heap_size += n_uniq * sizeof(index->stat_n_diff_key_vals[0]); |
| 285 |
heap_size += n_uniq * sizeof(index->stat_n_sample_sizes[0]); |
| 286 |
heap_size += n_uniq * sizeof(index->stat_n_non_null_key_vals[0]); |
| 287 |
} |
| 288 |
|
| 289 |
/* Allocate the memory and copy the members */ |
| 290 |
|
| 291 |
mem_heap_t *heap; |
| 292 |
|
| 293 |
heap = mem_heap_create(heap_size, UT_LOCATION_HERE); |
| 294 |
|
| 295 |
dict_table_t *t; |
| 296 |
|
| 297 |
t = (dict_table_t *)mem_heap_alloc(heap, sizeof(*t)); |
| 298 |
|
| 299 |
UNIV_MEM_ASSERT_RW_ABORT(&table->id, sizeof(table->id)); |
| 300 |
t->id = table->id; |
| 301 |
|
| 302 |
t->heap = heap; |
| 303 |
|
| 304 |
t->name.m_name = mem_heap_strdup(heap, table->name.m_name); |
| 305 |
|
| 306 |
/* This private object "t" is not shared with other threads, so |
| 307 |
we do not need the stats_latch (thus we pass false below). The |
| 308 |
dict_table_stats_lock()/unlock() routines will do nothing. */ |
| 309 |
dict_table_stats_latch_create(t, false); |
| 310 |
|
| 311 |
UT_LIST_INIT(t->indexes); |
| 312 |
|
| 313 |
for (index = table->first_index(); index != nullptr; index = index->next()) { |
| 314 |
if (dict_stats_should_ignore_index(index)) { |
| 315 |
continue; |
| 316 |
} |
| 317 |
|
| 318 |
ut_ad(!dict_index_is_ibuf(index)); |
| 319 |
|
| 320 |
dict_index_t *idx; |
| 321 |
|
| 322 |
idx = (dict_index_t *)mem_heap_alloc(heap, sizeof(*idx)); |
| 323 |
|
| 324 |
UNIV_MEM_ASSERT_RW_ABORT(&index->id, sizeof(index->id)); |
| 325 |
idx->id = index->id; |
| 326 |
idx->space = index->space; |
| 327 |
|
| 328 |
idx->name = mem_heap_strdup(heap, index->name); |
| 329 |
|
| 330 |
idx->table_name = t->name.m_name; |
| 331 |
|
| 332 |
idx->table = t; |
| 333 |
|
| 334 |
idx->type = index->type; |
| 335 |
|
| 336 |
idx->to_be_dropped = 0; |
| 337 |
|
| 338 |
idx->online_status = ONLINE_INDEX_COMPLETE; |
| 339 |
idx->set_committed(true); |
| 340 |
|
| 341 |
idx->n_uniq = index->n_uniq; |
| 342 |
|
| 343 |
idx->fields = (dict_field_t *)mem_heap_alloc( |
| 344 |
heap, idx->n_uniq * sizeof(idx->fields[0])); |
| 345 |
|
| 346 |
for (ulint i = 0; i < idx->n_uniq; i++) { |
| 347 |
idx->fields[i].name = mem_heap_strdup(heap, index->fields[i].name); |
| 348 |
} |
| 349 |
|
| 350 |
/* hook idx into t->indexes */ |
| 351 |
UT_LIST_ADD_LAST(t->indexes, idx); |
| 352 |
|
| 353 |
idx->stat_n_diff_key_vals = (uint64_t *)mem_heap_alloc( |
| 354 |
heap, idx->n_uniq * sizeof(idx->stat_n_diff_key_vals[0])); |
| 355 |
|
| 356 |
idx->stat_n_sample_sizes = (uint64_t *)mem_heap_alloc( |
| 357 |
heap, idx->n_uniq * sizeof(idx->stat_n_sample_sizes[0])); |
| 358 |
|
| 359 |
idx->stat_n_non_null_key_vals = (uint64_t *)mem_heap_alloc( |
| 360 |
heap, idx->n_uniq * sizeof(idx->stat_n_non_null_key_vals[0])); |
| 361 |
ut_d(idx->magic_n = DICT_INDEX_MAGIC_N); |
| 362 |
} |
| 363 |
|
| 364 |
ut_d(t->magic_n = DICT_TABLE_MAGIC_N); |
| 365 |
|
| 366 |
return (t); |
| 367 |
} |
| 368 |
|
| 369 |
/** Free the resources occupied by an object returned by |
| 370 |
dict_stats_table_clone_create(). */ |
| 371 |
static void dict_stats_table_clone_free( |
| 372 |
dict_table_t *t) /*!< in: dummy table object to free */ |
| 373 |
{ |
| 374 |
dict_table_stats_latch_destroy(t); |
| 375 |
mem_heap_free(t->heap); |
| 376 |
} |
| 377 |
|
| 378 |
/** Write all zeros (or 1 where it makes sense) into an index |
| 379 |
statistics members. The resulting stats correspond to an empty index. |
| 380 |
The caller must own index's table stats latch in X mode |
| 381 |
(dict_table_stats_lock(table, RW_X_LATCH)) */ |
| 382 |
static void dict_stats_empty_index(dict_index_t *index) /*!< in/out: index */ |
| 383 |
{ |
| 384 |
ut_ad(!(index->type & DICT_FTS)); |
| 385 |
ut_ad(!dict_index_is_ibuf(index)); |
| 386 |
|
| 387 |
ulint n_uniq = index->n_uniq; |
| 388 |
|
| 389 |
for (ulint i = 0; i < n_uniq; i++) { |
| 390 |
index->stat_n_diff_key_vals[i] = 0; |
| 391 |
index->stat_n_sample_sizes[i] = 1; |
| 392 |
index->stat_n_non_null_key_vals[i] = 0; |
| 393 |
} |
| 394 |
|
| 395 |
index->stat_index_size = 1; |
| 396 |
index->stat_n_leaf_pages = 1; |
| 397 |
} |
| 398 |
|
| 399 |
/** Write all zeros (or 1 where it makes sense) into a table and its indexes' |
| 400 |
statistics members. The resulting stats correspond to an empty table. */ |
| 401 |
static void dict_stats_empty_table(dict_table_t *table) /*!< in/out: table */ |
| 402 |
{ |
| 403 |
/* Zero the stats members */ |
| 404 |
|
| 405 |
dict_table_stats_lock(table, RW_X_LATCH); |
| 406 |
|
| 407 |
table->stat_n_rows = 0; |
| 408 |
table->stat_clustered_index_size = 1; |
| 409 |
/* 1 page for each index, not counting the clustered */ |
| 410 |
table->stat_sum_of_other_index_sizes = UT_LIST_GET_LEN(table->indexes) - 1; |
| 411 |
table->stat_modified_counter = 0; |
| 412 |
|
| 413 |
dict_index_t *index; |
| 414 |
|
| 415 |
for (index = table->first_index(); index != nullptr; index = index->next()) { |
| 416 |
if (index->type & DICT_FTS) { |
| 417 |
continue; |
| 418 |
} |
| 419 |
|
| 420 |
ut_ad(!dict_index_is_ibuf(index)); |
| 421 |
|
| 422 |
dict_stats_empty_index(index); |
| 423 |
} |
| 424 |
|
| 425 |
table->stat_initialized = true; |
| 426 |
|
| 427 |
dict_table_stats_unlock(table, RW_X_LATCH); |
| 428 |
} |
| 429 |
|
| 430 |
/** Check whether index's stats are initialized (assert if they are not). */ |
| 431 |
static void dict_stats_assert_initialized_index( |
| 432 |
const dict_index_t *index [[maybe_unused]]) /*!< in: index */ |
| 433 |
{ |
| 434 |
UNIV_MEM_ASSERT_RW_ABORT( |
| 435 |
index->stat_n_diff_key_vals, |
| 436 |
index->n_uniq * sizeof(index->stat_n_diff_key_vals[0])); |
| 437 |
|
| 438 |
UNIV_MEM_ASSERT_RW_ABORT( |
| 439 |
index->stat_n_sample_sizes, |
| 440 |
index->n_uniq * sizeof(index->stat_n_sample_sizes[0])); |
| 441 |
|
| 442 |
UNIV_MEM_ASSERT_RW_ABORT( |
| 443 |
index->stat_n_non_null_key_vals, |
| 444 |
index->n_uniq * sizeof(index->stat_n_non_null_key_vals[0])); |
| 445 |
|
| 446 |
UNIV_MEM_ASSERT_RW_ABORT(&index->stat_index_size, |
| 447 |
sizeof(index->stat_index_size)); |
| 448 |
|
| 449 |
UNIV_MEM_ASSERT_RW_ABORT(&index->stat_n_leaf_pages, |
| 450 |
sizeof(index->stat_n_leaf_pages)); |
| 451 |
} |
| 452 |
|
| 453 |
/** Check whether table's stats are initialized (assert if they are not). */ |
| 454 |
static void dict_stats_assert_initialized( |
| 455 |
const dict_table_t *table) /*!< in: table */ |
| 456 |
{ |
| 457 |
ut_a(table->stat_initialized); |
| 458 |
|
| 459 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stats_last_recalc, |
| 460 |
sizeof(table->stats_last_recalc)); |
| 461 |
|
| 462 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stat_persistent, |
| 463 |
sizeof(table->stat_persistent)); |
| 464 |
|
| 465 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stats_auto_recalc, |
| 466 |
sizeof(table->stats_auto_recalc)); |
| 467 |
|
| 468 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stats_sample_pages, |
| 469 |
sizeof(table->stats_sample_pages)); |
| 470 |
|
| 471 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stat_n_rows, sizeof(table->stat_n_rows)); |
| 472 |
|
| 473 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stat_clustered_index_size, |
| 474 |
sizeof(table->stat_clustered_index_size)); |
| 475 |
|
| 476 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stat_sum_of_other_index_sizes, |
| 477 |
sizeof(table->stat_sum_of_other_index_sizes)); |
| 478 |
|
| 479 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stat_modified_counter, |
| 480 |
sizeof(table->stat_modified_counter)); |
| 481 |
|
| 482 |
UNIV_MEM_ASSERT_RW_ABORT(&table->stats_bg_flag, sizeof(table->stats_bg_flag)); |
| 483 |
|
| 484 |
for (const dict_index_t *index = table->first_index(); index != nullptr; |
| 485 |
index = index->next()) { |
| 486 |
if (!dict_stats_should_ignore_index(index)) { |
| 487 |
dict_stats_assert_initialized_index(index); |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
#define INDEX_EQ(i1, i2) \ |
| 493 |
((i1) != NULL && (i2) != NULL && (i1)->space == (i2)->space && \ |
| 494 |
(i1)->id == (i2)->id && strcmp((i1)->name, (i2)->name) == 0) |
| 495 |
|
| 496 |
/** Copy table and index statistics from one table to another, including index |
| 497 |
stats. Extra indexes in src are ignored and extra indexes in dst are |
| 498 |
initialized to correspond to an empty index. */ |
| 499 |
static void dict_stats_copy(dict_table_t *dst, /*!< in/out: destination table */ |
| 500 |
const dict_table_t *src) /*!< in: source table */ |
| 501 |
{ |
| 502 |
dst->stats_last_recalc = src->stats_last_recalc; |
| 503 |
dst->stat_n_rows = src->stat_n_rows; |
| 504 |
dst->stat_clustered_index_size = src->stat_clustered_index_size; |
| 505 |
dst->stat_sum_of_other_index_sizes = src->stat_sum_of_other_index_sizes; |
| 506 |
dst->stat_modified_counter = src->stat_modified_counter; |
| 507 |
|
| 508 |
dict_index_t *dst_idx; |
| 509 |
const dict_index_t *src_idx; |
| 510 |
|
| 511 |
for (dst_idx = dst->first_index(), src_idx = src->first_index(); |
| 512 |
dst_idx != nullptr; dst_idx = dst_idx->next()) { |
| 513 |
if (src_idx != nullptr) { |
| 514 |
src_idx = src_idx->next(); |
| 515 |
} |
| 516 |
if (dict_stats_should_ignore_index(dst_idx)) { |
| 517 |
if (!(dst_idx->type & DICT_FTS)) { |
| 518 |
dict_stats_empty_index(dst_idx); |
| 519 |
} |
| 520 |
continue; |
| 521 |
} |
| 522 |
|
| 523 |
ut_ad(!dict_index_is_ibuf(dst_idx)); |
| 524 |
|
| 525 |
if (!INDEX_EQ(src_idx, dst_idx)) { |
| 526 |
for (src_idx = src->first_index(); src_idx != nullptr; |
| 527 |
src_idx = src_idx->next()) { |
| 528 |
if (INDEX_EQ(src_idx, dst_idx)) { |
| 529 |
break; |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
if (!INDEX_EQ(src_idx, dst_idx)) { |
| 535 |
dict_stats_empty_index(dst_idx); |
| 536 |
continue; |
| 537 |
} |
| 538 |
|
| 539 |
ulint n_copy_el; |
| 540 |
|
| 541 |
if (dst_idx->n_uniq > src_idx->n_uniq) { |
| 542 |
n_copy_el = src_idx->n_uniq; |
| 543 |
/* Since src is smaller some elements in dst |
| 544 |
will remain untouched by the following memmove(), |
| 545 |
thus we init all of them here. */ |
| 546 |
dict_stats_empty_index(dst_idx); |
| 547 |
} else { |
| 548 |
n_copy_el = dst_idx->n_uniq; |
| 549 |
} |
| 550 |
|
| 551 |
memmove(dst_idx->stat_n_diff_key_vals, src_idx->stat_n_diff_key_vals, |
| 552 |
n_copy_el * sizeof(dst_idx->stat_n_diff_key_vals[0])); |
| 553 |
|
| 554 |
memmove(dst_idx->stat_n_sample_sizes, src_idx->stat_n_sample_sizes, |
| 555 |
n_copy_el * sizeof(dst_idx->stat_n_sample_sizes[0])); |
| 556 |
|
| 557 |
memmove(dst_idx->stat_n_non_null_key_vals, |
| 558 |
src_idx->stat_n_non_null_key_vals, |
| 559 |
n_copy_el * sizeof(dst_idx->stat_n_non_null_key_vals[0])); |
| 560 |
|
| 561 |
dst_idx->stat_index_size = src_idx->stat_index_size; |
| 562 |
|
| 563 |
dst_idx->stat_n_leaf_pages = src_idx->stat_n_leaf_pages; |
| 564 |
} |
| 565 |
|
| 566 |
dst->stat_initialized = true; |
| 567 |
} |
| 568 |
|
| 569 |
/** Duplicate the stats of a table and its indexes. |
| 570 |
This function creates a dummy dict_table_t object and copies the input |
| 571 |
table's stats into it. The returned table object is not in the dictionary |
| 572 |
cache and cannot be accessed by any other threads. In addition to the |
| 573 |
members copied in dict_stats_table_clone_create() this function initializes |
| 574 |
the following: |
| 575 |
dict_table_t::stat_initialized |
| 576 |
dict_table_t::stat_persistent |
| 577 |
dict_table_t::stat_n_rows |
| 578 |
dict_table_t::stat_clustered_index_size |
| 579 |
dict_table_t::stat_sum_of_other_index_sizes |
| 580 |
dict_table_t::stat_modified_counter |
| 581 |
dict_index_t::stat_n_diff_key_vals[] |
| 582 |
dict_index_t::stat_n_sample_sizes[] |
| 583 |
dict_index_t::stat_n_non_null_key_vals[] |
| 584 |
dict_index_t::stat_index_size |
| 585 |
dict_index_t::stat_n_leaf_pages |
| 586 |
The returned object should be freed with dict_stats_snapshot_free() |
| 587 |
when no longer needed. |
| 588 |
@param[in] table table whose stats to copy |
| 589 |
@return incomplete table object */ |
| 590 |
static dict_table_t *dict_stats_snapshot_create(dict_table_t *table) { |
| 591 |
dict_sys_mutex_enter(); |
| 592 |
|
| 593 |
dict_table_stats_lock(table, RW_S_LATCH); |
| 594 |
|
| 595 |
dict_stats_assert_initialized(table); |
| 596 |
|
| 597 |
dict_table_t *t; |
| 598 |
|
| 599 |
t = dict_stats_table_clone_create(table); |
| 600 |
|
| 601 |
dict_stats_copy(t, table); |
| 602 |
|
| 603 |
t->stat_persistent = table->stat_persistent; |
| 604 |
t->stats_auto_recalc = table->stats_auto_recalc; |
| 605 |
t->stats_sample_pages = table->stats_sample_pages; |
| 606 |
t->stats_bg_flag = table->stats_bg_flag; |
| 607 |
|
| 608 |
dict_table_stats_unlock(table, RW_S_LATCH); |
| 609 |
|
| 610 |
dict_sys_mutex_exit(); |
| 611 |
|
| 612 |
return (t); |
| 613 |
} |
| 614 |
|
| 615 |
/** Free the resources occupied by an object returned by |
| 616 |
dict_stats_snapshot_create(). */ |
| 617 |
static void dict_stats_snapshot_free( |
| 618 |
dict_table_t *t) /*!< in: dummy table object to free */ |
| 619 |
{ |
| 620 |
dict_stats_table_clone_free(t); |
| 621 |
} |
| 622 |
|
| 623 |
/** Calculates new estimates for index statistics. This function is |
| 624 |
relatively quick and is used to calculate transient statistics that |
| 625 |
are not saved on disk. This was the only way to calculate statistics |
| 626 |
before the Persistent Statistics feature was introduced. */ |
| 627 |
static void dict_stats_update_transient_for_index( |
| 628 |
dict_index_t *index) /*!< in/out: index */ |
| 629 |
{ |
| 630 |
if (srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO && |
| 631 |
(srv_force_recovery >= SRV_FORCE_NO_LOG_REDO || !index->is_clustered())) { |
| 632 |
/* If we have set a high innodb_force_recovery |
| 633 |
level, do not calculate statistics, as a badly |
| 634 |
corrupted index can cause a crash in it. |
| 635 |
Initialize some bogus index cardinality |
| 636 |
statistics, so that the data can be queried in |
| 637 |
various means, also via secondary indexes. */ |
| 638 |
dict_stats_empty_index(index); |
| 639 |
#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG |
| 640 |
} else if (ibuf_debug && !index->is_clustered()) { |
| 641 |
dict_stats_empty_index(index); |
| 642 |
#endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */ |
| 643 |
} else { |
| 644 |
mtr_t mtr; |
| 645 |
ulint size; |
| 646 |
|
| 647 |
mtr_start(&mtr); |
| 648 |
dict_disable_redo_if_temporary(index->table, &mtr); |
| 649 |
|
| 650 |
mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 651 |
|
| 652 |
size = btr_get_size(index, BTR_TOTAL_SIZE, &mtr); |
| 653 |
|
| 654 |
if (size != ULINT_UNDEFINED) { |
| 655 |
index->stat_index_size = size; |
| 656 |
|
| 657 |
size = btr_get_size(index, BTR_N_LEAF_PAGES, &mtr); |
| 658 |
} |
| 659 |
|
| 660 |
mtr_commit(&mtr); |
| 661 |
|
| 662 |
switch (size) { |
| 663 |
case ULINT_UNDEFINED: |
| 664 |
dict_stats_empty_index(index); |
| 665 |
return; |
| 666 |
case 0: |
| 667 |
/* The root node of the tree is a leaf */ |
| 668 |
size = 1; |
| 669 |
} |
| 670 |
|
| 671 |
index->stat_n_leaf_pages = size; |
| 672 |
|
| 673 |
/* We don't handle the return value since it will be false |
| 674 |
only when some thread is dropping the table and we don't |
| 675 |
have to empty the statistics of the to be dropped index */ |
| 676 |
btr_estimate_number_of_different_key_vals(index); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
/** Calculates new estimates for table and index statistics. This function |
| 681 |
is relatively quick and is used to calculate transient statistics that |
| 682 |
are not saved on disk. |
| 683 |
This was the only way to calculate statistics before the |
| 684 |
Persistent Statistics feature was introduced. */ |
| 685 |
static void dict_stats_update_transient( |
| 686 |
dict_table_t *table) /*!< in/out: table */ |
| 687 |
{ |
| 688 |
dict_index_t *index; |
| 689 |
ulint sum_of_index_sizes = 0; |
| 690 |
|
| 691 |
/* Find out the sizes of the indexes and how many different values |
| 692 |
for the key they approximately have */ |
| 693 |
|
| 694 |
index = table->first_index(); |
| 695 |
|
| 696 |
if (dict_table_is_discarded(table)) { |
| 697 |
/* Nothing to do. */ |
| 698 |
dict_stats_empty_table(table); |
| 699 |
return; |
| 700 |
} else if (index == nullptr) { |
| 701 |
/* Table definition is corrupt */ |
| 702 |
|
| 703 |
ib::warn(ER_IB_MSG_221) << "Table " << table->name |
| 704 |
<< " has no indexes. Cannot calculate statistics."; |
| 705 |
dict_stats_empty_table(table); |
| 706 |
return; |
| 707 |
} |
| 708 |
|
| 709 |
for (; index != nullptr; index = index->next()) { |
| 710 |
ut_ad(!dict_index_is_ibuf(index)); |
| 711 |
|
| 712 |
if (index->type & DICT_FTS || dict_index_is_spatial(index)) { |
| 713 |
continue; |
| 714 |
} |
| 715 |
|
| 716 |
dict_stats_empty_index(index); |
| 717 |
|
| 718 |
if (dict_stats_should_ignore_index(index)) { |
| 719 |
continue; |
| 720 |
} |
| 721 |
|
| 722 |
dict_stats_update_transient_for_index(index); |
| 723 |
|
| 724 |
sum_of_index_sizes += index->stat_index_size; |
| 725 |
} |
| 726 |
|
| 727 |
index = table->first_index(); |
| 728 |
|
| 729 |
table->stat_n_rows = |
| 730 |
index->stat_n_diff_key_vals[dict_index_get_n_unique(index) - 1]; |
| 731 |
|
| 732 |
table->stat_clustered_index_size = index->stat_index_size; |
| 733 |
|
| 734 |
table->stat_sum_of_other_index_sizes = |
| 735 |
sum_of_index_sizes - index->stat_index_size; |
| 736 |
|
| 737 |
table->stats_last_recalc = std::chrono::steady_clock::now(); |
| 738 |
|
| 739 |
table->stat_modified_counter = 0; |
| 740 |
|
| 741 |
table->stat_initialized = true; |
| 742 |
} |
| 743 |
|
| 744 |
/** Confirms long waiters for the index lock exist. |
| 745 |
Wait time estimation is based on the last call of this function when not exist. |
| 746 |
@param[in] index index |
| 747 |
@param[in,out] wait_start_time last known time index lock wasn't awaited. |
| 748 |
Can be updated by this function if no waiter found now. |
| 749 |
@return true if long waiters exist */ |
| 750 |
static bool dict_stats_index_long_waiters( |
| 751 |
dict_index_t *index, |
| 752 |
std::chrono::steady_clock::time_point &wait_start_time) { |
| 753 |
if (rw_lock_get_waiters(dict_index_get_lock(index))) { |
| 754 |
const auto diff = std::chrono::steady_clock::now() - wait_start_time; |
| 755 |
|
| 756 |
return diff > get_srv_fatal_semaphore_wait_threshold() / 2; |
| 757 |
} else { |
| 758 |
/* estimated long wait not started yet. updates wait_start_time. */ |
| 759 |
wait_start_time = std::chrono::steady_clock::now(); |
| 760 |
|
| 761 |
return false; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
/** @{ Pseudo code about the relation between the following functions |
| 766 |
|
| 767 |
dict_stats_analyze_index() |
| 768 |
let N = N_SAMPLE_PAGES(index) |
| 769 |
try dict_stats_analyze_index_low(N) |
| 770 |
if timed out, try again with smaller N |
| 771 |
|
| 772 |
dict_stats_analyze_index_low(N) |
| 773 |
for each n_prefix |
| 774 |
search for good enough level: |
| 775 |
dict_stats_analyze_index_level() // only called if level has <= N pages |
| 776 |
// full scan of the level in one mtr |
| 777 |
collect statistics about the given level |
| 778 |
if we are not satisfied with the level, search next lower level |
| 779 |
we have found a good enough level here |
| 780 |
dict_stats_analyze_index_for_n_prefix(that level, stats collected above) |
| 781 |
// full scan of the level in one mtr |
| 782 |
dive below some records and analyze the leaf page there: |
| 783 |
dict_stats_analyze_index_below_cur() |
| 784 |
@} **/ |
| 785 |
|
| 786 |
/** Find the total number and the number of distinct keys on a given level in |
| 787 |
an index. Each of the 1..n_uniq prefixes are looked up and the results are |
| 788 |
saved in the array n_diff[0] .. n_diff[n_uniq - 1]. The total number of |
| 789 |
records on the level is saved in total_recs. |
| 790 |
Also, the index of the last record in each group of equal records is saved |
| 791 |
in n_diff_boundaries[0..n_uniq - 1], records indexing starts from the leftmost |
| 792 |
record on the level and continues cross pages boundaries, counting from 0. |
| 793 |
@return false if aborted */ |
| 794 |
static bool dict_stats_analyze_index_level( |
| 795 |
dict_index_t *index, /*!< in: index */ |
| 796 |
ulint level, /*!< in: level */ |
| 797 |
uint64_t *n_diff, /*!< out: array for number of |
| 798 |
distinct keys for all prefixes */ |
| 799 |
uint64_t *total_recs, /*!< out: total number of records */ |
| 800 |
uint64_t *total_pages, /*!< out: total number of pages */ |
| 801 |
boundaries_t *n_diff_boundaries, /*!< out: boundaries of the groups |
| 802 |
of distinct keys */ |
| 803 |
std::chrono::steady_clock::time_point &wait_start_time, |
| 804 |
/*!< in/out: last known time index lock wasn't awaited. */ |
| 805 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 806 |
{ |
| 807 |
ulint n_uniq; |
| 808 |
mem_heap_t *heap; |
| 809 |
btr_pcur_t pcur; |
| 810 |
const page_t *page; |
| 811 |
const rec_t *rec; |
| 812 |
const rec_t *prev_rec; |
| 813 |
bool prev_rec_is_copied; |
| 814 |
byte *prev_rec_buf = nullptr; |
| 815 |
size_t prev_rec_buf_size = 0; |
| 816 |
ulint *rec_offsets; |
| 817 |
ulint *prev_rec_offsets; |
| 818 |
ulint i; |
| 819 |
bool success = true; |
| 820 |
|
| 821 |
DEBUG_PRINTF(" %s(table=%s, index=%s, level=%lu)\n", __func__, |
| 822 |
index->table->name, index->name, level); |
| 823 |
|
| 824 |
ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_SX_LOCK)); |
| 825 |
|
| 826 |
n_uniq = dict_index_get_n_unique(index); |
| 827 |
|
| 828 |
/* elements in the n_diff array are 0..n_uniq-1 (inclusive) */ |
| 829 |
memset(n_diff, 0x0, n_uniq * sizeof(n_diff[0])); |
| 830 |
|
| 831 |
/* Allocate space for the offsets header (the allocation size at |
| 832 |
offsets[0] and the REC_OFFS_HEADER_SIZE bytes), and n_fields + 1, |
| 833 |
so that this will never be less than the size calculated in |
| 834 |
rec_get_offsets(). */ |
| 835 |
i = (REC_OFFS_HEADER_SIZE + 1 + 1) + index->n_fields; |
| 836 |
|
| 837 |
heap = mem_heap_create((2 * sizeof *rec_offsets) * i, UT_LOCATION_HERE); |
| 838 |
rec_offsets = |
| 839 |
static_cast<ulint *>(mem_heap_alloc(heap, i * sizeof *rec_offsets)); |
| 840 |
prev_rec_offsets = |
| 841 |
static_cast<ulint *>(mem_heap_alloc(heap, i * sizeof *prev_rec_offsets)); |
| 842 |
rec_offs_set_n_alloc(rec_offsets, i); |
| 843 |
rec_offs_set_n_alloc(prev_rec_offsets, i); |
| 844 |
|
| 845 |
/* reset the dynamic arrays n_diff_boundaries[0..n_uniq-1] */ |
| 846 |
if (n_diff_boundaries != nullptr) { |
| 847 |
for (i = 0; i < n_uniq; i++) { |
| 848 |
n_diff_boundaries[i].erase(n_diff_boundaries[i].begin(), |
| 849 |
n_diff_boundaries[i].end()); |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
/* Position pcur on the leftmost record on the leftmost page |
| 854 |
on the desired level. */ |
| 855 |
|
| 856 |
pcur.open_at_side(true, index, BTR_SEARCH_TREE | BTR_ALREADY_S_LATCHED, true, |
| 857 |
level, mtr); |
| 858 |
pcur.move_to_next_on_page(); |
| 859 |
|
| 860 |
page = pcur.get_page(); |
| 861 |
|
| 862 |
/* The page must not be empty, except when |
| 863 |
it is the root page (and the whole index is empty). */ |
| 864 |
ut_ad(pcur.is_on_user_rec() || page_is_leaf(page)); |
| 865 |
ut_ad(pcur.get_rec() == page_rec_get_next_const(page_get_infimum_rec(page))); |
| 866 |
|
| 867 |
/* check that we are indeed on the desired level */ |
| 868 |
ut_a(btr_page_get_level(page) == level); |
| 869 |
|
| 870 |
/* there should not be any pages on the left */ |
| 871 |
ut_a(btr_page_get_prev(page, mtr) == FIL_NULL); |
| 872 |
|
| 873 |
/* check whether the first record on the leftmost page is marked |
| 874 |
as such, if we are on a non-leaf level */ |
| 875 |
ut_a((level == 0) == |
| 876 |
!(REC_INFO_MIN_REC_FLAG & |
| 877 |
rec_get_info_bits(pcur.get_rec(), page_is_comp(page)))); |
| 878 |
|
| 879 |
prev_rec = nullptr; |
| 880 |
prev_rec_is_copied = false; |
| 881 |
|
| 882 |
/* We can release index->lock not to block others, when level==0. |
| 883 |
But the tree structure might be changed. */ |
| 884 |
if (level == 0) { |
| 885 |
mtr_memo_release(mtr, dict_index_get_lock(index), MTR_MEMO_SX_LOCK); |
| 886 |
} |
| 887 |
|
| 888 |
/* no records by default */ |
| 889 |
*total_recs = 0; |
| 890 |
|
| 891 |
*total_pages = 0; |
| 892 |
|
| 893 |
/* iterate over all user records on this level |
| 894 |
and compare each two adjacent ones, even the last on page |
| 895 |
X and the fist on page X+1 */ |
| 896 |
for (; pcur.is_on_user_rec(); pcur.move_to_next_user_rec(mtr)) { |
| 897 |
bool rec_is_last_on_page; |
| 898 |
|
| 899 |
rec = pcur.get_rec(); |
| 900 |
|
| 901 |
/* If rec and prev_rec are on different pages, then prev_rec |
| 902 |
must have been copied, because we hold latch only on the page |
| 903 |
where rec resides. */ |
| 904 |
if (prev_rec != nullptr && page_align(rec) != page_align(prev_rec)) { |
| 905 |
ut_a(prev_rec_is_copied); |
| 906 |
} |
| 907 |
|
| 908 |
rec_is_last_on_page = page_rec_is_supremum(page_rec_get_next_const(rec)); |
| 909 |
|
| 910 |
/* increment the pages counter at the end of each page */ |
| 911 |
if (rec_is_last_on_page) { |
| 912 |
(*total_pages)++; |
| 913 |
|
| 914 |
if (level != 0 && (*total_pages) % 100 == 0 && |
| 915 |
dict_stats_index_long_waiters(index, wait_start_time)) { |
| 916 |
/* long waiters exist. abort. */ |
| 917 |
success = false; |
| 918 |
goto end; |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
/* Skip delete-marked records on the leaf level. If we |
| 923 |
do not skip them, then ANALYZE quickly after DELETE |
| 924 |
could count them or not (purge may have already wiped |
| 925 |
them away) which brings non-determinism. We skip only |
| 926 |
leaf-level delete marks because delete marks on |
| 927 |
non-leaf level do not make sense. */ |
| 928 |
|
| 929 |
if (level == 0 && |
| 930 |
(srv_stats_include_delete_marked |
| 931 |
? 0 |
| 932 |
: rec_get_deleted_flag(rec, page_is_comp(pcur.get_page())))) { |
| 933 |
if (rec_is_last_on_page && !prev_rec_is_copied && prev_rec != nullptr) { |
| 934 |
/* copy prev_rec */ |
| 935 |
|
| 936 |
prev_rec_offsets = rec_get_offsets(prev_rec, index, prev_rec_offsets, |
| 937 |
n_uniq, UT_LOCATION_HERE, &heap); |
| 938 |
|
| 939 |
prev_rec = rec_copy_prefix_to_buf(prev_rec, index, |
| 940 |
rec_offs_n_fields(prev_rec_offsets), |
| 941 |
&prev_rec_buf, &prev_rec_buf_size); |
| 942 |
|
| 943 |
prev_rec_is_copied = true; |
| 944 |
} |
| 945 |
|
| 946 |
continue; |
| 947 |
} |
| 948 |
rec_offsets = rec_get_offsets(rec, index, rec_offsets, n_uniq, |
| 949 |
UT_LOCATION_HERE, &heap); |
| 950 |
|
| 951 |
(*total_recs)++; |
| 952 |
|
| 953 |
if (prev_rec != nullptr) { |
| 954 |
ulint matched_fields; |
| 955 |
|
| 956 |
prev_rec_offsets = rec_get_offsets(prev_rec, index, prev_rec_offsets, |
| 957 |
n_uniq, UT_LOCATION_HERE, &heap); |
| 958 |
|
| 959 |
cmp_rec_rec_with_match(rec, prev_rec, rec_offsets, prev_rec_offsets, |
| 960 |
index, false, false, &matched_fields); |
| 961 |
|
| 962 |
for (i = matched_fields; i < n_uniq; i++) { |
| 963 |
if (n_diff_boundaries != nullptr) { |
| 964 |
/* push the index of the previous |
| 965 |
record, that is - the last one from |
| 966 |
a group of equal keys */ |
| 967 |
|
| 968 |
uint64_t idx; |
| 969 |
|
| 970 |
/* the index of the current record |
| 971 |
is total_recs - 1, the index of the |
| 972 |
previous record is total_recs - 2; |
| 973 |
we know that idx is not going to |
| 974 |
become negative here because if we |
| 975 |
are in this branch then there is a |
| 976 |
previous record and thus |
| 977 |
total_recs >= 2 */ |
| 978 |
idx = *total_recs - 2; |
| 979 |
|
| 980 |
n_diff_boundaries[i].push_back(idx); |
| 981 |
} |
| 982 |
|
| 983 |
/* increment the number of different keys |
| 984 |
for n_prefix=i+1 (e.g. if i=0 then we increment |
| 985 |
for n_prefix=1 which is stored in n_diff[0]) */ |
| 986 |
n_diff[i]++; |
| 987 |
} |
| 988 |
} else { |
| 989 |
/* this is the first non-delete marked record */ |
| 990 |
for (i = 0; i < n_uniq; i++) { |
| 991 |
n_diff[i] = 1; |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
if (rec_is_last_on_page) { |
| 996 |
/* end of a page has been reached */ |
| 997 |
|
| 998 |
/* we need to copy the record instead of assigning |
| 999 |
like prev_rec = rec; because when we traverse the |
| 1000 |
records on this level at some point we will jump from |
| 1001 |
one page to the next and then rec and prev_rec will |
| 1002 |
be on different pages and |
| 1003 |
btr_pcur_t::move_to_next_user_rec() will release the |
| 1004 |
latch on the page that prev_rec is on */ |
| 1005 |
prev_rec = |
| 1006 |
rec_copy_prefix_to_buf(rec, index, rec_offs_n_fields(rec_offsets), |
| 1007 |
&prev_rec_buf, &prev_rec_buf_size); |
| 1008 |
prev_rec_is_copied = true; |
| 1009 |
|
| 1010 |
} else { |
| 1011 |
/* still on the same page, the next call to |
| 1012 |
btr_pcur_t::move_to_next_user_rec() will not jump |
| 1013 |
on the next page, we can simply assign pointers |
| 1014 |
instead of copying the records like above */ |
| 1015 |
|
| 1016 |
prev_rec = rec; |
| 1017 |
prev_rec_is_copied = false; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
/* if *total_pages is left untouched then the above loop was not |
| 1022 |
entered at all and there is one page in the whole tree which is |
| 1023 |
empty or the loop was entered but this is level 0, contains one page |
| 1024 |
and all records are delete-marked */ |
| 1025 |
if (*total_pages == 0) { |
| 1026 |
ut_ad(level == 0); |
| 1027 |
ut_ad(*total_recs == 0); |
| 1028 |
|
| 1029 |
*total_pages = 1; |
| 1030 |
} |
| 1031 |
|
| 1032 |
/* if there are records on this level and boundaries |
| 1033 |
should be saved */ |
| 1034 |
if (*total_recs > 0 && n_diff_boundaries != nullptr) { |
| 1035 |
/* remember the index of the last record on the level as the |
| 1036 |
last one from the last group of equal keys; this holds for |
| 1037 |
all possible prefixes */ |
| 1038 |
for (i = 0; i < n_uniq; i++) { |
| 1039 |
uint64_t idx; |
| 1040 |
|
| 1041 |
idx = *total_recs - 1; |
| 1042 |
|
| 1043 |
n_diff_boundaries[i].push_back(idx); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
/* now in n_diff_boundaries[i] there are exactly n_diff[i] integers, |
| 1048 |
for i=0..n_uniq-1 */ |
| 1049 |
|
| 1050 |
#ifdef UNIV_STATS_DEBUG |
| 1051 |
for (i = 0; i < n_uniq; i++) { |
| 1052 |
DEBUG_PRINTF(" %s(): total recs: " UINT64PF ", total pages: " UINT64PF |
| 1053 |
", n_diff[%lu]: " UINT64PF "\n", |
| 1054 |
__func__, *total_recs, *total_pages, i, n_diff[i]); |
| 1055 |
|
| 1056 |
#if 0 |
| 1057 |
if (n_diff_boundaries != NULL) { |
| 1058 |
uint64_t j; |
| 1059 |
|
| 1060 |
DEBUG_PRINTF(" %s(): boundaries[%lu]: ", |
| 1061 |
__func__, i); |
| 1062 |
|
| 1063 |
for (j = 0; j < n_diff[i]; j++) { |
| 1064 |
uint64_t idx; |
| 1065 |
|
| 1066 |
idx = n_diff_boundaries[i][j]; |
| 1067 |
|
| 1068 |
DEBUG_PRINTF(UINT64PF "=" UINT64PF ", ", |
| 1069 |
j, idx); |
| 1070 |
} |
| 1071 |
DEBUG_PRINTF("\n"); |
| 1072 |
} |
| 1073 |
#endif |
| 1074 |
} |
| 1075 |
#endif /* UNIV_STATS_DEBUG */ |
| 1076 |
|
| 1077 |
end: |
| 1078 |
/* Release the latch on the last page, because that is not done by |
| 1079 |
btr_pcur::close(). This function works also for non-leaf pages. */ |
| 1080 |
btr_leaf_page_release(pcur.get_block(), BTR_SEARCH_LEAF, mtr); |
| 1081 |
|
| 1082 |
pcur.close(); |
| 1083 |
ut::free(prev_rec_buf); |
| 1084 |
mem_heap_free(heap); |
| 1085 |
|
| 1086 |
return success; |
| 1087 |
} |
| 1088 |
|
| 1089 |
/** aux enum for controlling the behavior of dict_stats_scan_page() @{ */ |
| 1090 |
enum page_scan_method_t { |
| 1091 |
COUNT_ALL_NON_BORING_AND_SKIP_DEL_MARKED, /* scan all records on |
| 1092 |
the given page and count the number of |
| 1093 |
distinct ones, also ignore delete marked |
| 1094 |
records */ |
| 1095 |
QUIT_ON_FIRST_NON_BORING, /* quit when the first record that differs |
| 1096 |
from its right neighbor is found */ |
| 1097 |
COUNT_ALL_NON_BORING_INCLUDE_DEL_MARKED /* scan all records on |
| 1098 |
the given page and count the number of |
| 1099 |
distinct ones, include delete marked |
| 1100 |
records */ |
| 1101 |
}; |
| 1102 |
/** @} */ |
| 1103 |
|
| 1104 |
/** Scan a page, reading records from left to right and counting the number |
| 1105 |
of distinct records (looking only at the first n_prefix |
| 1106 |
columns) and the number of external pages pointed by records from this page. |
| 1107 |
If scan_method is QUIT_ON_FIRST_NON_BORING then the function |
| 1108 |
will return as soon as it finds a record that does not match its neighbor |
| 1109 |
to the right, which means that in the case of QUIT_ON_FIRST_NON_BORING the |
| 1110 |
returned n_diff can either be 0 (empty page), 1 (the whole page has all keys |
| 1111 |
equal) or 2 (the function found a non-boring record and returned). |
| 1112 |
@param[out] out_rec record, or NULL |
| 1113 |
@param[out] offsets1 rec_get_offsets() working space (must |
| 1114 |
be big enough) |
| 1115 |
@param[out] offsets2 rec_get_offsets() working space (must |
| 1116 |
be big enough) |
| 1117 |
@param[in] index index of the page |
| 1118 |
@param[in] page the page to scan |
| 1119 |
@param[in] n_prefix look at the first n_prefix columns |
| 1120 |
@param[in] scan_method scan to the end of the page or not |
| 1121 |
@param[out] n_diff number of distinct records encountered |
| 1122 |
@param[out] n_external_pages if this is non-NULL then it will be set |
| 1123 |
to the number of externally stored pages which were encountered |
| 1124 |
@return offsets1 or offsets2 (the offsets of *out_rec), |
| 1125 |
or NULL if the page is empty and does not contain user records. */ |
| 1126 |
static inline ulint *dict_stats_scan_page(const rec_t **out_rec, |
| 1127 |
ulint *offsets1, ulint *offsets2, |
| 1128 |
const dict_index_t *index, |
| 1129 |
const page_t *page, ulint n_prefix, |
| 1130 |
page_scan_method_t scan_method, |
| 1131 |
uint64_t *n_diff, |
| 1132 |
uint64_t *n_external_pages) { |
| 1133 |
ulint *offsets_rec = offsets1; |
| 1134 |
ulint *offsets_next_rec = offsets2; |
| 1135 |
const rec_t *rec; |
| 1136 |
const rec_t *next_rec; |
| 1137 |
/* A dummy heap, to be passed to rec_get_offsets(). |
| 1138 |
Because offsets1,offsets2 should be big enough, |
| 1139 |
this memory heap should never be used. */ |
| 1140 |
mem_heap_t *heap = nullptr; |
| 1141 |
const rec_t *(*get_next)(const rec_t *); |
| 1142 |
|
| 1143 |
if (scan_method == COUNT_ALL_NON_BORING_AND_SKIP_DEL_MARKED) { |
| 1144 |
get_next = page_rec_get_next_non_del_marked; |
| 1145 |
} else { |
| 1146 |
get_next = page_rec_get_next_const; |
| 1147 |
} |
| 1148 |
|
| 1149 |
const bool should_count_external_pages = n_external_pages != nullptr; |
| 1150 |
|
| 1151 |
if (should_count_external_pages) { |
| 1152 |
*n_external_pages = 0; |
| 1153 |
} |
| 1154 |
|
| 1155 |
rec = get_next(page_get_infimum_rec(page)); |
| 1156 |
|
| 1157 |
if (page_rec_is_supremum(rec)) { |
| 1158 |
/* the page is empty or contains only delete-marked records */ |
| 1159 |
*n_diff = 0; |
| 1160 |
*out_rec = nullptr; |
| 1161 |
return (nullptr); |
| 1162 |
} |
| 1163 |
|
| 1164 |
offsets_rec = rec_get_offsets(rec, index, offsets_rec, ULINT_UNDEFINED, |
| 1165 |
UT_LOCATION_HERE, &heap); |
| 1166 |
|
| 1167 |
if (should_count_external_pages) { |
| 1168 |
*n_external_pages += |
| 1169 |
lob::btr_rec_get_externally_stored_len(index, rec, offsets_rec); |
| 1170 |
} |
| 1171 |
|
| 1172 |
next_rec = get_next(rec); |
| 1173 |
|
| 1174 |
*n_diff = 1; |
| 1175 |
|
| 1176 |
while (!page_rec_is_supremum(next_rec)) { |
| 1177 |
ulint matched_fields; |
| 1178 |
|
| 1179 |
offsets_next_rec = |
| 1180 |
rec_get_offsets(next_rec, index, offsets_next_rec, ULINT_UNDEFINED, |
| 1181 |
UT_LOCATION_HERE, &heap); |
| 1182 |
|
| 1183 |
/* check whether rec != next_rec when looking at |
| 1184 |
the first n_prefix fields */ |
| 1185 |
cmp_rec_rec_with_match(rec, next_rec, offsets_rec, offsets_next_rec, index, |
| 1186 |
false, false, &matched_fields); |
| 1187 |
|
| 1188 |
if (matched_fields < n_prefix) { |
| 1189 |
/* rec != next_rec, => rec is non-boring */ |
| 1190 |
|
| 1191 |
(*n_diff)++; |
| 1192 |
|
| 1193 |
if (scan_method == QUIT_ON_FIRST_NON_BORING) { |
| 1194 |
break; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
rec = next_rec; |
| 1199 |
{ |
| 1200 |
/* Assign offsets_rec = offsets_next_rec |
| 1201 |
so that offsets_rec matches with rec which |
| 1202 |
was just assigned rec = next_rec above. |
| 1203 |
Also need to point offsets_next_rec to the |
| 1204 |
place where offsets_rec was pointing before |
| 1205 |
because we have just 2 placeholders where |
| 1206 |
data is actually stored: |
| 1207 |
offsets1 and offsets2 and we |
| 1208 |
are using them in circular fashion |
| 1209 |
(offsets[_next]_rec are just pointers to |
| 1210 |
those placeholders). */ |
| 1211 |
ulint *offsets_tmp; |
| 1212 |
offsets_tmp = offsets_rec; |
| 1213 |
offsets_rec = offsets_next_rec; |
| 1214 |
offsets_next_rec = offsets_tmp; |
| 1215 |
} |
| 1216 |
|
| 1217 |
if (should_count_external_pages) { |
| 1218 |
*n_external_pages += |
| 1219 |
lob::btr_rec_get_externally_stored_len(index, rec, offsets_rec); |
| 1220 |
} |
| 1221 |
|
| 1222 |
next_rec = get_next(next_rec); |
| 1223 |
} |
| 1224 |
|
| 1225 |
/* offsets1,offsets2 should have been big enough */ |
| 1226 |
ut_a(heap == nullptr); |
| 1227 |
*out_rec = rec; |
| 1228 |
return (offsets_rec); |
| 1229 |
} |
| 1230 |
|
| 1231 |
/** Dive below the current position of a cursor and calculate the number of |
| 1232 |
distinct records on the leaf page, when looking at the fist n_prefix |
| 1233 |
columns. Also calculate the number of external pages pointed by records |
| 1234 |
on the leaf page. |
| 1235 |
@param[in] cur cursor |
| 1236 |
@param[in] n_prefix look at the first n_prefix columns |
| 1237 |
when comparing records |
| 1238 |
@param[out] n_diff number of distinct records |
| 1239 |
@param[out] n_external_pages number of external pages |
| 1240 |
*/ |
| 1241 |
static void dict_stats_analyze_index_below_cur(const btr_cur_t *cur, |
| 1242 |
ulint n_prefix, uint64_t *n_diff, |
| 1243 |
uint64_t *n_external_pages) { |
| 1244 |
dict_index_t *index; |
| 1245 |
buf_block_t *block; |
| 1246 |
const page_t *page; |
| 1247 |
mem_heap_t *heap; |
| 1248 |
const rec_t *rec; |
| 1249 |
ulint *offsets1; |
| 1250 |
ulint *offsets2; |
| 1251 |
ulint *offsets_rec; |
| 1252 |
ulint size; |
| 1253 |
mtr_t mtr; |
| 1254 |
|
| 1255 |
index = cur->index; |
| 1256 |
|
| 1257 |
/* Allocate offsets for the record and the node pointer, for |
| 1258 |
node pointer records. In a secondary index, the node pointer |
| 1259 |
record will consist of all index fields followed by a child |
| 1260 |
page number. |
| 1261 |
Allocate space for the offsets header (the allocation size at |
| 1262 |
offsets[0] and the REC_OFFS_HEADER_SIZE bytes), and n_fields + 1, |
| 1263 |
so that this will never be less than the size calculated in |
| 1264 |
rec_get_offsets(). */ |
| 1265 |
size = (1 + REC_OFFS_HEADER_SIZE) + 1 + dict_index_get_n_fields(index); |
| 1266 |
|
| 1267 |
heap = mem_heap_create(size * (sizeof *offsets1 + sizeof *offsets2), |
| 1268 |
UT_LOCATION_HERE); |
| 1269 |
|
| 1270 |
offsets1 = |
| 1271 |
static_cast<ulint *>(mem_heap_alloc(heap, size * sizeof *offsets1)); |
| 1272 |
|
| 1273 |
offsets2 = |
| 1274 |
static_cast<ulint *>(mem_heap_alloc(heap, size * sizeof *offsets2)); |
| 1275 |
|
| 1276 |
rec_offs_set_n_alloc(offsets1, size); |
| 1277 |
rec_offs_set_n_alloc(offsets2, size); |
| 1278 |
|
| 1279 |
rec = btr_cur_get_rec(cur); |
| 1280 |
|
| 1281 |
offsets_rec = rec_get_offsets(rec, index, offsets1, ULINT_UNDEFINED, |
| 1282 |
UT_LOCATION_HERE, &heap); |
| 1283 |
|
| 1284 |
page_id_t page_id(dict_index_get_space(index), |
| 1285 |
btr_node_ptr_get_child_page_no(rec, offsets_rec)); |
| 1286 |
const page_size_t page_size(dict_table_page_size(index->table)); |
| 1287 |
|
| 1288 |
/* assume no external pages by default - in case we quit from this |
| 1289 |
function without analyzing any leaf pages */ |
| 1290 |
*n_external_pages = 0; |
| 1291 |
|
| 1292 |
mtr_start(&mtr); |
| 1293 |
|
| 1294 |
/* descend to the leaf level on the B-tree */ |
| 1295 |
for (;;) { |
| 1296 |
block = buf_page_get_gen(page_id, page_size, RW_S_LATCH, |
| 1297 |
nullptr /* no guessed block */, Page_fetch::NORMAL, |
| 1298 |
UT_LOCATION_HERE, &mtr); |
| 1299 |
|
| 1300 |
page = buf_block_get_frame(block); |
| 1301 |
|
| 1302 |
if (btr_page_get_level(page) == 0) { |
| 1303 |
/* leaf level */ |
| 1304 |
break; |
| 1305 |
} |
| 1306 |
/* else */ |
| 1307 |
|
| 1308 |
/* search for the first non-boring record on the page */ |
| 1309 |
offsets_rec = |
| 1310 |
dict_stats_scan_page(&rec, offsets1, offsets2, index, page, n_prefix, |
| 1311 |
QUIT_ON_FIRST_NON_BORING, n_diff, nullptr); |
| 1312 |
|
| 1313 |
/* pages on level > 0 are not allowed to be empty */ |
| 1314 |
ut_a(offsets_rec != nullptr); |
| 1315 |
/* if page is not empty (offsets_rec != NULL) then n_diff must |
| 1316 |
be > 0, otherwise there is a bug in dict_stats_scan_page() */ |
| 1317 |
ut_a(*n_diff > 0); |
| 1318 |
|
| 1319 |
if (*n_diff == 1) { |
| 1320 |
mtr_commit(&mtr); |
| 1321 |
|
| 1322 |
/* page has all keys equal and the end of the page |
| 1323 |
was reached by dict_stats_scan_page(), no need to |
| 1324 |
descend to the leaf level */ |
| 1325 |
mem_heap_free(heap); |
| 1326 |
/* can't get an estimate for n_external_pages here |
| 1327 |
because we do not dive to the leaf level, assume no |
| 1328 |
external pages (*n_external_pages was assigned to 0 |
| 1329 |
above). */ |
| 1330 |
return; |
| 1331 |
} |
| 1332 |
/* else */ |
| 1333 |
|
| 1334 |
/* when we instruct dict_stats_scan_page() to quit on the |
| 1335 |
first non-boring record it finds, then the returned n_diff |
| 1336 |
can either be 0 (empty page), 1 (page has all keys equal) or |
| 1337 |
2 (non-boring record was found) */ |
| 1338 |
ut_a(*n_diff == 2); |
| 1339 |
|
| 1340 |
/* we have a non-boring record in rec, descend below it */ |
| 1341 |
|
| 1342 |
page_id.set_page_no(btr_node_ptr_get_child_page_no(rec, offsets_rec)); |
| 1343 |
} |
| 1344 |
|
| 1345 |
/* make sure we got a leaf page as a result from the above loop */ |
| 1346 |
ut_ad(btr_page_get_level(page) == 0); |
| 1347 |
|
| 1348 |
/* scan the leaf page and find the number of distinct keys, |
| 1349 |
when looking only at the first n_prefix columns; also estimate |
| 1350 |
the number of externally stored pages pointed by records on this |
| 1351 |
page */ |
| 1352 |
|
| 1353 |
offsets_rec = |
| 1354 |
dict_stats_scan_page(&rec, offsets1, offsets2, index, page, n_prefix, |
| 1355 |
srv_stats_include_delete_marked |
| 1356 |
? COUNT_ALL_NON_BORING_INCLUDE_DEL_MARKED |
| 1357 |
: COUNT_ALL_NON_BORING_AND_SKIP_DEL_MARKED, |
| 1358 |
n_diff, n_external_pages); |
| 1359 |
|
| 1360 |
#if 0 |
| 1361 |
DEBUG_PRINTF(" %s(): n_diff below page_no=%lu: " UINT64PF "\n", |
| 1362 |
__func__, page_no, n_diff); |
| 1363 |
#endif |
| 1364 |
|
| 1365 |
mtr_commit(&mtr); |
| 1366 |
mem_heap_free(heap); |
| 1367 |
} |
| 1368 |
|
| 1369 |
/** Input data that is used to calculate dict_index_t::stat_n_diff_key_vals[] |
| 1370 |
for each n-columns prefix (n from 1 to n_uniq). */ |
| 1371 |
struct n_diff_data_t { |
| 1372 |
/** Index of the level on which the descent through the btree |
| 1373 |
stopped. level 0 is the leaf level. This is >= 1 because we |
| 1374 |
avoid scanning the leaf level because it may contain too many |
| 1375 |
pages and doing so is useless when combined with the random dives - |
| 1376 |
if we are to scan the leaf level, this means a full scan and we can |
| 1377 |
simply do that instead of fiddling with picking random records higher |
| 1378 |
in the tree and to dive below them. At the start of the analyzing |
| 1379 |
we may decide to do full scan of the leaf level, but then this |
| 1380 |
structure is not used in that code path. */ |
| 1381 |
ulint level; |
| 1382 |
|
| 1383 |
/** Number of records on the level where the descend through the btree |
| 1384 |
stopped. When we scan the btree from the root, we stop at some mid |
| 1385 |
level, choose some records from it and dive below them towards a leaf |
| 1386 |
page to analyze. */ |
| 1387 |
uint64_t n_recs_on_level; |
| 1388 |
|
| 1389 |
/** Number of different key values that were found on the mid level. */ |
| 1390 |
uint64_t n_diff_on_level; |
| 1391 |
|
| 1392 |
/** Number of leaf pages that are analyzed. This is also the same as |
| 1393 |
the number of records that we pick from the mid level and dive below |
| 1394 |
them. */ |
| 1395 |
uint64_t n_leaf_pages_to_analyze; |
| 1396 |
|
| 1397 |
/** Cumulative sum of the number of different key values that were |
| 1398 |
found on all analyzed pages. */ |
| 1399 |
uint64_t n_diff_all_analyzed_pages; |
| 1400 |
|
| 1401 |
/** Cumulative sum of the number of external pages (stored outside of |
| 1402 |
the btree but in the same file segment). */ |
| 1403 |
uint64_t n_external_pages_sum; |
| 1404 |
}; |
| 1405 |
|
| 1406 |
/** Estimate the number of different key values in an index when looking at |
| 1407 |
the first n_prefix columns. For a given level in an index select |
| 1408 |
n_diff_data->n_leaf_pages_to_analyze records from that level and dive below |
| 1409 |
them to the corresponding leaf pages, then scan those leaf pages and save the |
| 1410 |
sampling results in n_diff_data->n_diff_all_analyzed_pages. |
| 1411 |
@param[in] index Index |
| 1412 |
@param[in] n_prefix Look at first 'n_prefix' columns when |
| 1413 |
comparing records |
| 1414 |
@param[in] boundaries A vector that contains |
| 1415 |
n_diff_data->n_diff_on_level integers each of which represents the index (on |
| 1416 |
level 'level', counting from left/smallest to right/biggest from 0) of the |
| 1417 |
last record from each group of distinct keys |
| 1418 |
@param[in,out] n_diff_data n_diff_all_analyzed_pages and |
| 1419 |
n_external_pages_sum in this structure will be set by this function. The |
| 1420 |
members level, n_diff_on_level and n_leaf_pages_to_analyze must be set by the |
| 1421 |
caller in advance - they are used by some calculations inside this function |
| 1422 |
@param[in,out] wait_start_time last known time index lock wasn't |
| 1423 |
awaited. |
| 1424 |
@param[in,out] mtr Mini-transaction |
| 1425 |
@return false if aborted */ |
| 1426 |
static bool dict_stats_analyze_index_for_n_prefix( |
| 1427 |
dict_index_t *index, ulint n_prefix, const boundaries_t *boundaries, |
| 1428 |
n_diff_data_t *n_diff_data, |
| 1429 |
std::chrono::steady_clock::time_point &wait_start_time, mtr_t *mtr) { |
| 1430 |
btr_pcur_t pcur; |
| 1431 |
const page_t *page; |
| 1432 |
uint64_t rec_idx; |
| 1433 |
uint64_t i; |
| 1434 |
|
| 1435 |
#if 0 |
| 1436 |
DEBUG_PRINTF(" %s(table=%s, index=%s, level=%lu, n_prefix=%lu," |
| 1437 |
" n_diff_on_level=" UINT64PF ")\n", |
| 1438 |
__func__, index->table->name, index->name, level, |
| 1439 |
n_prefix, n_diff_data->n_diff_on_level); |
| 1440 |
#endif |
| 1441 |
|
| 1442 |
ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_SX_LOCK)); |
| 1443 |
|
| 1444 |
/* Position pcur on the leftmost record on the leftmost page |
| 1445 |
on the desired level. */ |
| 1446 |
|
| 1447 |
pcur.open_at_side(true, index, BTR_SEARCH_TREE | BTR_ALREADY_S_LATCHED, true, |
| 1448 |
n_diff_data->level, mtr); |
| 1449 |
pcur.move_to_next_on_page(); |
| 1450 |
|
| 1451 |
page = pcur.get_page(); |
| 1452 |
|
| 1453 |
const rec_t *first_rec = pcur.get_rec(); |
| 1454 |
|
| 1455 |
/* We shouldn't be scanning the leaf level. The caller of this function |
| 1456 |
should have stopped the descend on level 1 or higher. */ |
| 1457 |
ut_ad(n_diff_data->level > 0); |
| 1458 |
ut_ad(!page_is_leaf(page)); |
| 1459 |
|
| 1460 |
/* The page must not be empty, except when |
| 1461 |
it is the root page (and the whole index is empty). */ |
| 1462 |
ut_ad(pcur.is_on_user_rec()); |
| 1463 |
ut_ad(first_rec == page_rec_get_next_const(page_get_infimum_rec(page))); |
| 1464 |
|
| 1465 |
/* check that we are indeed on the desired level */ |
| 1466 |
ut_a(btr_page_get_level(page) == n_diff_data->level); |
| 1467 |
|
| 1468 |
/* there should not be any pages on the left */ |
| 1469 |
ut_a(btr_page_get_prev(page, mtr) == FIL_NULL); |
| 1470 |
|
| 1471 |
/* check whether the first record on the leftmost page is marked |
| 1472 |
as such; we are on a non-leaf level */ |
| 1473 |
ut_a(rec_get_info_bits(first_rec, page_is_comp(page)) & |
| 1474 |
REC_INFO_MIN_REC_FLAG); |
| 1475 |
|
| 1476 |
const uint64_t last_idx_on_level = |
| 1477 |
boundaries->at(static_cast<unsigned>(n_diff_data->n_diff_on_level - 1)); |
| 1478 |
|
| 1479 |
rec_idx = 0; |
| 1480 |
|
| 1481 |
n_diff_data->n_diff_all_analyzed_pages = 0; |
| 1482 |
n_diff_data->n_external_pages_sum = 0; |
| 1483 |
|
| 1484 |
for (i = 0; i < n_diff_data->n_leaf_pages_to_analyze; i++) { |
| 1485 |
/* there are n_diff_on_level elements |
| 1486 |
in 'boundaries' and we divide those elements |
| 1487 |
into n_leaf_pages_to_analyze segments, for example: |
| 1488 |
|
| 1489 |
let n_diff_on_level=100, n_leaf_pages_to_analyze=4, then: |
| 1490 |
segment i=0: [0, 24] |
| 1491 |
segment i=1: [25, 49] |
| 1492 |
segment i=2: [50, 74] |
| 1493 |
segment i=3: [75, 99] or |
| 1494 |
|
| 1495 |
let n_diff_on_level=1, n_leaf_pages_to_analyze=1, then: |
| 1496 |
segment i=0: [0, 0] or |
| 1497 |
|
| 1498 |
let n_diff_on_level=2, n_leaf_pages_to_analyze=2, then: |
| 1499 |
segment i=0: [0, 0] |
| 1500 |
segment i=1: [1, 1] or |
| 1501 |
|
| 1502 |
let n_diff_on_level=13, n_leaf_pages_to_analyze=7, then: |
| 1503 |
segment i=0: [0, 0] |
| 1504 |
segment i=1: [1, 2] |
| 1505 |
segment i=2: [3, 4] |
| 1506 |
segment i=3: [5, 6] |
| 1507 |
segment i=4: [7, 8] |
| 1508 |
segment i=5: [9, 10] |
| 1509 |
segment i=6: [11, 12] |
| 1510 |
|
| 1511 |
then we select a random record from each segment and dive |
| 1512 |
below it */ |
| 1513 |
const uint64_t n_diff = n_diff_data->n_diff_on_level; |
| 1514 |
const uint64_t n_pick = n_diff_data->n_leaf_pages_to_analyze; |
| 1515 |
|
| 1516 |
const uint64_t left = n_diff * i / n_pick; |
| 1517 |
const uint64_t right = n_diff * (i + 1) / n_pick - 1; |
| 1518 |
|
| 1519 |
ut_a(left <= right); |
| 1520 |
ut_a(right <= last_idx_on_level); |
| 1521 |
|
| 1522 |
const uint64_t rnd = ut::random_from_interval(left, right); |
| 1523 |
|
| 1524 |
const uint64_t dive_below_idx = boundaries->at(rnd); |
| 1525 |
|
| 1526 |
#if 0 |
| 1527 |
DEBUG_PRINTF(" %s(): dive below record with index=" |
| 1528 |
UINT64PF "\n", __func__, dive_below_idx); |
| 1529 |
#endif |
| 1530 |
|
| 1531 |
/* seek to the record with index dive_below_idx */ |
| 1532 |
while (rec_idx < dive_below_idx && pcur.is_on_user_rec()) { |
| 1533 |
pcur.move_to_next_user_rec(mtr); |
| 1534 |
rec_idx++; |
| 1535 |
} |
| 1536 |
|
| 1537 |
/* if the level has finished before the record we are |
| 1538 |
searching for, this means that the B-tree has changed in |
| 1539 |
the meantime, quit our sampling and use whatever stats |
| 1540 |
we have collected so far */ |
| 1541 |
if (rec_idx < dive_below_idx) { |
| 1542 |
ut_ad(!pcur.is_on_user_rec()); |
| 1543 |
break; |
| 1544 |
} |
| 1545 |
|
| 1546 |
/* it could be that the tree has changed in such a way that |
| 1547 |
the record under dive_below_idx is the supremum record, in |
| 1548 |
this case rec_idx == dive_below_idx and pcur is positioned |
| 1549 |
on the supremum, we do not want to dive below it */ |
| 1550 |
if (!pcur.is_on_user_rec()) { |
| 1551 |
break; |
| 1552 |
} |
| 1553 |
|
| 1554 |
if ((i + 1) % 100 == 0 && |
| 1555 |
dict_stats_index_long_waiters(index, wait_start_time)) { |
| 1556 |
/* long waiters exist. abort. */ |
| 1557 |
break; |
| 1558 |
} |
| 1559 |
|
| 1560 |
ut_a(rec_idx == dive_below_idx); |
| 1561 |
|
| 1562 |
uint64_t n_diff_on_leaf_page; |
| 1563 |
uint64_t n_external_pages; |
| 1564 |
|
| 1565 |
dict_stats_analyze_index_below_cur(pcur.get_btr_cur(), n_prefix, |
| 1566 |
&n_diff_on_leaf_page, &n_external_pages); |
| 1567 |
|
| 1568 |
/* We adjust n_diff_on_leaf_page here to avoid counting |
| 1569 |
one value twice - once as the last on some page and once |
| 1570 |
as the first on another page. Consider the following example: |
| 1571 |
Leaf level: |
| 1572 |
page: (2,2,2,2,3,3) |
| 1573 |
... many pages like (3,3,3,3,3,3) ... |
| 1574 |
page: (3,3,3,3,5,5) |
| 1575 |
... many pages like (5,5,5,5,5,5) ... |
| 1576 |
page: (5,5,5,5,8,8) |
| 1577 |
page: (8,8,8,8,9,9) |
| 1578 |
our algo would (correctly) get an estimate that there are |
| 1579 |
2 distinct records per page (average). Having 4 pages below |
| 1580 |
non-boring records, it would (wrongly) estimate the number |
| 1581 |
of distinct records to 8. */ |
| 1582 |
if (n_diff_on_leaf_page > 0) { |
| 1583 |
n_diff_on_leaf_page--; |
| 1584 |
} |
| 1585 |
|
| 1586 |
n_diff_data->n_diff_all_analyzed_pages += n_diff_on_leaf_page; |
| 1587 |
|
| 1588 |
n_diff_data->n_external_pages_sum += n_external_pages; |
| 1589 |
} |
| 1590 |
|
| 1591 |
pcur.close(); |
| 1592 |
|
| 1593 |
if (i < n_diff_data->n_leaf_pages_to_analyze) { |
| 1594 |
/* return how much progressed */ |
| 1595 |
n_diff_data->n_leaf_pages_to_analyze = i; |
| 1596 |
return false; |
| 1597 |
} |
| 1598 |
|
| 1599 |
return true; |
| 1600 |
} |
| 1601 |
|
| 1602 |
/** Set dict_index_t::stat_n_diff_key_vals[] and stat_n_sample_sizes[]. |
| 1603 |
@param[in] n_diff_data input data to use to derive the results |
| 1604 |
@param[in,out] index index whose stat_n_diff_key_vals[] to set */ |
| 1605 |
static inline void dict_stats_index_set_n_diff(const n_diff_data_t *n_diff_data, |
| 1606 |
dict_index_t *index) { |
| 1607 |
for (ulint n_prefix = dict_index_get_n_unique(index); n_prefix >= 1; |
| 1608 |
n_prefix--) { |
| 1609 |
/* n_diff_all_analyzed_pages can be 0 here if |
| 1610 |
all the leaf pages sampled contained only |
| 1611 |
delete-marked records. In this case we should assign |
| 1612 |
0 to index->stat_n_diff_key_vals[n_prefix - 1], which |
| 1613 |
the formula below does. */ |
| 1614 |
|
| 1615 |
const n_diff_data_t *data = &n_diff_data[n_prefix - 1]; |
| 1616 |
|
| 1617 |
ut_ad(data->n_leaf_pages_to_analyze > 0); |
| 1618 |
ut_ad(data->n_recs_on_level > 0); |
| 1619 |
|
| 1620 |
uint64_t n_ordinary_leaf_pages; |
| 1621 |
|
| 1622 |
if (data->level == 1) { |
| 1623 |
/* If we know the number of records on level 1, then |
| 1624 |
this number is the same as the number of pages on |
| 1625 |
level 0 (leaf). */ |
| 1626 |
n_ordinary_leaf_pages = data->n_recs_on_level; |
| 1627 |
} else { |
| 1628 |
/* If we analyzed D ordinary leaf pages and found E |
| 1629 |
external pages in total linked from those D ordinary |
| 1630 |
leaf pages, then this means that the ratio |
| 1631 |
ordinary/external is D/E. Then the ratio ordinary/total |
| 1632 |
is D / (D + E). Knowing that the total number of pages |
| 1633 |
is T (including ordinary and external) then we estimate |
| 1634 |
that the total number of ordinary leaf pages is |
| 1635 |
T * D / (D + E). */ |
| 1636 |
n_ordinary_leaf_pages = |
| 1637 |
index->stat_n_leaf_pages * data->n_leaf_pages_to_analyze / |
| 1638 |
(data->n_leaf_pages_to_analyze + data->n_external_pages_sum); |
| 1639 |
} |
| 1640 |
|
| 1641 |
/* See REF01 for an explanation of the algorithm */ |
| 1642 |
index->stat_n_diff_key_vals[n_prefix - 1] = |
| 1643 |
n_ordinary_leaf_pages |
| 1644 |
|
| 1645 |
* data->n_diff_on_level / data->n_recs_on_level |
| 1646 |
|
| 1647 |
* data->n_diff_all_analyzed_pages / data->n_leaf_pages_to_analyze; |
| 1648 |
|
| 1649 |
index->stat_n_sample_sizes[n_prefix - 1] = data->n_leaf_pages_to_analyze; |
| 1650 |
|
| 1651 |
DEBUG_PRINTF( |
| 1652 |
" %s(): n_diff=" UINT64PF |
| 1653 |
" for n_prefix=%lu" |
| 1654 |
" (%lu" |
| 1655 |
" * " UINT64PF " / " UINT64PF " * " UINT64PF " / " UINT64PF ")\n", |
| 1656 |
__func__, index->stat_n_diff_key_vals[n_prefix - 1], n_prefix, |
| 1657 |
index->stat_n_leaf_pages, data->n_diff_on_level, data->n_recs_on_level, |
| 1658 |
data->n_diff_all_analyzed_pages, data->n_leaf_pages_to_analyze); |
| 1659 |
} |
| 1660 |
} |
| 1661 |
|
| 1662 |
/** Calculates new statistics for a given index and saves them to the index |
| 1663 |
members stat_n_diff_key_vals[], stat_n_sample_sizes[], stat_index_size and |
| 1664 |
stat_n_leaf_pages, based on the specified n_sample_pages. |
| 1665 |
@param[in,out] n_sample_pages number of leaf pages to sample. and suggested |
| 1666 |
next value to retry if aborted. |
| 1667 |
@param[in,out] index index to analyze. |
| 1668 |
@return false if aborted */ |
| 1669 |
static bool dict_stats_analyze_index_low(uint64_t &n_sample_pages, |
| 1670 |
dict_index_t *index) { |
| 1671 |
ulint root_level; |
| 1672 |
ulint level; |
| 1673 |
bool level_is_analyzed; |
| 1674 |
ulint n_uniq; |
| 1675 |
ulint n_prefix; |
| 1676 |
uint64_t total_recs; |
| 1677 |
uint64_t total_pages; |
| 1678 |
const uint64_t n_diff_required = n_sample_pages * 10; |
| 1679 |
bool succeeded = true; |
| 1680 |
mtr_t mtr; |
| 1681 |
ulint size; |
| 1682 |
DBUG_TRACE; |
| 1683 |
|
| 1684 |
DBUG_PRINT("info", ("index: %s, online status: %d", index->name(), |
| 1685 |
dict_index_get_online_status(index))); |
| 1686 |
|
| 1687 |
/* Disable update statistic for Rtree */ |
| 1688 |
if (dict_index_is_spatial(index)) { |
| 1689 |
return true; |
| 1690 |
} |
| 1691 |
|
| 1692 |
DEBUG_PRINTF(" %s(index=%s)\n", __func__, index->name()); |
| 1693 |
|
| 1694 |
dict_stats_empty_index(index); |
| 1695 |
|
| 1696 |
mtr_start(&mtr); |
| 1697 |
|
| 1698 |
mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 1699 |
|
| 1700 |
size = btr_get_size(index, BTR_TOTAL_SIZE, &mtr); |
| 1701 |
|
| 1702 |
if (size != ULINT_UNDEFINED) { |
| 1703 |
index->stat_index_size = size; |
| 1704 |
size = btr_get_size(index, BTR_N_LEAF_PAGES, &mtr); |
| 1705 |
} |
| 1706 |
|
| 1707 |
/* Release the X locks on the root page taken by btr_get_size() */ |
| 1708 |
mtr_commit(&mtr); |
| 1709 |
|
| 1710 |
switch (size) { |
| 1711 |
case ULINT_UNDEFINED: |
| 1712 |
dict_stats_assert_initialized_index(index); |
| 1713 |
return true; |
| 1714 |
case 0: |
| 1715 |
/* The root node of the tree is a leaf */ |
| 1716 |
size = 1; |
| 1717 |
} |
| 1718 |
|
| 1719 |
index->stat_n_leaf_pages = size; |
| 1720 |
|
| 1721 |
mtr_start(&mtr); |
| 1722 |
|
| 1723 |
mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 1724 |
auto wait_start_time = std::chrono::steady_clock::now(); |
| 1725 |
|
| 1726 |
root_level = btr_height_get(index, &mtr); |
| 1727 |
|
| 1728 |
n_uniq = dict_index_get_n_unique(index); |
| 1729 |
|
| 1730 |
/* If the tree has just one level (and one page) or if the user |
| 1731 |
has requested to sample too many pages then do full scan. |
| 1732 |
|
| 1733 |
For each n-column prefix (for n=1..n_uniq) N_SAMPLE_PAGES(index) |
| 1734 |
will be sampled, so in total N_SAMPLE_PAGES(index) * n_uniq leaf |
| 1735 |
pages will be sampled. If that number is bigger than the total |
| 1736 |
number of leaf pages then do full scan of the leaf level instead |
| 1737 |
since it will be faster and will give better results. |
| 1738 |
|
| 1739 |
Also, we don't want to hold the dict_index_get_lock(index) SX_LOCK, |
| 1740 |
(which is needed for scans on level>0) for too long, so we prefer a slower |
| 1741 |
full scan without the SX_LOCK to a faster scan under SX_LOCK over 1e6+ pages. |
| 1742 |
*/ |
| 1743 |
|
| 1744 |
if (root_level == 0 || n_sample_pages * n_uniq > |
| 1745 |
std::min<ulint>(index->stat_n_leaf_pages, 1e6)) { |
| 1746 |
if (root_level == 0) { |
| 1747 |
DEBUG_PRINTF( |
| 1748 |
" %s(): just one page," |
| 1749 |
" doing full scan\n", |
| 1750 |
__func__); |
| 1751 |
} else { |
| 1752 |
DEBUG_PRINTF( |
| 1753 |
" %s(): too many pages requested for" |
| 1754 |
" sampling, doing full scan\n", |
| 1755 |
__func__); |
| 1756 |
} |
| 1757 |
|
| 1758 |
/* do full scan of level 0; save results directly |
| 1759 |
into the index */ |
| 1760 |
|
| 1761 |
(void)dict_stats_analyze_index_level( |
| 1762 |
index, 0 /* leaf level */, index->stat_n_diff_key_vals, &total_recs, |
| 1763 |
&total_pages, nullptr /* boundaries not needed */, wait_start_time, |
| 1764 |
&mtr); |
| 1765 |
|
| 1766 |
for (ulint i = 0; i < n_uniq; i++) { |
| 1767 |
index->stat_n_sample_sizes[i] = total_pages; |
| 1768 |
} |
| 1769 |
|
| 1770 |
mtr_commit(&mtr); |
| 1771 |
|
| 1772 |
dict_stats_assert_initialized_index(index); |
| 1773 |
return true; |
| 1774 |
} |
| 1775 |
|
| 1776 |
/* For each level that is being scanned in the btree, this contains the |
| 1777 |
number of different key values for all possible n-column prefixes. */ |
| 1778 |
uint64_t *n_diff_on_level = ut::new_arr_withkey<uint64_t>( |
| 1779 |
ut::make_psi_memory_key(mem_key_dict_stats_n_diff_on_level), |
| 1780 |
ut::Count{n_uniq}); |
| 1781 |
|
| 1782 |
/* For each level that is being scanned in the btree, this contains the |
| 1783 |
index of the last record from each group of equal records (when |
| 1784 |
comparing only the first n columns, n=1..n_uniq). */ |
| 1785 |
boundaries_t *n_diff_boundaries = ut::new_arr_withkey<boundaries_t>( |
| 1786 |
UT_NEW_THIS_FILE_PSI_KEY, ut::Count{n_uniq}); |
| 1787 |
|
| 1788 |
/* For each n-column prefix this array contains the input data that is |
| 1789 |
used to calculate dict_index_t::stat_n_diff_key_vals[]. */ |
| 1790 |
n_diff_data_t *n_diff_data = ut::new_arr_withkey<n_diff_data_t>( |
| 1791 |
UT_NEW_THIS_FILE_PSI_KEY, ut::Count{n_uniq}); |
| 1792 |
|
| 1793 |
/* total_recs is also used to estimate the number of pages on one |
| 1794 |
level below, so at the start we have 1 page (the root) */ |
| 1795 |
total_recs = 1; |
| 1796 |
|
| 1797 |
/* Here we use the following optimization: |
| 1798 |
If we find that level L is the first one (searching from the |
| 1799 |
root) that contains at least D distinct keys when looking at |
| 1800 |
the first n_prefix columns, then: |
| 1801 |
if we look at the first n_prefix-1 columns then the first |
| 1802 |
level that contains D distinct keys will be either L or a |
| 1803 |
lower one. |
| 1804 |
So if we find that the first level containing D distinct |
| 1805 |
keys (on n_prefix columns) is L, we continue from L when |
| 1806 |
searching for D distinct keys on n_prefix-1 columns. */ |
| 1807 |
level = root_level; |
| 1808 |
level_is_analyzed = false; |
| 1809 |
|
| 1810 |
for (n_prefix = n_uniq; n_prefix >= 1; n_prefix--) { |
| 1811 |
DEBUG_PRINTF( |
| 1812 |
" %s(): searching level with >=%llu" |
| 1813 |
" distinct records, n_prefix=%lu\n", |
| 1814 |
__func__, n_diff_required, n_prefix); |
| 1815 |
|
| 1816 |
/* Commit the mtr to release the tree SX lock to allow |
| 1817 |
other threads to do some work too. */ |
| 1818 |
mtr_commit(&mtr); |
| 1819 |
mtr_start(&mtr); |
| 1820 |
mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 1821 |
wait_start_time = std::chrono::steady_clock::now(); |
| 1822 |
if (root_level != btr_height_get(index, &mtr)) { |
| 1823 |
/* Just quit if the tree has changed beyond |
| 1824 |
recognition here. The old stats from previous |
| 1825 |
runs will remain in the values that we have |
| 1826 |
not calculated yet. Initially when the index |
| 1827 |
object is created the stats members are given |
| 1828 |
some sensible values so leaving them untouched |
| 1829 |
here even the first time will not cause us to |
| 1830 |
read uninitialized memory later. */ |
| 1831 |
break; |
| 1832 |
} |
| 1833 |
|
| 1834 |
/* check whether we should pick the current level; |
| 1835 |
we pick level 1 even if it does not have enough |
| 1836 |
distinct records because we do not want to scan the |
| 1837 |
leaf level because it may contain too many records */ |
| 1838 |
if (level_is_analyzed && |
| 1839 |
(n_diff_on_level[n_prefix - 1] >= n_diff_required || level == 1)) { |
| 1840 |
goto found_level; |
| 1841 |
} |
| 1842 |
|
| 1843 |
/* search for a level that contains enough distinct records */ |
| 1844 |
|
| 1845 |
if (level_is_analyzed && level > 1) { |
| 1846 |
/* if this does not hold we should be on |
| 1847 |
"found_level" instead of here */ |
| 1848 |
ut_ad(n_diff_on_level[n_prefix - 1] < n_diff_required); |
| 1849 |
|
| 1850 |
level--; |
| 1851 |
level_is_analyzed = false; |
| 1852 |
} |
| 1853 |
|
| 1854 |
/* descend into the tree, searching for "good enough" level */ |
| 1855 |
for (;;) { |
| 1856 |
/* make sure we do not scan the leaf level |
| 1857 |
accidentally, it may contain too many pages */ |
| 1858 |
ut_ad(level > 0); |
| 1859 |
|
| 1860 |
/* scanning the same level twice is an optimization |
| 1861 |
bug */ |
| 1862 |
ut_ad(!level_is_analyzed); |
| 1863 |
|
| 1864 |
/* Do not scan if this would read too many pages. |
| 1865 |
Here we use the following fact: |
| 1866 |
the number of pages on level L equals the number |
| 1867 |
of records on level L+1, thus we deduce that the |
| 1868 |
following call would scan total_recs pages, because |
| 1869 |
total_recs is left from the previous iteration when |
| 1870 |
we scanned one level upper or we have not scanned any |
| 1871 |
levels yet in which case total_recs is 1. */ |
| 1872 |
if (total_recs > n_sample_pages) { |
| 1873 |
/* if the above cond is true then we are |
| 1874 |
not at the root level since on the root |
| 1875 |
level total_recs == 1 (set before we |
| 1876 |
enter the n-prefix loop) and cannot |
| 1877 |
be > N_SAMPLE_PAGES(index) */ |
| 1878 |
ut_a(level != root_level); |
| 1879 |
|
| 1880 |
/* step one level back and be satisfied with |
| 1881 |
whatever it contains */ |
| 1882 |
level++; |
| 1883 |
level_is_analyzed = true; |
| 1884 |
|
| 1885 |
break; |
| 1886 |
} |
| 1887 |
|
| 1888 |
const uint64_t prev_total_recs = total_recs; |
| 1889 |
if (!dict_stats_analyze_index_level( |
| 1890 |
index, level, n_diff_on_level, &total_recs, &total_pages, |
| 1891 |
n_diff_boundaries, wait_start_time, &mtr)) { |
| 1892 |
/* The other index->lock waiter is near to timeout. |
| 1893 |
We should reduce requested pages for safety. */ |
| 1894 |
ut_ad(prev_total_recs <= n_sample_pages); |
| 1895 |
n_sample_pages = prev_total_recs / 2; |
| 1896 |
|
| 1897 |
/* abort */ |
| 1898 |
succeeded = false; |
| 1899 |
goto end; |
| 1900 |
} |
| 1901 |
|
| 1902 |
level_is_analyzed = true; |
| 1903 |
|
| 1904 |
if (level == 1 || n_diff_on_level[n_prefix - 1] >= n_diff_required) { |
| 1905 |
/* we have reached the last level we could scan |
| 1906 |
or we found a good level with many distinct |
| 1907 |
records */ |
| 1908 |
break; |
| 1909 |
} |
| 1910 |
|
| 1911 |
level--; |
| 1912 |
level_is_analyzed = false; |
| 1913 |
} |
| 1914 |
found_level: |
| 1915 |
|
| 1916 |
DEBUG_PRINTF(" %s(): found level %lu that has " UINT64PF |
| 1917 |
" distinct records for n_prefix=%lu\n", |
| 1918 |
__func__, level, n_diff_on_level[n_prefix - 1], n_prefix); |
| 1919 |
/* here we are either on level 1 or the level that we are on |
| 1920 |
contains >= N_DIFF_REQUIRED distinct keys or we did not scan |
| 1921 |
deeper levels because they would contain too many pages */ |
| 1922 |
|
| 1923 |
ut_ad(level > 0); |
| 1924 |
|
| 1925 |
ut_ad(level_is_analyzed); |
| 1926 |
|
| 1927 |
/* if any of these is 0 then there is exactly one page in the |
| 1928 |
B-tree and it is empty and we should have done full scan and |
| 1929 |
should not be here */ |
| 1930 |
ut_ad(total_recs > 0); |
| 1931 |
ut_ad(n_diff_on_level[n_prefix - 1] > 0); |
| 1932 |
|
| 1933 |
ut_ad(n_sample_pages > 0); |
| 1934 |
|
| 1935 |
n_diff_data_t *data = &n_diff_data[n_prefix - 1]; |
| 1936 |
|
| 1937 |
data->level = level; |
| 1938 |
|
| 1939 |
data->n_recs_on_level = total_recs; |
| 1940 |
|
| 1941 |
data->n_diff_on_level = n_diff_on_level[n_prefix - 1]; |
| 1942 |
|
| 1943 |
data->n_leaf_pages_to_analyze = |
| 1944 |
std::min(n_sample_pages, n_diff_on_level[n_prefix - 1]); |
| 1945 |
|
| 1946 |
/* pick some records from this level and dive below them for |
| 1947 |
the given n_prefix */ |
| 1948 |
|
| 1949 |
if (!dict_stats_analyze_index_for_n_prefix(index, n_prefix, |
| 1950 |
&n_diff_boundaries[n_prefix - 1], |
| 1951 |
data, wait_start_time, &mtr)) { |
| 1952 |
/* The other index->lock waiter is near to timeout. |
| 1953 |
We should reduce requested pages for safety. */ |
| 1954 |
ut_ad(data->n_leaf_pages_to_analyze <= n_sample_pages); |
| 1955 |
n_sample_pages = data->n_leaf_pages_to_analyze / 2; |
| 1956 |
|
| 1957 |
/* abort */ |
| 1958 |
succeeded = false; |
| 1959 |
goto end; |
| 1960 |
} |
| 1961 |
} |
| 1962 |
|
| 1963 |
end: |
| 1964 |
mtr_commit(&mtr); |
| 1965 |
|
| 1966 |
ut::delete_arr(n_diff_boundaries); |
| 1967 |
|
| 1968 |
ut::delete_arr(n_diff_on_level); |
| 1969 |
|
| 1970 |
/* n_prefix == 0 means that the above loop did not end up prematurely |
| 1971 |
due to tree being changed and so n_diff_data[] is set up. */ |
| 1972 |
if (succeeded && n_prefix == 0) { |
| 1973 |
dict_stats_index_set_n_diff(n_diff_data, index); |
| 1974 |
} |
| 1975 |
|
| 1976 |
ut::delete_arr(n_diff_data); |
| 1977 |
|
| 1978 |
if (succeeded) { |
| 1979 |
dict_stats_assert_initialized_index(index); |
| 1980 |
} |
| 1981 |
|
| 1982 |
return succeeded; |
| 1983 |
} |
| 1984 |
|
| 1985 |
/** Calculates new statistics for a given index and saves them to the index |
| 1986 |
members stat_n_diff_key_vals[], stat_n_sample_sizes[], stat_index_size and |
| 1987 |
stat_n_leaf_pages. This function could be slow. */ |
| 1988 |
static void dict_stats_analyze_index( |
| 1989 |
dict_index_t *index) /*!< in/out: index to analyze */ |
| 1990 |
{ |
| 1991 |
uint64_t n_sample_pages = N_SAMPLE_PAGES(index); |
| 1992 |
while (n_sample_pages > 0 && |
| 1993 |
!dict_stats_analyze_index_low(n_sample_pages, index)) { |
| 1994 |
/* aborted. retrying. */ |
| 1995 |
ib::warn(ER_IB_MSG_STATS_SAMPLING_TOO_LARGE) |
| 1996 |
<< "Detected too long lock waiting around " << index->table->name << "." |
| 1997 |
<< index->name |
| 1998 |
<< " stats_sample_pages. Retrying with the smaller value " |
| 1999 |
<< n_sample_pages << "."; |
| 2000 |
|
| 2001 |
/* Certain delay is needed for waiters to lock the index next. */ |
| 2002 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 2003 |
} |
| 2004 |
} |
| 2005 |
|
| 2006 |
/** Calculates new estimates for table and index statistics. This function |
| 2007 |
is relatively slow and is used to calculate persistent statistics that |
| 2008 |
will be saved on disk. |
| 2009 |
@return DB_SUCCESS or error code */ |
| 2010 |
static dberr_t dict_stats_update_persistent( |
| 2011 |
dict_table_t *table) /*!< in/out: table */ |
| 2012 |
{ |
| 2013 |
dict_index_t *index; |
| 2014 |
|
| 2015 |
DEBUG_PRINTF("%s(table=%s)\n", __func__, table->name); |
| 2016 |
|
| 2017 |
dict_table_stats_lock(table, RW_X_LATCH); |
| 2018 |
|
| 2019 |
/* analyze the clustered index first */ |
| 2020 |
|
| 2021 |
index = table->first_index(); |
| 2022 |
|
| 2023 |
if (index == nullptr || index->is_corrupted() || |
| 2024 |
(index->type | DICT_UNIQUE) != (DICT_CLUSTERED | DICT_UNIQUE)) { |
| 2025 |
/* Table definition is corrupt */ |
| 2026 |
dict_table_stats_unlock(table, RW_X_LATCH); |
| 2027 |
dict_stats_empty_table(table); |
| 2028 |
|
| 2029 |
return (DB_CORRUPTION); |
| 2030 |
} |
| 2031 |
|
| 2032 |
ut_ad(!dict_index_is_ibuf(index)); |
| 2033 |
|
| 2034 |
dict_stats_analyze_index(index); |
| 2035 |
|
| 2036 |
ulint n_unique = dict_index_get_n_unique(index); |
| 2037 |
|
| 2038 |
table->stat_n_rows = index->stat_n_diff_key_vals[n_unique - 1]; |
| 2039 |
|
| 2040 |
table->stat_clustered_index_size = index->stat_index_size; |
| 2041 |
|
| 2042 |
/* analyze other indexes from the table, if any */ |
| 2043 |
|
| 2044 |
table->stat_sum_of_other_index_sizes = 0; |
| 2045 |
|
| 2046 |
for (index = index->next(); index != nullptr; index = index->next()) { |
| 2047 |
ut_ad(!dict_index_is_ibuf(index)); |
| 2048 |
|
| 2049 |
if (index->type & DICT_FTS || dict_index_is_spatial(index)) { |
| 2050 |
continue; |
| 2051 |
} |
| 2052 |
|
| 2053 |
dict_stats_empty_index(index); |
| 2054 |
|
| 2055 |
if (dict_stats_should_ignore_index(index)) { |
| 2056 |
continue; |
| 2057 |
} |
| 2058 |
|
| 2059 |
if (!(table->stats_bg_flag & BG_STAT_SHOULD_QUIT)) { |
| 2060 |
dict_stats_analyze_index(index); |
| 2061 |
} |
| 2062 |
|
| 2063 |
table->stat_sum_of_other_index_sizes += index->stat_index_size; |
| 2064 |
} |
| 2065 |
|
| 2066 |
table->stats_last_recalc = std::chrono::steady_clock::now(); |
| 2067 |
|
| 2068 |
table->stat_modified_counter = 0; |
| 2069 |
|
| 2070 |
table->stat_initialized = true; |
| 2071 |
|
| 2072 |
dict_stats_assert_initialized(table); |
| 2073 |
|
| 2074 |
dict_table_stats_unlock(table, RW_X_LATCH); |
| 2075 |
|
| 2076 |
return (DB_SUCCESS); |
| 2077 |
} |
| 2078 |
|
| 2079 |
#include "mysql_com.h" |
| 2080 |
|
| 2081 |
/** Save an individual index's statistic into the persistent statistics |
| 2082 |
storage. |
| 2083 |
@param[in] index index to be updated |
| 2084 |
@param[in] last_update timestamp of the stat |
| 2085 |
@param[in] stat_name name of the stat |
| 2086 |
@param[in] stat_value value of the stat |
| 2087 |
@param[in] sample_size n pages sampled or NULL |
| 2088 |
@param[in] stat_description description of the stat |
| 2089 |
@param[in,out] trx in case of NULL the function will |
| 2090 |
allocate and free the trx object. If it is not NULL then it will be |
| 2091 |
rolled back only in the case of error, but not freed. |
| 2092 |
@return DB_SUCCESS or error code */ |
| 2093 |
static dberr_t dict_stats_save_index_stat(dict_index_t *index, lint last_update, |
| 2094 |
const char *stat_name, |
| 2095 |
uint64_t stat_value, |
| 2096 |
uint64_t *sample_size, |
| 2097 |
const char *stat_description, |
| 2098 |
trx_t *trx) { |
| 2099 |
/* During upgrade, the indexes are loaded in dict_load_index_low and the |
| 2100 |
clustered indexes are renamed as PRIMARY as MySQL understands only |
| 2101 |
PRIMARY indexes. The following condition will be true only if such an index |
| 2102 |
was renamed earlier during upgrade. |
| 2103 |
During upgrade, the existing mysql.innodb_index_stats is backed up as |
| 2104 |
mysql.innodb_index_stats_backup57 and new table mysql.innodb_index_stats |
| 2105 |
is populated from the backup. After the upgrade, any entries missing |
| 2106 |
in the new mysql.innodb_index_stats is populated here. Since, the clustered |
| 2107 |
indexes were renamed to PRIMARY, a new entry in made into innodb_index_stats |
| 2108 |
causing duplicate entries as the backup already has an entry for the |
| 2109 |
corresponding clustered index. */ |
| 2110 |
if (strcmp(index->name, "PRIMARY") == 0 && index->type == DICT_CLUSTERED) { |
| 2111 |
return DB_SUCCESS; |
| 2112 |
} |
| 2113 |
|
| 2114 |
dberr_t ret; |
| 2115 |
pars_info_t *pinfo; |
| 2116 |
char db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 2117 |
char table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 2118 |
|
| 2119 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 2120 |
|
| 2121 |
dict_fs2utf8(index->table->name.m_name, db_utf8mb3, sizeof(db_utf8mb3), |
| 2122 |
table_utf8mb3, sizeof(table_utf8mb3)); |
| 2123 |
|
| 2124 |
pinfo = pars_info_create(); |
| 2125 |
pars_info_add_str_literal(pinfo, "database_name", db_utf8mb3); |
| 2126 |
pars_info_add_str_literal(pinfo, "table_name", table_utf8mb3); |
| 2127 |
pars_info_add_str_literal(pinfo, "index_name", index->name); |
| 2128 |
UNIV_MEM_ASSERT_RW_ABORT(&last_update, 4); |
| 2129 |
pars_info_add_int4_literal(pinfo, "last_update", last_update); |
| 2130 |
UNIV_MEM_ASSERT_RW_ABORT(stat_name, strlen(stat_name)); |
| 2131 |
pars_info_add_str_literal(pinfo, "stat_name", stat_name); |
| 2132 |
UNIV_MEM_ASSERT_RW_ABORT(&stat_value, 8); |
| 2133 |
pars_info_add_ull_literal(pinfo, "stat_value", stat_value); |
| 2134 |
if (sample_size != nullptr) { |
| 2135 |
UNIV_MEM_ASSERT_RW_ABORT(sample_size, 8); |
| 2136 |
pars_info_add_ull_literal(pinfo, "sample_size", *sample_size); |
| 2137 |
} else { |
| 2138 |
pars_info_add_literal(pinfo, "sample_size", nullptr, UNIV_SQL_NULL, |
| 2139 |
DATA_FIXBINARY, 0); |
| 2140 |
} |
| 2141 |
UNIV_MEM_ASSERT_RW_ABORT(stat_description, strlen(stat_description)); |
| 2142 |
pars_info_add_str_literal(pinfo, "stat_description", stat_description); |
| 2143 |
|
| 2144 |
ret = dict_stats_exec_sql(pinfo, |
| 2145 |
"PROCEDURE INDEX_STATS_SAVE () IS\n" |
| 2146 |
"BEGIN\n" |
| 2147 |
|
| 2148 |
"DELETE FROM \"" INDEX_STATS_NAME |
| 2149 |
"\"\n" |
| 2150 |
"WHERE\n" |
| 2151 |
"database_name = :database_name AND\n" |
| 2152 |
"table_name = :table_name AND\n" |
| 2153 |
"index_name = :index_name AND\n" |
| 2154 |
"stat_name = :stat_name;\n" |
| 2155 |
|
| 2156 |
"INSERT INTO \"" INDEX_STATS_NAME |
| 2157 |
"\"\n" |
| 2158 |
"VALUES\n" |
| 2159 |
"(\n" |
| 2160 |
":database_name,\n" |
| 2161 |
":table_name,\n" |
| 2162 |
":index_name,\n" |
| 2163 |
":last_update,\n" |
| 2164 |
":stat_name,\n" |
| 2165 |
":stat_value,\n" |
| 2166 |
":sample_size,\n" |
| 2167 |
":stat_description\n" |
| 2168 |
");\n" |
| 2169 |
"END;", |
| 2170 |
trx); |
| 2171 |
|
| 2172 |
if (ret != DB_SUCCESS) { |
| 2173 |
ib::error(ER_IB_MSG_222) |
| 2174 |
<< "Cannot save index statistics for table " << index->table->name |
| 2175 |
<< ", index " << index->name << ", stat name \"" << stat_name |
| 2176 |
<< "\": " << ut_strerr(ret); |
| 2177 |
} |
| 2178 |
|
| 2179 |
return (ret); |
| 2180 |
} |
| 2181 |
|
| 2182 |
/** Save the table's statistics into the persistent statistics storage. |
| 2183 |
@param[in] table_orig table whose stats to save |
| 2184 |
@param[in] only_for_index if this is non-NULL, then stats for indexes |
| 2185 |
that are not equal to it will not be saved, if NULL, then all indexes' stats |
| 2186 |
are saved |
| 2187 |
@return DB_SUCCESS or error code */ |
| 2188 |
static dberr_t dict_stats_save(dict_table_t *table_orig, |
| 2189 |
const index_id_t *only_for_index) { |
| 2190 |
ut_ad(!dict_sys_mutex_own()); |
| 2191 |
log_free_check(); |
| 2192 |
pars_info_t *pinfo; |
| 2193 |
dberr_t ret; |
| 2194 |
dict_table_t *table; |
| 2195 |
char db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 2196 |
char table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 2197 |
|
| 2198 |
table = dict_stats_snapshot_create(table_orig); |
| 2199 |
|
| 2200 |
dict_fs2utf8(table->name.m_name, db_utf8mb3, sizeof(db_utf8mb3), |
| 2201 |
table_utf8mb3, sizeof(table_utf8mb3)); |
| 2202 |
|
| 2203 |
rw_lock_x_lock(dict_operation_lock, UT_LOCATION_HERE); |
| 2204 |
|
| 2205 |
/* MySQL's timestamp is 4 byte, so we use |
| 2206 |
pars_info_add_int4_literal() which takes a lint arg, so "now" is |
| 2207 |
lint */ |
| 2208 |
auto now = static_cast<uint32_t>(time(nullptr)); |
| 2209 |
|
| 2210 |
pinfo = pars_info_create(); |
| 2211 |
|
| 2212 |
pars_info_add_str_literal(pinfo, "database_name", db_utf8mb3); |
| 2213 |
pars_info_add_str_literal(pinfo, "table_name", table_utf8mb3); |
| 2214 |
pars_info_add_int4_literal(pinfo, "last_update", now); |
| 2215 |
pars_info_add_ull_literal(pinfo, "n_rows", table->stat_n_rows); |
| 2216 |
pars_info_add_ull_literal(pinfo, "clustered_index_size", |
| 2217 |
table->stat_clustered_index_size); |
| 2218 |
pars_info_add_ull_literal(pinfo, "sum_of_other_index_sizes", |
| 2219 |
table->stat_sum_of_other_index_sizes); |
| 2220 |
|
| 2221 |
ret = dict_stats_exec_sql(pinfo, |
| 2222 |
"PROCEDURE TABLE_STATS_SAVE () IS\n" |
| 2223 |
"BEGIN\n" |
| 2224 |
|
| 2225 |
"DELETE FROM \"" TABLE_STATS_NAME |
| 2226 |
"\"\n" |
| 2227 |
"WHERE\n" |
| 2228 |
"database_name = :database_name AND\n" |
| 2229 |
"table_name = :table_name;\n" |
| 2230 |
|
| 2231 |
"INSERT INTO \"" TABLE_STATS_NAME |
| 2232 |
"\"\n" |
| 2233 |
"VALUES\n" |
| 2234 |
"(\n" |
| 2235 |
":database_name,\n" |
| 2236 |
":table_name,\n" |
| 2237 |
":last_update,\n" |
| 2238 |
":n_rows,\n" |
| 2239 |
":clustered_index_size,\n" |
| 2240 |
":sum_of_other_index_sizes\n" |
| 2241 |
");\n" |
| 2242 |
"END;", |
| 2243 |
nullptr); |
| 2244 |
|
| 2245 |
if (ret != DB_SUCCESS) { |
| 2246 |
ib::error(ER_IB_MSG_223) << "Cannot save table statistics for table " |
| 2247 |
<< table->name << ": " << ut_strerr(ret); |
| 2248 |
|
| 2249 |
rw_lock_x_unlock(dict_operation_lock); |
| 2250 |
|
| 2251 |
dict_stats_snapshot_free(table); |
| 2252 |
|
| 2253 |
return (ret); |
| 2254 |
} |
| 2255 |
|
| 2256 |
trx_t *trx = trx_allocate_for_background(); |
| 2257 |
|
| 2258 |
if (srv_read_only_mode) { |
| 2259 |
trx_start_internal_read_only(trx, UT_LOCATION_HERE); |
| 2260 |
} else { |
| 2261 |
trx_start_internal(trx, UT_LOCATION_HERE); |
| 2262 |
} |
| 2263 |
|
| 2264 |
dict_index_t *index; |
| 2265 |
index_map_t indexes((ut_strcmp_functor()), |
| 2266 |
index_map_t_allocator(mem_key_dict_stats_index_map_t)); |
| 2267 |
|
| 2268 |
/* Below we do all the modifications in innodb_index_stats in a single |
| 2269 |
transaction for performance reasons. Modifying more than one row in a |
| 2270 |
single transaction may deadlock with other transactions if they |
| 2271 |
lock the rows in different order. Other transaction could be for |
| 2272 |
example when we DROP a table and do |
| 2273 |
DELETE FROM innodb_index_stats WHERE database_name = '...' |
| 2274 |
AND table_name = '...'; which will affect more than one row. To |
| 2275 |
prevent deadlocks we always lock the rows in the same order - the |
| 2276 |
order of the PK, which is (database_name, table_name, index_name, |
| 2277 |
stat_name). This is why below we sort the indexes by name and then |
| 2278 |
for each index, do the mods ordered by stat_name. */ |
| 2279 |
|
| 2280 |
for (index = table->first_index(); index != nullptr; index = index->next()) { |
| 2281 |
indexes[index->name] = index; |
| 2282 |
} |
| 2283 |
|
| 2284 |
index_map_t::const_iterator it; |
| 2285 |
|
| 2286 |
for (it = indexes.begin(); it != indexes.end(); ++it) { |
| 2287 |
index = it->second; |
| 2288 |
|
| 2289 |
if (only_for_index != nullptr && |
| 2290 |
index->space != only_for_index->m_space_id && |
| 2291 |
index->id != only_for_index->m_index_id) { |
| 2292 |
continue; |
| 2293 |
} |
| 2294 |
|
| 2295 |
if (dict_stats_should_ignore_index(index)) { |
| 2296 |
continue; |
| 2297 |
} |
| 2298 |
|
| 2299 |
ut_ad(!dict_index_is_ibuf(index)); |
| 2300 |
|
| 2301 |
for (ulint i = 0; i < index->n_uniq; i++) { |
| 2302 |
char stat_name[16]; |
| 2303 |
char stat_description[1024]; |
| 2304 |
ulint j; |
| 2305 |
|
| 2306 |
snprintf(stat_name, sizeof(stat_name), "n_diff_pfx%02" ULINTPFS, i + 1); |
| 2307 |
|
| 2308 |
/* craft a string that contains the column names */ |
| 2309 |
snprintf(stat_description, sizeof(stat_description), "%s", |
| 2310 |
index->fields[0].name()); |
| 2311 |
for (j = 1; j <= i; j++) { |
| 2312 |
size_t len; |
| 2313 |
|
| 2314 |
len = strlen(stat_description); |
| 2315 |
|
| 2316 |
snprintf(stat_description + len, sizeof(stat_description) - len, ",%s", |
| 2317 |
index->fields[j].name()); |
| 2318 |
} |
| 2319 |
|
| 2320 |
ret = dict_stats_save_index_stat( |
| 2321 |
index, now, stat_name, index->stat_n_diff_key_vals[i], |
| 2322 |
&index->stat_n_sample_sizes[i], stat_description, trx); |
| 2323 |
|
| 2324 |
if (ret != DB_SUCCESS) { |
| 2325 |
goto end; |
| 2326 |
} |
| 2327 |
} |
| 2328 |
|
| 2329 |
ret = dict_stats_save_index_stat(index, now, "n_leaf_pages", |
| 2330 |
index->stat_n_leaf_pages, nullptr, |
| 2331 |
"Number of leaf pages " |
| 2332 |
"in the index", |
| 2333 |
trx); |
| 2334 |
if (ret != DB_SUCCESS) { |
| 2335 |
goto end; |
| 2336 |
} |
| 2337 |
|
| 2338 |
ret = dict_stats_save_index_stat(index, now, "size", index->stat_index_size, |
| 2339 |
nullptr, |
| 2340 |
"Number of pages " |
| 2341 |
"in the index", |
| 2342 |
trx); |
| 2343 |
if (ret != DB_SUCCESS) { |
| 2344 |
goto end; |
| 2345 |
} |
| 2346 |
} |
| 2347 |
|
| 2348 |
trx_commit_for_mysql(trx); |
| 2349 |
|
| 2350 |
end: |
| 2351 |
trx_free_for_background(trx); |
| 2352 |
|
| 2353 |
rw_lock_x_unlock(dict_operation_lock); |
| 2354 |
|
| 2355 |
dict_stats_snapshot_free(table); |
| 2356 |
|
| 2357 |
return (ret); |
| 2358 |
} |
| 2359 |
|
| 2360 |
/** Called for the row that is selected by |
| 2361 |
SELECT ... FROM mysql.innodb_table_stats WHERE table='...' |
| 2362 |
The second argument is a pointer to the table and the fetched stats are |
| 2363 |
written to it. |
| 2364 |
@return non-NULL dummy */ |
| 2365 |
static bool dict_stats_fetch_table_stats_step( |
| 2366 |
void *node_void, /*!< in: select node */ |
| 2367 |
void *table_void) /*!< out: table */ |
| 2368 |
{ |
| 2369 |
sel_node_t *node = (sel_node_t *)node_void; |
| 2370 |
dict_table_t *table = (dict_table_t *)table_void; |
| 2371 |
que_common_t *cnode; |
| 2372 |
int i; |
| 2373 |
|
| 2374 |
/* this should loop exactly 3 times - for |
| 2375 |
n_rows,clustered_index_size,sum_of_other_index_sizes */ |
| 2376 |
for (cnode = static_cast<que_common_t *>(node->select_list), i = 0; |
| 2377 |
cnode != nullptr; |
| 2378 |
cnode = static_cast<que_common_t *>(que_node_get_next(cnode)), i++) { |
| 2379 |
const byte *data; |
| 2380 |
dfield_t *dfield = que_node_get_val(cnode); |
| 2381 |
dtype_t *type = dfield_get_type(dfield); |
| 2382 |
ulint len = dfield_get_len(dfield); |
| 2383 |
|
| 2384 |
data = static_cast<const byte *>(dfield_get_data(dfield)); |
| 2385 |
|
| 2386 |
switch (i) { |
| 2387 |
case 0: /* mysql.innodb_table_stats.n_rows */ |
| 2388 |
|
| 2389 |
ut_a(dtype_get_mtype(type) == DATA_INT); |
| 2390 |
ut_a(len == 8); |
| 2391 |
|
| 2392 |
table->stat_n_rows = mach_read_from_8(data); |
| 2393 |
|
| 2394 |
break; |
| 2395 |
|
| 2396 |
case 1: /* mysql.innodb_table_stats.clustered_index_size */ |
| 2397 |
|
| 2398 |
ut_a(dtype_get_mtype(type) == DATA_INT); |
| 2399 |
ut_a(len == 8); |
| 2400 |
|
| 2401 |
table->stat_clustered_index_size = (ulint)mach_read_from_8(data); |
| 2402 |
|
| 2403 |
break; |
| 2404 |
|
| 2405 |
case 2: /* mysql.innodb_table_stats.sum_of_other_index_sizes */ |
| 2406 |
|
| 2407 |
ut_a(dtype_get_mtype(type) == DATA_INT); |
| 2408 |
ut_a(len == 8); |
| 2409 |
|
| 2410 |
table->stat_sum_of_other_index_sizes = (ulint)mach_read_from_8(data); |
| 2411 |
|
| 2412 |
break; |
| 2413 |
|
| 2414 |
default: |
| 2415 |
|
| 2416 |
/* someone changed SELECT |
| 2417 |
n_rows,clustered_index_size,sum_of_other_index_sizes |
| 2418 |
to select more columns from innodb_table_stats without |
| 2419 |
adjusting here */ |
| 2420 |
ut_error; |
| 2421 |
} |
| 2422 |
} |
| 2423 |
|
| 2424 |
/* if i < 3 this means someone changed the |
| 2425 |
SELECT n_rows,clustered_index_size,sum_of_other_index_sizes |
| 2426 |
to select less columns from innodb_table_stats without adjusting here; |
| 2427 |
if i > 3 we would have ut_error'ed earlier */ |
| 2428 |
ut_a(i == 3 /*n_rows,clustered_index_size,sum_of_other_index_sizes*/); |
| 2429 |
|
| 2430 |
/* XXX this is not used but returning non-NULL is necessary */ |
| 2431 |
return true; |
| 2432 |
} |
| 2433 |
|
| 2434 |
/** Aux struct used to pass a table and a boolean to |
| 2435 |
dict_stats_fetch_index_stats_step(). */ |
| 2436 |
struct index_fetch_t { |
| 2437 |
dict_table_t *table; /*!< table whose indexes are to be modified */ |
| 2438 |
bool stats_were_modified; /*!< will be set to true if at |
| 2439 |
least one index stats were modified */ |
| 2440 |
}; |
| 2441 |
|
| 2442 |
/** Called for the rows that are selected by |
| 2443 |
SELECT ... FROM mysql.innodb_index_stats WHERE table='...' |
| 2444 |
The second argument is a pointer to the table and the fetched stats are |
| 2445 |
written to its indexes. |
| 2446 |
Let a table has N indexes and each index has Ui unique columns for i=1..N, |
| 2447 |
then mysql.innodb_index_stats will have SUM(Ui) i=1..N rows for that table. |
| 2448 |
So this function will be called SUM(Ui) times where SUM(Ui) is of magnitude |
| 2449 |
N*AVG(Ui). In each call it searches for the currently fetched index into |
| 2450 |
table->indexes linearly, assuming this list is not sorted. Thus, overall, |
| 2451 |
fetching all indexes' stats from mysql.innodb_index_stats is O(N^2) where N |
| 2452 |
is the number of indexes. |
| 2453 |
This can be improved if we sort table->indexes in a temporary area just once |
| 2454 |
and then search in that sorted list. Then the complexity will be O(N*log(N)). |
| 2455 |
We assume a table will not have more than 100 indexes, so we go with the |
| 2456 |
simpler N^2 algorithm. |
| 2457 |
@return non-NULL dummy */ |
| 2458 |
static bool dict_stats_fetch_index_stats_step( |
| 2459 |
void *node_void, /*!< in: select node */ |
| 2460 |
void *arg_void) /*!< out: table + a flag that tells if we |
| 2461 |
modified anything */ |
| 2462 |
{ |
| 2463 |
sel_node_t *node = (sel_node_t *)node_void; |
| 2464 |
index_fetch_t *arg = (index_fetch_t *)arg_void; |
| 2465 |
dict_table_t *table = arg->table; |
| 2466 |
dict_index_t *index = nullptr; |
| 2467 |
que_common_t *cnode; |
| 2468 |
const char *stat_name = nullptr; |
| 2469 |
ulint stat_name_len = ULINT_UNDEFINED; |
| 2470 |
uint64_t stat_value = UINT64_UNDEFINED; |
| 2471 |
uint64_t sample_size = UINT64_UNDEFINED; |
| 2472 |
int i; |
| 2473 |
|
| 2474 |
/* this should loop exactly 4 times - for the columns that |
| 2475 |
were selected: index_name,stat_name,stat_value,sample_size */ |
| 2476 |
for (cnode = static_cast<que_common_t *>(node->select_list), i = 0; |
| 2477 |
cnode != nullptr; |
| 2478 |
cnode = static_cast<que_common_t *>(que_node_get_next(cnode)), i++) { |
| 2479 |
const byte *data; |
| 2480 |
dfield_t *dfield = que_node_get_val(cnode); |
| 2481 |
dtype_t *type = dfield_get_type(dfield); |
| 2482 |
ulint len = dfield_get_len(dfield); |
| 2483 |
|
| 2484 |
data = static_cast<const byte *>(dfield_get_data(dfield)); |
| 2485 |
|
| 2486 |
switch (i) { |
| 2487 |
case 0: /* mysql.innodb_index_stats.index_name */ |
| 2488 |
|
| 2489 |
ut_a(dtype_get_mtype(type) == DATA_VARMYSQL); |
| 2490 |
|
| 2491 |
/* search for index in table's indexes whose name |
| 2492 |
matches data; the fetched index name is in data, |
| 2493 |
has no terminating '\0' and has length len */ |
| 2494 |
for (index = table->first_index(); index != nullptr; |
| 2495 |
index = index->next()) { |
| 2496 |
if (index->is_committed() && strlen(index->name) == len && |
| 2497 |
memcmp(index->name, data, len) == 0) { |
| 2498 |
/* the corresponding index was found */ |
| 2499 |
break; |
| 2500 |
} |
| 2501 |
} |
| 2502 |
|
| 2503 |
/* if index is NULL here this means that |
| 2504 |
mysql.innodb_index_stats contains more rows than the |
| 2505 |
number of indexes in the table; this is ok, we just |
| 2506 |
return ignoring those extra rows; in other words |
| 2507 |
dict_stats_fetch_index_stats_step() has been called |
| 2508 |
for a row from index_stats with unknown index_name |
| 2509 |
column */ |
| 2510 |
if (index == nullptr) { |
| 2511 |
return true; |
| 2512 |
} |
| 2513 |
|
| 2514 |
break; |
| 2515 |
|
| 2516 |
case 1: /* mysql.innodb_index_stats.stat_name */ |
| 2517 |
|
| 2518 |
ut_a(dtype_get_mtype(type) == DATA_VARMYSQL); |
| 2519 |
|
| 2520 |
ut_a(index != nullptr); |
| 2521 |
|
| 2522 |
stat_name = (const char *)data; |
| 2523 |
stat_name_len = len; |
| 2524 |
|
| 2525 |
break; |
| 2526 |
|
| 2527 |
case 2: /* mysql.innodb_index_stats.stat_value */ |
| 2528 |
|
| 2529 |
ut_a(dtype_get_mtype(type) == DATA_INT); |
| 2530 |
ut_a(len == 8); |
| 2531 |
|
| 2532 |
ut_a(index != nullptr); |
| 2533 |
ut_a(stat_name != nullptr); |
| 2534 |
ut_a(stat_name_len != ULINT_UNDEFINED); |
| 2535 |
|
| 2536 |
stat_value = mach_read_from_8(data); |
| 2537 |
|
| 2538 |
break; |
| 2539 |
|
| 2540 |
case 3: /* mysql.innodb_index_stats.sample_size */ |
| 2541 |
|
| 2542 |
ut_a(dtype_get_mtype(type) == DATA_INT); |
| 2543 |
ut_a(len == 8 || len == UNIV_SQL_NULL); |
| 2544 |
|
| 2545 |
ut_a(index != nullptr); |
| 2546 |
ut_a(stat_name != nullptr); |
| 2547 |
ut_a(stat_name_len != ULINT_UNDEFINED); |
| 2548 |
ut_a(stat_value != UINT64_UNDEFINED); |
| 2549 |
|
| 2550 |
if (len == UNIV_SQL_NULL) { |
| 2551 |
break; |
| 2552 |
} |
| 2553 |
/* else */ |
| 2554 |
|
| 2555 |
sample_size = mach_read_from_8(data); |
| 2556 |
|
| 2557 |
break; |
| 2558 |
|
| 2559 |
default: |
| 2560 |
|
| 2561 |
/* someone changed |
| 2562 |
SELECT index_name,stat_name,stat_value,sample_size |
| 2563 |
to select more columns from innodb_index_stats without |
| 2564 |
adjusting here */ |
| 2565 |
ut_error; |
| 2566 |
} |
| 2567 |
} |
| 2568 |
|
| 2569 |
/* if i < 4 this means someone changed the |
| 2570 |
SELECT index_name,stat_name,stat_value,sample_size |
| 2571 |
to select less columns from innodb_index_stats without adjusting here; |
| 2572 |
if i > 4 we would have ut_error'ed earlier */ |
| 2573 |
ut_a(i == 4 /* index_name,stat_name,stat_value,sample_size */); |
| 2574 |
|
| 2575 |
ut_a(index != nullptr); |
| 2576 |
ut_a(stat_name != nullptr); |
| 2577 |
ut_a(stat_name_len != ULINT_UNDEFINED); |
| 2578 |
ut_a(stat_value != UINT64_UNDEFINED); |
| 2579 |
/* sample_size could be UINT64_UNDEFINED here, if it is NULL */ |
| 2580 |
|
| 2581 |
#define PFX "n_diff_pfx" |
| 2582 |
#define PFX_LEN 10 |
| 2583 |
|
| 2584 |
if (stat_name_len == 4 /* strlen("size") */ |
| 2585 |
&& native_strncasecmp("size", stat_name, stat_name_len) == 0) { |
| 2586 |
index->stat_index_size = (ulint)stat_value; |
| 2587 |
arg->stats_were_modified = true; |
| 2588 |
} else if (stat_name_len == 12 /* strlen("n_leaf_pages") */ |
| 2589 |
&& native_strncasecmp("n_leaf_pages", stat_name, stat_name_len) == |
| 2590 |
0) { |
| 2591 |
index->stat_n_leaf_pages = (ulint)stat_value; |
| 2592 |
arg->stats_were_modified = true; |
| 2593 |
} else if (stat_name_len > PFX_LEN /* e.g. stat_name=="n_diff_pfx01" */ |
| 2594 |
&& native_strncasecmp(PFX, stat_name, PFX_LEN) == 0) { |
| 2595 |
const char *num_ptr; |
| 2596 |
unsigned long n_pfx; |
| 2597 |
|
| 2598 |
/* point num_ptr into "1" from "n_diff_pfx12..." */ |
| 2599 |
num_ptr = stat_name + PFX_LEN; |
| 2600 |
|
| 2601 |
/* stat_name should have exactly 2 chars appended to PFX |
| 2602 |
and they should be digits */ |
| 2603 |
if (stat_name_len != PFX_LEN + 2 || num_ptr[0] < '0' || num_ptr[0] > '9' || |
| 2604 |
num_ptr[1] < '0' || num_ptr[1] > '9') { |
| 2605 |
char db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 2606 |
char table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 2607 |
|
| 2608 |
dict_fs2utf8(table->name.m_name, db_utf8mb3, sizeof(db_utf8mb3), |
| 2609 |
table_utf8mb3, sizeof(table_utf8mb3)); |
| 2610 |
|
| 2611 |
ib::info out(ER_IB_MSG_1218); |
| 2612 |
|
| 2613 |
out << "Ignoring strange row from " << INDEX_STATS_NAME_PRINT |
| 2614 |
<< " WHERE" |
| 2615 |
" database_name = '" |
| 2616 |
<< db_utf8mb3 << "' AND table_name = '" << table_utf8mb3 |
| 2617 |
<< "' AND index_name = '" << index->name() << "' AND stat_name = '"; |
| 2618 |
out.write(stat_name, stat_name_len); |
| 2619 |
out << "'; because stat_name is malformed"; |
| 2620 |
return true; |
| 2621 |
} |
| 2622 |
/* else */ |
| 2623 |
|
| 2624 |
/* extract 12 from "n_diff_pfx12..." into n_pfx |
| 2625 |
note that stat_name does not have a terminating '\0' */ |
| 2626 |
n_pfx = (num_ptr[0] - '0') * 10 + (num_ptr[1] - '0'); |
| 2627 |
|
| 2628 |
ulint n_uniq = index->n_uniq; |
| 2629 |
|
| 2630 |
if (n_pfx == 0 || n_pfx > n_uniq) { |
| 2631 |
char db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 2632 |
char table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 2633 |
|
| 2634 |
dict_fs2utf8(table->name.m_name, db_utf8mb3, sizeof(db_utf8mb3), |
| 2635 |
table_utf8mb3, sizeof(table_utf8mb3)); |
| 2636 |
|
| 2637 |
ib::info out(ER_IB_MSG_1219); |
| 2638 |
|
| 2639 |
out << "Ignoring strange row from " << INDEX_STATS_NAME_PRINT |
| 2640 |
<< " WHERE" |
| 2641 |
" database_name = '" |
| 2642 |
<< db_utf8mb3 << "' AND table_name = '" << table_utf8mb3 |
| 2643 |
<< "' AND index_name = '" << index->name() << "' AND stat_name = '"; |
| 2644 |
out.write(stat_name, stat_name_len); |
| 2645 |
out << "'; because stat_name is out of range, the index" |
| 2646 |
" has " |
| 2647 |
<< n_uniq << " unique columns"; |
| 2648 |
|
| 2649 |
return true; |
| 2650 |
} |
| 2651 |
/* else */ |
| 2652 |
|
| 2653 |
index->stat_n_diff_key_vals[n_pfx - 1] = stat_value; |
| 2654 |
|
| 2655 |
if (sample_size != UINT64_UNDEFINED) { |
| 2656 |
index->stat_n_sample_sizes[n_pfx - 1] = sample_size; |
| 2657 |
} else { |
| 2658 |
/* hmm, strange... the user must have UPDATEd the |
| 2659 |
table manually and SET sample_size = NULL */ |
| 2660 |
index->stat_n_sample_sizes[n_pfx - 1] = 0; |
| 2661 |
} |
| 2662 |
|
| 2663 |
index->stat_n_non_null_key_vals[n_pfx - 1] = 0; |
| 2664 |
|
| 2665 |
arg->stats_were_modified = true; |
| 2666 |
} else { |
| 2667 |
/* silently ignore rows with unknown stat_name, the |
| 2668 |
user may have developed her own stats */ |
| 2669 |
} |
| 2670 |
|
| 2671 |
/* XXX this is not used but returning non-NULL is necessary */ |
| 2672 |
return true; |
| 2673 |
} |
| 2674 |
|
| 2675 |
/** Read table's statistics from the persistent statistics storage. |
| 2676 |
@return DB_SUCCESS or error code */ |
| 2677 |
static dberr_t dict_stats_fetch_from_ps( |
| 2678 |
dict_table_t *table) /*!< in/out: table */ |
| 2679 |
{ |
| 2680 |
index_fetch_t index_fetch_arg; |
| 2681 |
trx_t *trx; |
| 2682 |
pars_info_t *pinfo; |
| 2683 |
dberr_t ret; |
| 2684 |
char db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 2685 |
char table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 2686 |
|
| 2687 |
ut_ad(!dict_sys_mutex_own()); |
| 2688 |
|
| 2689 |
/* Initialize all stats to dummy values before fetching because if |
| 2690 |
the persistent storage contains incomplete stats (e.g. missing stats |
| 2691 |
for some index) then we would end up with (partially) uninitialized |
| 2692 |
stats. */ |
| 2693 |
dict_stats_empty_table(table); |
| 2694 |
|
| 2695 |
trx = trx_allocate_for_background(); |
| 2696 |
|
| 2697 |
/* Use 'read-uncommitted' so that the SELECTs we execute |
| 2698 |
do not get blocked in case some user has locked the rows we |
| 2699 |
are SELECTing */ |
| 2700 |
|
| 2701 |
trx->isolation_level = TRX_ISO_READ_UNCOMMITTED; |
| 2702 |
|
| 2703 |
if (srv_read_only_mode) { |
| 2704 |
trx_start_internal_read_only(trx, UT_LOCATION_HERE); |
| 2705 |
} else { |
| 2706 |
trx_start_internal(trx, UT_LOCATION_HERE); |
| 2707 |
} |
| 2708 |
|
| 2709 |
dict_fs2utf8(table->name.m_name, db_utf8mb3, sizeof(db_utf8mb3), |
| 2710 |
table_utf8mb3, sizeof(table_utf8mb3)); |
| 2711 |
|
| 2712 |
pinfo = pars_info_create(); |
| 2713 |
|
| 2714 |
pars_info_add_str_literal(pinfo, "database_name", db_utf8mb3); |
| 2715 |
|
| 2716 |
pars_info_add_str_literal(pinfo, "table_name", table_utf8mb3); |
| 2717 |
|
| 2718 |
pars_info_bind_function(pinfo, "fetch_table_stats_step", |
| 2719 |
dict_stats_fetch_table_stats_step, table); |
| 2720 |
|
| 2721 |
index_fetch_arg.table = table; |
| 2722 |
index_fetch_arg.stats_were_modified = false; |
| 2723 |
pars_info_bind_function(pinfo, "fetch_index_stats_step", |
| 2724 |
dict_stats_fetch_index_stats_step, &index_fetch_arg); |
| 2725 |
|
| 2726 |
ret = que_eval_sql(pinfo, |
| 2727 |
"PROCEDURE FETCH_STATS () IS\n" |
| 2728 |
"found INT;\n" |
| 2729 |
"DECLARE FUNCTION fetch_table_stats_step;\n" |
| 2730 |
"DECLARE FUNCTION fetch_index_stats_step;\n" |
| 2731 |
"DECLARE CURSOR table_stats_cur IS\n" |
| 2732 |
" SELECT\n" |
| 2733 |
/* if you change the selected fields, be |
| 2734 |
sure to adjust |
| 2735 |
dict_stats_fetch_table_stats_step() */ |
| 2736 |
" n_rows,\n" |
| 2737 |
" clustered_index_size,\n" |
| 2738 |
" sum_of_other_index_sizes\n" |
| 2739 |
" FROM \"" TABLE_STATS_NAME |
| 2740 |
"\"\n" |
| 2741 |
" WHERE\n" |
| 2742 |
" database_name = :database_name AND\n" |
| 2743 |
" table_name = :table_name;\n" |
| 2744 |
"DECLARE CURSOR index_stats_cur IS\n" |
| 2745 |
" SELECT\n" |
| 2746 |
/* if you change the selected fields, be |
| 2747 |
sure to adjust |
| 2748 |
dict_stats_fetch_index_stats_step() */ |
| 2749 |
" index_name,\n" |
| 2750 |
" stat_name,\n" |
| 2751 |
" stat_value,\n" |
| 2752 |
" sample_size\n" |
| 2753 |
" FROM \"" INDEX_STATS_NAME |
| 2754 |
"\"\n" |
| 2755 |
" WHERE\n" |
| 2756 |
" database_name = :database_name AND\n" |
| 2757 |
" table_name = :table_name;\n" |
| 2758 |
|
| 2759 |
"BEGIN\n" |
| 2760 |
|
| 2761 |
"OPEN table_stats_cur;\n" |
| 2762 |
"FETCH table_stats_cur INTO\n" |
| 2763 |
" fetch_table_stats_step();\n" |
| 2764 |
"IF (SQL % NOTFOUND) THEN\n" |
| 2765 |
" CLOSE table_stats_cur;\n" |
| 2766 |
" RETURN;\n" |
| 2767 |
"END IF;\n" |
| 2768 |
"CLOSE table_stats_cur;\n" |
| 2769 |
|
| 2770 |
"OPEN index_stats_cur;\n" |
| 2771 |
"found := 1;\n" |
| 2772 |
"WHILE found = 1 LOOP\n" |
| 2773 |
" FETCH index_stats_cur INTO\n" |
| 2774 |
" fetch_index_stats_step();\n" |
| 2775 |
" IF (SQL % NOTFOUND) THEN\n" |
| 2776 |
" found := 0;\n" |
| 2777 |
" END IF;\n" |
| 2778 |
"END LOOP;\n" |
| 2779 |
"CLOSE index_stats_cur;\n" |
| 2780 |
|
| 2781 |
"END;", |
| 2782 |
trx); |
| 2783 |
/* pinfo is freed by que_eval_sql() */ |
| 2784 |
|
| 2785 |
trx_commit_for_mysql(trx); |
| 2786 |
|
| 2787 |
trx_free_for_background(trx); |
| 2788 |
|
| 2789 |
if (!index_fetch_arg.stats_were_modified) { |
| 2790 |
return (DB_STATS_DO_NOT_EXIST); |
| 2791 |
} |
| 2792 |
|
| 2793 |
return (ret); |
| 2794 |
} |
| 2795 |
|
| 2796 |
/** Fetches or calculates new estimates for index statistics. */ |
| 2797 |
void dict_stats_update_for_index(dict_index_t *index) /*!< in/out: index */ |
| 2798 |
{ |
| 2799 |
DBUG_TRACE; |
| 2800 |
|
| 2801 |
ut_ad(!dict_sys_mutex_own()); |
| 2802 |
|
| 2803 |
if (dict_stats_is_persistent_enabled(index->table)) { |
| 2804 |
dict_table_stats_lock(index->table, RW_X_LATCH); |
| 2805 |
dict_stats_analyze_index(index); |
| 2806 |
dict_table_stats_unlock(index->table, RW_X_LATCH); |
| 2807 |
index_id_t index_id(index->space, index->id); |
| 2808 |
dict_stats_save(index->table, &index_id); |
| 2809 |
return; |
| 2810 |
} |
| 2811 |
|
| 2812 |
dict_table_stats_lock(index->table, RW_X_LATCH); |
| 2813 |
dict_stats_update_transient_for_index(index); |
| 2814 |
dict_table_stats_unlock(index->table, RW_X_LATCH); |
| 2815 |
} |
| 2816 |
|
| 2817 |
/** Calculates new estimates for table and index statistics. The statistics |
| 2818 |
are used in query optimization. |
| 2819 |
@return DB_SUCCESS or error code */ |
| 2820 |
dberr_t dict_stats_update(dict_table_t *table, /*!< in/out: table */ |
| 2821 |
dict_stats_upd_option_t stats_upd_option) |
| 2822 |
/*!< in: whether to (re) calc |
| 2823 |
the stats or to fetch them from |
| 2824 |
the persistent statistics |
| 2825 |
storage */ |
| 2826 |
{ |
| 2827 |
ut_ad(!dict_sys_mutex_own()); |
| 2828 |
|
| 2829 |
if (table->ibd_file_missing) { |
| 2830 |
if (!dict_table_is_discarded(table)) { |
| 2831 |
ib::warn(ER_IB_MSG_224) |
| 2832 |
<< "Cannot calculate statistics for table " << table->name |
| 2833 |
<< " because the .ibd file is missing. " << TROUBLESHOOTING_MSG; |
| 2834 |
} |
| 2835 |
dict_stats_empty_table(table); |
| 2836 |
return (DB_TABLESPACE_DELETED); |
| 2837 |
} else if (srv_force_recovery >= SRV_FORCE_NO_IBUF_MERGE) { |
| 2838 |
/* If we have set a high innodb_force_recovery level, do |
| 2839 |
not calculate statistics, as a badly corrupted index can |
| 2840 |
cause a crash in it. */ |
| 2841 |
dict_stats_empty_table(table); |
| 2842 |
return (DB_SUCCESS); |
| 2843 |
} |
| 2844 |
|
| 2845 |
switch (stats_upd_option) { |
| 2846 |
dberr_t err; |
| 2847 |
|
| 2848 |
case DICT_STATS_RECALC_PERSISTENT: |
| 2849 |
|
| 2850 |
if (srv_read_only_mode) { |
| 2851 |
break; |
| 2852 |
} |
| 2853 |
|
| 2854 |
/* wakes the last purge batch for exact recalculation */ |
| 2855 |
if (trx_sys->rseg_history_len.load() > 0) { |
| 2856 |
srv_wake_purge_thread_if_not_active(); |
| 2857 |
} |
| 2858 |
|
| 2859 |
/* Persistent recalculation requested, called from |
| 2860 |
1) ANALYZE TABLE, or |
| 2861 |
2) the auto recalculation background thread, or |
| 2862 |
3) open table if stats do not exist on disk and auto recalc |
| 2863 |
is enabled */ |
| 2864 |
|
| 2865 |
/* InnoDB internal tables (e.g. SYS_TABLES) cannot have |
| 2866 |
persistent stats enabled */ |
| 2867 |
ut_a(strchr(table->name.m_name, '/') != nullptr); |
| 2868 |
|
| 2869 |
err = dict_stats_update_persistent(table); |
| 2870 |
|
| 2871 |
if (err != DB_SUCCESS) { |
| 2872 |
return (err); |
| 2873 |
} |
| 2874 |
|
| 2875 |
return (dict_stats_save(table, nullptr)); |
| 2876 |
|
| 2877 |
case DICT_STATS_RECALC_TRANSIENT: |
| 2878 |
break; |
| 2879 |
|
| 2880 |
case DICT_STATS_EMPTY_TABLE: |
| 2881 |
|
| 2882 |
dict_stats_empty_table(table); |
| 2883 |
|
| 2884 |
/* If table is using persistent stats, |
| 2885 |
then save the stats on disk */ |
| 2886 |
|
| 2887 |
if (dict_stats_is_persistent_enabled(table)) { |
| 2888 |
return (dict_stats_save(table, nullptr)); |
| 2889 |
} |
| 2890 |
|
| 2891 |
return (DB_SUCCESS); |
| 2892 |
|
| 2893 |
case DICT_STATS_FETCH_ONLY_IF_NOT_IN_MEMORY: |
| 2894 |
|
| 2895 |
/* fetch requested, either fetch from persistent statistics |
| 2896 |
storage or use the old method */ |
| 2897 |
|
| 2898 |
if (table->stat_initialized) { |
| 2899 |
return (DB_SUCCESS); |
| 2900 |
} |
| 2901 |
|
| 2902 |
/* InnoDB internal tables (e.g. SYS_TABLES) cannot have |
| 2903 |
persistent stats enabled */ |
| 2904 |
ut_a(strchr(table->name.m_name, '/') != nullptr); |
| 2905 |
|
| 2906 |
/* Create a dummy table object with the same name and |
| 2907 |
indexes, suitable for fetching the stats into it. */ |
| 2908 |
dict_table_t *t = dict_stats_table_clone_create(table); |
| 2909 |
|
| 2910 |
err = dict_stats_fetch_from_ps(t); |
| 2911 |
|
| 2912 |
t->stats_last_recalc = table->stats_last_recalc; |
| 2913 |
t->stat_modified_counter = 0; |
| 2914 |
|
| 2915 |
switch (err) { |
| 2916 |
case DB_SUCCESS: |
| 2917 |
|
| 2918 |
dict_table_stats_lock(table, RW_X_LATCH); |
| 2919 |
|
| 2920 |
dict_stats_copy(table, t); |
| 2921 |
|
| 2922 |
dict_stats_assert_initialized(table); |
| 2923 |
|
| 2924 |
dict_table_stats_unlock(table, RW_X_LATCH); |
| 2925 |
|
| 2926 |
dict_stats_table_clone_free(t); |
| 2927 |
|
| 2928 |
return (DB_SUCCESS); |
| 2929 |
case DB_STATS_DO_NOT_EXIST: |
| 2930 |
|
| 2931 |
dict_stats_table_clone_free(t); |
| 2932 |
|
| 2933 |
if (srv_read_only_mode) { |
| 2934 |
break; |
| 2935 |
} |
| 2936 |
|
| 2937 |
if (dict_stats_auto_recalc_is_enabled(table)) { |
| 2938 |
return (dict_stats_update(table, DICT_STATS_RECALC_PERSISTENT)); |
| 2939 |
} |
| 2940 |
|
| 2941 |
ib::info(ER_IB_MSG_225) |
| 2942 |
<< "Trying to use table " << table->name |
| 2943 |
<< " which has persistent statistics enabled," |
| 2944 |
" but auto recalculation turned off and the" |
| 2945 |
" statistics do not exist in " TABLE_STATS_NAME_PRINT |
| 2946 |
" and " INDEX_STATS_NAME_PRINT |
| 2947 |
". Please either run \"ANALYZE TABLE " |
| 2948 |
<< table->name |
| 2949 |
<< ";\" manually or enable the" |
| 2950 |
" auto recalculation with \"ALTER TABLE " |
| 2951 |
<< table->name |
| 2952 |
<< " STATS_AUTO_RECALC=1;\"." |
| 2953 |
" InnoDB will now use transient statistics for " |
| 2954 |
<< table->name << "."; |
| 2955 |
|
| 2956 |
break; |
| 2957 |
default: |
| 2958 |
|
| 2959 |
dict_stats_table_clone_free(t); |
| 2960 |
|
| 2961 |
ib::error(ER_IB_MSG_226) |
| 2962 |
<< "Error fetching persistent statistics" |
| 2963 |
" for table " |
| 2964 |
<< table->name |
| 2965 |
<< " from " TABLE_STATS_NAME_PRINT " and " INDEX_STATS_NAME_PRINT |
| 2966 |
": " |
| 2967 |
<< ut_strerr(err) << ". Using transient stats method instead."; |
| 2968 |
|
| 2969 |
break; |
| 2970 |
} |
| 2971 |
/* no "default:" in order to produce a compilation warning |
| 2972 |
about unhandled enumeration value */ |
| 2973 |
} |
| 2974 |
|
| 2975 |
dict_table_stats_lock(table, RW_X_LATCH); |
| 2976 |
|
| 2977 |
dict_stats_update_transient(table); |
| 2978 |
|
| 2979 |
dict_table_stats_unlock(table, RW_X_LATCH); |
| 2980 |
|
| 2981 |
return (DB_SUCCESS); |
| 2982 |
} |
| 2983 |
|
| 2984 |
/** Removes the information for a particular index's stats from the persistent |
| 2985 |
storage if it exists and if there is data stored for this index. |
| 2986 |
This function creates its own trx and commits it. |
| 2987 |
A note from Marko why we cannot edit user and sys_* tables in one trx: |
| 2988 |
marko: The problem is that ibuf merges should be disabled while we are |
| 2989 |
rolling back dict transactions. |
| 2990 |
marko: If ibuf merges are not disabled, we need to scan the *.ibd files. |
| 2991 |
But we shouldn't open *.ibd files before we have rolled back dict |
| 2992 |
transactions and opened the SYS_* records for the *.ibd files. |
| 2993 |
@return DB_SUCCESS or error code */ |
| 2994 |
dberr_t dict_stats_drop_index( |
| 2995 |
const char *db_and_table, /*!< in: db and table, e.g. 'db/table' */ |
| 2996 |
const char *iname, /*!< in: index name */ |
| 2997 |
char *errstr, /*!< out: error message if != DB_SUCCESS |
| 2998 |
is returned */ |
| 2999 |
ulint errstr_sz) /*!< in: size of the errstr buffer */ |
| 3000 |
{ |
| 3001 |
char db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 3002 |
char table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 3003 |
pars_info_t *pinfo; |
| 3004 |
dberr_t ret; |
| 3005 |
|
| 3006 |
ut_ad(!dict_sys_mutex_own()); |
| 3007 |
|
| 3008 |
/* skip indexes whose table names do not contain a database name |
| 3009 |
e.g. if we are dropping an index from SYS_TABLES */ |
| 3010 |
if (strchr(db_and_table, '/') == nullptr) { |
| 3011 |
return (DB_SUCCESS); |
| 3012 |
} |
| 3013 |
|
| 3014 |
dict_fs2utf8(db_and_table, db_utf8mb3, sizeof(db_utf8mb3), table_utf8mb3, |
| 3015 |
sizeof(table_utf8mb3)); |
| 3016 |
|
| 3017 |
pinfo = pars_info_create(); |
| 3018 |
|
| 3019 |
pars_info_add_str_literal(pinfo, "database_name", db_utf8mb3); |
| 3020 |
|
| 3021 |
pars_info_add_str_literal(pinfo, "table_name", table_utf8mb3); |
| 3022 |
|
| 3023 |
pars_info_add_str_literal(pinfo, "index_name", iname); |
| 3024 |
|
| 3025 |
rw_lock_x_lock(dict_operation_lock, UT_LOCATION_HERE); |
| 3026 |
|
| 3027 |
ret = dict_stats_exec_sql(pinfo, |
| 3028 |
"PROCEDURE DROP_INDEX_STATS () IS\n" |
| 3029 |
"BEGIN\n" |
| 3030 |
"DELETE FROM \"" INDEX_STATS_NAME |
| 3031 |
"\" WHERE\n" |
| 3032 |
"database_name = :database_name AND\n" |
| 3033 |
"table_name = :table_name AND\n" |
| 3034 |
"index_name = :index_name;\n" |
| 3035 |
"END;\n", |
| 3036 |
nullptr); |
| 3037 |
|
| 3038 |
rw_lock_x_unlock(dict_operation_lock); |
| 3039 |
|
| 3040 |
if (ret == DB_STATS_DO_NOT_EXIST) { |
| 3041 |
ret = DB_SUCCESS; |
| 3042 |
} |
| 3043 |
|
| 3044 |
if (ret != DB_SUCCESS) { |
| 3045 |
snprintf( |
| 3046 |
errstr, errstr_sz, |
| 3047 |
"Unable to delete statistics for index %s" |
| 3048 |
" from %s%s: %s. They can be deleted later using" |
| 3049 |
" DELETE FROM %s WHERE" |
| 3050 |
" database_name = '%s' AND" |
| 3051 |
" table_name = '%s' AND" |
| 3052 |
" index_name = '%s';", |
| 3053 |
iname, INDEX_STATS_NAME_PRINT, |
| 3054 |
(ret == DB_LOCK_WAIT_TIMEOUT ? " because the rows are locked" : ""), |
| 3055 |
ut_strerr(ret), INDEX_STATS_NAME_PRINT, db_utf8mb3, table_utf8mb3, |
| 3056 |
iname); |
| 3057 |
|
| 3058 |
ut_print_timestamp(stderr); |
| 3059 |
fprintf(stderr, " InnoDB: %s\n", errstr); |
| 3060 |
} |
| 3061 |
|
| 3062 |
return (ret); |
| 3063 |
} |
| 3064 |
|
| 3065 |
/** Executes |
| 3066 |
DELETE FROM mysql.innodb_table_stats |
| 3067 |
WHERE database_name = '...' AND table_name = '...'; |
| 3068 |
Creates its own transaction and commits it. |
| 3069 |
@return DB_SUCCESS or error code */ |
| 3070 |
static inline dberr_t dict_stats_delete_from_table_stats( |
| 3071 |
const char *database_name, /*!< in: database name, e.g. 'db' */ |
| 3072 |
const char *table_name) /*!< in: table name, e.g. 'table' */ |
| 3073 |
{ |
| 3074 |
pars_info_t *pinfo; |
| 3075 |
dberr_t ret; |
| 3076 |
|
| 3077 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 3078 |
|
| 3079 |
pinfo = pars_info_create(); |
| 3080 |
|
| 3081 |
pars_info_add_str_literal(pinfo, "database_name", database_name); |
| 3082 |
pars_info_add_str_literal(pinfo, "table_name", table_name); |
| 3083 |
|
| 3084 |
ret = dict_stats_exec_sql(pinfo, |
| 3085 |
"PROCEDURE DELETE_FROM_TABLE_STATS () IS\n" |
| 3086 |
"BEGIN\n" |
| 3087 |
"DELETE FROM \"" TABLE_STATS_NAME |
| 3088 |
"\" WHERE\n" |
| 3089 |
"database_name = :database_name AND\n" |
| 3090 |
"table_name = :table_name;\n" |
| 3091 |
"END;\n", |
| 3092 |
nullptr); |
| 3093 |
|
| 3094 |
return (ret); |
| 3095 |
} |
| 3096 |
|
| 3097 |
/** Executes |
| 3098 |
DELETE FROM mysql.innodb_index_stats |
| 3099 |
WHERE database_name = '...' AND table_name = '...'; |
| 3100 |
Creates its own transaction and commits it. |
| 3101 |
@return DB_SUCCESS or error code */ |
| 3102 |
static inline dberr_t dict_stats_delete_from_index_stats( |
| 3103 |
const char *database_name, /*!< in: database name, e.g. 'db' */ |
| 3104 |
const char *table_name) /*!< in: table name, e.g. 'table' */ |
| 3105 |
{ |
| 3106 |
pars_info_t *pinfo; |
| 3107 |
dberr_t ret; |
| 3108 |
|
| 3109 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 3110 |
|
| 3111 |
pinfo = pars_info_create(); |
| 3112 |
|
| 3113 |
pars_info_add_str_literal(pinfo, "database_name", database_name); |
| 3114 |
pars_info_add_str_literal(pinfo, "table_name", table_name); |
| 3115 |
|
| 3116 |
ret = dict_stats_exec_sql(pinfo, |
| 3117 |
"PROCEDURE DELETE_FROM_INDEX_STATS () IS\n" |
| 3118 |
"BEGIN\n" |
| 3119 |
"DELETE FROM \"" INDEX_STATS_NAME |
| 3120 |
"\" WHERE\n" |
| 3121 |
"database_name = :database_name AND\n" |
| 3122 |
"table_name = :table_name;\n" |
| 3123 |
"END;\n", |
| 3124 |
nullptr); |
| 3125 |
|
| 3126 |
return (ret); |
| 3127 |
} |
| 3128 |
|
| 3129 |
/** Removes the statistics for a table and all of its indexes from the |
| 3130 |
persistent statistics storage if it exists and if there is data stored for |
| 3131 |
the table. This function creates its own transaction and commits it. |
| 3132 |
@return DB_SUCCESS or error code */ |
| 3133 |
dberr_t dict_stats_drop_table( |
| 3134 |
const char *db_and_table, /*!< in: db and table, e.g. 'db/table' */ |
| 3135 |
char *errstr, /*!< out: error message |
| 3136 |
if != DB_SUCCESS is returned */ |
| 3137 |
ulint errstr_sz) /*!< in: size of errstr buffer */ |
| 3138 |
{ |
| 3139 |
char db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 3140 |
char table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 3141 |
dberr_t ret; |
| 3142 |
|
| 3143 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 3144 |
/* WL#9536 TODO: Once caller don't hold dict sys mutex, clean |
| 3145 |
this and following(exit&enter) up */ |
| 3146 |
ut_ad(dict_sys_mutex_own()); |
| 3147 |
|
| 3148 |
/* skip tables that do not contain a database name |
| 3149 |
e.g. if we are dropping SYS_TABLES */ |
| 3150 |
if (strchr(db_and_table, '/') == nullptr) { |
| 3151 |
return (DB_SUCCESS); |
| 3152 |
} |
| 3153 |
|
| 3154 |
/* skip innodb_table_stats and innodb_index_stats themselves */ |
| 3155 |
if (strcmp(db_and_table, TABLE_STATS_NAME) == 0 || |
| 3156 |
strcmp(db_and_table, INDEX_STATS_NAME) == 0) { |
| 3157 |
return (DB_SUCCESS); |
| 3158 |
} |
| 3159 |
|
| 3160 |
dict_fs2utf8(db_and_table, db_utf8mb3, sizeof(db_utf8mb3), table_utf8mb3, |
| 3161 |
sizeof(table_utf8mb3)); |
| 3162 |
|
| 3163 |
dict_sys_mutex_exit(); |
| 3164 |
|
| 3165 |
ret = dict_stats_delete_from_table_stats(db_utf8mb3, table_utf8mb3); |
| 3166 |
|
| 3167 |
if (ret == DB_SUCCESS) { |
| 3168 |
ret = dict_stats_delete_from_index_stats(db_utf8mb3, table_utf8mb3); |
| 3169 |
} |
| 3170 |
|
| 3171 |
dict_sys_mutex_enter(); |
| 3172 |
|
| 3173 |
if (ret == DB_STATS_DO_NOT_EXIST) { |
| 3174 |
ret = DB_SUCCESS; |
| 3175 |
} |
| 3176 |
|
| 3177 |
if (ret != DB_SUCCESS) { |
| 3178 |
snprintf(errstr, errstr_sz, |
| 3179 |
"Unable to delete statistics for table %s.%s: %s." |
| 3180 |
" They can be deleted later using" |
| 3181 |
|
| 3182 |
" DELETE FROM %s WHERE" |
| 3183 |
" database_name = '%s' AND" |
| 3184 |
" table_name = '%s';" |
| 3185 |
|
| 3186 |
" DELETE FROM %s WHERE" |
| 3187 |
" database_name = '%s' AND" |
| 3188 |
" table_name = '%s';", |
| 3189 |
|
| 3190 |
db_utf8mb3, table_utf8mb3, ut_strerr(ret), |
| 3191 |
|
| 3192 |
INDEX_STATS_NAME_PRINT, db_utf8mb3, table_utf8mb3, |
| 3193 |
|
| 3194 |
TABLE_STATS_NAME_PRINT, db_utf8mb3, table_utf8mb3); |
| 3195 |
} |
| 3196 |
|
| 3197 |
return (ret); |
| 3198 |
} |
| 3199 |
|
| 3200 |
/** Executes |
| 3201 |
UPDATE mysql.innodb_table_stats SET |
| 3202 |
database_name = '...', table_name = '...' |
| 3203 |
WHERE database_name = '...' AND table_name = '...'; |
| 3204 |
Creates its own transaction and commits it. |
| 3205 |
@return DB_SUCCESS or error code */ |
| 3206 |
static inline dberr_t dict_stats_rename_table_in_table_stats( |
| 3207 |
const char *old_dbname_utf8mb3, /*!< in: database name, e.g. 'olddb' */ |
| 3208 |
const char *old_tablename_utf8mb3, /*!< in: table name, e.g. 'oldtable' */ |
| 3209 |
const char *new_dbname_utf8mb3, /*!< in: database name, e.g. 'newdb' */ |
| 3210 |
const char *new_tablename_utf8mb3) /*!< in: table name, e.g. 'newtable' */ |
| 3211 |
{ |
| 3212 |
pars_info_t *pinfo; |
| 3213 |
dberr_t ret; |
| 3214 |
|
| 3215 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 3216 |
|
| 3217 |
pinfo = pars_info_create(); |
| 3218 |
|
| 3219 |
pars_info_add_str_literal(pinfo, "old_dbname_utf8mb3", old_dbname_utf8mb3); |
| 3220 |
pars_info_add_str_literal(pinfo, "old_tablename_utf8mb3", |
| 3221 |
old_tablename_utf8mb3); |
| 3222 |
pars_info_add_str_literal(pinfo, "new_dbname_utf8mb3", new_dbname_utf8mb3); |
| 3223 |
pars_info_add_str_literal(pinfo, "new_tablename_utf8mb3", |
| 3224 |
new_tablename_utf8mb3); |
| 3225 |
|
| 3226 |
ret = dict_stats_exec_sql(pinfo, |
| 3227 |
"PROCEDURE RENAME_TABLE_IN_TABLE_STATS () IS\n" |
| 3228 |
"BEGIN\n" |
| 3229 |
"UPDATE \"" TABLE_STATS_NAME |
| 3230 |
"\" SET\n" |
| 3231 |
"database_name = :new_dbname_utf8mb3,\n" |
| 3232 |
"table_name = :new_tablename_utf8mb3\n" |
| 3233 |
"WHERE\n" |
| 3234 |
"database_name = :old_dbname_utf8mb3 AND\n" |
| 3235 |
"table_name = :old_tablename_utf8mb3;\n" |
| 3236 |
"END;\n", |
| 3237 |
nullptr); |
| 3238 |
|
| 3239 |
return (ret); |
| 3240 |
} |
| 3241 |
|
| 3242 |
/** Executes |
| 3243 |
UPDATE mysql.innodb_index_stats SET |
| 3244 |
database_name = '...', table_name = '...' |
| 3245 |
WHERE database_name = '...' AND table_name = '...'; |
| 3246 |
Creates its own transaction and commits it. |
| 3247 |
@return DB_SUCCESS or error code */ |
| 3248 |
static inline dberr_t dict_stats_rename_table_in_index_stats( |
| 3249 |
const char *old_dbname_utf8mb3, /*!< in: database name, e.g. 'olddb' */ |
| 3250 |
const char *old_tablename_utf8mb3, /*!< in: table name, e.g. 'oldtable' */ |
| 3251 |
const char *new_dbname_utf8mb3, /*!< in: database name, e.g. 'newdb' */ |
| 3252 |
const char *new_tablename_utf8mb3) /*!< in: table name, e.g. 'newtable' */ |
| 3253 |
{ |
| 3254 |
pars_info_t *pinfo; |
| 3255 |
dberr_t ret; |
| 3256 |
|
| 3257 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 3258 |
|
| 3259 |
pinfo = pars_info_create(); |
| 3260 |
|
| 3261 |
pars_info_add_str_literal(pinfo, "old_dbname_utf8mb3", old_dbname_utf8mb3); |
| 3262 |
pars_info_add_str_literal(pinfo, "old_tablename_utf8mb3", |
| 3263 |
old_tablename_utf8mb3); |
| 3264 |
pars_info_add_str_literal(pinfo, "new_dbname_utf8mb3", new_dbname_utf8mb3); |
| 3265 |
pars_info_add_str_literal(pinfo, "new_tablename_utf8mb3", |
| 3266 |
new_tablename_utf8mb3); |
| 3267 |
|
| 3268 |
ret = dict_stats_exec_sql(pinfo, |
| 3269 |
"PROCEDURE RENAME_TABLE_IN_INDEX_STATS () IS\n" |
| 3270 |
"BEGIN\n" |
| 3271 |
"UPDATE \"" INDEX_STATS_NAME |
| 3272 |
"\" SET\n" |
| 3273 |
"database_name = :new_dbname_utf8mb3,\n" |
| 3274 |
"table_name = :new_tablename_utf8mb3\n" |
| 3275 |
"WHERE\n" |
| 3276 |
"database_name = :old_dbname_utf8mb3 AND\n" |
| 3277 |
"table_name = :old_tablename_utf8mb3;\n" |
| 3278 |
"END;\n", |
| 3279 |
nullptr); |
| 3280 |
|
| 3281 |
return (ret); |
| 3282 |
} |
| 3283 |
|
| 3284 |
/** Renames a table in InnoDB persistent stats storage. |
| 3285 |
This function creates its own transaction and commits it. |
| 3286 |
@return DB_SUCCESS or error code */ |
| 3287 |
dberr_t dict_stats_rename_table( |
| 3288 |
const char *old_name, /*!< in: old name, e.g. 'db/table' */ |
| 3289 |
const char *new_name, /*!< in: new name, e.g. 'db/table' */ |
| 3290 |
char *errstr, /*!< out: error string if != DB_SUCCESS |
| 3291 |
is returned */ |
| 3292 |
size_t errstr_sz) /*!< in: errstr size */ |
| 3293 |
{ |
| 3294 |
char old_db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 3295 |
char new_db_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 3296 |
char old_table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 3297 |
char new_table_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 3298 |
dberr_t ret; |
| 3299 |
|
| 3300 |
ut_ad(!rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 3301 |
|
| 3302 |
/* skip innodb_table_stats and innodb_index_stats themselves */ |
| 3303 |
if (strcmp(old_name, TABLE_STATS_NAME) == 0 || |
| 3304 |
strcmp(old_name, INDEX_STATS_NAME) == 0 || |
| 3305 |
strcmp(new_name, TABLE_STATS_NAME) == 0 || |
| 3306 |
strcmp(new_name, INDEX_STATS_NAME) == 0) { |
| 3307 |
return (DB_SUCCESS); |
| 3308 |
} |
| 3309 |
|
| 3310 |
dict_fs2utf8(old_name, old_db_utf8mb3, sizeof(old_db_utf8mb3), |
| 3311 |
old_table_utf8mb3, sizeof(old_table_utf8mb3)); |
| 3312 |
|
| 3313 |
dict_fs2utf8(new_name, new_db_utf8mb3, sizeof(new_db_utf8mb3), |
| 3314 |
new_table_utf8mb3, sizeof(new_table_utf8mb3)); |
| 3315 |
|
| 3316 |
rw_lock_x_lock(dict_operation_lock, UT_LOCATION_HERE); |
| 3317 |
|
| 3318 |
ulint n_attempts = 0; |
| 3319 |
do { |
| 3320 |
n_attempts++; |
| 3321 |
|
| 3322 |
ret = dict_stats_rename_table_in_table_stats( |
| 3323 |
old_db_utf8mb3, old_table_utf8mb3, new_db_utf8mb3, new_table_utf8mb3); |
| 3324 |
|
| 3325 |
if (ret == DB_DUPLICATE_KEY) { |
| 3326 |
dict_stats_delete_from_table_stats(new_db_utf8mb3, new_table_utf8mb3); |
| 3327 |
} |
| 3328 |
|
| 3329 |
if (ret == DB_STATS_DO_NOT_EXIST) { |
| 3330 |
ret = DB_SUCCESS; |
| 3331 |
} |
| 3332 |
|
| 3333 |
if (ret != DB_SUCCESS) { |
| 3334 |
rw_lock_x_unlock(dict_operation_lock); |
| 3335 |
std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 3336 |
rw_lock_x_lock(dict_operation_lock, UT_LOCATION_HERE); |
| 3337 |
} |
| 3338 |
} while ((ret == DB_DEADLOCK || ret == DB_DUPLICATE_KEY || |
| 3339 |
ret == DB_LOCK_WAIT_TIMEOUT) && |
| 3340 |
n_attempts < 5); |
| 3341 |
|
| 3342 |
if (ret != DB_SUCCESS) { |
| 3343 |
snprintf(errstr, errstr_sz, |
| 3344 |
"Unable to rename statistics from" |
| 3345 |
" %s.%s to %s.%s in %s: %s." |
| 3346 |
" They can be renamed later using" |
| 3347 |
|
| 3348 |
" UPDATE %s SET" |
| 3349 |
" database_name = '%s'," |
| 3350 |
" table_name = '%s'" |
| 3351 |
" WHERE" |
| 3352 |
" database_name = '%s' AND" |
| 3353 |
" table_name = '%s';", |
| 3354 |
|
| 3355 |
old_db_utf8mb3, old_table_utf8mb3, new_db_utf8mb3, |
| 3356 |
new_table_utf8mb3, TABLE_STATS_NAME_PRINT, ut_strerr(ret), |
| 3357 |
|
| 3358 |
TABLE_STATS_NAME_PRINT, new_db_utf8mb3, new_table_utf8mb3, |
| 3359 |
old_db_utf8mb3, old_table_utf8mb3); |
| 3360 |
rw_lock_x_unlock(dict_operation_lock); |
| 3361 |
return (ret); |
| 3362 |
} |
| 3363 |
/* else */ |
| 3364 |
|
| 3365 |
n_attempts = 0; |
| 3366 |
do { |
| 3367 |
n_attempts++; |
| 3368 |
|
| 3369 |
ret = dict_stats_rename_table_in_index_stats( |
| 3370 |
old_db_utf8mb3, old_table_utf8mb3, new_db_utf8mb3, new_table_utf8mb3); |
| 3371 |
|
| 3372 |
if (ret == DB_DUPLICATE_KEY) { |
| 3373 |
dict_stats_delete_from_index_stats(new_db_utf8mb3, new_table_utf8mb3); |
| 3374 |
} |
| 3375 |
|
| 3376 |
if (ret == DB_STATS_DO_NOT_EXIST) { |
| 3377 |
ret = DB_SUCCESS; |
| 3378 |
} |
| 3379 |
|
| 3380 |
if (ret != DB_SUCCESS) { |
| 3381 |
rw_lock_x_unlock(dict_operation_lock); |
| 3382 |
std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 3383 |
rw_lock_x_lock(dict_operation_lock, UT_LOCATION_HERE); |
| 3384 |
} |
| 3385 |
} while ((ret == DB_DEADLOCK || ret == DB_DUPLICATE_KEY || |
| 3386 |
ret == DB_LOCK_WAIT_TIMEOUT) && |
| 3387 |
n_attempts < 5); |
| 3388 |
|
| 3389 |
rw_lock_x_unlock(dict_operation_lock); |
| 3390 |
|
| 3391 |
if (ret != DB_SUCCESS) { |
| 3392 |
snprintf(errstr, errstr_sz, |
| 3393 |
"Unable to rename statistics from" |
| 3394 |
" %s.%s to %s.%s in %s: %s." |
| 3395 |
" They can be renamed later using" |
| 3396 |
|
| 3397 |
" UPDATE %s SET" |
| 3398 |
" database_name = '%s'," |
| 3399 |
" table_name = '%s'" |
| 3400 |
" WHERE" |
| 3401 |
" database_name = '%s' AND" |
| 3402 |
" table_name = '%s';", |
| 3403 |
|
| 3404 |
old_db_utf8mb3, old_table_utf8mb3, new_db_utf8mb3, |
| 3405 |
new_table_utf8mb3, INDEX_STATS_NAME_PRINT, ut_strerr(ret), |
| 3406 |
|
| 3407 |
INDEX_STATS_NAME_PRINT, new_db_utf8mb3, new_table_utf8mb3, |
| 3408 |
old_db_utf8mb3, old_table_utf8mb3); |
| 3409 |
} |
| 3410 |
|
| 3411 |
return (ret); |
| 3412 |
} |
| 3413 |
|
| 3414 |
/** Renames an index in InnoDB persistent stats storage. |
| 3415 |
This function creates its own transaction and commits it. |
| 3416 |
@return DB_SUCCESS or error code. DB_STATS_DO_NOT_EXIST will be returned |
| 3417 |
if the persistent stats do not exist. */ |
| 3418 |
dberr_t dict_stats_rename_index( |
| 3419 |
const dict_table_t *table, /*!< in: table whose index |
| 3420 |
is renamed */ |
| 3421 |
const char *old_index_name, /*!< in: old index name */ |
| 3422 |
const char *new_index_name) /*!< in: new index name */ |
| 3423 |
{ |
| 3424 |
rw_lock_x_lock(dict_operation_lock, UT_LOCATION_HERE); |
| 3425 |
|
| 3426 |
char dbname_utf8mb3[dict_name::MAX_DB_UTF8MB3_LEN]; |
| 3427 |
char tablename_utf8mb3[dict_name::MAX_TABLE_UTF8MB3_LEN]; |
| 3428 |
|
| 3429 |
dict_fs2utf8(table->name.m_name, dbname_utf8mb3, sizeof(dbname_utf8mb3), |
| 3430 |
tablename_utf8mb3, sizeof(tablename_utf8mb3)); |
| 3431 |
|
| 3432 |
pars_info_t *pinfo; |
| 3433 |
|
| 3434 |
pinfo = pars_info_create(); |
| 3435 |
|
| 3436 |
pars_info_add_str_literal(pinfo, "dbname_utf8mb3", dbname_utf8mb3); |
| 3437 |
pars_info_add_str_literal(pinfo, "tablename_utf8mb3", tablename_utf8mb3); |
| 3438 |
pars_info_add_str_literal(pinfo, "new_index_name", new_index_name); |
| 3439 |
pars_info_add_str_literal(pinfo, "old_index_name", old_index_name); |
| 3440 |
|
| 3441 |
dberr_t ret; |
| 3442 |
|
| 3443 |
ret = dict_stats_exec_sql(pinfo, |
| 3444 |
"PROCEDURE RENAME_INDEX_IN_INDEX_STATS () IS\n" |
| 3445 |
"BEGIN\n" |
| 3446 |
"UPDATE \"" INDEX_STATS_NAME |
| 3447 |
"\" SET\n" |
| 3448 |
"index_name = :new_index_name\n" |
| 3449 |
"WHERE\n" |
| 3450 |
"database_name = :dbname_utf8mb3 AND\n" |
| 3451 |
"table_name = :tablename_utf8mb3 AND\n" |
| 3452 |
"index_name = :old_index_name;\n" |
| 3453 |
"END;\n", |
| 3454 |
nullptr); |
| 3455 |
|
| 3456 |
rw_lock_x_unlock(dict_operation_lock); |
| 3457 |
|
| 3458 |
return (ret); |
| 3459 |
} |
| 3460 |
|
| 3461 |
/** Evict the stats tables if they loaded in tablespace cache and also |
| 3462 |
close the stats .ibd files. We have to close stats tables because |
| 3463 |
8.0 stats tables will use the same name. We load the stats from 5.7 |
| 3464 |
with a suffix "_backup57" and migrate the statistics. */ |
| 3465 |
void dict_stats_evict_tablespaces() { |
| 3466 |
ut_ad(srv_is_upgrade_mode); |
| 3467 |
|
| 3468 |
space_id_t space_id_index_stats = fil_space_get_id_by_name(INDEX_STATS_NAME); |
| 3469 |
|
| 3470 |
space_id_t space_id_table_stats = fil_space_get_id_by_name(TABLE_STATS_NAME); |
| 3471 |
|
| 3472 |
trx_t *trx = trx_allocate_for_background(); |
| 3473 |
|
| 3474 |
trx_start_internal(trx, UT_LOCATION_HERE); |
| 3475 |
|
| 3476 |
if (space_id_index_stats != SPACE_UNKNOWN) { |
| 3477 |
dberr_t err; |
| 3478 |
|
| 3479 |
err = fil_close_tablespace(space_id_index_stats); |
| 3480 |
|
| 3481 |
if (err != DB_SUCCESS) { |
| 3482 |
ib::info(ER_IB_MSG_227) |
| 3483 |
<< "dict_stats_evict_tablespace: " |
| 3484 |
<< " fil_close_tablespace(" << space_id_index_stats << ") failed! " |
| 3485 |
<< ut_strerr(err); |
| 3486 |
} |
| 3487 |
} |
| 3488 |
|
| 3489 |
if (space_id_table_stats != SPACE_UNKNOWN) { |
| 3490 |
dberr_t err; |
| 3491 |
|
| 3492 |
err = fil_close_tablespace(space_id_table_stats); |
| 3493 |
|
| 3494 |
if (err != DB_SUCCESS) { |
| 3495 |
ib::info(ER_IB_MSG_228) |
| 3496 |
<< "dict_stats_evict_tablespace: " |
| 3497 |
<< " fil_close_tablespace(" << space_id_index_stats << ") failed! " |
| 3498 |
<< ut_strerr(err); |
| 3499 |
} |
| 3500 |
} |
| 3501 |
|
| 3502 |
trx_commit_for_mysql(trx); |
| 3503 |
|
| 3504 |
trx_free_for_background(trx); |
| 3505 |
} |
| 3506 |
|
| 3507 |
TableStatsRecord::TableStatsRecord() { m_heap = nullptr; } |
| 3508 |
|
| 3509 |
TableStatsRecord::~TableStatsRecord() { |
| 3510 |
if (m_heap != nullptr) { |
| 3511 |
mem_heap_free(m_heap); |
| 3512 |
} |
| 3513 |
} |
| 3514 |
|
| 3515 |
uint64_t TableStatsRecord::get_n_rows() const { return (m_n_rows); } |
| 3516 |
|
| 3517 |
void TableStatsRecord::set_n_rows(uint64_t no_of_rows) { |
| 3518 |
m_n_rows = no_of_rows; |
| 3519 |
} |
| 3520 |
|
| 3521 |
ulint TableStatsRecord::get_clustered_index_size() const { |
| 3522 |
return (m_clustered_index_size); |
| 3523 |
} |
| 3524 |
|
| 3525 |
void TableStatsRecord::set_clustered_index_size(ulint clust_size) { |
| 3526 |
m_clustered_index_size = clust_size; |
| 3527 |
} |
| 3528 |
|
| 3529 |
ulint TableStatsRecord::get_sum_of_other_index_size() const { |
| 3530 |
return (m_sum_of_other_index_sizes); |
| 3531 |
} |
| 3532 |
|
| 3533 |
void TableStatsRecord::set_sum_of_other_index_size( |
| 3534 |
ulint sum_of_other_index_size) { |
| 3535 |
m_sum_of_other_index_sizes = sum_of_other_index_size; |
| 3536 |
} |
| 3537 |
|
| 3538 |
char *TableStatsRecord::get_db_name() const { return (m_db_name); } |
| 3539 |
|
| 3540 |
void TableStatsRecord::set_db_name(const byte *data, ulint len) { |
| 3541 |
if (m_heap == nullptr) { |
| 3542 |
m_heap = mem_heap_create(MAX_DATABASE_NAME_LEN + 1, UT_LOCATION_HERE); |
| 3543 |
} |
| 3544 |
|
| 3545 |
m_db_name = static_cast<char *>(mem_heap_dup(m_heap, data, len + 1)); |
| 3546 |
m_db_name[len] = '\0'; |
| 3547 |
} |
| 3548 |
|
| 3549 |
char *TableStatsRecord::get_tbl_name() const { return (m_tbl_name); } |
| 3550 |
|
| 3551 |
void TableStatsRecord::set_tbl_name(const byte *data, ulint len) { |
| 3552 |
if (m_heap == nullptr) { |
| 3553 |
m_heap = mem_heap_create(MAX_TABLE_NAME_LEN + 1, UT_LOCATION_HERE); |
| 3554 |
} |
| 3555 |
|
| 3556 |
m_tbl_name = static_cast<char *>(mem_heap_dup(m_heap, data, len + 1)); |
| 3557 |
m_tbl_name[len] = '\0'; |
| 3558 |
} |
| 3559 |
|
| 3560 |
void TableStatsRecord::set_data(const byte *data, ulint col_offset, ulint len) { |
| 3561 |
dict_table_t *table = dict_sys->table_stats; |
| 3562 |
dict_index_t *index = table->first_index(); |
| 3563 |
ulint value; |
| 3564 |
uint64_t n_row; |
| 3565 |
ulint index_col_offset = index->get_col_no(col_offset); |
| 3566 |
|
| 3567 |
switch (index_col_offset) { |
| 3568 |
case DB_NAME_COL_NO: |
| 3569 |
set_db_name(data, len); |
| 3570 |
break; |
| 3571 |
case TABLE_NAME_COL_NO: |
| 3572 |
set_tbl_name(data, len); |
| 3573 |
break; |
| 3574 |
case N_ROWS_COL_NO: |
| 3575 |
n_row = mach_read_from_8(data); |
| 3576 |
set_n_rows(n_row); |
| 3577 |
break; |
| 3578 |
case CLUST_INDEX_SIZE_COL_NO: |
| 3579 |
value = mach_read_from_8(data); |
| 3580 |
set_clustered_index_size(value); |
| 3581 |
break; |
| 3582 |
case SUM_OF_OTHER_INDEX_SIZE_COL_NO: |
| 3583 |
value = mach_read_from_8(data); |
| 3584 |
set_sum_of_other_index_size(value); |
| 3585 |
break; |
| 3586 |
default: |
| 3587 |
break; |
| 3588 |
} |
| 3589 |
} |
| 3590 |
|
| 3591 |
/** tests @{ */ |
| 3592 |
#ifdef UNIV_COMPILE_TEST_FUNCS |
| 3593 |
/** save/fetch aux macros @{ */ |
| 3594 |
#define TEST_DATABASE_NAME "foobardb" |
| 3595 |
#define TEST_TABLE_NAME "test_dict_stats" |
| 3596 |
|
| 3597 |
#define TEST_N_ROWS 111 |
| 3598 |
#define TEST_CLUSTERED_INDEX_SIZE 222 |
| 3599 |
#define TEST_SUM_OF_OTHER_INDEX_SIZES 333 |
| 3600 |
|
| 3601 |
#define TEST_IDX1_NAME "tidx1" |
| 3602 |
#define TEST_IDX1_COL1_NAME "tidx1_col1" |
| 3603 |
#define TEST_IDX1_INDEX_SIZE 123 |
| 3604 |
#define TEST_IDX1_N_LEAF_PAGES 234 |
| 3605 |
#define TEST_IDX1_N_DIFF1 50 |
| 3606 |
#define TEST_IDX1_N_DIFF1_SAMPLE_SIZE 500 |
| 3607 |
|
| 3608 |
#define TEST_IDX2_NAME "tidx2" |
| 3609 |
#define TEST_IDX2_COL1_NAME "tidx2_col1" |
| 3610 |
#define TEST_IDX2_COL2_NAME "tidx2_col2" |
| 3611 |
#define TEST_IDX2_COL3_NAME "tidx2_col3" |
| 3612 |
#define TEST_IDX2_COL4_NAME "tidx2_col4" |
| 3613 |
#define TEST_IDX2_INDEX_SIZE 321 |
| 3614 |
#define TEST_IDX2_N_LEAF_PAGES 432 |
| 3615 |
#define TEST_IDX2_N_DIFF1 60 |
| 3616 |
#define TEST_IDX2_N_DIFF1_SAMPLE_SIZE 600 |
| 3617 |
#define TEST_IDX2_N_DIFF2 61 |
| 3618 |
#define TEST_IDX2_N_DIFF2_SAMPLE_SIZE 610 |
| 3619 |
#define TEST_IDX2_N_DIFF3 62 |
| 3620 |
#define TEST_IDX2_N_DIFF3_SAMPLE_SIZE 620 |
| 3621 |
#define TEST_IDX2_N_DIFF4 63 |
| 3622 |
#define TEST_IDX2_N_DIFF4_SAMPLE_SIZE 630 |
| 3623 |
/** @} */ |
| 3624 |
|
| 3625 |
/** test_dict_stats_save() @{ */ |
| 3626 |
void test_dict_stats_save() { |
| 3627 |
dict_table_t table; |
| 3628 |
dict_index_t index1; |
| 3629 |
dict_field_t index1_fields[1]; |
| 3630 |
uint64_t index1_stat_n_diff_key_vals[1]; |
| 3631 |
uint64_t index1_stat_n_sample_sizes[1]; |
| 3632 |
dict_index_t index2; |
| 3633 |
dict_field_t index2_fields[4]; |
| 3634 |
uint64_t index2_stat_n_diff_key_vals[4]; |
| 3635 |
uint64_t index2_stat_n_sample_sizes[4]; |
| 3636 |
dberr_t ret; |
| 3637 |
|
| 3638 |
/* craft a dummy dict_table_t */ |
| 3639 |
table.name.m_name = (char *)(TEST_DATABASE_NAME "/" TEST_TABLE_NAME); |
| 3640 |
table.stat_n_rows = TEST_N_ROWS; |
| 3641 |
table.stat_clustered_index_size = TEST_CLUSTERED_INDEX_SIZE; |
| 3642 |
table.stat_sum_of_other_index_sizes = TEST_SUM_OF_OTHER_INDEX_SIZES; |
| 3643 |
UT_LIST_INIT(table.indexes); |
| 3644 |
UT_LIST_ADD_LAST(table.indexes, &index1); |
| 3645 |
UT_LIST_ADD_LAST(table.indexes, &index2); |
| 3646 |
ut_d(table.magic_n = DICT_TABLE_MAGIC_N); |
| 3647 |
ut_d(index1.magic_n = DICT_INDEX_MAGIC_N); |
| 3648 |
|
| 3649 |
index1.name = TEST_IDX1_NAME; |
| 3650 |
index1.table = &table; |
| 3651 |
index1.cached = 1; |
| 3652 |
index1.n_uniq = 1; |
| 3653 |
index1.fields = index1_fields; |
| 3654 |
index1.stat_n_diff_key_vals = index1_stat_n_diff_key_vals; |
| 3655 |
index1.stat_n_sample_sizes = index1_stat_n_sample_sizes; |
| 3656 |
index1.stat_index_size = TEST_IDX1_INDEX_SIZE; |
| 3657 |
index1.stat_n_leaf_pages = TEST_IDX1_N_LEAF_PAGES; |
| 3658 |
index1_fields[0].name = TEST_IDX1_COL1_NAME; |
| 3659 |
index1_stat_n_diff_key_vals[0] = TEST_IDX1_N_DIFF1; |
| 3660 |
index1_stat_n_sample_sizes[0] = TEST_IDX1_N_DIFF1_SAMPLE_SIZE; |
| 3661 |
|
| 3662 |
ut_d(index2.magic_n = DICT_INDEX_MAGIC_N); |
| 3663 |
index2.name = TEST_IDX2_NAME; |
| 3664 |
index2.table = &table; |
| 3665 |
index2.cached = 1; |
| 3666 |
index2.n_uniq = 4; |
| 3667 |
index2.fields = index2_fields; |
| 3668 |
index2.stat_n_diff_key_vals = index2_stat_n_diff_key_vals; |
| 3669 |
index2.stat_n_sample_sizes = index2_stat_n_sample_sizes; |
| 3670 |
index2.stat_index_size = TEST_IDX2_INDEX_SIZE; |
| 3671 |
index2.stat_n_leaf_pages = TEST_IDX2_N_LEAF_PAGES; |
| 3672 |
index2_fields[0].name = TEST_IDX2_COL1_NAME; |
| 3673 |
index2_fields[1].name = TEST_IDX2_COL2_NAME; |
| 3674 |
index2_fields[2].name = TEST_IDX2_COL3_NAME; |
| 3675 |
index2_fields[3].name = TEST_IDX2_COL4_NAME; |
| 3676 |
index2_stat_n_diff_key_vals[0] = TEST_IDX2_N_DIFF1; |
| 3677 |
index2_stat_n_diff_key_vals[1] = TEST_IDX2_N_DIFF2; |
| 3678 |
index2_stat_n_diff_key_vals[2] = TEST_IDX2_N_DIFF3; |
| 3679 |
index2_stat_n_diff_key_vals[3] = TEST_IDX2_N_DIFF4; |
| 3680 |
index2_stat_n_sample_sizes[0] = TEST_IDX2_N_DIFF1_SAMPLE_SIZE; |
| 3681 |
index2_stat_n_sample_sizes[1] = TEST_IDX2_N_DIFF2_SAMPLE_SIZE; |
| 3682 |
index2_stat_n_sample_sizes[2] = TEST_IDX2_N_DIFF3_SAMPLE_SIZE; |
| 3683 |
index2_stat_n_sample_sizes[3] = TEST_IDX2_N_DIFF4_SAMPLE_SIZE; |
| 3684 |
|
| 3685 |
ret = dict_stats_save(&table, NULL); |
| 3686 |
|
| 3687 |
ut_a(ret == DB_SUCCESS); |
| 3688 |
|
| 3689 |
printf( |
| 3690 |
"\nOK: stats saved successfully, now go ahead and read" |
| 3691 |
" what's inside %s and %s:\n\n", |
| 3692 |
TABLE_STATS_NAME_PRINT, INDEX_STATS_NAME_PRINT); |
| 3693 |
|
| 3694 |
printf( |
| 3695 |
"SELECT COUNT(*) = 1 AS table_stats_saved_successfully\n" |
| 3696 |
"FROM %s\n" |
| 3697 |
"WHERE\n" |
| 3698 |
"database_name = '%s' AND\n" |
| 3699 |
"table_name = '%s' AND\n" |
| 3700 |
"n_rows = %d AND\n" |
| 3701 |
"clustered_index_size = %d AND\n" |
| 3702 |
"sum_of_other_index_sizes = %d;\n" |
| 3703 |
"\n", |
| 3704 |
TABLE_STATS_NAME_PRINT, TEST_DATABASE_NAME, TEST_TABLE_NAME, TEST_N_ROWS, |
| 3705 |
TEST_CLUSTERED_INDEX_SIZE, TEST_SUM_OF_OTHER_INDEX_SIZES); |
| 3706 |
|
| 3707 |
printf( |
| 3708 |
"SELECT COUNT(*) = 3 AS tidx1_stats_saved_successfully\n" |
| 3709 |
"FROM %s\n" |
| 3710 |
"WHERE\n" |
| 3711 |
"database_name = '%s' AND\n" |
| 3712 |
"table_name = '%s' AND\n" |
| 3713 |
"index_name = '%s' AND\n" |
| 3714 |
"(\n" |
| 3715 |
" (stat_name = 'size' AND stat_value = %d AND" |
| 3716 |
" sample_size IS NULL) OR\n" |
| 3717 |
" (stat_name = 'n_leaf_pages' AND stat_value = %d AND" |
| 3718 |
" sample_size IS NULL) OR\n" |
| 3719 |
" (stat_name = 'n_diff_pfx01' AND stat_value = %d AND" |
| 3720 |
" sample_size = '%d' AND stat_description = '%s')\n" |
| 3721 |
");\n" |
| 3722 |
"\n", |
| 3723 |
INDEX_STATS_NAME_PRINT, TEST_DATABASE_NAME, TEST_TABLE_NAME, |
| 3724 |
TEST_IDX1_NAME, TEST_IDX1_INDEX_SIZE, TEST_IDX1_N_LEAF_PAGES, |
| 3725 |
TEST_IDX1_N_DIFF1, TEST_IDX1_N_DIFF1_SAMPLE_SIZE, TEST_IDX1_COL1_NAME); |
| 3726 |
|
| 3727 |
printf( |
| 3728 |
"SELECT COUNT(*) = 6 AS tidx2_stats_saved_successfully\n" |
| 3729 |
"FROM %s\n" |
| 3730 |
"WHERE\n" |
| 3731 |
"database_name = '%s' AND\n" |
| 3732 |
"table_name = '%s' AND\n" |
| 3733 |
"index_name = '%s' AND\n" |
| 3734 |
"(\n" |
| 3735 |
" (stat_name = 'size' AND stat_value = %d AND" |
| 3736 |
" sample_size IS NULL) OR\n" |
| 3737 |
" (stat_name = 'n_leaf_pages' AND stat_value = %d AND" |
| 3738 |
" sample_size IS NULL) OR\n" |
| 3739 |
" (stat_name = 'n_diff_pfx01' AND stat_value = %d AND" |
| 3740 |
" sample_size = '%d' AND stat_description = '%s') OR\n" |
| 3741 |
" (stat_name = 'n_diff_pfx02' AND stat_value = %d AND" |
| 3742 |
" sample_size = '%d' AND stat_description = '%s,%s') OR\n" |
| 3743 |
" (stat_name = 'n_diff_pfx03' AND stat_value = %d AND" |
| 3744 |
" sample_size = '%d' AND stat_description = '%s,%s,%s') OR\n" |
| 3745 |
" (stat_name = 'n_diff_pfx04' AND stat_value = %d AND" |
| 3746 |
" sample_size = '%d' AND stat_description = '%s,%s,%s,%s')\n" |
| 3747 |
");\n" |
| 3748 |
"\n", |
| 3749 |
INDEX_STATS_NAME_PRINT, TEST_DATABASE_NAME, TEST_TABLE_NAME, |
| 3750 |
TEST_IDX2_NAME, TEST_IDX2_INDEX_SIZE, TEST_IDX2_N_LEAF_PAGES, |
| 3751 |
TEST_IDX2_N_DIFF1, TEST_IDX2_N_DIFF1_SAMPLE_SIZE, TEST_IDX2_COL1_NAME, |
| 3752 |
TEST_IDX2_N_DIFF2, TEST_IDX2_N_DIFF2_SAMPLE_SIZE, TEST_IDX2_COL1_NAME, |
| 3753 |
TEST_IDX2_COL2_NAME, TEST_IDX2_N_DIFF3, TEST_IDX2_N_DIFF3_SAMPLE_SIZE, |
| 3754 |
TEST_IDX2_COL1_NAME, TEST_IDX2_COL2_NAME, TEST_IDX2_COL3_NAME, |
| 3755 |
TEST_IDX2_N_DIFF4, TEST_IDX2_N_DIFF4_SAMPLE_SIZE, TEST_IDX2_COL1_NAME, |
| 3756 |
TEST_IDX2_COL2_NAME, TEST_IDX2_COL3_NAME, TEST_IDX2_COL4_NAME); |
| 3757 |
} |
| 3758 |
/** @} */ |
| 3759 |
|
| 3760 |
/** test_dict_stats_fetch_from_ps() @{ */ |
| 3761 |
void test_dict_stats_fetch_from_ps() { |
| 3762 |
dict_table_t table; |
| 3763 |
dict_index_t index1; |
| 3764 |
uint64_t index1_stat_n_diff_key_vals[1]; |
| 3765 |
uint64_t index1_stat_n_sample_sizes[1]; |
| 3766 |
dict_index_t index2; |
| 3767 |
uint64_t index2_stat_n_diff_key_vals[4]; |
| 3768 |
uint64_t index2_stat_n_sample_sizes[4]; |
| 3769 |
dberr_t ret; |
| 3770 |
|
| 3771 |
/* craft a dummy dict_table_t */ |
| 3772 |
table.name.m_name = (char *)(TEST_DATABASE_NAME "/" TEST_TABLE_NAME); |
| 3773 |
UT_LIST_INIT(table.indexes); |
| 3774 |
UT_LIST_ADD_LAST(table.indexes, &index1); |
| 3775 |
UT_LIST_ADD_LAST(table.indexes, &index2); |
| 3776 |
ut_d(table.magic_n = DICT_TABLE_MAGIC_N); |
| 3777 |
|
| 3778 |
index1.name = TEST_IDX1_NAME; |
| 3779 |
ut_d(index1.magic_n = DICT_INDEX_MAGIC_N); |
| 3780 |
index1.cached = 1; |
| 3781 |
index1.n_uniq = 1; |
| 3782 |
index1.stat_n_diff_key_vals = index1_stat_n_diff_key_vals; |
| 3783 |
index1.stat_n_sample_sizes = index1_stat_n_sample_sizes; |
| 3784 |
|
| 3785 |
index2.name = TEST_IDX2_NAME; |
| 3786 |
ut_d(index2.magic_n = DICT_INDEX_MAGIC_N); |
| 3787 |
index2.cached = 1; |
| 3788 |
index2.n_uniq = 4; |
| 3789 |
index2.stat_n_diff_key_vals = index2_stat_n_diff_key_vals; |
| 3790 |
index2.stat_n_sample_sizes = index2_stat_n_sample_sizes; |
| 3791 |
|
| 3792 |
ret = dict_stats_fetch_from_ps(&table); |
| 3793 |
|
| 3794 |
ut_a(ret == DB_SUCCESS); |
| 3795 |
|
| 3796 |
ut_a(table.stat_n_rows == TEST_N_ROWS); |
| 3797 |
ut_a(table.stat_clustered_index_size == TEST_CLUSTERED_INDEX_SIZE); |
| 3798 |
ut_a(table.stat_sum_of_other_index_sizes == TEST_SUM_OF_OTHER_INDEX_SIZES); |
| 3799 |
|
| 3800 |
ut_a(index1.stat_index_size == TEST_IDX1_INDEX_SIZE); |
| 3801 |
ut_a(index1.stat_n_leaf_pages == TEST_IDX1_N_LEAF_PAGES); |
| 3802 |
ut_a(index1_stat_n_diff_key_vals[0] == TEST_IDX1_N_DIFF1); |
| 3803 |
ut_a(index1_stat_n_sample_sizes[0] == TEST_IDX1_N_DIFF1_SAMPLE_SIZE); |
| 3804 |
|
| 3805 |
ut_a(index2.stat_index_size == TEST_IDX2_INDEX_SIZE); |
| 3806 |
ut_a(index2.stat_n_leaf_pages == TEST_IDX2_N_LEAF_PAGES); |
| 3807 |
ut_a(index2_stat_n_diff_key_vals[0] == TEST_IDX2_N_DIFF1); |
| 3808 |
ut_a(index2_stat_n_sample_sizes[0] == TEST_IDX2_N_DIFF1_SAMPLE_SIZE); |
| 3809 |
ut_a(index2_stat_n_diff_key_vals[1] == TEST_IDX2_N_DIFF2); |
| 3810 |
ut_a(index2_stat_n_sample_sizes[1] == TEST_IDX2_N_DIFF2_SAMPLE_SIZE); |
| 3811 |
ut_a(index2_stat_n_diff_key_vals[2] == TEST_IDX2_N_DIFF3); |
| 3812 |
ut_a(index2_stat_n_sample_sizes[2] == TEST_IDX2_N_DIFF3_SAMPLE_SIZE); |
| 3813 |
ut_a(index2_stat_n_diff_key_vals[3] == TEST_IDX2_N_DIFF4); |
| 3814 |
ut_a(index2_stat_n_sample_sizes[3] == TEST_IDX2_N_DIFF4_SAMPLE_SIZE); |
| 3815 |
|
| 3816 |
printf("OK: fetch successful\n"); |
| 3817 |
} |
| 3818 |
/** @} */ |
| 3819 |
|
| 3820 |
/** test_dict_stats_all() @{ */ |
| 3821 |
void test_dict_stats_all() { |
| 3822 |
test_dict_stats_save(); |
| 3823 |
|
| 3824 |
test_dict_stats_fetch_from_ps(); |
| 3825 |
} |
| 3826 |
/** @} */ |
| 3827 |
|
| 3828 |
#endif /* UNIV_COMPILE_TEST_FUNCS */ |
| 3829 |
/** @} */ |