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.

283 lines
10 KiB

9 years ago
MDEV-16283 ALTER TABLE...DISCARD TABLESPACE still takes long on a large buffer pool Also fixes MDEV-14727, MDEV-14491 InnoDB: Error: Waited for 5 secs for hash index ref_count (1) to drop to 0 by replacing the flawed wait logic in dict_index_remove_from_cache_low(). On DISCARD TABLESPACE, there is no need to drop the adaptive hash index. We must drop it on IMPORT TABLESPACE, and eventually on DROP TABLE or DROP INDEX. As long as the dict_index_t object remains in the cache and the table remains inaccessible, the adaptive hash index entries to orphaned pages would not do any harm. They would be dropped when buffer pool pages are reused for something else. btr_search_drop_page_hash_when_freed(), buf_LRU_drop_page_hash_batch(): Remove the parameter zip_size, and pass 0 to buf_page_get_gen(). buf_page_get_gen(): Ignore zip_size if mode==BUF_PEEK_IF_IN_POOL. buf_LRU_drop_page_hash_for_tablespace(): Drop the adaptive hash index even if the tablespace is inaccessible. buf_LRU_drop_page_hash_for_tablespace(): New global function, to drop the adaptive hash index. buf_LRU_flush_or_remove_pages(), fil_delete_tablespace(): Remove the parameter drop_ahi. dict_index_remove_from_cache_low(): Actively drop the adaptive hash index if entries exist. This should prevent InnoDB hangs on DROP TABLE or DROP INDEX. row_import_for_mysql(): Drop any adaptive hash index entries for the table. row_drop_table_for_mysql(): Drop any adaptive hash index for the table, except if the table resides in the system tablespace. (DISCARD TABLESPACE does not apply to the system tablespace, and we do no want to drop the adaptive hash index for other tables than the one that is being dropped.) row_truncate_table_for_mysql(): Drop any adaptive hash index entries for the table, except if the table resides in the system tablespace.
7 years ago
9 years ago
MDEV-16283 ALTER TABLE...DISCARD TABLESPACE still takes long on a large buffer pool Also fixes MDEV-14727, MDEV-14491 InnoDB: Error: Waited for 5 secs for hash index ref_count (1) to drop to 0 by replacing the flawed wait logic in dict_index_remove_from_cache_low(). On DISCARD TABLESPACE, there is no need to drop the adaptive hash index. We must drop it on IMPORT TABLESPACE, and eventually on DROP TABLE or DROP INDEX. As long as the dict_index_t object remains in the cache and the table remains inaccessible, the adaptive hash index entries to orphaned pages would not do any harm. They would be dropped when buffer pool pages are reused for something else. btr_search_drop_page_hash_when_freed(), buf_LRU_drop_page_hash_batch(): Remove the parameter zip_size, and pass 0 to buf_page_get_gen(). buf_page_get_gen(): Ignore zip_size if mode==BUF_PEEK_IF_IN_POOL. buf_LRU_drop_page_hash_for_tablespace(): Drop the adaptive hash index even if the tablespace is inaccessible. buf_LRU_drop_page_hash_for_tablespace(): New global function, to drop the adaptive hash index. buf_LRU_flush_or_remove_pages(), fil_delete_tablespace(): Remove the parameter drop_ahi. dict_index_remove_from_cache_low(): Actively drop the adaptive hash index if entries exist. This should prevent InnoDB hangs on DROP TABLE or DROP INDEX. row_import_for_mysql(): Drop any adaptive hash index entries for the table. row_drop_table_for_mysql(): Drop any adaptive hash index for the table, except if the table resides in the system tablespace. (DISCARD TABLESPACE does not apply to the system tablespace, and we do no want to drop the adaptive hash index for other tables than the one that is being dropped.) row_truncate_table_for_mysql(): Drop any adaptive hash index entries for the table, except if the table resides in the system tablespace.
7 years ago
MDEV-16283 ALTER TABLE...DISCARD TABLESPACE still takes long on a large buffer pool Also fixes MDEV-14727, MDEV-14491 InnoDB: Error: Waited for 5 secs for hash index ref_count (1) to drop to 0 by replacing the flawed wait logic in dict_index_remove_from_cache_low(). On DISCARD TABLESPACE, there is no need to drop the adaptive hash index. We must drop it on IMPORT TABLESPACE, and eventually on DROP TABLE or DROP INDEX. As long as the dict_index_t object remains in the cache and the table remains inaccessible, the adaptive hash index entries to orphaned pages would not do any harm. They would be dropped when buffer pool pages are reused for something else. btr_search_drop_page_hash_when_freed(), buf_LRU_drop_page_hash_batch(): Remove the parameter zip_size, and pass 0 to buf_page_get_gen(). buf_page_get_gen(): Ignore zip_size if mode==BUF_PEEK_IF_IN_POOL. buf_LRU_drop_page_hash_for_tablespace(): Drop the adaptive hash index even if the tablespace is inaccessible. buf_LRU_drop_page_hash_for_tablespace(): New global function, to drop the adaptive hash index. buf_LRU_flush_or_remove_pages(), fil_delete_tablespace(): Remove the parameter drop_ahi. dict_index_remove_from_cache_low(): Actively drop the adaptive hash index if entries exist. This should prevent InnoDB hangs on DROP TABLE or DROP INDEX. row_import_for_mysql(): Drop any adaptive hash index entries for the table. row_drop_table_for_mysql(): Drop any adaptive hash index for the table, except if the table resides in the system tablespace. (DISCARD TABLESPACE does not apply to the system tablespace, and we do no want to drop the adaptive hash index for other tables than the one that is being dropped.) row_truncate_table_for_mysql(): Drop any adaptive hash index entries for the table, except if the table resides in the system tablespace.
7 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2018, MariaDB Corporation.
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
  13. *****************************************************************************/
  14. /********************************************************************//**
  15. @file include/btr0sea.h
  16. The index tree adaptive search
  17. Created 2/17/1996 Heikki Tuuri
  18. *************************************************************************/
  19. #ifndef btr0sea_h
  20. #define btr0sea_h
  21. #include "univ.i"
  22. #include "rem0rec.h"
  23. #include "dict0dict.h"
  24. #include "btr0types.h"
  25. #include "mtr0mtr.h"
  26. #include "ha0ha.h"
  27. /*****************************************************************//**
  28. Creates and initializes the adaptive search system at a database start. */
  29. UNIV_INTERN
  30. void
  31. btr_search_sys_create(
  32. /*==================*/
  33. ulint hash_size); /*!< in: hash index hash table size */
  34. /*****************************************************************//**
  35. Frees the adaptive search system at a database shutdown. */
  36. UNIV_INTERN
  37. void
  38. btr_search_sys_free(void);
  39. /*=====================*/
  40. /********************************************************************//**
  41. Disable the adaptive hash search system and empty the index. */
  42. UNIV_INTERN
  43. void
  44. btr_search_disable(void);
  45. /*====================*/
  46. /********************************************************************//**
  47. Enable the adaptive hash search system. */
  48. UNIV_INTERN
  49. void
  50. btr_search_enable(void);
  51. /*====================*/
  52. /********************************************************************//**
  53. Returns search info for an index.
  54. @return search info; search mutex reserved */
  55. UNIV_INLINE
  56. btr_search_t*
  57. btr_search_get_info(
  58. /*================*/
  59. dict_index_t* index) /*!< in: index */
  60. MY_ATTRIBUTE((nonnull));
  61. /*****************************************************************//**
  62. Creates and initializes a search info struct.
  63. @return own: search info struct */
  64. UNIV_INTERN
  65. btr_search_t*
  66. btr_search_info_create(
  67. /*===================*/
  68. mem_heap_t* heap); /*!< in: heap where created */
  69. /*****************************************************************//**
  70. Returns the value of ref_count. The value is protected by
  71. btr_search_latch.
  72. @return ref_count value. */
  73. UNIV_INTERN
  74. ulint
  75. btr_search_info_get_ref_count(
  76. /*==========================*/
  77. btr_search_t* info); /*!< in: search info. */
  78. /*********************************************************************//**
  79. Updates the search info. */
  80. UNIV_INLINE
  81. void
  82. btr_search_info_update(
  83. /*===================*/
  84. dict_index_t* index, /*!< in: index of the cursor */
  85. btr_cur_t* cursor);/*!< in: cursor which was just positioned */
  86. /******************************************************************//**
  87. Tries to guess the right search position based on the hash search info
  88. of the index. Note that if mode is PAGE_CUR_LE, which is used in inserts,
  89. and the function returns TRUE, then cursor->up_match and cursor->low_match
  90. both have sensible values.
  91. @return TRUE if succeeded */
  92. UNIV_INTERN
  93. ibool
  94. btr_search_guess_on_hash(
  95. /*=====================*/
  96. dict_index_t* index, /*!< in: index */
  97. btr_search_t* info, /*!< in: index search info */
  98. const dtuple_t* tuple, /*!< in: logical record */
  99. ulint mode, /*!< in: PAGE_CUR_L, ... */
  100. ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */
  101. btr_cur_t* cursor, /*!< out: tree cursor */
  102. ulint has_search_latch,/*!< in: latch mode the caller
  103. currently has on btr_search_latch:
  104. RW_S_LATCH, RW_X_LATCH, or 0 */
  105. mtr_t* mtr); /*!< in: mtr */
  106. /********************************************************************//**
  107. Moves or deletes hash entries for moved records. If new_page is already hashed,
  108. then the hash index for page, if any, is dropped. If new_page is not hashed,
  109. and page is hashed, then a new hash index is built to new_page with the same
  110. parameters as page (this often happens when a page is split). */
  111. UNIV_INTERN
  112. void
  113. btr_search_move_or_delete_hash_entries(
  114. /*===================================*/
  115. buf_block_t* new_block, /*!< in: records are copied
  116. to this page */
  117. buf_block_t* block, /*!< in: index page from which
  118. records were copied, and the
  119. copied records will be deleted
  120. from this page */
  121. dict_index_t* index); /*!< in: record descriptor */
  122. /********************************************************************//**
  123. Drops a page hash index. */
  124. UNIV_INTERN
  125. void
  126. btr_search_drop_page_hash_index(
  127. /*============================*/
  128. buf_block_t* block); /*!< in: block containing index page,
  129. s- or x-latched, or an index page
  130. for which we know that
  131. block->buf_fix_count == 0 */
  132. /** Drop possible adaptive hash index entries when a page is evicted
  133. from the buffer pool or freed in a file, or the index is being dropped. */
  134. UNIV_INTERN
  135. void
  136. btr_search_drop_page_hash_when_freed(ulint space, ulint page_no);
  137. /********************************************************************//**
  138. Updates the page hash index when a single record is inserted on a page. */
  139. UNIV_INTERN
  140. void
  141. btr_search_update_hash_node_on_insert(
  142. /*==================================*/
  143. btr_cur_t* cursor);/*!< in: cursor which was positioned to the
  144. place to insert using btr_cur_search_...,
  145. and the new record has been inserted next
  146. to the cursor */
  147. /********************************************************************//**
  148. Updates the page hash index when a single record is inserted on a page. */
  149. UNIV_INTERN
  150. void
  151. btr_search_update_hash_on_insert(
  152. /*=============================*/
  153. btr_cur_t* cursor);/*!< in: cursor which was positioned to the
  154. place to insert using btr_cur_search_...,
  155. and the new record has been inserted next
  156. to the cursor */
  157. /********************************************************************//**
  158. Updates the page hash index when a single record is deleted from a page. */
  159. UNIV_INTERN
  160. void
  161. btr_search_update_hash_on_delete(
  162. /*=============================*/
  163. btr_cur_t* cursor);/*!< in: cursor which was positioned on the
  164. record to delete using btr_cur_search_...,
  165. the record is not yet deleted */
  166. #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
  167. /********************************************************************//**
  168. Validates the search system.
  169. @return TRUE if ok */
  170. UNIV_INTERN
  171. ibool
  172. btr_search_validate(void);
  173. /*======================*/
  174. #endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */
  175. /** The search info struct in an index */
  176. struct btr_search_t{
  177. ulint ref_count; /*!< Number of blocks in this index tree
  178. that have search index built
  179. i.e. block->index points to this index.
  180. Protected by btr_search_latch except
  181. when during initialization in
  182. btr_search_info_create(). */
  183. /* @{ The following fields are not protected by any latch.
  184. Unfortunately, this means that they must be aligned to
  185. the machine word, i.e., they cannot be turned into bit-fields. */
  186. buf_block_t* root_guess;/*!< the root page frame when it was last time
  187. fetched, or NULL */
  188. ulint hash_analysis; /*!< when this exceeds
  189. BTR_SEARCH_HASH_ANALYSIS, the hash
  190. analysis starts; this is reset if no
  191. success noticed */
  192. ibool last_hash_succ; /*!< TRUE if the last search would have
  193. succeeded, or did succeed, using the hash
  194. index; NOTE that the value here is not exact:
  195. it is not calculated for every search, and the
  196. calculation itself is not always accurate! */
  197. ulint n_hash_potential;
  198. /*!< number of consecutive searches
  199. which would have succeeded, or did succeed,
  200. using the hash index;
  201. the range is 0 .. BTR_SEARCH_BUILD_LIMIT + 5 */
  202. /* @} */
  203. /*---------------------- @{ */
  204. ulint n_fields; /*!< recommended prefix length for hash search:
  205. number of full fields */
  206. ulint n_bytes; /*!< recommended prefix: number of bytes in
  207. an incomplete field
  208. @see BTR_PAGE_MAX_REC_SIZE */
  209. ibool left_side; /*!< TRUE or FALSE, depending on whether
  210. the leftmost record of several records with
  211. the same prefix should be indexed in the
  212. hash index */
  213. /*---------------------- @} */
  214. #ifdef UNIV_SEARCH_PERF_STAT
  215. ulint n_hash_succ; /*!< number of successful hash searches thus
  216. far */
  217. ulint n_hash_fail; /*!< number of failed hash searches */
  218. ulint n_patt_succ; /*!< number of successful pattern searches thus
  219. far */
  220. ulint n_searches; /*!< number of searches */
  221. #endif /* UNIV_SEARCH_PERF_STAT */
  222. #ifdef UNIV_DEBUG
  223. ulint magic_n; /*!< magic number @see BTR_SEARCH_MAGIC_N */
  224. /** value of btr_search_t::magic_n, used in assertions */
  225. # define BTR_SEARCH_MAGIC_N 1112765
  226. #endif /* UNIV_DEBUG */
  227. };
  228. /** The hash index system */
  229. struct btr_search_sys_t{
  230. hash_table_t* hash_index; /*!< the adaptive hash index,
  231. mapping dtuple_fold values
  232. to rec_t pointers on index pages */
  233. };
  234. /** The adaptive hash index */
  235. extern btr_search_sys_t* btr_search_sys;
  236. #ifdef UNIV_SEARCH_PERF_STAT
  237. /** Number of successful adaptive hash index lookups */
  238. extern ulint btr_search_n_succ;
  239. /** Number of failed adaptive hash index lookups */
  240. extern ulint btr_search_n_hash_fail;
  241. #endif /* UNIV_SEARCH_PERF_STAT */
  242. /** After change in n_fields or n_bytes in info, this many rounds are waited
  243. before starting the hash analysis again: this is to save CPU time when there
  244. is no hope in building a hash index. */
  245. #define BTR_SEARCH_HASH_ANALYSIS 17
  246. /** Limit of consecutive searches for trying a search shortcut on the search
  247. pattern */
  248. #define BTR_SEARCH_ON_PATTERN_LIMIT 3
  249. /** Limit of consecutive searches for trying a search shortcut using
  250. the hash index */
  251. #define BTR_SEARCH_ON_HASH_LIMIT 3
  252. /** We do this many searches before trying to keep the search latch
  253. over calls from MySQL. If we notice someone waiting for the latch, we
  254. again set this much timeout. This is to reduce contention. */
  255. #define BTR_SEA_TIMEOUT 10000
  256. #ifndef UNIV_NONINL
  257. #include "btr0sea.ic"
  258. #endif
  259. #endif