MySQL 9.2.0
Source Code Documentation
dict0stats.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2009, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/dict0stats.h
29 Code used for calculating and manipulating table statistics.
30
31 Created Jan 06, 2010 Vasil Dimov
32 *******************************************************/
33
34#ifndef dict0stats_h
35#define dict0stats_h
36
37#include "univ.i"
38
39#include "dict0types.h"
40#include "mem0mem.h"
41#include "trx0types.h"
42
44 DICT_STATS_RECALC_PERSISTENT, /* (re) calculate the
45 statistics using a precise and slow
46 algo and save them to the persistent
47 storage, if the persistent storage is
48 not present then emit a warning and
49 fall back to transient stats */
50 DICT_STATS_RECALC_TRANSIENT, /* (re) calculate the statistics
51 using an imprecise quick algo
52 without saving the results
53 persistently */
54 DICT_STATS_EMPTY_TABLE, /* Write all zeros (or 1 where it makes sense)
55 into a table and its indexes' statistics
56 members. The resulting stats correspond to an
57 empty table. If the table is using persistent
58 statistics, then they are saved on disk. */
60 from the persistent storage if the in-memory
61 structures have not been initialized yet,
62 otherwise do nothing */
63};
64
65/** Set the persistent statistics flag for a given table. This is set only in
66the in-memory table object and is not saved on disk. It will be read from the
67.frm file upon first open from MySQL after a server restart.
68@param[in,out] table table
69@param[in] ps_on persistent stats explicitly enabled
70@param[in] ps_off persistent stats explicitly disabled */
71static inline void dict_stats_set_persistent(dict_table_t *table, bool ps_on,
72 bool ps_off);
73
74/** Check whether persistent statistics is enabled for a given table.
75 @return true if enabled, false otherwise */
76[[nodiscard]] static inline bool dict_stats_is_persistent_enabled(
77 const dict_table_t *table); /*!< in: table */
78
79/** Set the auto recalc flag for a given table (only honored for a persistent
80stats enabled table). The flag is set only in the in-memory table object and is
81not saved in InnoDB files. It will be read from the .frm file upon first open
82from MySQL after a server restart.
83@param[in,out] table table
84@param[in] auto_recalc_on explicitly enabled
85@param[in] auto_recalc_off explicitly disabled */
87 bool auto_recalc_on,
88 bool auto_recalc_off);
89
90/** Check whether auto recalc is enabled for a given table.
91 @return true if enabled, false otherwise */
93 const dict_table_t *table); /*!< in: table */
94
95/** Initialize table's stats for the first time when opening a table. */
96static inline void dict_stats_init(dict_table_t *table); /*!< in/out: table */
97
98/** Deinitialize table's stats after the last close of the table. This is
99 used to detect "FLUSH TABLE" and refresh the stats upon next open. */
100static inline void dict_stats_deinit(dict_table_t *table); /*!< in/out: table */
101
102/** Calculates new estimates for table and index statistics. The statistics
103are used in query optimization.
104@param[in] table table for which statistics are to be updated.
105@param[in] stats_upd_option statistics update option (persistent, etc).
106@param[in] trx statistics update will be done as part of this transaction.
107 if null, a new transaction will be internally created.
108@param[in] silent if true, don't report error in the server log file.
109 if false (default), report error in log file.
110@return DB_* error code or DB_SUCCESS */
112 dict_stats_upd_option_t stats_upd_option,
113 trx_t *trx = nullptr, bool silent = false);
114
116 dict_stats_upd_option_t stats_upd_option,
117 size_t max_retries);
118
119/** Removes the information for a particular index's stats from the persistent
120 storage if it exists and if there is data stored for this index.
121 This function creates its own trx and commits it.
122 @return DB_SUCCESS or error code */
124 const char *tname, /*!< in: table name */
125 const char *iname, /*!< in: index name */
126 char *errstr, /*!< out: error message if != DB_SUCCESS
127 is returned */
128 ulint errstr_sz); /*!< in: size of the errstr buffer */
129
130/** Removes the statistics for a table and all of its indexes from the
131 persistent storage if it exists and if there is data stored for the table.
132 This function creates its own transaction and commits it.
133 @return DB_SUCCESS or error code */
135 const char *table_name, /*!< in: table name */
136 char *errstr, /*!< out: error message
137 if != DB_SUCCESS is returned */
138 ulint errstr_sz); /*!< in: size of errstr buffer */
139
140/** Fetches or calculates new estimates for index statistics. */
141void dict_stats_update_for_index(dict_index_t *index); /*!< in/out: index */
142
143/** Renames a table in InnoDB persistent stats storage.
144 This function creates its own transaction and commits it.
145 @return DB_SUCCESS or error code */
146dberr_t dict_stats_rename_table(const char *old_name, /*!< in: old table name */
147 const char *new_name, /*!< in: new table name */
148 char *errstr, /*!< out: error string if !=
149 DB_SUCCESS is returned */
150 size_t errstr_sz); /*!< in: errstr size */
151
152/** Renames an index in InnoDB persistent stats storage.
153 This function creates its own transaction and commits it.
154 @return DB_SUCCESS or error code. DB_STATS_DO_NOT_EXIST will be returned
155 if the persistent stats do not exist. */
156[[nodiscard]] dberr_t dict_stats_rename_index(
157 const dict_table_t *table, /*!< in: table whose index
158 is renamed */
159 const char *old_index_name, /*!< in: old index name */
160 const char *new_index_name); /*!< in: new index name */
161
162/** Represent the record of innodb_table_stats table. */
164 public:
165 /** Constructor. */
167
168 /** Destructor. */
170
171 /** Set the data for the innodb_table_stats record.
172 @param[in] data data to be set in the record
173 @param[in] col_offset column offset
174 @param[in] len length of the data. */
175 void set_data(const byte *data, ulint col_offset, ulint len);
176
177 /** Get the table name from innodb_table_stats record.
178 @retval table name of the table_stats record. */
179 char *get_tbl_name() const;
180
181 /** Set the table name for the innodb_table_stats record.
182 @param[in] data data to be set in the record
183 @param[in] len length of the data. */
184 void set_tbl_name(const byte *data, ulint len);
185
186 /** Get the db name from the innodb_table_stats record.
187 @retval db name of the table stats record. */
188 char *get_db_name() const;
189
190 /** Set the db name for the innodb_table_stats record.
191 @param[in] data data to be set
192 @param[in] len length of the data. */
193 void set_db_name(const byte *data, ulint len);
194
195 /** Get the n_rows from the innodb_table_stats record.
196 @retval n_rows from the record. */
197 uint64_t get_n_rows() const;
198
199 /** Set the n_rows for the innodb_table_stats record.
200 @param[in] no_of_rows number of rows. */
201 void set_n_rows(uint64_t no_of_rows);
202
203 /** Get the clustered index size from
204 innodb_table_stats record.
205 @retval size of the clustered index. */
207
208 /** Set the clustered index size for the
209 innodb_table_stats record.
210 @param[in] clust_size clustered index size. */
211 void set_clustered_index_size(ulint clust_size);
212
213 /** Get the sum of other index size.
214 @retval sum of secondary index size. */
216
217 /** Set the sum of sec index size.
218 @param[in] sum_of_other_index_size sum of secondary index size. */
219 void set_sum_of_other_index_size(ulint sum_of_other_index_size);
220
221 /** Column number of innodb_table_stats.database_name. */
222 static constexpr unsigned DB_NAME_COL_NO = 0;
223 /** Column number of innodb_table_stats.table_name. */
224 static constexpr unsigned TABLE_NAME_COL_NO = 1;
225 /** Column number of innodb_table_stats.n_rows. */
226 static constexpr unsigned N_ROWS_COL_NO = 3;
227 /** Column number of innodb_table_stats.clustered_index_size. */
228 static constexpr unsigned CLUST_INDEX_SIZE_COL_NO = 4;
229 /** Column number of innodb_table_stats.sum_of_other_index_sizes. */
230 static constexpr unsigned SUM_OF_OTHER_INDEX_SIZE_COL_NO = 5;
231
232 private:
233 /** Database name. */
235 /** Table name. */
237 /** Number of rows. */
238 uint64_t m_n_rows;
239 /** Clustered index size. */
241 /** Sum of other index size. */
243 /** Heap to store db_name, tbl_name for the record. */
245};
246
247#include "dict0stats.ic"
248
249#ifdef UNIV_ENABLE_UNIT_TEST_DICT_STATS
250void test_dict_stats_all();
251#endif /* UNIV_ENABLE_UNIT_TEST_DICT_STATS */
252
253#endif /* dict0stats_h */
Represent the record of innodb_table_stats table.
Definition: dict0stats.h:163
ulint m_sum_of_other_index_sizes
Sum of other index size.
Definition: dict0stats.h:242
void set_db_name(const byte *data, ulint len)
Set the db name for the innodb_table_stats record.
Definition: dict0stats.cc:3530
void set_n_rows(uint64_t no_of_rows)
Set the n_rows for the innodb_table_stats record.
Definition: dict0stats.cc:3507
void set_clustered_index_size(ulint clust_size)
Set the clustered index size for the innodb_table_stats record.
Definition: dict0stats.cc:3515
char * m_tbl_name
Table name.
Definition: dict0stats.h:236
void set_data(const byte *data, ulint col_offset, ulint len)
Set the data for the innodb_table_stats record.
Definition: dict0stats.cc:3550
static constexpr unsigned CLUST_INDEX_SIZE_COL_NO
Column number of innodb_table_stats.clustered_index_size.
Definition: dict0stats.h:228
static constexpr unsigned DB_NAME_COL_NO
Column number of innodb_table_stats.database_name.
Definition: dict0stats.h:222
mem_heap_t * m_heap
Heap to store db_name, tbl_name for the record.
Definition: dict0stats.h:244
void set_tbl_name(const byte *data, ulint len)
Set the table name for the innodb_table_stats record.
Definition: dict0stats.cc:3541
ulint m_clustered_index_size
Clustered index size.
Definition: dict0stats.h:240
static constexpr unsigned TABLE_NAME_COL_NO
Column number of innodb_table_stats.table_name.
Definition: dict0stats.h:224
void set_sum_of_other_index_size(ulint sum_of_other_index_size)
Set the sum of sec index size.
Definition: dict0stats.cc:3523
static constexpr unsigned N_ROWS_COL_NO
Column number of innodb_table_stats.n_rows.
Definition: dict0stats.h:226
ulint get_sum_of_other_index_size() const
Get the sum of other index size.
Definition: dict0stats.cc:3519
char * get_db_name() const
Get the db name from the innodb_table_stats record.
Definition: dict0stats.cc:3528
TableStatsRecord()
Constructor.
Definition: dict0stats.cc:3497
~TableStatsRecord()
Destructor.
Definition: dict0stats.cc:3499
static constexpr unsigned SUM_OF_OTHER_INDEX_SIZE_COL_NO
Column number of innodb_table_stats.sum_of_other_index_sizes.
Definition: dict0stats.h:230
char * m_db_name
Database name.
Definition: dict0stats.h:234
char * get_tbl_name() const
Get the table name from innodb_table_stats record.
Definition: dict0stats.cc:3539
uint64_t m_n_rows
Number of rows.
Definition: dict0stats.h:238
uint64_t get_n_rows() const
Get the n_rows from the innodb_table_stats record.
Definition: dict0stats.cc:3505
ulint get_clustered_index_size() const
Get the clustered index size from innodb_table_stats record.
Definition: dict0stats.cc:3511
dberr_t
Definition: db0err.h:39
static bool dict_stats_auto_recalc_is_enabled(const dict_table_t *table)
Check whether auto recalc is enabled for a given table.
static void dict_stats_init(dict_table_t *table)
Initialize table's stats for the first time when opening a table.
dberr_t dict_stats_rename_table(const char *old_name, const char *new_name, char *errstr, size_t errstr_sz)
Renames a table in InnoDB persistent stats storage.
Definition: dict0stats.cc:3323
dict_stats_upd_option_t
Definition: dict0stats.h:43
@ DICT_STATS_RECALC_PERSISTENT
Definition: dict0stats.h:44
@ DICT_STATS_RECALC_TRANSIENT
Definition: dict0stats.h:50
@ DICT_STATS_FETCH_ONLY_IF_NOT_IN_MEMORY
Definition: dict0stats.h:59
@ DICT_STATS_EMPTY_TABLE
Definition: dict0stats.h:54
dberr_t dict_stats_drop_table(const char *table_name, char *errstr, ulint errstr_sz)
Removes the statistics for a table and all of its indexes from the persistent storage if it exists an...
Definition: dict0stats.cc:3169
dberr_t dict_stats_update_retry(dict_table_t *table, dict_stats_upd_option_t stats_upd_option, size_t max_retries)
Definition: dict0stats.cc:2840
void dict_stats_update_for_index(dict_index_t *index)
Fetches or calculates new estimates for index statistics.
Definition: dict0stats.cc:2819
static void dict_stats_auto_recalc_set(dict_table_t *table, bool auto_recalc_on, bool auto_recalc_off)
Set the auto recalc flag for a given table (only honored for a persistent stats enabled table).
dberr_t dict_stats_drop_index(const char *tname, const char *iname, char *errstr, ulint errstr_sz)
Removes the information for a particular index's stats from the persistent storage if it exists and i...
Definition: dict0stats.cc:3030
static bool dict_stats_is_persistent_enabled(const dict_table_t *table)
Check whether persistent statistics is enabled for a given table.
static void dict_stats_set_persistent(dict_table_t *table, bool ps_on, bool ps_off)
Set the persistent statistics flag for a given table.
dberr_t dict_stats_rename_index(const dict_table_t *table, const char *old_index_name, const char *new_index_name)
Renames an index in InnoDB persistent stats storage.
Definition: dict0stats.cc:3454
dberr_t dict_stats_update(dict_table_t *table, dict_stats_upd_option_t stats_upd_option, trx_t *trx=nullptr, bool silent=false)
Calculates new estimates for table and index statistics.
Definition: dict0stats.cc:2860
static void dict_stats_deinit(dict_table_t *table)
Deinitialize table's stats after the last close of the table.
Code used for calculating and manipulating table statistics.
Data dictionary global types.
The memory management.
static bool silent
Definition: mysqlimport.cc:70
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
const char * table_name
Definition: rules_table_service.cc:56
Data structure for an index.
Definition: dict0mem.h:1041
Data structure for a database table.
Definition: dict0mem.h:1913
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:302
Definition: trx0trx.h:675
Transaction system global type definitions.
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:406