Debian Codegraph @

storage/innobase/include/dict0stats.h

raw
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 /** @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
43 enum dict_stats_upd_option_t {
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. */
59 DICT_STATS_FETCH_ONLY_IF_NOT_IN_MEMORY /* fetch the stats
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
66 the 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 */
71 static 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
80 stats enabled table). The flag is set only in the in-memory table object and is
81 not saved in InnoDB files. It will be read from the .frm file upon first open
82 from 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 */
86 static inline void dict_stats_auto_recalc_set(dict_table_t *table,
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 */
92 static inline bool dict_stats_auto_recalc_is_enabled(
93 const dict_table_t *table); /*!< in: table */
94
95 /** Initialize table's stats for the first time when opening a table. */
96 static 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. */
100 static inline void dict_stats_deinit(dict_table_t *table); /*!< in/out: table */
101
102 /** Calculates new estimates for table and index statistics. The statistics
103 are used in query optimization.
104 @return DB_* error code or DB_SUCCESS */
105 dberr_t dict_stats_update(dict_table_t *table, /*!< in/out: table */
106 dict_stats_upd_option_t stats_upd_option);
107 /*!< in: whether to (re) calc
108 the stats or to fetch them from
109 the persistent storage */
110
111 /** Removes the information for a particular index's stats from the persistent
112 storage if it exists and if there is data stored for this index.
113 This function creates its own trx and commits it.
114 @return DB_SUCCESS or error code */
115 dberr_t dict_stats_drop_index(
116 const char *tname, /*!< in: table name */
117 const char *iname, /*!< in: index name */
118 char *errstr, /*!< out: error message if != DB_SUCCESS
119 is returned */
120 ulint errstr_sz); /*!< in: size of the errstr buffer */
121
122 /** Removes the statistics for a table and all of its indexes from the
123 persistent storage if it exists and if there is data stored for the table.
124 This function creates its own transaction and commits it.
125 @return DB_SUCCESS or error code */
126 dberr_t dict_stats_drop_table(
127 const char *table_name, /*!< in: table name */
128 char *errstr, /*!< out: error message
129 if != DB_SUCCESS is returned */
130 ulint errstr_sz); /*!< in: size of errstr buffer */
131
132 /** Fetches or calculates new estimates for index statistics. */
133 void dict_stats_update_for_index(dict_index_t *index); /*!< in/out: index */
134
135 /** Renames a table in InnoDB persistent stats storage.
136 This function creates its own transaction and commits it.
137 @return DB_SUCCESS or error code */
138 dberr_t dict_stats_rename_table(const char *old_name, /*!< in: old table name */
139 const char *new_name, /*!< in: new table name */
140 char *errstr, /*!< out: error string if !=
141 DB_SUCCESS is returned */
142 size_t errstr_sz); /*!< in: errstr size */
143
144 /** Renames an index in InnoDB persistent stats storage.
145 This function creates its own transaction and commits it.
146 @return DB_SUCCESS or error code. DB_STATS_DO_NOT_EXIST will be returned
147 if the persistent stats do not exist. */
148 [[nodiscard]] dberr_t dict_stats_rename_index(
149 const dict_table_t *table, /*!< in: table whose index
150 is renamed */
151 const char *old_index_name, /*!< in: old index name */
152 const char *new_index_name); /*!< in: new index name */
153
154 /** Evict the stats tables if they loaded in tablespace cache and also
155 close the stats .ibd files. We have to close stats tables because
156 8.0 stats tables will use the same name. We load the stats from 5.7
157 with a suffix "_backup57" and migrate the statistics. */
158 void dict_stats_evict_tablespaces();
159
160 /** Represent the record of innodb_table_stats table. */
161 class TableStatsRecord {
162 public:
163 /** Constructor. */
164 TableStatsRecord();
165
166 /** Destructor. */
167 ~TableStatsRecord();
168
169 /** Set the data for the innodb_table_stats record.
170 @param[in] data data to be set in the record
171 @param[in] col_offset column offset
172 @param[in] len length of the data. */
173 void set_data(const byte *data, ulint col_offset, ulint len);
174
175 /** Get the table name from innodb_table_stats record.
176 @retval table name of the table_stats record. */
177 char *get_tbl_name() const;
178
179 /** Set the table name for the innodb_table_stats record.
180 @param[in] data data to be set in the record
181 @param[in] len length of the data. */
182 void set_tbl_name(const byte *data, ulint len);
183
184 /** Get the db name from the innodb_table_stats record.
185 @retval db name of the table stats record. */
186 char *get_db_name() const;
187
188 /** Set the db name for the innodb_table_stats record.
189 @param[in] data data to be set
190 @param[in] len length of the data. */
191 void set_db_name(const byte *data, ulint len);
192
193 /** Get the n_rows from the innodb_table_stats record.
194 @retval n_rows from the record. */
195 uint64_t get_n_rows() const;
196
197 /** Set the n_rows for the innodb_table_stats record.
198 @param[in] no_of_rows number of rows. */
199 void set_n_rows(uint64_t no_of_rows);
200
201 /** Get the clustered index size from
202 innodb_table_stats record.
203 @retval size of the clustered index. */
204 ulint get_clustered_index_size() const;
205
206 /** Set the clustered index size for the
207 innodb_table_stats record.
208 @param[in] clust_size clustered index size. */
209 void set_clustered_index_size(ulint clust_size);
210
211 /** Get the sum of other index size.
212 @retval sum of secondary index size. */
213 ulint get_sum_of_other_index_size() const;
214
215 /** Set the sum of sec index size.
216 @param[in] sum_of_other_index_size sum of secondary index size. */
217 void set_sum_of_other_index_size(ulint sum_of_other_index_size);
218
219 /** Column number of innodb_table_stats.database_name. */
220 static constexpr unsigned DB_NAME_COL_NO = 0;
221 /** Column number of innodb_table_stats.table_name. */
222 static constexpr unsigned TABLE_NAME_COL_NO = 1;
223 /** Column number of innodb_table_stats.n_rows. */
224 static constexpr unsigned N_ROWS_COL_NO = 3;
225 /** Column number of innodb_table_stats.clustered_index_size. */
226 static constexpr unsigned CLUST_INDEX_SIZE_COL_NO = 4;
227 /** Column number of innodb_table_stats.sum_of_other_index_sizes. */
228 static constexpr unsigned SUM_OF_OTHER_INDEX_SIZE_COL_NO = 5;
229
230 private:
231 /** Database name. */
232 char *m_db_name;
233 /** Table name. */
234 char *m_tbl_name;
235 /** Number of rows. */
236 uint64_t m_n_rows;
237 /** Clustered index size. */
238 ulint m_clustered_index_size;
239 /** Sum of other index size. */
240 ulint m_sum_of_other_index_sizes;
241 /** Heap to store db_name, tbl_name for the record. */
242 mem_heap_t *m_heap;
243 };
244
245 #include "dict0stats.ic"
246
247 #ifdef UNIV_ENABLE_UNIT_TEST_DICT_STATS
248 void test_dict_stats_all();
249 #endif /* UNIV_ENABLE_UNIT_TEST_DICT_STATS */
250
251 #endif /* dict0stats_h */