1 /***************************************************************************** 2 3 Copyright (c) 2009, 2021, Oracle and/or its affiliates. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License, version 2.0, 7 as published by the Free Software Foundation. 8 9 This program is also distributed 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 included with MySQL. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License, version 2.0, for more details. 20 21 You should have received a copy of the GNU General Public License along with 22 this program; if not, write to the Free Software Foundation, Inc., 23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA 24 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 "trx0types.h" 41 42 #define TABLE_STATS_NAME "mysql/innodb_table_stats" 43 #define INDEX_STATS_NAME "mysql/innodb_index_stats" 44 45 enum dict_stats_upd_option_t { 46 DICT_STATS_RECALC_PERSISTENT,/* (re) calculate the 47 statistics using a precise and slow 48 algo and save them to the persistent 49 storage, if the persistent storage is 50 not present then emit a warning and 51 fall back to transient stats */ 52 DICT_STATS_RECALC_TRANSIENT,/* (re) calculate the statistics 53 using an imprecise quick algo 54 without saving the results 55 persistently */ 56 DICT_STATS_EMPTY_TABLE, /* Write all zeros (or 1 where it makes sense) 57 into a table and its indexes' statistics 58 members. The resulting stats correspond to an 59 empty table. If the table is using persistent 60 statistics, then they are saved on disk. */ 61 DICT_STATS_FETCH_ONLY_IF_NOT_IN_MEMORY /* fetch the stats 62 from the persistent storage if the in-memory 63 structures have not been initialized yet, 64 otherwise do nothing */ 65 }; 66 67 /*********************************************************************//** 68 Calculates new estimates for table and index statistics. This function 69 is relatively quick and is used to calculate transient statistics that 70 are not saved on disk. 71 This was the only way to calculate statistics before the 72 Persistent Statistics feature was introduced. */ 73 void 74 dict_stats_update_transient( 75 /*========================*/ 76 dict_table_t* table); /*!< in/out: table */ 77 78 /*********************************************************************//** 79 Set the persistent statistics flag for a given table. This is set only 80 in the in-memory table object and is not saved on disk. It will be read 81 from the .frm file upon first open from MySQL after a server restart. */ 82 UNIV_INLINE 83 void 84 dict_stats_set_persistent( 85 /*======================*/ 86 dict_table_t* table, /*!< in/out: table */ 87 ibool ps_on, /*!< in: persistent stats explicitly enabled */ 88 ibool ps_off) /*!< in: persistent stats explicitly disabled */ 89 MY_ATTRIBUTE((nonnull)); 90 91 /*********************************************************************//** 92 Check whether persistent statistics is enabled for a given table. 93 @return TRUE if enabled, FALSE otherwise */ 94 UNIV_INLINE 95 ibool 96 dict_stats_is_persistent_enabled( 97 /*=============================*/ 98 const dict_table_t* table) /*!< in: table */ 99 MY_ATTRIBUTE((nonnull, warn_unused_result)); 100 101 /*********************************************************************//** 102 Set the auto recalc flag for a given table (only honored for a persistent 103 stats enabled table). The flag is set only in the in-memory table object 104 and is not saved in InnoDB files. It will be read from the .frm file upon 105 first open from MySQL after a server restart. */ 106 UNIV_INLINE 107 void 108 dict_stats_auto_recalc_set( 109 /*=======================*/ 110 dict_table_t* table, /*!< in/out: table */ 111 ibool auto_recalc_on, /*!< in: explicitly enabled */ 112 ibool auto_recalc_off); /*!< in: explicitly disabled */ 113 114 /*********************************************************************//** 115 Check whether auto recalc is enabled for a given table. 116 @return TRUE if enabled, FALSE otherwise */ 117 UNIV_INLINE 118 ibool 119 dict_stats_auto_recalc_is_enabled( 120 /*==============================*/ 121 const dict_table_t* table); /*!< in: table */ 122 123 /*********************************************************************//** 124 Initialize table's stats for the first time when opening a table. */ 125 UNIV_INLINE 126 void 127 dict_stats_init( 128 /*============*/ 129 dict_table_t* table); /*!< in/out: table */ 130 131 /*********************************************************************//** 132 Deinitialize table's stats after the last close of the table. This is 133 used to detect "FLUSH TABLE" and refresh the stats upon next open. */ 134 UNIV_INLINE 135 void 136 dict_stats_deinit( 137 /*==============*/ 138 dict_table_t* table) /*!< in/out: table */ 139 MY_ATTRIBUTE((nonnull)); 140 141 /*********************************************************************//** 142 Calculates new estimates for table and index statistics. The statistics 143 are used in query optimization. 144 @return DB_* error code or DB_SUCCESS */ 145 dberr_t 146 dict_stats_update( 147 /*==============*/ 148 dict_table_t* table, /*!< in/out: table */ 149 dict_stats_upd_option_t stats_upd_option); 150 /*!< in: whether to (re) calc 151 the stats or to fetch them from 152 the persistent storage */ 153 154 /*********************************************************************//** 155 Removes the information for a particular index's stats from the persistent 156 storage if it exists and if there is data stored for this index. 157 This function creates its own trx and commits it. 158 @return DB_SUCCESS or error code */ 159 dberr_t 160 dict_stats_drop_index( 161 /*==================*/ 162 const char* tname, /*!< in: table name */ 163 const char* iname, /*!< in: index name */ 164 char* errstr, /*!< out: error message if != DB_SUCCESS 165 is returned */ 166 ulint errstr_sz);/*!< in: size of the errstr buffer */ 167 168 /*********************************************************************//** 169 Removes the statistics for a table and all of its indexes from the 170 persistent storage if it exists and if there is data stored for the table. 171 This function creates its own transaction and commits it. 172 @return DB_SUCCESS or error code */ 173 dberr_t 174 dict_stats_drop_table( 175 /*==================*/ 176 const char* table_name, /*!< in: table name */ 177 char* errstr, /*!< out: error message 178 if != DB_SUCCESS is returned */ 179 ulint errstr_sz); /*!< in: size of errstr buffer */ 180 181 /*********************************************************************//** 182 Fetches or calculates new estimates for index statistics. */ 183 void 184 dict_stats_update_for_index( 185 /*========================*/ 186 dict_index_t* index) /*!< in/out: index */ 187 MY_ATTRIBUTE((nonnull)); 188 189 /*********************************************************************//** 190 Renames a table in InnoDB persistent stats storage. 191 This function creates its own transaction and commits it. 192 @return DB_SUCCESS or error code */ 193 dberr_t 194 dict_stats_rename_table( 195 /*====================*/ 196 bool dict_locked, /*!< in: true if dict_sys mutex 197 and dict_operation_lock are held, 198 otherwise false*/ 199 const char* old_name, /*!< in: old table name */ 200 const char* new_name, /*!< in: new table name */ 201 char* errstr, /*!< out: error string if != DB_SUCCESS 202 is returned */ 203 size_t errstr_sz); /*!< in: errstr size */ 204 205 /*********************************************************************//** 206 Renames an index in InnoDB persistent stats storage. 207 This function creates its own transaction and commits it. 208 @return DB_SUCCESS or error code. DB_STATS_DO_NOT_EXIST will be returned 209 if the persistent stats do not exist. */ 210 dberr_t 211 dict_stats_rename_index( 212 /*====================*/ 213 const dict_table_t* table, /*!< in: table whose index 214 is renamed */ 215 const char* old_index_name, /*!< in: old index name */ 216 const char* new_index_name) /*!< in: new index name */ 217 MY_ATTRIBUTE((warn_unused_result)); 218 219 #ifndef UNIV_NONINL 220 #include "dict0stats.ic" 221 #endif 222 223 #ifdef UNIV_ENABLE_UNIT_TEST_DICT_STATS 224 void test_dict_stats_all(); 225 #endif /* UNIV_ENABLE_UNIT_TEST_DICT_STATS */ 226 227 #endif /* dict0stats_h */ 228