You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.4 KiB

  1. /*****************************************************************************
  2. Copyright (c) 2012, 2013, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file include/dict0stats.ic
  15. Code used for calculating and manipulating table statistics.
  16. Created Jan 23, 2012 Vasil Dimov
  17. *******************************************************/
  18. #include "univ.i"
  19. #include "dict0dict.h" /* dict_table_stats_lock() */
  20. #include "dict0types.h" /* dict_table_t */
  21. #include "srv0srv.h" /* srv_stats_persistent, srv_stats_auto_recalc */
  22. /*********************************************************************//**
  23. Set the persistent statistics flag for a given table. This is set only
  24. in the in-memory table object and is not saved on disk. It will be read
  25. from the .frm file upon first open from MySQL after a server restart. */
  26. UNIV_INLINE
  27. void
  28. dict_stats_set_persistent(
  29. /*======================*/
  30. dict_table_t* table, /*!< in/out: table */
  31. ibool ps_on, /*!< in: persistent stats explicitly enabled */
  32. ibool ps_off) /*!< in: persistent stats explicitly disabled */
  33. {
  34. /* Not allowed to have both flags set, but a CREATE or ALTER
  35. statement that contains "STATS_PERSISTENT=0 STATS_PERSISTENT=1" would
  36. end up having both set. In this case we clear the OFF flag. */
  37. if (ps_on && ps_off) {
  38. ps_off = FALSE;
  39. }
  40. ib_uint32_t stat_persistent = 0;
  41. if (ps_on) {
  42. stat_persistent |= DICT_STATS_PERSISTENT_ON;
  43. }
  44. if (ps_off) {
  45. stat_persistent |= DICT_STATS_PERSISTENT_OFF;
  46. }
  47. /* we rely on this assignment to be atomic */
  48. table->stat_persistent = stat_persistent;
  49. }
  50. /*********************************************************************//**
  51. Check whether persistent statistics is enabled for a given table.
  52. @return TRUE if enabled, FALSE otherwise */
  53. UNIV_INLINE
  54. ibool
  55. dict_stats_is_persistent_enabled(
  56. /*=============================*/
  57. const dict_table_t* table) /*!< in: table */
  58. {
  59. /* Because of the nature of this check (non-locking) it is possible
  60. that a table becomes:
  61. * PS-disabled immediately after this function has returned TRUE or
  62. * PS-enabled immediately after this function has returned FALSE.
  63. This means that it is possible that we do:
  64. + dict_stats_update(DICT_STATS_RECALC_PERSISTENT) on a table that has
  65. just been PS-disabled or
  66. + dict_stats_update(DICT_STATS_RECALC_TRANSIENT) on a table that has
  67. just been PS-enabled.
  68. This is acceptable. Avoiding this would mean that we would have to
  69. protect the ::stat_persistent with dict_table_stats_lock() like the
  70. other ::stat_ members which would be too big performance penalty,
  71. especially when this function is called from
  72. row_update_statistics_if_needed(). */
  73. /* we rely on this read to be atomic */
  74. ib_uint32_t stat_persistent = table->stat_persistent;
  75. if (stat_persistent & DICT_STATS_PERSISTENT_ON) {
  76. ut_ad(!(stat_persistent & DICT_STATS_PERSISTENT_OFF));
  77. return(TRUE);
  78. } else if (stat_persistent & DICT_STATS_PERSISTENT_OFF) {
  79. return(FALSE);
  80. } else {
  81. return(srv_stats_persistent);
  82. }
  83. }
  84. /*********************************************************************//**
  85. Set the auto recalc flag for a given table (only honored for a persistent
  86. stats enabled table). The flag is set only in the in-memory table object
  87. and is not saved in InnoDB files. It will be read from the .frm file upon
  88. first open from MySQL after a server restart. */
  89. UNIV_INLINE
  90. void
  91. dict_stats_auto_recalc_set(
  92. /*=======================*/
  93. dict_table_t* table, /*!< in/out: table */
  94. ibool auto_recalc_on, /*!< in: explicitly enabled */
  95. ibool auto_recalc_off) /*!< in: explicitly disabled */
  96. {
  97. ut_ad(!auto_recalc_on || !auto_recalc_off);
  98. ib_uint32_t stats_auto_recalc = 0;
  99. if (auto_recalc_on) {
  100. stats_auto_recalc |= DICT_STATS_AUTO_RECALC_ON;
  101. }
  102. if (auto_recalc_off) {
  103. stats_auto_recalc |= DICT_STATS_AUTO_RECALC_OFF;
  104. }
  105. /* we rely on this assignment to be atomic */
  106. table->stats_auto_recalc = stats_auto_recalc;
  107. }
  108. /*********************************************************************//**
  109. Check whether auto recalc is enabled for a given table.
  110. @return TRUE if enabled, FALSE otherwise */
  111. UNIV_INLINE
  112. ibool
  113. dict_stats_auto_recalc_is_enabled(
  114. /*==============================*/
  115. const dict_table_t* table) /*!< in: table */
  116. {
  117. /* we rely on this read to be atomic */
  118. ib_uint32_t stats_auto_recalc = table->stats_auto_recalc;
  119. if (stats_auto_recalc & DICT_STATS_AUTO_RECALC_ON) {
  120. ut_ad(!(stats_auto_recalc & DICT_STATS_AUTO_RECALC_OFF));
  121. return(TRUE);
  122. } else if (stats_auto_recalc & DICT_STATS_AUTO_RECALC_OFF) {
  123. return(FALSE);
  124. } else {
  125. return(srv_stats_auto_recalc);
  126. }
  127. }
  128. /*********************************************************************//**
  129. Initialize table's stats for the first time when opening a table. */
  130. UNIV_INLINE
  131. void
  132. dict_stats_init(
  133. /*============*/
  134. dict_table_t* table) /*!< in/out: table */
  135. {
  136. ut_ad(!mutex_own(&dict_sys->mutex));
  137. if (table->stat_initialized) {
  138. return;
  139. }
  140. dict_stats_upd_option_t opt;
  141. if (dict_stats_is_persistent_enabled(table)) {
  142. opt = DICT_STATS_FETCH_ONLY_IF_NOT_IN_MEMORY;
  143. } else {
  144. opt = DICT_STATS_RECALC_TRANSIENT;
  145. }
  146. dict_stats_update(table, opt);
  147. }
  148. /*********************************************************************//**
  149. Deinitialize table's stats after the last close of the table. This is
  150. used to detect "FLUSH TABLE" and refresh the stats upon next open. */
  151. UNIV_INLINE
  152. void
  153. dict_stats_deinit(
  154. /*==============*/
  155. dict_table_t* table) /*!< in/out: table */
  156. {
  157. ut_ad(mutex_own(&dict_sys->mutex));
  158. ut_a(table->n_ref_count == 0);
  159. dict_table_stats_lock(table, RW_X_LATCH);
  160. if (!table->stat_initialized) {
  161. dict_table_stats_unlock(table, RW_X_LATCH);
  162. return;
  163. }
  164. table->stat_initialized = FALSE;
  165. #ifdef UNIV_DEBUG_VALGRIND
  166. UNIV_MEM_INVALID(&table->stat_n_rows,
  167. sizeof(table->stat_n_rows));
  168. UNIV_MEM_INVALID(&table->stat_clustered_index_size,
  169. sizeof(table->stat_clustered_index_size));
  170. UNIV_MEM_INVALID(&table->stat_sum_of_other_index_sizes,
  171. sizeof(table->stat_sum_of_other_index_sizes));
  172. UNIV_MEM_INVALID(&table->stat_modified_counter,
  173. sizeof(table->stat_modified_counter));
  174. dict_index_t* index;
  175. for (index = dict_table_get_first_index(table);
  176. index != NULL;
  177. index = dict_table_get_next_index(index)) {
  178. ulint n_uniq = dict_index_get_n_unique(index);
  179. UNIV_MEM_INVALID(
  180. index->stat_n_diff_key_vals,
  181. n_uniq * sizeof(index->stat_n_diff_key_vals[0]));
  182. UNIV_MEM_INVALID(
  183. index->stat_n_sample_sizes,
  184. n_uniq * sizeof(index->stat_n_sample_sizes[0]));
  185. UNIV_MEM_INVALID(
  186. index->stat_n_non_null_key_vals,
  187. n_uniq * sizeof(index->stat_n_non_null_key_vals[0]));
  188. UNIV_MEM_INVALID(
  189. &index->stat_index_size,
  190. sizeof(index->stat_index_size));
  191. UNIV_MEM_INVALID(
  192. &index->stat_n_leaf_pages,
  193. sizeof(index->stat_n_leaf_pages));
  194. }
  195. #endif /* UNIV_DEBUG_VALGRIND */
  196. dict_table_stats_unlock(table, RW_X_LATCH);
  197. }