1
/************************************************************************
2
The index tree adaptive search
6
Created 2/17/1996 Heikki Tuuri
7
*************************************************************************/
15
#include "dict0dict.h"
16
#include "btr0types.h"
20
/*********************************************************************
21
Creates and initializes the adaptive search system at a database start. */
24
btr_search_sys_create(
25
/*==================*/
26
ulint hash_size); /* in: hash index hash table size */
28
/************************************************************************
29
Disable the adaptive hash search system and empty the index. */
32
btr_search_disable(void);
33
/*====================*/
34
/************************************************************************
35
Enable the adaptive hash search system. */
38
btr_search_enable(void);
39
/*====================*/
41
/************************************************************************
42
Returns search info for an index. */
47
/* out: search info; search mutex reserved */
48
dict_index_t* index); /* in: index */
49
/*********************************************************************
50
Creates and initializes a search info struct. */
53
btr_search_info_create(
54
/*===================*/
55
/* out, own: search info struct */
56
mem_heap_t* heap); /* in: heap where created */
57
/*********************************************************************
58
Returns the value of ref_count. The value is protected by
62
btr_search_info_get_ref_count(
63
/*==========================*/
64
/* out: ref_count value. */
65
btr_search_t* info); /* in: search info. */
66
/*************************************************************************
67
Updates the search info. */
70
btr_search_info_update(
71
/*===================*/
72
dict_index_t* index, /* in: index of the cursor */
73
btr_cur_t* cursor);/* in: cursor which was just positioned */
74
/**********************************************************************
75
Tries to guess the right search position based on the hash search info
76
of the index. Note that if mode is PAGE_CUR_LE, which is used in inserts,
77
and the function returns TRUE, then cursor->up_match and cursor->low_match
78
both have sensible values. */
81
btr_search_guess_on_hash(
82
/*=====================*/
83
/* out: TRUE if succeeded */
84
dict_index_t* index, /* in: index */
85
btr_search_t* info, /* in: index search info */
86
const dtuple_t* tuple, /* in: logical record */
87
ulint mode, /* in: PAGE_CUR_L, ... */
88
ulint latch_mode, /* in: BTR_SEARCH_LEAF, ... */
89
btr_cur_t* cursor, /* out: tree cursor */
90
ulint has_search_latch,/* in: latch mode the caller
91
currently has on btr_search_latch:
92
RW_S_LATCH, RW_X_LATCH, or 0 */
93
mtr_t* mtr); /* in: mtr */
94
/************************************************************************
95
Moves or deletes hash entries for moved records. If new_page is already hashed,
96
then the hash index for page, if any, is dropped. If new_page is not hashed,
97
and page is hashed, then a new hash index is built to new_page with the same
98
parameters as page (this often happens when a page is split). */
101
btr_search_move_or_delete_hash_entries(
102
/*===================================*/
103
buf_block_t* new_block, /* in: records are copied
105
buf_block_t* block, /* in: index page from which
106
records were copied, and the
107
copied records will be deleted
109
dict_index_t* index); /* in: record descriptor */
110
/************************************************************************
111
Drops a page hash index. */
114
btr_search_drop_page_hash_index(
115
/*============================*/
116
buf_block_t* block); /* in: block containing index page,
117
s- or x-latched, or an index page
118
for which we know that
119
block->buf_fix_count == 0 */
120
/************************************************************************
121
Drops a page hash index when a page is freed from a fseg to the file system.
122
Drops possible hash index if the page happens to be in the buffer pool. */
125
btr_search_drop_page_hash_when_freed(
126
/*=================================*/
127
ulint space, /* in: space id */
128
ulint zip_size, /* in: compressed page size in bytes
129
or 0 for uncompressed pages */
130
ulint page_no); /* in: page number */
131
/************************************************************************
132
Updates the page hash index when a single record is inserted on a page. */
135
btr_search_update_hash_node_on_insert(
136
/*==================================*/
137
btr_cur_t* cursor);/* in: cursor which was positioned to the
138
place to insert using btr_cur_search_...,
139
and the new record has been inserted next
141
/************************************************************************
142
Updates the page hash index when a single record is inserted on a page. */
145
btr_search_update_hash_on_insert(
146
/*=============================*/
147
btr_cur_t* cursor);/* in: cursor which was positioned to the
148
place to insert using btr_cur_search_...,
149
and the new record has been inserted next
151
/************************************************************************
152
Updates the page hash index when a single record is deleted from a page. */
155
btr_search_update_hash_on_delete(
156
/*=============================*/
157
btr_cur_t* cursor);/* in: cursor which was positioned on the
158
record to delete using btr_cur_search_...,
159
the record is not yet deleted */
160
/************************************************************************
161
Validates the search system. */
164
btr_search_validate(void);
165
/*======================*/
166
/* out: TRUE if ok */
168
/* Flag: has the search system been disabled? */
169
extern ibool btr_search_disabled;
171
/* The search info struct in an index */
173
struct btr_search_struct{
174
ulint ref_count; /* Number of blocks in this index tree
175
that have search index built
176
i.e. block->index points to this index.
177
Protected by btr_search_latch except
178
when during initialization in
179
btr_search_info_create(). */
181
/* The following fields are not protected by any latch.
182
Unfortunately, this means that they must be aligned to
183
the machine word, i.e., they cannot be turned into bit-fields. */
184
buf_block_t* root_guess;/* the root page frame when it was last time
186
ulint hash_analysis; /* when this exceeds BTR_SEARCH_HASH_ANALYSIS,
187
the hash analysis starts; this is reset if no
189
ibool last_hash_succ; /* TRUE if the last search would have
190
succeeded, or did succeed, using the hash
191
index; NOTE that the value here is not exact:
192
it is not calculated for every search, and the
193
calculation itself is not always accurate! */
194
ulint n_hash_potential;
195
/* number of consecutive searches
196
which would have succeeded, or did succeed,
197
using the hash index;
198
the range is 0 .. BTR_SEARCH_BUILD_LIMIT + 5 */
199
/*----------------------*/
200
ulint n_fields; /* recommended prefix length for hash search:
201
number of full fields */
202
ulint n_bytes; /* recommended prefix: number of bytes in
204
see also BTR_PAGE_MAX_REC_SIZE */
205
ibool left_side; /* TRUE or FALSE, depending on whether
206
the leftmost record of several records with
207
the same prefix should be indexed in the
209
/*----------------------*/
210
#ifdef UNIV_SEARCH_PERF_STAT
211
ulint n_hash_succ; /* number of successful hash searches thus
213
ulint n_hash_fail; /* number of failed hash searches */
214
ulint n_patt_succ; /* number of successful pattern searches thus
216
ulint n_searches; /* number of searches */
217
#endif /* UNIV_SEARCH_PERF_STAT */
219
ulint magic_n; /* magic number */
220
# define BTR_SEARCH_MAGIC_N 1112765
221
#endif /* UNIV_DEBUG */
224
/* The hash index system */
226
typedef struct btr_search_sys_struct btr_search_sys_t;
228
struct btr_search_sys_struct{
229
hash_table_t* hash_index;
232
extern btr_search_sys_t* btr_search_sys;
234
/* The latch protecting the adaptive search system: this latch protects the
236
(2) columns of a record to which we have a pointer in the hash index;
238
but does NOT protect:
240
(3) next record offset field in a record;
241
(4) next or previous records on the same page.
243
Bear in mind (3) and (4) when using the hash index.
246
extern rw_lock_t* btr_search_latch_temp;
248
#define btr_search_latch (*btr_search_latch_temp)
250
#ifdef UNIV_SEARCH_PERF_STAT
251
extern ulint btr_search_n_succ;
252
extern ulint btr_search_n_hash_fail;
253
#endif /* UNIV_SEARCH_PERF_STAT */
255
/* After change in n_fields or n_bytes in info, this many rounds are waited
256
before starting the hash analysis again: this is to save CPU time when there
257
is no hope in building a hash index. */
259
#define BTR_SEARCH_HASH_ANALYSIS 17
261
/* Limit of consecutive searches for trying a search shortcut on the search
264
#define BTR_SEARCH_ON_PATTERN_LIMIT 3
266
/* Limit of consecutive searches for trying a search shortcut using the hash
269
#define BTR_SEARCH_ON_HASH_LIMIT 3
271
/* We do this many searches before trying to keep the search latch over calls
272
from MySQL. If we notice someone waiting for the latch, we again set this
273
much timeout. This is to reduce contention. */
275
#define BTR_SEA_TIMEOUT 10000
278
#include "btr0sea.ic"