1 /*****************************************************************************
2 
3 Copyright (c) 2009, 2016, Oracle and/or its affiliates. All Rights Reserved.
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 "db0err.h"
40 #include "dict0types.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 /*********************************************************************//**
66 Calculates new estimates for table and index statistics. This function
67 is relatively quick and is used to calculate transient statistics that
68 are not saved on disk.
69 This was the only way to calculate statistics before the
70 Persistent Statistics feature was introduced. */
71 UNIV_INTERN
72 void
73 dict_stats_update_transient(
74 /*========================*/
75 	dict_table_t*	table);	/*!< in/out: table */
76 
77 /*********************************************************************//**
78 Set the persistent statistics flag for a given table. This is set only
79 in the in-memory table object and is not saved on disk. It will be read
80 from the .frm file upon first open from MySQL after a server restart. */
81 UNIV_INLINE
82 void
83 dict_stats_set_persistent(
84 /*======================*/
85 	dict_table_t*	table,	/*!< in/out: table */
86 	ibool		ps_on,	/*!< in: persistent stats explicitly enabled */
87 	ibool		ps_off)	/*!< in: persistent stats explicitly disabled */
88 	MY_ATTRIBUTE((nonnull));
89 
90 /*********************************************************************//**
91 Check whether persistent statistics is enabled for a given table.
92 @return TRUE if enabled, FALSE otherwise */
93 UNIV_INLINE
94 ibool
95 dict_stats_is_persistent_enabled(
96 /*=============================*/
97 	const dict_table_t*	table)	/*!< in: table */
98 	MY_ATTRIBUTE((nonnull, warn_unused_result));
99 
100 /*********************************************************************//**
101 Set the auto recalc flag for a given table (only honored for a persistent
102 stats enabled table). The flag is set only in the in-memory table object
103 and is not saved in InnoDB files. It will be read from the .frm file upon
104 first open from MySQL after a server restart. */
105 UNIV_INLINE
106 void
107 dict_stats_auto_recalc_set(
108 /*=======================*/
109 	dict_table_t*	table,			/*!< in/out: table */
110 	ibool		auto_recalc_on,		/*!< in: explicitly enabled */
111 	ibool		auto_recalc_off);	/*!< in: explicitly disabled */
112 
113 /*********************************************************************//**
114 Check whether auto recalc is enabled for a given table.
115 @return TRUE if enabled, FALSE otherwise */
116 UNIV_INLINE
117 ibool
118 dict_stats_auto_recalc_is_enabled(
119 /*==============================*/
120 	const dict_table_t*	table);	/*!< in: table */
121 
122 /*********************************************************************//**
123 Initialize table's stats for the first time when opening a table. */
124 UNIV_INLINE
125 void
126 dict_stats_init(
127 /*============*/
128 	dict_table_t*	table);	/*!< in/out: table */
129 
130 /*********************************************************************//**
131 Deinitialize table's stats after the last close of the table. This is
132 used to detect "FLUSH TABLE" and refresh the stats upon next open. */
133 UNIV_INLINE
134 void
135 dict_stats_deinit(
136 /*==============*/
137 	dict_table_t*	table)	/*!< in/out: table */
138 	MY_ATTRIBUTE((nonnull));
139 
140 /*********************************************************************//**
141 Calculates new estimates for table and index statistics. The statistics
142 are used in query optimization.
143 @return DB_* error code or DB_SUCCESS */
144 UNIV_INTERN
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 UNIV_INTERN
160 dberr_t
161 dict_stats_drop_index(
162 /*==================*/
163 	const char*	tname,	/*!< in: table name */
164 	const char*	iname,	/*!< in: index name */
165 	char*		errstr, /*!< out: error message if != DB_SUCCESS
166 				is returned */
167 	ulint		errstr_sz);/*!< in: size of the errstr buffer */
168 
169 /*********************************************************************//**
170 Removes the statistics for a table and all of its indexes from the
171 persistent storage if it exists and if there is data stored for the table.
172 This function creates its own transaction and commits it.
173 @return DB_SUCCESS or error code */
174 UNIV_INTERN
175 dberr_t
176 dict_stats_drop_table(
177 /*==================*/
178 	const char*	table_name,	/*!< in: table name */
179 	char*		errstr,		/*!< out: error message
180 					if != DB_SUCCESS is returned */
181 	ulint		errstr_sz);	/*!< in: size of errstr buffer */
182 
183 /*********************************************************************//**
184 Fetches or calculates new estimates for index statistics. */
185 UNIV_INTERN
186 void
187 dict_stats_update_for_index(
188 /*========================*/
189 	dict_index_t*	index)	/*!< in/out: index */
190 	MY_ATTRIBUTE((nonnull));
191 
192 /*********************************************************************//**
193 Renames a table in InnoDB persistent stats storage.
194 This function creates its own transaction and commits it.
195 @return DB_SUCCESS or error code */
196 UNIV_INTERN
197 dberr_t
198 dict_stats_rename_table(
199 /*====================*/
200 	const char*	old_name,	/*!< in: old table name */
201 	const char*	new_name,	/*!< in: new table name */
202 	char*		errstr,		/*!< out: error string if != DB_SUCCESS
203 					is returned */
204 	size_t		errstr_sz);	/*!< in: errstr size */
205 
206 #ifndef UNIV_NONINL
207 #include "dict0stats.ic"
208 #endif
209 
210 #endif /* dict0stats_h */
211