]> git.pld-linux.org Git - packages/mysql.git/blame - innodb_stats.patch
- up to 5.5.11
[packages/mysql.git] / innodb_stats.patch
CommitLineData
b4e1fa2c
AM
1# name : innodb_stats.patch
2# introduced : 11 or before
3# maintainer : Yasufumi
4#
5#!!! notice !!!
6# Any small change to this file in the main branch
7# should be done or reviewed by the maintainer!
b4e1fa2c
AM
8diff -ruN a/storage/innobase/dict/dict0boot.c b/storage/innobase/dict/dict0boot.c
9--- a/storage/innobase/dict/dict0boot.c 2010-12-03 15:48:03.034036843 +0900
10+++ b/storage/innobase/dict/dict0boot.c 2010-12-03 17:19:24.835112632 +0900
11@@ -266,6 +266,29 @@
12 /* Get the dictionary header */
13 dict_hdr = dict_hdr_get(&mtr);
14
15+ if (mach_read_from_8(dict_hdr + DICT_HDR_XTRADB_MARK)
16+ != DICT_HDR_XTRADB_FLAG) {
17+ /* not extended yet by XtraDB, need to be extended */
18+ ulint root_page_no;
19+
20+ root_page_no = btr_create(DICT_CLUSTERED | DICT_UNIQUE,
21+ DICT_HDR_SPACE, 0, DICT_STATS_ID,
22+ dict_ind_redundant, &mtr);
23+ if (root_page_no == FIL_NULL) {
24+ fprintf(stderr, "InnoDB: Warning: failed to create SYS_STATS btr.\n");
25+ srv_use_sys_stats_table = FALSE;
26+ } else {
27+ mlog_write_ulint(dict_hdr + DICT_HDR_STATS, root_page_no,
28+ MLOG_4BYTES, &mtr);
29+ mlog_write_ull(dict_hdr + DICT_HDR_XTRADB_MARK,
30+ DICT_HDR_XTRADB_FLAG, &mtr);
31+ }
32+ mtr_commit(&mtr);
33+ /* restart mtr */
34+ mtr_start(&mtr);
35+ dict_hdr = dict_hdr_get(&mtr);
36+ }
37+
38 /* Because we only write new row ids to disk-based data structure
39 (dictionary header) when it is divisible by
40 DICT_HDR_ROW_ID_WRITE_MARGIN, in recovery we will not recover
41@@ -425,7 +448,7 @@
42 table->id = DICT_FIELDS_ID;
43 dict_table_add_to_cache(table, heap);
44 dict_sys->sys_fields = table;
45- mem_heap_free(heap);
46+ mem_heap_empty(heap);
47
48 index = dict_mem_index_create("SYS_FIELDS", "CLUST_IND",
49 DICT_HDR_SPACE,
11822e22 50@@ -442,6 +465,45 @@
b4e1fa2c
AM
51 FALSE);
52 ut_a(error == DB_SUCCESS);
53
54+ /*-------------------------*/
11822e22 55+ table = dict_mem_table_create("SYS_STATS", DICT_HDR_SPACE, 4, 0);
b4e1fa2c
AM
56+ table->n_mysql_handles_opened = 1; /* for pin */
57+
58+ dict_mem_table_add_col(table, heap, "INDEX_ID", DATA_BINARY, 0, 0);
59+ dict_mem_table_add_col(table, heap, "KEY_COLS", DATA_INT, 0, 4);
60+ dict_mem_table_add_col(table, heap, "DIFF_VALS", DATA_BINARY, 0, 0);
11822e22 61+ dict_mem_table_add_col(table, heap, "NON_NULL_VALS", DATA_BINARY, 0, 0);
b4e1fa2c
AM
62+
63+ /* The '+ 2' below comes from the fields DB_TRX_ID, DB_ROLL_PTR */
64+#if DICT_SYS_STATS_DIFF_VALS_FIELD != 2 + 2
65+#error "DICT_SYS_STATS_DIFF_VALS_FIELD != 2 + 2"
66+#endif
11822e22
AM
67+#if DICT_SYS_STATS_NON_NULL_VALS_FIELD != 3 + 2
68+#error "DICT_SYS_STATS_NON_NULL_VALS_FIELD != 3 + 2"
69+#endif
b4e1fa2c
AM
70+
71+ table->id = DICT_STATS_ID;
72+ dict_table_add_to_cache(table, heap);
73+ dict_sys->sys_stats = table;
74+ mem_heap_empty(heap);
75+
76+ index = dict_mem_index_create("SYS_STATS", "CLUST_IND",
77+ DICT_HDR_SPACE,
78+ DICT_UNIQUE | DICT_CLUSTERED, 2);
79+
80+ dict_mem_index_add_field(index, "INDEX_ID", 0);
81+ dict_mem_index_add_field(index, "KEY_COLS", 0);
82+
83+ index->id = DICT_STATS_ID;
84+ error = dict_index_add_to_cache(table, index,
85+ mtr_read_ulint(dict_hdr
86+ + DICT_HDR_STATS,
87+ MLOG_4BYTES, &mtr),
88+ FALSE);
89+ ut_a(error == DB_SUCCESS);
90+
91+ mem_heap_free(heap);
92+
93 mtr_commit(&mtr);
94 /*-------------------------*/
95
11822e22 96@@ -455,6 +517,7 @@
b4e1fa2c
AM
97 dict_load_sys_table(dict_sys->sys_columns);
98 dict_load_sys_table(dict_sys->sys_indexes);
99 dict_load_sys_table(dict_sys->sys_fields);
100+ dict_load_sys_table(dict_sys->sys_stats);
101
102 mutex_exit(&(dict_sys->mutex));
103 }
104diff -ruN a/storage/innobase/dict/dict0crea.c b/storage/innobase/dict/dict0crea.c
105--- a/storage/innobase/dict/dict0crea.c 2010-12-03 15:48:03.036081059 +0900
106+++ b/storage/innobase/dict/dict0crea.c 2010-12-03 17:19:24.836964976 +0900
11822e22 107@@ -508,6 +508,56 @@
b4e1fa2c
AM
108 }
109
110 /*****************************************************************//**
111+Based on an index object, this function builds the entry to be inserted
112+in the SYS_STATS system table.
113+@return the tuple which should be inserted */
114+static
115+dtuple_t*
116+dict_create_sys_stats_tuple(
117+/*========================*/
118+ const dict_index_t* index,
119+ ulint i,
120+ mem_heap_t* heap)
121+{
122+ dict_table_t* sys_stats;
123+ dtuple_t* entry;
124+ dfield_t* dfield;
125+ byte* ptr;
126+
127+ ut_ad(index);
128+ ut_ad(heap);
129+
130+ sys_stats = dict_sys->sys_stats;
131+
11822e22 132+ entry = dtuple_create(heap, 4 + DATA_N_SYS_COLS);
b4e1fa2c
AM
133+
134+ dict_table_copy_types(entry, sys_stats);
135+
136+ /* 0: INDEX_ID -----------------------*/
137+ dfield = dtuple_get_nth_field(entry, 0/*INDEX_ID*/);
138+ ptr = mem_heap_alloc(heap, 8);
139+ mach_write_to_8(ptr, index->id);
140+ dfield_set_data(dfield, ptr, 8);
141+ /* 1: KEY_COLS -----------------------*/
142+ dfield = dtuple_get_nth_field(entry, 1/*KEY_COLS*/);
143+ ptr = mem_heap_alloc(heap, 4);
144+ mach_write_to_4(ptr, i);
145+ dfield_set_data(dfield, ptr, 4);
146+ /* 4: DIFF_VALS ----------------------*/
147+ dfield = dtuple_get_nth_field(entry, 2/*DIFF_VALS*/);
148+ ptr = mem_heap_alloc(heap, 8);
149+ mach_write_to_8(ptr, 0); /* initial value is 0 */
150+ dfield_set_data(dfield, ptr, 8);
11822e22
AM
151+ /* 5: NON_NULL_VALS ------------------*/
152+ dfield = dtuple_get_nth_field(entry, 3/*NON_NULL_VALS*/);
153+ ptr = mem_heap_alloc(heap, 8);
154+ mach_write_to_8(ptr, 0); /* initial value is 0 */
155+ dfield_set_data(dfield, ptr, 8);
b4e1fa2c
AM
156+
157+ return(entry);
158+}
159+
160+/*****************************************************************//**
161 Creates the tuple with which the index entry is searched for writing the index
162 tree root page number, if such a tree is created.
163 @return the tuple for search */
11822e22 164@@ -617,6 +667,27 @@
b4e1fa2c
AM
165 }
166
167 /***************************************************************//**
168+Builds a row for storing stats to insert.
169+@return DB_SUCCESS */
170+static
171+ulint
172+dict_build_stats_def_step(
173+/*======================*/
174+ ind_node_t* node)
175+{
176+ dict_index_t* index;
177+ dtuple_t* row;
178+
179+ index = node->index;
180+
181+ row = dict_create_sys_stats_tuple(index, node->stats_no, node->heap);
182+
183+ ins_node_set_new_row(node->stats_def, row);
184+
185+ return(DB_SUCCESS);
186+}
187+
188+/***************************************************************//**
189 Creates an index tree for the index if it is not a member of a cluster.
190 @return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
191 static
11822e22 192@@ -937,6 +1008,49 @@
b4e1fa2c
AM
193 dict_sys->sys_fields, heap);
194 node->field_def->common.parent = node;
195
196+ if (srv_use_sys_stats_table) {
197+ node->stats_def = ins_node_create(INS_DIRECT,
198+ dict_sys->sys_stats, heap);
199+ node->stats_def->common.parent = node;
200+ } else {
201+ node->stats_def = NULL;
202+ }
203+
204+ node->commit_node = commit_node_create(heap);
205+ node->commit_node->common.parent = node;
206+
207+ return(node);
208+}
209+
210+/*********************************************************************//**
211+*/
212+UNIV_INTERN
213+ind_node_t*
214+ind_insert_stats_graph_create(
215+/*==========================*/
216+ dict_index_t* index,
217+ mem_heap_t* heap)
218+{
219+ ind_node_t* node;
220+
221+ node = mem_heap_alloc(heap, sizeof(ind_node_t));
222+
223+ node->common.type = QUE_NODE_INSERT_STATS;
224+
225+ node->index = index;
226+
227+ node->state = INDEX_BUILD_STATS_COLS;
228+ node->page_no = FIL_NULL;
229+ node->heap = mem_heap_create(256);
230+
231+ node->ind_def = NULL;
232+ node->field_def = NULL;
233+
234+ node->stats_def = ins_node_create(INS_DIRECT,
235+ dict_sys->sys_stats, heap);
236+ node->stats_def->common.parent = node;
237+ node->stats_no = 0;
238+
239 node->commit_node = commit_node_create(heap);
240 node->commit_node->common.parent = node;
241
11822e22 242@@ -1087,6 +1201,7 @@
b4e1fa2c
AM
243
244 node->state = INDEX_BUILD_FIELD_DEF;
245 node->field_no = 0;
246+ node->stats_no = 0;
247
248 thr->run_node = node->ind_def;
249
11822e22 250@@ -1132,7 +1247,31 @@
b4e1fa2c
AM
251 goto function_exit;
252 }
253
254- node->state = INDEX_CREATE_INDEX_TREE;
255+ if (srv_use_sys_stats_table
256+ && !((node->table->flags >> DICT_TF2_SHIFT) & DICT_TF2_TEMPORARY)) {
257+ node->state = INDEX_BUILD_STATS_COLS;
258+ } else {
259+ node->state = INDEX_CREATE_INDEX_TREE;
260+ }
261+ }
262+ if (node->state == INDEX_BUILD_STATS_COLS) {
263+ if (node->stats_no <= dict_index_get_n_unique(node->index)) {
264+
265+ err = dict_build_stats_def_step(node);
266+
267+ if (err != DB_SUCCESS) {
268+
269+ goto function_exit;
270+ }
271+
272+ node->stats_no++;
273+
274+ thr->run_node = node->stats_def;
275+
276+ return(thr);
277+ } else {
278+ node->state = INDEX_CREATE_INDEX_TREE;
279+ }
280 }
281
282 if (node->state == INDEX_CREATE_INDEX_TREE) {
11822e22 283@@ -1178,6 +1317,66 @@
b4e1fa2c
AM
284 return(NULL);
285 }
286
287+ thr->run_node = que_node_get_parent(node);
288+
289+ return(thr);
290+}
291+
292+/****************************************************************//**
293+*/
294+UNIV_INTERN
295+que_thr_t*
296+dict_insert_stats_step(
297+/*===================*/
298+ que_thr_t* thr) /*!< in: query thread */
299+{
300+ ind_node_t* node;
301+ ulint err = DB_ERROR;
302+ trx_t* trx;
303+
304+ ut_ad(thr);
305+
306+ trx = thr_get_trx(thr);
307+
308+ node = thr->run_node;
309+
310+ if (thr->prev_node == que_node_get_parent(node)) {
311+ node->state = INDEX_BUILD_STATS_COLS;
312+ }
313+
314+ if (node->state == INDEX_BUILD_STATS_COLS) {
315+ if (node->stats_no <= dict_index_get_n_unique(node->index)) {
316+
317+ err = dict_build_stats_def_step(node);
318+
319+ if (err != DB_SUCCESS) {
320+
321+ goto function_exit;
322+ }
323+
324+ node->stats_no++;
325+
326+ thr->run_node = node->stats_def;
327+
328+ return(thr);
329+ } else {
330+ node->state = INDEX_COMMIT_WORK;
331+ }
332+ }
333+
334+ if (node->state == INDEX_COMMIT_WORK) {
335+
336+ /* do not commit transaction here for now */
337+ }
338+
339+function_exit:
340+ trx->error_state = err;
341+
342+ if (err == DB_SUCCESS) {
343+ } else {
344+ return(NULL);
345+ }
346+
347 thr->run_node = que_node_get_parent(node);
348
349 return(thr);
350diff -ruN a/storage/innobase/dict/dict0dict.c b/storage/innobase/dict/dict0dict.c
351--- a/storage/innobase/dict/dict0dict.c 2010-12-03 15:48:03.040222428 +0900
352+++ b/storage/innobase/dict/dict0dict.c 2010-12-03 17:19:24.841947690 +0900
d8778560 353@@ -756,7 +756,7 @@
b4e1fa2c
AM
354 print an error message and return without doing
355 anything. */
356 dict_update_statistics(table, TRUE /* only update stats
357- if they have not been initialized */);
358+ if they have not been initialized */, FALSE);
359 }
360
361 return(table);
11822e22 362@@ -4310,6 +4310,289 @@
b4e1fa2c
AM
363 }
364
365 /*********************************************************************//**
366+functions to use SYS_STATS system table. */
367+static
368+ibool
369+dict_reload_statistics(
370+/*===================*/
371+ dict_table_t* table,
372+ ulint* sum_of_index_sizes)
373+{
374+ dict_index_t* index;
375+ ulint size;
376+ mem_heap_t* heap;
377+
378+ index = dict_table_get_first_index(table);
379+
380+ if (index == NULL) {
381+ /* Table definition is corrupt */
382+
383+ return(FALSE);
384+ }
385+
386+ heap = mem_heap_create(1000);
387+
388+ while (index) {
389+ size = btr_get_size(index, BTR_TOTAL_SIZE);
390+
391+ index->stat_index_size = size;
392+
393+ *sum_of_index_sizes += size;
394+
395+ size = btr_get_size(index, BTR_N_LEAF_PAGES);
396+
397+ if (size == 0) {
398+ /* The root node of the tree is a leaf */
399+ size = 1;
400+ }
401+
402+ index->stat_n_leaf_pages = size;
403+
404+/*===========================================*/
405+{
406+ dict_table_t* sys_stats;
407+ dict_index_t* sys_index;
408+ btr_pcur_t pcur;
409+ dtuple_t* tuple;
410+ dfield_t* dfield;
411+ ulint key_cols;
412+ ulint n_cols;
413+ const rec_t* rec;
11822e22 414+ ulint n_fields;
b4e1fa2c
AM
415+ const byte* field;
416+ ulint len;
417+ ib_int64_t* stat_n_diff_key_vals_tmp;
11822e22 418+ ib_int64_t* stat_n_non_null_key_vals_tmp;
b4e1fa2c
AM
419+ byte* buf;
420+ ulint i;
421+ mtr_t mtr;
422+
423+ n_cols = dict_index_get_n_unique(index);
424+ stat_n_diff_key_vals_tmp = mem_heap_zalloc(heap, (n_cols + 1) * sizeof(ib_int64_t));
11822e22 425+ stat_n_non_null_key_vals_tmp = mem_heap_zalloc(heap, (n_cols + 1) * sizeof(ib_int64_t));
b4e1fa2c
AM
426+
427+ sys_stats = dict_sys->sys_stats;
428+ sys_index = UT_LIST_GET_FIRST(sys_stats->indexes);
429+ ut_a(!dict_table_is_comp(sys_stats));
430+
431+ tuple = dtuple_create(heap, 1);
432+ dfield = dtuple_get_nth_field(tuple, 0);
433+
434+ buf = mem_heap_alloc(heap, 8);
435+ mach_write_to_8(buf, index->id);
436+
437+ dfield_set_data(dfield, buf, 8);
438+ dict_index_copy_types(tuple, sys_index, 1);
439+
440+ mtr_start(&mtr);
441+
442+ btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
443+ BTR_SEARCH_LEAF, &pcur, &mtr);
444+ for (i = 0; i <= n_cols; i++) {
445+ rec = btr_pcur_get_rec(&pcur);
446+
447+ if (!btr_pcur_is_on_user_rec(&pcur)
448+ || mach_read_from_8(rec_get_nth_field_old(rec, 0, &len))
449+ != index->id) {
450+ /* not found: even 1 if not found should not be alowed */
451+ fprintf(stderr, "InnoDB: Warning: stats for %s/%s (%lu/%lu)"
d8778560 452+ " not found in SYS_STATS\n",
b4e1fa2c
AM
453+ index->table_name, index->name, i, n_cols);
454+ btr_pcur_close(&pcur);
455+ mtr_commit(&mtr);
456+ mem_heap_free(heap);
457+ return(FALSE);
458+ }
459+
460+ if (rec_get_deleted_flag(rec, 0)) {
11822e22
AM
461+ /* don't count */
462+ i--;
b4e1fa2c
AM
463+ goto next_rec;
464+ }
465+
11822e22
AM
466+ n_fields = rec_get_n_fields_old(rec);
467+
b4e1fa2c
AM
468+ field = rec_get_nth_field_old(rec, 1, &len);
469+ ut_a(len == 4);
470+
471+ key_cols = mach_read_from_4(field);
472+
473+ ut_a(i == key_cols);
474+
475+ field = rec_get_nth_field_old(rec, DICT_SYS_STATS_DIFF_VALS_FIELD, &len);
476+ ut_a(len == 8);
477+
478+ stat_n_diff_key_vals_tmp[i] = mach_read_from_8(field);
11822e22
AM
479+
480+ if (n_fields > DICT_SYS_STATS_NON_NULL_VALS_FIELD) {
481+ field = rec_get_nth_field_old(rec, DICT_SYS_STATS_NON_NULL_VALS_FIELD, &len);
482+ ut_a(len == 8);
483+
484+ stat_n_non_null_key_vals_tmp[i] = mach_read_from_8(field);
485+ } else {
486+ /* not enough fields: should be older */
487+ fprintf(stderr, "InnoDB: Notice: stats for %s/%s (%lu/%lu)"
488+ " in SYS_STATS seems older format. "
489+ "Please execute ANALYZE TABLE for it.\n",
490+ index->table_name, index->name, i, n_cols);
491+
492+ stat_n_non_null_key_vals_tmp[i] = ((ib_int64_t)(-1));
493+ }
b4e1fa2c
AM
494+next_rec:
495+ btr_pcur_move_to_next_user_rec(&pcur, &mtr);
496+ }
497+
498+ btr_pcur_close(&pcur);
499+ mtr_commit(&mtr);
500+
501+ for (i = 0; i <= n_cols; i++) {
502+ index->stat_n_diff_key_vals[i] = stat_n_diff_key_vals_tmp[i];
11822e22
AM
503+ if (stat_n_non_null_key_vals_tmp[i] == ((ib_int64_t)(-1))) {
504+ /* approximate value */
505+ index->stat_n_non_null_key_vals[i] = stat_n_diff_key_vals_tmp[n_cols];
506+ } else {
507+ index->stat_n_non_null_key_vals[i] = stat_n_non_null_key_vals_tmp[i];
508+ }
b4e1fa2c
AM
509+ }
510+}
511+/*===========================================*/
512+
513+ index = dict_table_get_next_index(index);
514+ }
515+
516+ mem_heap_free(heap);
517+ return(TRUE);
518+}
519+
520+static
521+void
522+dict_store_statistics(
523+/*==================*/
524+ dict_table_t* table)
525+{
526+ dict_index_t* index;
527+ mem_heap_t* heap;
528+
529+ index = dict_table_get_first_index(table);
530+
531+ ut_a(index);
532+
533+ heap = mem_heap_create(1000);
534+
535+ while (index) {
536+/*===========================================*/
537+{
538+ dict_table_t* sys_stats;
539+ dict_index_t* sys_index;
540+ btr_pcur_t pcur;
541+ dtuple_t* tuple;
542+ dfield_t* dfield;
543+ ulint key_cols;
544+ ulint n_cols;
545+ ulint rests;
546+ const rec_t* rec;
11822e22 547+ ulint n_fields;
b4e1fa2c
AM
548+ const byte* field;
549+ ulint len;
550+ ib_int64_t* stat_n_diff_key_vals_tmp;
11822e22 551+ ib_int64_t* stat_n_non_null_key_vals_tmp;
b4e1fa2c
AM
552+ byte* buf;
553+ ulint i;
554+ mtr_t mtr;
555+
556+ n_cols = dict_index_get_n_unique(index);
557+ stat_n_diff_key_vals_tmp = mem_heap_zalloc(heap, (n_cols + 1) * sizeof(ib_int64_t));
11822e22 558+ stat_n_non_null_key_vals_tmp = mem_heap_zalloc(heap, (n_cols + 1) * sizeof(ib_int64_t));
b4e1fa2c
AM
559+
560+ for (i = 0; i <= n_cols; i++) {
561+ stat_n_diff_key_vals_tmp[i] = index->stat_n_diff_key_vals[i];
11822e22 562+ stat_n_non_null_key_vals_tmp[i] = index->stat_n_non_null_key_vals[i];
b4e1fa2c
AM
563+ }
564+
565+ sys_stats = dict_sys->sys_stats;
566+ sys_index = UT_LIST_GET_FIRST(sys_stats->indexes);
567+ ut_a(!dict_table_is_comp(sys_stats));
568+
569+ tuple = dtuple_create(heap, 1);
570+ dfield = dtuple_get_nth_field(tuple, 0);
571+
572+ buf = mem_heap_alloc(heap, 8);
573+ mach_write_to_8(buf, index->id);
574+
575+ dfield_set_data(dfield, buf, 8);
576+ dict_index_copy_types(tuple, sys_index, 1);
577+
578+ mtr_start(&mtr);
579+
580+ btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
581+ BTR_MODIFY_LEAF, &pcur, &mtr);
582+ rests = n_cols + 1;
583+ for (i = 0; i <= n_cols; i++) {
584+ rec = btr_pcur_get_rec(&pcur);
585+
586+ if (!btr_pcur_is_on_user_rec(&pcur)
587+ || mach_read_from_8(rec_get_nth_field_old(rec, 0, &len))
588+ != index->id) {
589+ /* not found */
d8778560
AM
590+
591+
b4e1fa2c
AM
592+ break;
593+ }
594+
595+ if (rec_get_deleted_flag(rec, 0)) {
11822e22
AM
596+ /* don't count */
597+ i--;
598+ goto next_rec;
599+ }
600+
601+ n_fields = rec_get_n_fields_old(rec);
602+
603+ if (n_fields <= DICT_SYS_STATS_NON_NULL_VALS_FIELD) {
604+ /* not update for the older smaller format */
605+ fprintf(stderr, "InnoDB: Notice: stats for %s/%s (%lu/%lu)"
606+ " in SYS_STATS seems older format. Please ANALYZE TABLE it.\n",
607+ index->table_name, index->name, i, n_cols);
b4e1fa2c
AM
608+ goto next_rec;
609+ }
610+
611+ field = rec_get_nth_field_old(rec, 1, &len);
612+ ut_a(len == 4);
613+
614+ key_cols = mach_read_from_4(field);
615+
616+ field = rec_get_nth_field_old(rec, DICT_SYS_STATS_DIFF_VALS_FIELD, &len);
617+ ut_a(len == 8);
618+
619+ mlog_write_ull((byte*)field, stat_n_diff_key_vals_tmp[key_cols], &mtr);
620+
11822e22
AM
621+ field = rec_get_nth_field_old(rec, DICT_SYS_STATS_NON_NULL_VALS_FIELD, &len);
622+ ut_a(len == 8);
623+
624+ mlog_write_ull((byte*)field, stat_n_non_null_key_vals_tmp[key_cols], &mtr);
625+
b4e1fa2c
AM
626+ rests--;
627+
628+next_rec:
629+ btr_pcur_move_to_next_user_rec(&pcur, &mtr);
630+ }
631+ btr_pcur_close(&pcur);
632+ mtr_commit(&mtr);
633+
634+ if (rests) {
635+ fprintf(stderr, "InnoDB: Warning: failed to store %lu stats entries"
636+ " of %s/%s to SYS_STATS system table.\n",
637+ rests, index->table_name, index->name);
638+ }
639+}
640+/*===========================================*/
641+
642+ index = dict_table_get_next_index(index);
643+ }
644+
645+ mem_heap_free(heap);
646+}
647+
648+/*********************************************************************//**
649 Calculates new estimates for table and index statistics. The statistics
650 are used in query optimization. */
651 UNIV_INTERN
11822e22 652@@ -4317,10 +4600,11 @@
b4e1fa2c
AM
653 dict_update_statistics(
654 /*===================*/
655 dict_table_t* table, /*!< in/out: table */
656- ibool only_calc_if_missing_stats)/*!< in: only
657+ ibool only_calc_if_missing_stats,/*!< in: only
658 update/recalc the stats if they have
659 not been initialized yet, otherwise
660 do nothing */
661+ ibool sync) /*!< in: TRUE if must update SYS_STATS */
662 {
663 dict_index_t* index;
664 ulint sum_of_index_sizes = 0;
11822e22 665@@ -4337,6 +4621,27 @@
b4e1fa2c
AM
666 return;
667 }
668
669+ if (srv_use_sys_stats_table && !((table->flags >> DICT_TF2_SHIFT) & DICT_TF2_TEMPORARY) && !sync) {
670+ dict_table_stats_lock(table, RW_X_LATCH);
671+
672+ /* reload statistics from SYS_STATS table */
673+ if (dict_reload_statistics(table, &sum_of_index_sizes)) {
674+ /* success */
675+#ifdef UNIV_DEBUG
d8778560 676+ fprintf(stderr, "InnoDB: DEBUG: reload_statistics succeeded for %s.\n",
b4e1fa2c
AM
677+ table->name);
678+#endif
679+ goto end;
680+ }
681+
682+ dict_table_stats_unlock(table, RW_X_LATCH);
683+ }
684+#ifdef UNIV_DEBUG
685+ fprintf(stderr, "InnoDB: DEBUG: update_statistics for %s.\n",
686+ table->name);
687+#endif
688+ sum_of_index_sizes = 0;
689+
690 /* Find out the sizes of the indexes and how many different values
691 for the key they approximately have */
692
11822e22 693@@ -4401,6 +4706,11 @@
b4e1fa2c
AM
694 index = dict_table_get_next_index(index);
695 } while (index);
696
697+ if (srv_use_sys_stats_table && !((table->flags >> DICT_TF2_SHIFT) & DICT_TF2_TEMPORARY)) {
698+ /* store statistics to SYS_STATS table */
699+ dict_store_statistics(table);
700+ }
701+end:
702 index = dict_table_get_first_index(table);
703
704 table->stat_n_rows = index->stat_n_diff_key_vals[
11822e22
AM
705@@ -4418,6 +4728,78 @@
706 dict_table_stats_unlock(table, RW_X_LATCH);
707 }
708
709+/*********************************************************************//**
710+*/
711+UNIV_INTERN
712+ibool
713+dict_is_older_statistics(
714+/*=====================*/
715+ dict_index_t* index)
716+{
717+ mem_heap_t* heap;
718+ dict_table_t* sys_stats;
719+ dict_index_t* sys_index;
720+ btr_pcur_t pcur;
721+ dtuple_t* tuple;
722+ dfield_t* dfield;
723+ const rec_t* rec;
724+ ulint n_fields;
725+ ulint len;
726+ byte* buf;
727+ mtr_t mtr;
728+
729+ heap = mem_heap_create(100);
730+
731+ sys_stats = dict_sys->sys_stats;
732+ sys_index = UT_LIST_GET_FIRST(sys_stats->indexes);
733+ ut_a(!dict_table_is_comp(sys_stats));
734+
735+ tuple = dtuple_create(heap, 1);
736+ dfield = dtuple_get_nth_field(tuple, 0);
737+
738+ buf = mem_heap_alloc(heap, 8);
739+ mach_write_to_8(buf, index->id);
740+
741+ dfield_set_data(dfield, buf, 8);
742+ dict_index_copy_types(tuple, sys_index, 1);
743+
744+ mtr_start(&mtr);
745+
746+ btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
747+ BTR_SEARCH_LEAF, &pcur, &mtr);
748+
749+next_rec:
750+ rec = btr_pcur_get_rec(&pcur);
751+
752+ if (!btr_pcur_is_on_user_rec(&pcur)
753+ || mach_read_from_8(rec_get_nth_field_old(rec, 0, &len))
754+ != index->id) {
755+ /* not found */
756+ btr_pcur_close(&pcur);
757+ mtr_commit(&mtr);
758+ mem_heap_free(heap);
759+ /* no statistics == not older statistics */
760+ return(FALSE);
761+ }
762+
763+ if (rec_get_deleted_flag(rec, 0)) {
764+ btr_pcur_move_to_next_user_rec(&pcur, &mtr);
765+ goto next_rec;
766+ }
767+
768+ n_fields = rec_get_n_fields_old(rec);
769+
770+ btr_pcur_close(&pcur);
771+ mtr_commit(&mtr);
772+ mem_heap_free(heap);
773+
774+ if (n_fields > DICT_SYS_STATS_NON_NULL_VALS_FIELD) {
775+ return(FALSE);
776+ } else {
777+ return(TRUE);
778+ }
779+}
780+
781 /**********************************************************************//**
782 Prints info of a foreign key constraint. */
783 static
784@@ -4495,7 +4877,8 @@
b4e1fa2c
AM
785
786 ut_ad(mutex_own(&(dict_sys->mutex)));
787
788- dict_update_statistics(table, FALSE /* update even if initialized */);
789+ if (srv_stats_auto_update)
790+ dict_update_statistics(table, FALSE /* update even if initialized */, FALSE);
791
792 dict_table_stats_lock(table, RW_S_LATCH);
793
794diff -ruN a/storage/innobase/dict/dict0load.c b/storage/innobase/dict/dict0load.c
795--- a/storage/innobase/dict/dict0load.c 2010-11-03 07:01:13.000000000 +0900
796+++ b/storage/innobase/dict/dict0load.c 2010-12-03 17:19:24.845947460 +0900
d8778560 797@@ -50,7 +50,8 @@
b4e1fa2c
AM
798 "SYS_COLUMNS",
799 "SYS_FIELDS",
800 "SYS_FOREIGN",
801- "SYS_FOREIGN_COLS"
802+ "SYS_FOREIGN_COLS",
803+ "SYS_STATS"
804 };
805 /****************************************************************//**
806 Compare the name of an index column.
d8778560 807@@ -343,12 +344,13 @@
b4e1fa2c
AM
808 }
809
810 if ((status & DICT_TABLE_UPDATE_STATS)
811+ && srv_stats_auto_update
812 && dict_table_get_first_index(*table)) {
813
814 /* Update statistics if DICT_TABLE_UPDATE_STATS
815 is set */
816 dict_update_statistics(*table, FALSE /* update even if
817- initialized */);
818+ initialized */, FALSE);
819 }
820
821 return(NULL);
11822e22 822@@ -582,6 +584,75 @@
d8778560 823 //#endif /* FOREIGN_NOT_USED */
b4e1fa2c 824
d8778560 825 /********************************************************************//**
b4e1fa2c
AM
826+This function parses a SYS_STATS record and extract necessary
827+information from the record and return to caller.
828+@return error message, or NULL on success */
829+UNIV_INTERN
830+const char*
831+dict_process_sys_stats_rec(
832+/*=============================*/
d8778560 833+ mem_heap_t* heap __attribute__((unused)), /*!< in/out: heap memory */
b4e1fa2c
AM
834+ const rec_t* rec, /*!< in: current SYS_STATS rec */
835+ index_id_t* index_id, /*!< out: INDEX_ID */
836+ ulint* key_cols, /*!< out: KEY_COLS */
11822e22
AM
837+ ib_uint64_t* diff_vals, /*!< out: DIFF_VALS */
838+ ib_uint64_t* non_null_vals) /*!< out: NON_NULL_VALS */
b4e1fa2c
AM
839+{
840+ ulint len;
841+ const byte* field;
11822e22 842+ ulint n_fields;
b4e1fa2c
AM
843+
844+ if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
845+ return("delete-marked record in SYS_STATS");
846+ }
847+
11822e22
AM
848+ n_fields = rec_get_n_fields_old(rec);
849+
850+ if (UNIV_UNLIKELY(n_fields < 5)) {
b4e1fa2c
AM
851+ return("wrong number of columns in SYS_STATS record");
852+ }
853+
854+ field = rec_get_nth_field_old(rec, 0/*INDEX_ID*/, &len);
855+ if (UNIV_UNLIKELY(len != 8)) {
856+err_len:
857+ return("incorrect column length in SYS_STATS");
858+ }
859+ *index_id = mach_read_from_8(field);
860+
861+ field = rec_get_nth_field_old(rec, 1/*KEY_COLS*/, &len);
862+ if (UNIV_UNLIKELY(len != 4)) {
863+ goto err_len;
864+ }
865+ *key_cols = mach_read_from_4(field);
866+
867+ rec_get_nth_field_offs_old(rec, 2/*DB_TRX_ID*/, &len);
868+ if (UNIV_UNLIKELY(len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL)) {
869+ goto err_len;
870+ }
871+ rec_get_nth_field_offs_old(rec, 3/*DB_ROLL_PTR*/, &len);
872+ if (UNIV_UNLIKELY(len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL)) {
873+ goto err_len;
874+ }
875+
876+ field = rec_get_nth_field_old(rec, 4/*DIFF_VALS*/, &len);
877+ if (UNIV_UNLIKELY(len != 8)) {
878+ goto err_len;
879+ }
880+ *diff_vals = mach_read_from_8(field);
881+
11822e22
AM
882+ if (n_fields < 6) {
883+ *non_null_vals = ((ib_uint64_t)(-1));
884+ } else {
885+ field = rec_get_nth_field_old(rec, 5/*NON_NULL_VALS*/, &len);
886+ if (UNIV_UNLIKELY(len != 8)) {
887+ goto err_len;
888+ }
889+ *non_null_vals = mach_read_from_8(field);
890+ }
891+
b4e1fa2c
AM
892+ return(NULL);
893+}
d8778560 894+/********************************************************************//**
b4e1fa2c
AM
895 Determine the flags of a table described in SYS_TABLES.
896 @return compressed page size in kilobytes; or 0 if the tablespace is
d8778560 897 uncompressed, ULINT_UNDEFINED on error */
b4e1fa2c
AM
898diff -ruN a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
899--- a/storage/innobase/handler/ha_innodb.cc 2010-12-03 17:17:03.665960357 +0900
900+++ b/storage/innobase/handler/ha_innodb.cc 2010-12-03 17:22:21.586939783 +0900
901@@ -187,6 +187,7 @@
902 static my_bool innobase_rollback_on_timeout = FALSE;
903 static my_bool innobase_create_status_file = FALSE;
904 static my_bool innobase_stats_on_metadata = TRUE;
905+static my_bool innobase_use_sys_stats_table = FALSE;
906
907
908 static char* internal_innobase_data_file_path = NULL;
df1b5770 909@@ -2407,6 +2408,8 @@
b4e1fa2c
AM
910 goto error;
911 }
912
913+ srv_use_sys_stats_table = (ibool) innobase_use_sys_stats_table;
914+
915 /* -------------- Log files ---------------------------*/
916
917 /* The default dir for log files is the datadir of MySQL */
df1b5770 918@@ -5211,6 +5214,10 @@
b4e1fa2c
AM
919
920 error = row_insert_for_mysql((byte*) record, prebuilt);
921
922+#ifdef EXTENDED_FOR_USERSTAT
923+ if (error == DB_SUCCESS) rows_changed++;
924+#endif
925+
926 /* Handle duplicate key errors */
927 if (auto_inc_used) {
928 ulint err;
df1b5770 929@@ -5547,6 +5554,10 @@
b4e1fa2c
AM
930 }
931 }
932
933+#ifdef EXTENDED_FOR_USERSTAT
934+ if (error == DB_SUCCESS) rows_changed++;
935+#endif
936+
937 innodb_srv_conc_exit_innodb(trx);
938
939 error = convert_error_code_to_mysql(error,
df1b5770 940@@ -5600,6 +5611,10 @@
b4e1fa2c
AM
941
942 error = row_update_for_mysql((byte*) record, prebuilt);
943
944+#ifdef EXTENDED_FOR_USERSTAT
945+ if (error == DB_SUCCESS) rows_changed++;
946+#endif
947+
948 innodb_srv_conc_exit_innodb(trx);
949
950 error = convert_error_code_to_mysql(
df1b5770 951@@ -5918,6 +5933,11 @@
d8778560
AM
952 case DB_SUCCESS:
953 error = 0;
954 table->status = 0;
955+#ifdef EXTENDED_FOR_USERSTAT
956+ rows_read++;
957+ if (active_index >= 0 && active_index < MAX_KEY)
958+ index_rows_read[active_index]++;
959+#endif
960 break;
961 case DB_RECORD_NOT_FOUND:
962 error = HA_ERR_KEY_NOT_FOUND;
df1b5770 963@@ -6127,6 +6147,11 @@
b4e1fa2c
AM
964 case DB_SUCCESS:
965 error = 0;
966 table->status = 0;
967+#ifdef EXTENDED_FOR_USERSTAT
968+ rows_read++;
969+ if (active_index >= 0 && active_index < MAX_KEY)
970+ index_rows_read[active_index]++;
971+#endif
972 break;
973 case DB_RECORD_NOT_FOUND:
974 error = HA_ERR_END_OF_FILE;
11822e22 975@@ -8077,11 +8102,35 @@
b4e1fa2c
AM
976 /* In sql_show we call with this flag: update
977 then statistics so that they are up-to-date */
978
979+ if (srv_use_sys_stats_table && !((ib_table->flags >> DICT_TF2_SHIFT) & DICT_TF2_TEMPORARY)
980+ && called_from_analyze) {
981+ /* If the indexes on the table don't have enough rows in SYS_STATS system table, */
982+ /* they need to be created. */
983+ dict_index_t* index;
984+
985+ prebuilt->trx->op_info = "confirming rows of SYS_STATS to store statistics";
986+
987+ ut_a(prebuilt->trx->conc_state == TRX_NOT_STARTED);
988+
989+ for (index = dict_table_get_first_index(ib_table);
990+ index != NULL;
991+ index = dict_table_get_next_index(index)) {
11822e22
AM
992+ if (dict_is_older_statistics(index)) {
993+ row_delete_stats_for_mysql(index, prebuilt->trx);
994+ innobase_commit_low(prebuilt->trx);
995+ }
b4e1fa2c
AM
996+ row_insert_stats_for_mysql(index, prebuilt->trx);
997+ innobase_commit_low(prebuilt->trx);
998+ }
999+
1000+ ut_a(prebuilt->trx->conc_state == TRX_NOT_STARTED);
1001+ }
1002+
1003 prebuilt->trx->op_info = "updating table statistics";
1004
1005 dict_update_statistics(ib_table,
1006 FALSE /* update even if stats
1007- are initialized */);
1008+ are initialized */, called_from_analyze);
1009
1010 prebuilt->trx->op_info = "returning various info to MySQL";
1011 }
11822e22 1012@@ -8159,7 +8208,7 @@
b4e1fa2c
AM
1013 are asked by MySQL to avoid locking. Another reason to
1014 avoid the call is that it uses quite a lot of CPU.
1015 See Bug#38185. */
d8778560
AM
1016- if (flag & HA_STATUS_NO_LOCK
1017+ if (flag & HA_STATUS_NO_LOCK || !srv_stats_update_need_lock
1018 || !(flag & HA_STATUS_VARIABLE_EXTRA)) {
b4e1fa2c
AM
1019 /* We do not update delete_length if no
1020 locking is requested so the "old" value can
11822e22 1021@@ -11361,6 +11410,26 @@
b4e1fa2c
AM
1022 "The number of index pages to sample when calculating statistics (default 8)",
1023 NULL, NULL, 8, 1, ~0ULL, 0);
1024
b4e1fa2c
AM
1025+static MYSQL_SYSVAR_ULONG(stats_auto_update, srv_stats_auto_update,
1026+ PLUGIN_VAR_RQCMDARG,
1027+ "Enable/Disable InnoDB's auto update statistics of indexes. "
1028+ "(except for ANALYZE TABLE command) 0:disable 1:enable",
1029+ NULL, NULL, 1, 0, 1, 0);
1030+
1031+static MYSQL_SYSVAR_ULONG(stats_update_need_lock, srv_stats_update_need_lock,
1032+ PLUGIN_VAR_RQCMDARG,
1033+ "Enable/Disable InnoDB's update statistics which needs to lock dictionary. "
1034+ "e.g. Data_free.",
1035+ NULL, NULL, 1, 0, 1, 0);
1036+
1037+static MYSQL_SYSVAR_BOOL(use_sys_stats_table, innobase_use_sys_stats_table,
1038+ PLUGIN_VAR_NOCMDARG | PLUGIN_VAR_READONLY,
1039+ "Enable to use SYS_STATS system table to store statistics statically, "
1040+ "And avoids to calculate statistics at every first open of the tables. "
1041+ "This option may make the opportunities of update statistics less. "
1042+ "So you should use ANALYZE TABLE command intentionally.",
1043+ NULL, NULL, FALSE);
1044+
1045 static MYSQL_SYSVAR_BOOL(adaptive_hash_index, btr_search_enabled,
1046 PLUGIN_VAR_OPCMDARG,
1047 "Enable InnoDB adaptive hash index (enabled by default). "
11822e22 1048@@ -11686,6 +11755,9 @@
a9ee80b9 1049 MYSQL_SYSVAR(recovery_update_relay_log),
b4e1fa2c
AM
1050 MYSQL_SYSVAR(rollback_on_timeout),
1051 MYSQL_SYSVAR(stats_on_metadata),
b4e1fa2c
AM
1052+ MYSQL_SYSVAR(stats_auto_update),
1053+ MYSQL_SYSVAR(stats_update_need_lock),
1054+ MYSQL_SYSVAR(use_sys_stats_table),
1055 MYSQL_SYSVAR(stats_sample_pages),
1056 MYSQL_SYSVAR(adaptive_hash_index),
df1b5770 1057 MYSQL_SYSVAR(stats_method),
11822e22 1058@@ -11755,7 +11827,10 @@
b4e1fa2c
AM
1059 i_s_innodb_sys_columns,
1060 i_s_innodb_sys_fields,
1061 i_s_innodb_sys_foreign,
1062-i_s_innodb_sys_foreign_cols
1063+i_s_innodb_sys_foreign_cols,
1064+i_s_innodb_sys_stats,
1065+i_s_innodb_table_stats,
1066+i_s_innodb_index_stats
1067 mysql_declare_plugin_end;
1068
1069 /** @brief Initialize the default value of innodb_commit_concurrency.
1070diff -ruN a/storage/innobase/handler/i_s.cc b/storage/innobase/handler/i_s.cc
1071--- a/storage/innobase/handler/i_s.cc 2010-12-03 17:17:03.666956117 +0900
1072+++ b/storage/innobase/handler/i_s.cc 2010-12-03 17:19:24.880964526 +0900
1073@@ -49,6 +49,7 @@
1074 #include "trx0trx.h" /* for TRX_QUE_STATE_STR_MAX_LEN */
1075 #include "trx0rseg.h" /* for trx_rseg_struct */
1076 #include "trx0sys.h" /* for trx_sys */
1077+#include "dict0dict.h" /* for dict_sys */
1078 }
1079
1080 static const char plugin_author[] = "Innobase Oy";
11822e22 1081@@ -3457,6 +3458,221 @@
b4e1fa2c
AM
1082 STRUCT_FLD(__reserved1, NULL)
1083 };
1084
1085+/* Fields of the dynamic table INFORMATION_SCHEMA.innodb_sys_stats */
1086+static ST_FIELD_INFO innodb_sys_stats_fields_info[] =
1087+{
1088+#define SYS_STATS_INDEX_ID 0
1089+ {STRUCT_FLD(field_name, "INDEX_ID"),
1090+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1091+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1092+ STRUCT_FLD(value, 0),
1093+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1094+ STRUCT_FLD(old_name, ""),
1095+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1096+
1097+#define SYS_STATS_KEY_COLS 1
1098+ {STRUCT_FLD(field_name, "KEY_COLS"),
1099+ STRUCT_FLD(field_length, MY_INT32_NUM_DECIMAL_DIGITS),
1100+ STRUCT_FLD(field_type, MYSQL_TYPE_LONG),
1101+ STRUCT_FLD(value, 0),
1102+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1103+ STRUCT_FLD(old_name, ""),
1104+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1105+
1106+#define SYS_STATS_DIFF_VALS 2
1107+ {STRUCT_FLD(field_name, "DIFF_VALS"),
1108+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1109+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1110+ STRUCT_FLD(value, 0),
1111+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1112+ STRUCT_FLD(old_name, ""),
1113+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1114+
11822e22
AM
1115+#define SYS_STATS_NON_NULL_VALS 3
1116+ {STRUCT_FLD(field_name, "NON_NULL_VALS"),
1117+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1118+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1119+ STRUCT_FLD(value, 0),
1120+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL),
1121+ STRUCT_FLD(old_name, ""),
1122+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1123+
b4e1fa2c
AM
1124+ END_OF_ST_FIELD_INFO
1125+};
1126+/**********************************************************************//**
1127+Function to fill information_schema.innodb_sys_stats
1128+@return 0 on success */
1129+static
1130+int
1131+i_s_dict_fill_sys_stats(
1132+/*====================*/
1133+ THD* thd, /*!< in: thread */
1134+ index_id_t index_id, /*!< in: INDEX_ID */
1135+ ulint key_cols, /*!< in: KEY_COLS */
1136+ ib_uint64_t diff_vals, /*!< in: DIFF_VALS */
11822e22 1137+ ib_uint64_t non_null_vals, /*!< in: NON_NULL_VALS */
b4e1fa2c
AM
1138+ TABLE* table_to_fill) /*!< in/out: fill this table */
1139+{
1140+ Field** fields;
1141+
1142+ DBUG_ENTER("i_s_dict_fill_sys_stats");
1143+
1144+ fields = table_to_fill->field;
1145+
1146+ OK(fields[SYS_STATS_INDEX_ID]->store(longlong(index_id), TRUE));
1147+
1148+ OK(fields[SYS_STATS_KEY_COLS]->store(key_cols));
1149+
1150+ OK(fields[SYS_STATS_DIFF_VALS]->store(longlong(diff_vals), TRUE));
1151+
11822e22
AM
1152+ if (non_null_vals == ((ib_uint64_t)(-1))) {
1153+ fields[SYS_STATS_NON_NULL_VALS]->set_null();
1154+ } else {
1155+ OK(fields[SYS_STATS_NON_NULL_VALS]->store(longlong(non_null_vals), TRUE));
1156+ fields[SYS_STATS_NON_NULL_VALS]->set_notnull();
1157+ }
1158+
b4e1fa2c
AM
1159+ OK(schema_table_store_record(thd, table_to_fill));
1160+
1161+ DBUG_RETURN(0);
1162+}
1163+/*******************************************************************//**
1164+Function to populate INFORMATION_SCHEMA.innodb_sys_stats table.
1165+@return 0 on success */
1166+static
1167+int
1168+i_s_sys_stats_fill_table(
1169+/*=====================*/
1170+ THD* thd, /*!< in: thread */
1171+ TABLE_LIST* tables, /*!< in/out: tables to fill */
1172+ COND* cond) /*!< in: condition (not used) */
1173+{
1174+ btr_pcur_t pcur;
1175+ const rec_t* rec;
1176+ mem_heap_t* heap;
1177+ mtr_t mtr;
1178+
1179+ DBUG_ENTER("i_s_sys_stats_fill_table");
1180+
1181+ /* deny access to non-superusers */
1182+ if (check_global_access(thd, PROCESS_ACL)) {
1183+ DBUG_RETURN(0);
1184+ }
1185+
1186+ heap = mem_heap_create(1000);
1187+ mutex_enter(&dict_sys->mutex);
1188+ mtr_start(&mtr);
1189+
1190+ rec = dict_startscan_system(&pcur, &mtr, SYS_STATS);
1191+
1192+ while (rec) {
1193+ const char* err_msg;
1194+ index_id_t index_id;
1195+ ulint key_cols;
1196+ ib_uint64_t diff_vals;
11822e22 1197+ ib_uint64_t non_null_vals;
b4e1fa2c
AM
1198+
1199+ /* Extract necessary information from a SYS_FOREIGN_COLS row */
1200+ err_msg = dict_process_sys_stats_rec(
11822e22 1201+ heap, rec, &index_id, &key_cols, &diff_vals, &non_null_vals);
b4e1fa2c
AM
1202+
1203+ mtr_commit(&mtr);
1204+ mutex_exit(&dict_sys->mutex);
1205+
1206+ if (!err_msg) {
1207+ i_s_dict_fill_sys_stats(
11822e22 1208+ thd, index_id, key_cols, diff_vals, non_null_vals,
b4e1fa2c
AM
1209+ tables->table);
1210+ } else {
1211+ push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1212+ ER_CANT_FIND_SYSTEM_REC,
1213+ err_msg);
1214+ }
1215+
1216+ mem_heap_empty(heap);
1217+
1218+ /* Get the next record */
1219+ mutex_enter(&dict_sys->mutex);
1220+ mtr_start(&mtr);
1221+ rec = dict_getnext_system(&pcur, &mtr);
1222+ }
1223+
1224+ mtr_commit(&mtr);
1225+ mutex_exit(&dict_sys->mutex);
1226+ mem_heap_free(heap);
1227+
1228+ DBUG_RETURN(0);
1229+}
1230+/*******************************************************************//**
1231+Bind the dynamic table INFORMATION_SCHEMA.innodb_sys_stats
1232+@return 0 on success */
1233+static
1234+int
1235+innodb_sys_stats_init(
1236+/*========================*/
1237+ void* p) /*!< in/out: table schema object */
1238+{
1239+ ST_SCHEMA_TABLE* schema;
1240+
1241+ DBUG_ENTER("innodb_sys_stats_init");
1242+
1243+ schema = (ST_SCHEMA_TABLE*) p;
1244+
1245+ schema->fields_info = innodb_sys_stats_fields_info;
1246+ schema->fill_table = i_s_sys_stats_fill_table;
1247+
1248+ DBUG_RETURN(0);
1249+}
1250+
1251+UNIV_INTERN struct st_mysql_plugin i_s_innodb_sys_stats =
1252+{
1253+ /* the plugin type (a MYSQL_XXX_PLUGIN value) */
1254+ /* int */
1255+ STRUCT_FLD(type, MYSQL_INFORMATION_SCHEMA_PLUGIN),
1256+
1257+ /* pointer to type-specific plugin descriptor */
1258+ /* void* */
1259+ STRUCT_FLD(info, &i_s_info),
1260+
1261+ /* plugin name */
1262+ /* const char* */
1263+ STRUCT_FLD(name, "INNODB_SYS_STATS"),
1264+
1265+ /* plugin author (for SHOW PLUGINS) */
1266+ /* const char* */
1267+ STRUCT_FLD(author, plugin_author),
1268+
1269+ /* general descriptive text (for SHOW PLUGINS) */
1270+ /* const char* */
1271+ STRUCT_FLD(descr, "XtraDB SYS_STATS table"),
1272+
1273+ /* the plugin license (PLUGIN_LICENSE_XXX) */
1274+ /* int */
1275+ STRUCT_FLD(license, PLUGIN_LICENSE_GPL),
1276+
1277+ /* the function to invoke when plugin is loaded */
1278+ /* int (*)(void*); */
1279+ STRUCT_FLD(init, innodb_sys_stats_init),
1280+
1281+ /* the function to invoke when plugin is unloaded */
1282+ /* int (*)(void*); */
1283+ STRUCT_FLD(deinit, i_s_common_deinit),
1284+
1285+ /* plugin version (for SHOW PLUGINS) */
1286+ /* unsigned int */
1287+ STRUCT_FLD(version, INNODB_VERSION_SHORT),
1288+
1289+ /* struct st_mysql_show_var* */
1290+ STRUCT_FLD(status_vars, NULL),
1291+
1292+ /* struct st_mysql_sys_var** */
1293+ STRUCT_FLD(system_vars, NULL),
1294+
1295+ /* reserved for dependency checking */
1296+ /* void* */
1297+ STRUCT_FLD(__reserved1, NULL)
1298+};
1299+
1300 /***********************************************************************
1301 */
1302 static ST_FIELD_INFO i_s_innodb_rseg_fields_info[] =
11822e22 1303@@ -3619,3 +3835,347 @@
b4e1fa2c
AM
1304 /* void* */
1305 STRUCT_FLD(__reserved1, NULL)
1306 };
1307+
1308+/***********************************************************************
1309+*/
1310+static ST_FIELD_INFO i_s_innodb_table_stats_info[] =
1311+{
1312+ {STRUCT_FLD(field_name, "table_schema"),
1313+ STRUCT_FLD(field_length, NAME_LEN),
1314+ STRUCT_FLD(field_type, MYSQL_TYPE_STRING),
1315+ STRUCT_FLD(value, 0),
1316+ STRUCT_FLD(field_flags, 0),
1317+ STRUCT_FLD(old_name, ""),
1318+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1319+
1320+ {STRUCT_FLD(field_name, "table_name"),
1321+ STRUCT_FLD(field_length, NAME_LEN),
1322+ STRUCT_FLD(field_type, MYSQL_TYPE_STRING),
1323+ STRUCT_FLD(value, 0),
1324+ STRUCT_FLD(field_flags, 0),
1325+ STRUCT_FLD(old_name, ""),
1326+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1327+
1328+ {STRUCT_FLD(field_name, "rows"),
1329+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1330+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1331+ STRUCT_FLD(value, 0),
1332+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1333+ STRUCT_FLD(old_name, ""),
1334+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1335+
1336+ {STRUCT_FLD(field_name, "clust_size"),
1337+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1338+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1339+ STRUCT_FLD(value, 0),
1340+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1341+ STRUCT_FLD(old_name, ""),
1342+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1343+
1344+ {STRUCT_FLD(field_name, "other_size"),
1345+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1346+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1347+ STRUCT_FLD(value, 0),
1348+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1349+ STRUCT_FLD(old_name, ""),
1350+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1351+
1352+ {STRUCT_FLD(field_name, "modified"),
1353+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1354+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1355+ STRUCT_FLD(value, 0),
1356+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1357+ STRUCT_FLD(old_name, ""),
1358+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1359+
1360+ END_OF_ST_FIELD_INFO
1361+};
1362+
1363+static ST_FIELD_INFO i_s_innodb_index_stats_info[] =
1364+{
1365+ {STRUCT_FLD(field_name, "table_schema"),
1366+ STRUCT_FLD(field_length, NAME_LEN),
1367+ STRUCT_FLD(field_type, MYSQL_TYPE_STRING),
1368+ STRUCT_FLD(value, 0),
1369+ STRUCT_FLD(field_flags, 0),
1370+ STRUCT_FLD(old_name, ""),
1371+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1372+
1373+ {STRUCT_FLD(field_name, "table_name"),
1374+ STRUCT_FLD(field_length, NAME_LEN),
1375+ STRUCT_FLD(field_type, MYSQL_TYPE_STRING),
1376+ STRUCT_FLD(value, 0),
1377+ STRUCT_FLD(field_flags, 0),
1378+ STRUCT_FLD(old_name, ""),
1379+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1380+
1381+ {STRUCT_FLD(field_name, "index_name"),
1382+ STRUCT_FLD(field_length, NAME_LEN),
1383+ STRUCT_FLD(field_type, MYSQL_TYPE_STRING),
1384+ STRUCT_FLD(value, 0),
1385+ STRUCT_FLD(field_flags, 0),
1386+ STRUCT_FLD(old_name, ""),
1387+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1388+
1389+ {STRUCT_FLD(field_name, "fields"),
1390+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1391+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1392+ STRUCT_FLD(value, 0),
1393+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1394+ STRUCT_FLD(old_name, ""),
1395+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1396+
1397+ {STRUCT_FLD(field_name, "rows_per_key"),
1398+ STRUCT_FLD(field_length, 256),
1399+ STRUCT_FLD(field_type, MYSQL_TYPE_STRING),
1400+ STRUCT_FLD(value, 0),
1401+ STRUCT_FLD(field_flags, 0),
1402+ STRUCT_FLD(old_name, ""),
1403+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1404+
1405+ {STRUCT_FLD(field_name, "index_total_pages"),
1406+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1407+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1408+ STRUCT_FLD(value, 0),
1409+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1410+ STRUCT_FLD(old_name, ""),
1411+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1412+
1413+ {STRUCT_FLD(field_name, "index_leaf_pages"),
1414+ STRUCT_FLD(field_length, MY_INT64_NUM_DECIMAL_DIGITS),
1415+ STRUCT_FLD(field_type, MYSQL_TYPE_LONGLONG),
1416+ STRUCT_FLD(value, 0),
1417+ STRUCT_FLD(field_flags, MY_I_S_UNSIGNED),
1418+ STRUCT_FLD(old_name, ""),
1419+ STRUCT_FLD(open_method, SKIP_OPEN_TABLE)},
1420+
1421+ END_OF_ST_FIELD_INFO
1422+};
1423+
1424+static
1425+int
1426+i_s_innodb_table_stats_fill(
1427+/*========================*/
1428+ THD* thd,
1429+ TABLE_LIST* tables,
1430+ COND* cond)
1431+{
1432+ TABLE* i_s_table = (TABLE *) tables->table;
1433+ int status = 0;
1434+ dict_table_t* table;
1435+
1436+ DBUG_ENTER("i_s_innodb_table_stats_fill");
1437+
1438+ /* deny access to non-superusers */
1439+ if (check_global_access(thd, PROCESS_ACL)) {
1440+ DBUG_RETURN(0);
1441+ }
1442+
1443+ mutex_enter(&(dict_sys->mutex));
1444+
1445+ table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
1446+
1447+ while (table) {
1448+ char buf[NAME_LEN * 2 + 2];
1449+ char* ptr;
1450+
1451+ if (table->stat_clustered_index_size == 0) {
1452+ table = UT_LIST_GET_NEXT(table_LRU, table);
1453+ continue;
1454+ }
1455+
1456+ buf[NAME_LEN * 2 + 1] = 0;
1457+ strncpy(buf, table->name, NAME_LEN * 2 + 1);
1458+ ptr = strchr(buf, '/');
1459+ if (ptr) {
1460+ *ptr = '\0';
1461+ ++ptr;
1462+ } else {
1463+ ptr = buf;
1464+ }
1465+
1466+ field_store_string(i_s_table->field[0], buf);
1467+ field_store_string(i_s_table->field[1], ptr);
1468+ i_s_table->field[2]->store(table->stat_n_rows);
1469+ i_s_table->field[3]->store(table->stat_clustered_index_size);
1470+ i_s_table->field[4]->store(table->stat_sum_of_other_index_sizes);
1471+ i_s_table->field[5]->store(table->stat_modified_counter);
1472+
1473+ if (schema_table_store_record(thd, i_s_table)) {
1474+ status = 1;
1475+ break;
1476+ }
1477+
1478+ table = UT_LIST_GET_NEXT(table_LRU, table);
1479+ }
1480+
1481+ mutex_exit(&(dict_sys->mutex));
1482+
1483+ DBUG_RETURN(status);
1484+}
1485+
1486+static
1487+int
1488+i_s_innodb_index_stats_fill(
1489+/*========================*/
1490+ THD* thd,
1491+ TABLE_LIST* tables,
1492+ COND* cond)
1493+{
1494+ TABLE* i_s_table = (TABLE *) tables->table;
1495+ int status = 0;
1496+ dict_table_t* table;
1497+ dict_index_t* index;
1498+
1499+ DBUG_ENTER("i_s_innodb_index_stats_fill");
1500+
1501+ /* deny access to non-superusers */
1502+ if (check_global_access(thd, PROCESS_ACL)) {
1503+ DBUG_RETURN(0);
1504+ }
1505+
1506+ mutex_enter(&(dict_sys->mutex));
1507+
1508+ table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
1509+
1510+ while (table) {
1511+ if (table->stat_clustered_index_size == 0) {
1512+ table = UT_LIST_GET_NEXT(table_LRU, table);
1513+ continue;
1514+ }
1515+
1516+ ib_int64_t n_rows = table->stat_n_rows;
1517+
1518+ if (n_rows < 0) {
1519+ n_rows = 0;
1520+ }
1521+
1522+ index = dict_table_get_first_index(table);
1523+
1524+ while (index) {
1525+ char buff[256+1];
1526+ char row_per_keys[256+1];
1527+ char buf[NAME_LEN * 2 + 2];
1528+ char* ptr;
1529+ ulint i;
1530+
1531+ buf[NAME_LEN * 2 + 1] = 0;
1532+ strncpy(buf, table->name, NAME_LEN * 2 + 1);
1533+ ptr = strchr(buf, '/');
1534+ if (ptr) {
1535+ *ptr = '\0';
1536+ ++ptr;
1537+ } else {
1538+ ptr = buf;
1539+ }
1540+
1541+ field_store_string(i_s_table->field[0], buf);
1542+ field_store_string(i_s_table->field[1], ptr);
1543+ field_store_string(i_s_table->field[2], index->name);
1544+ i_s_table->field[3]->store(index->n_uniq);
1545+
1546+ row_per_keys[0] = '\0';
1547+
1548+ /* It is remained optimistic operation still for now */
1549+ //dict_index_stat_mutex_enter(index);
1550+ if (index->stat_n_diff_key_vals) {
1551+ for (i = 1; i <= index->n_uniq; i++) {
1552+ ib_int64_t rec_per_key;
1553+ if (index->stat_n_diff_key_vals[i]) {
1554+ rec_per_key = n_rows / index->stat_n_diff_key_vals[i];
1555+ } else {
1556+ rec_per_key = n_rows;
1557+ }
1558+ ut_snprintf(buff, 256, (i == index->n_uniq)?"%llu":"%llu, ",
1559+ rec_per_key);
1560+ strncat(row_per_keys, buff, 256 - strlen(row_per_keys));
1561+ }
1562+ }
1563+ //dict_index_stat_mutex_exit(index);
1564+
1565+ field_store_string(i_s_table->field[4], row_per_keys);
1566+
1567+ i_s_table->field[5]->store(index->stat_index_size);
1568+ i_s_table->field[6]->store(index->stat_n_leaf_pages);
1569+
1570+ if (schema_table_store_record(thd, i_s_table)) {
1571+ status = 1;
1572+ break;
1573+ }
1574+
1575+ index = dict_table_get_next_index(index);
1576+ }
1577+
1578+ if (status == 1) {
1579+ break;
1580+ }
1581+
1582+ table = UT_LIST_GET_NEXT(table_LRU, table);
1583+ }
1584+
1585+ mutex_exit(&(dict_sys->mutex));
1586+
1587+ DBUG_RETURN(status);
1588+}
1589+
1590+static
1591+int
1592+i_s_innodb_table_stats_init(
1593+/*========================*/
1594+ void* p)
1595+{
1596+ DBUG_ENTER("i_s_innodb_table_stats_init");
1597+ ST_SCHEMA_TABLE* schema = (ST_SCHEMA_TABLE*) p;
1598+
1599+ schema->fields_info = i_s_innodb_table_stats_info;
1600+ schema->fill_table = i_s_innodb_table_stats_fill;
1601+
1602+ DBUG_RETURN(0);
1603+}
1604+
1605+static
1606+int
1607+i_s_innodb_index_stats_init(
1608+/*========================*/
1609+ void* p)
1610+{
1611+ DBUG_ENTER("i_s_innodb_index_stats_init");
1612+ ST_SCHEMA_TABLE* schema = (ST_SCHEMA_TABLE*) p;
1613+
1614+ schema->fields_info = i_s_innodb_index_stats_info;
1615+ schema->fill_table = i_s_innodb_index_stats_fill;
1616+
1617+ DBUG_RETURN(0);
1618+}
1619+
1620+UNIV_INTERN struct st_mysql_plugin i_s_innodb_table_stats =
1621+{
1622+ STRUCT_FLD(type, MYSQL_INFORMATION_SCHEMA_PLUGIN),
1623+ STRUCT_FLD(info, &i_s_info),
1624+ STRUCT_FLD(name, "INNODB_TABLE_STATS"),
1625+ STRUCT_FLD(author, plugin_author),
1626+ STRUCT_FLD(descr, "InnoDB table statistics in memory"),
1627+ STRUCT_FLD(license, PLUGIN_LICENSE_GPL),
1628+ STRUCT_FLD(init, i_s_innodb_table_stats_init),
1629+ STRUCT_FLD(deinit, i_s_common_deinit),
1630+ STRUCT_FLD(version, 0x0100 /* 1.0 */),
1631+ STRUCT_FLD(status_vars, NULL),
1632+ STRUCT_FLD(system_vars, NULL),
1633+ STRUCT_FLD(__reserved1, NULL)
1634+};
1635+
1636+UNIV_INTERN struct st_mysql_plugin i_s_innodb_index_stats =
1637+{
1638+ STRUCT_FLD(type, MYSQL_INFORMATION_SCHEMA_PLUGIN),
1639+ STRUCT_FLD(info, &i_s_info),
1640+ STRUCT_FLD(name, "INNODB_INDEX_STATS"),
1641+ STRUCT_FLD(author, plugin_author),
1642+ STRUCT_FLD(descr, "InnoDB index statistics in memory"),
1643+ STRUCT_FLD(license, PLUGIN_LICENSE_GPL),
1644+ STRUCT_FLD(init, i_s_innodb_index_stats_init),
1645+ STRUCT_FLD(deinit, i_s_common_deinit),
1646+ STRUCT_FLD(version, 0x0100 /* 1.0 */),
1647+ STRUCT_FLD(status_vars, NULL),
1648+ STRUCT_FLD(system_vars, NULL),
1649+ STRUCT_FLD(__reserved1, NULL)
1650+};
1651diff -ruN a/storage/innobase/handler/i_s.h b/storage/innobase/handler/i_s.h
1652--- a/storage/innobase/handler/i_s.h 2010-12-03 17:17:03.668953884 +0900
1653+++ b/storage/innobase/handler/i_s.h 2010-12-03 17:19:24.882947826 +0900
1654@@ -41,5 +41,8 @@
1655 extern struct st_mysql_plugin i_s_innodb_sys_foreign;
1656 extern struct st_mysql_plugin i_s_innodb_sys_foreign_cols;
1657 extern struct st_mysql_plugin i_s_innodb_rseg;
1658+extern struct st_mysql_plugin i_s_innodb_sys_stats;
1659+extern struct st_mysql_plugin i_s_innodb_table_stats;
1660+extern struct st_mysql_plugin i_s_innodb_index_stats;
1661
1662 #endif /* i_s_h */
1663diff -ruN a/storage/innobase/include/dict0boot.h b/storage/innobase/include/dict0boot.h
1664--- a/storage/innobase/include/dict0boot.h 2010-11-03 07:01:13.000000000 +0900
1665+++ b/storage/innobase/include/dict0boot.h 2010-12-03 17:19:24.885947372 +0900
1666@@ -104,6 +104,7 @@
1667 #define DICT_COLUMNS_ID 2
1668 #define DICT_INDEXES_ID 3
1669 #define DICT_FIELDS_ID 4
1670+#define DICT_STATS_ID 6
1671 /* The following is a secondary index on SYS_TABLES */
1672 #define DICT_TABLE_IDS_ID 5
1673
1674@@ -131,10 +132,13 @@
1675 #define DICT_HDR_INDEXES 44 /* Root of the index index tree */
1676 #define DICT_HDR_FIELDS 48 /* Root of the index field
1677 index tree */
1678+#define DICT_HDR_STATS 52 /* Root of the stats tree */
1679
1680 #define DICT_HDR_FSEG_HEADER 56 /* Segment header for the tablespace
1681 segment into which the dictionary
1682 header is created */
1683+
1684+#define DICT_HDR_XTRADB_MARK 256 /* Flag to distinguish expansion of XtraDB */
1685 /*-------------------------------------------------------------*/
1686
1687 /* The field number of the page number field in the sys_indexes table
11822e22 1688@@ -144,11 +148,16 @@
b4e1fa2c
AM
1689 #define DICT_SYS_INDEXES_TYPE_FIELD 6
1690 #define DICT_SYS_INDEXES_NAME_FIELD 4
1691
1692+#define DICT_SYS_STATS_DIFF_VALS_FIELD 4
11822e22 1693+#define DICT_SYS_STATS_NON_NULL_VALS_FIELD 5
b4e1fa2c
AM
1694+
1695 /* When a row id which is zero modulo this number (which must be a power of
1696 two) is assigned, the field DICT_HDR_ROW_ID on the dictionary header page is
1697 updated */
1698 #define DICT_HDR_ROW_ID_WRITE_MARGIN 256
1699
1700+#define DICT_HDR_XTRADB_FLAG 0x5854524144425F31ULL /* "XTRADB_1" */
1701+
1702 #ifndef UNIV_NONINL
1703 #include "dict0boot.ic"
1704 #endif
1705diff -ruN a/storage/innobase/include/dict0crea.h b/storage/innobase/include/dict0crea.h
1706--- a/storage/innobase/include/dict0crea.h 2010-11-03 07:01:13.000000000 +0900
1707+++ b/storage/innobase/include/dict0crea.h 2010-12-03 17:19:24.886949643 +0900
1708@@ -53,6 +53,14 @@
1709 dict_index_t* index, /*!< in: index to create, built as a memory data
1710 structure */
1711 mem_heap_t* heap); /*!< in: heap where created */
1712+/*********************************************************************//**
1713+*/
1714+UNIV_INTERN
1715+ind_node_t*
1716+ind_insert_stats_graph_create(
1717+/*==========================*/
1718+ dict_index_t* index,
1719+ mem_heap_t* heap);
1720 /***********************************************************//**
1721 Creates a table. This is a high-level function used in SQL execution graphs.
1722 @return query thread to run next or NULL */
1723@@ -62,6 +70,13 @@
1724 /*===================*/
1725 que_thr_t* thr); /*!< in: query thread */
1726 /***********************************************************//**
1727+*/
1728+UNIV_INTERN
1729+que_thr_t*
1730+dict_insert_stats_step(
1731+/*===================*/
1732+ que_thr_t* thr);
1733+/***********************************************************//**
1734 Creates an index. This is a high-level function used in SQL execution
1735 graphs.
1736 @return query thread to run next or NULL */
1737@@ -170,6 +185,7 @@
1738 ins_node_t* field_def; /* child node which does the inserts of
1739 the field definitions; the row to be inserted
1740 is built by the parent node */
1741+ ins_node_t* stats_def;
1742 commit_node_t* commit_node;
1743 /* child node which performs a commit after
1744 a successful index creation */
1745@@ -180,6 +196,7 @@
1746 dict_table_t* table; /*!< table which owns the index */
1747 dtuple_t* ind_row;/* index definition row built */
1748 ulint field_no;/* next field definition to insert */
1749+ ulint stats_no;
1750 mem_heap_t* heap; /*!< memory heap used as auxiliary storage */
1751 };
1752
1753@@ -189,6 +206,7 @@
1754 #define INDEX_CREATE_INDEX_TREE 3
1755 #define INDEX_COMMIT_WORK 4
1756 #define INDEX_ADD_TO_CACHE 5
1757+#define INDEX_BUILD_STATS_COLS 6
1758
1759 #ifndef UNIV_NONINL
1760 #include "dict0crea.ic"
1761diff -ruN a/storage/innobase/include/dict0dict.h b/storage/innobase/include/dict0dict.h
1762--- a/storage/innobase/include/dict0dict.h 2010-12-03 15:48:03.073024387 +0900
1763+++ b/storage/innobase/include/dict0dict.h 2010-12-03 17:19:24.888965622 +0900
11822e22 1764@@ -1096,10 +1096,18 @@
b4e1fa2c
AM
1765 dict_update_statistics(
1766 /*===================*/
1767 dict_table_t* table, /*!< in/out: table */
1768- ibool only_calc_if_missing_stats);/*!< in: only
1769+ ibool only_calc_if_missing_stats, /*!< in: only
1770 update/recalc the stats if they have
1771 not been initialized yet, otherwise
1772 do nothing */
1773+ ibool sync);
11822e22
AM
1774+/*********************************************************************//**
1775+*/
1776+UNIV_INTERN
1777+ibool
1778+dict_is_older_statistics(
1779+/*=====================*/
1780+ dict_index_t* index);
b4e1fa2c
AM
1781 /********************************************************************//**
1782 Reserves the dictionary system mutex for MySQL. */
1783 UNIV_INTERN
11822e22 1784@@ -1214,6 +1222,7 @@
b4e1fa2c
AM
1785 dict_table_t* sys_columns; /*!< SYS_COLUMNS table */
1786 dict_table_t* sys_indexes; /*!< SYS_INDEXES table */
1787 dict_table_t* sys_fields; /*!< SYS_FIELDS table */
1788+ dict_table_t* sys_stats; /*!< SYS_STATS table */
1789 };
1790 #endif /* !UNIV_HOTBACKUP */
1791
1792diff -ruN a/storage/innobase/include/dict0load.h b/storage/innobase/include/dict0load.h
1793--- a/storage/innobase/include/dict0load.h 2010-11-03 07:01:13.000000000 +0900
1794+++ b/storage/innobase/include/dict0load.h 2010-12-03 17:19:24.889947481 +0900
1795@@ -41,6 +41,7 @@
1796 SYS_FIELDS,
1797 SYS_FOREIGN,
1798 SYS_FOREIGN_COLS,
1799+ SYS_STATS,
1800
1801 /* This must be last item. Defines the number of system tables. */
1802 SYS_NUM_SYSTEM_TABLES
11822e22 1803@@ -322,6 +323,20 @@
b4e1fa2c
AM
1804 const char** ref_col_name, /*!< out: referenced column name
1805 in referenced table */
1806 ulint* pos); /*!< out: column position */
1807+/********************************************************************//**
1808+This function parses a SYS_STATS record and extract necessary
1809+information from the record and return to caller.
1810+@return error message, or NULL on success */
1811+UNIV_INTERN
1812+const char*
1813+dict_process_sys_stats_rec(
1814+/*=============================*/
1815+ mem_heap_t* heap, /*!< in/out: heap memory */
1816+ const rec_t* rec, /*!< in: current SYS_STATS rec */
1817+ index_id_t* index_id, /*!< out: INDEX_ID */
1818+ ulint* key_cols, /*!< out: KEY_COLS */
11822e22
AM
1819+ ib_uint64_t* diff_vals, /*!< out: DIFF_VALS */
1820+ ib_uint64_t* non_null_vals); /*!< out: NON_NULL_VALS */
b4e1fa2c
AM
1821 #ifndef UNIV_NONINL
1822 #include "dict0load.ic"
1823 #endif
b4e1fa2c
AM
1824diff -ruN a/storage/innobase/include/que0que.h b/storage/innobase/include/que0que.h
1825--- a/storage/innobase/include/que0que.h 2010-11-03 07:01:13.000000000 +0900
1826+++ b/storage/innobase/include/que0que.h 2010-12-03 17:19:24.892947946 +0900
1827@@ -492,6 +492,8 @@
1828 #define QUE_NODE_CALL 31
1829 #define QUE_NODE_EXIT 32
1830
1831+#define QUE_NODE_INSERT_STATS 34
1832+
1833 /* Query thread states */
1834 #define QUE_THR_RUNNING 1
1835 #define QUE_THR_PROCEDURE_WAIT 2
b4e1fa2c
AM
1836diff -ruN a/storage/innobase/include/row0mysql.h b/storage/innobase/include/row0mysql.h
1837--- a/storage/innobase/include/row0mysql.h 2010-11-03 07:01:13.000000000 +0900
1838+++ b/storage/innobase/include/row0mysql.h 2010-12-03 17:19:24.904973020 +0900
11822e22 1839@@ -387,6 +387,22 @@
b4e1fa2c
AM
1840 then checked for not being too
1841 large. */
1842 /*********************************************************************//**
1843+*/
1844+UNIV_INTERN
1845+int
1846+row_insert_stats_for_mysql(
1847+/*=======================*/
1848+ dict_index_t* index,
1849+ trx_t* trx);
11822e22
AM
1850+/*********************************************************************//**
1851+*/
1852+UNIV_INTERN
1853+int
1854+row_delete_stats_for_mysql(
1855+/*=======================*/
1856+ dict_index_t* index,
1857+ trx_t* trx);
b4e1fa2c
AM
1858+/*********************************************************************//**
1859 Scans a table create SQL string and adds to the data dictionary
1860 the foreign key constraints declared in the string. This function
1861 should be called after the indexes for a table have been created.
1862diff -ruN a/storage/innobase/include/srv0srv.h b/storage/innobase/include/srv0srv.h
1863--- a/storage/innobase/include/srv0srv.h 2010-12-03 15:53:54.622036720 +0900
1864+++ b/storage/innobase/include/srv0srv.h 2010-12-03 17:19:24.906953188 +0900
df1b5770 1865@@ -214,6 +214,9 @@
b4e1fa2c
AM
1866 extern ibool srv_innodb_status;
1867
1868 extern unsigned long long srv_stats_sample_pages;
b4e1fa2c
AM
1869+extern ulint srv_stats_auto_update;
1870+extern ulint srv_stats_update_need_lock;
1871+extern ibool srv_use_sys_stats_table;
1872
1873 extern ibool srv_use_doublewrite_buf;
1874 extern ibool srv_use_checksums;
b4e1fa2c
AM
1875diff -ruN a/storage/innobase/que/que0que.c b/storage/innobase/que/que0que.c
1876--- a/storage/innobase/que/que0que.c 2010-11-03 07:01:13.000000000 +0900
1877+++ b/storage/innobase/que/que0que.c 2010-12-03 17:19:24.910953422 +0900
1878@@ -621,11 +621,21 @@
1879
1880 que_graph_free_recursive(cre_ind->ind_def);
1881 que_graph_free_recursive(cre_ind->field_def);
1882+ if (srv_use_sys_stats_table)
1883+ que_graph_free_recursive(cre_ind->stats_def);
1884 que_graph_free_recursive(cre_ind->commit_node);
1885
1886 mem_heap_free(cre_ind->heap);
1887
1888 break;
1889+ case QUE_NODE_INSERT_STATS:
1890+ cre_ind = node;
1891+
1892+ que_graph_free_recursive(cre_ind->stats_def);
1893+ que_graph_free_recursive(cre_ind->commit_node);
1894+
1895+ mem_heap_free(cre_ind->heap);
1896+ break;
1897 case QUE_NODE_PROC:
1898 que_graph_free_stat_list(((proc_node_t*)node)->stat_list);
1899
1900@@ -1138,6 +1148,8 @@
1901 str = "CREATE TABLE";
1902 } else if (type == QUE_NODE_CREATE_INDEX) {
1903 str = "CREATE INDEX";
1904+ } else if (type == QUE_NODE_INSERT_STATS) {
1905+ str = "INSERT TO SYS_STATS";
1906 } else if (type == QUE_NODE_FOR) {
1907 str = "FOR LOOP";
1908 } else if (type == QUE_NODE_RETURN) {
1909@@ -1255,6 +1267,8 @@
1910 thr = dict_create_table_step(thr);
1911 } else if (type == QUE_NODE_CREATE_INDEX) {
1912 thr = dict_create_index_step(thr);
1913+ } else if (type == QUE_NODE_INSERT_STATS) {
1914+ thr = dict_insert_stats_step(thr);
1915 } else if (type == QUE_NODE_ROW_PRINTF) {
1916 thr = row_printf_step(thr);
1917 } else {
11822e22
AM
1918diff -ruN a/storage/innobase/row/row0ins.c b/storage/innobase/row/row0ins.c
1919--- a/storage/innobase/row/row0ins.c 2011-03-10 00:11:38.000000000 +0900
1920+++ b/storage/innobase/row/row0ins.c 2011-03-30 11:44:58.000000000 +0900
1921@@ -2012,6 +2012,8 @@
1922 }
1923
1924 #ifdef UNIV_DEBUG
1925+ if (!srv_use_sys_stats_table
1926+ || index != UT_LIST_GET_FIRST(dict_sys->sys_stats->indexes))
1927 {
1928 page_t* page = btr_cur_get_page(&cursor);
1929 rec_t* first_rec = page_rec_get_next(
b4e1fa2c
AM
1930diff -ruN a/storage/innobase/row/row0merge.c b/storage/innobase/row/row0merge.c
1931--- a/storage/innobase/row/row0merge.c 2010-11-03 07:01:13.000000000 +0900
1932+++ b/storage/innobase/row/row0merge.c 2010-12-03 17:19:24.914955391 +0900
1933@@ -2020,6 +2020,8 @@
1934 "UPDATE SYS_INDEXES SET NAME=CONCAT('"
1935 TEMP_INDEX_PREFIX_STR "', NAME) WHERE ID = :indexid;\n"
1936 "COMMIT WORK;\n"
1937+ /* Drop the statistics of the index. */
1938+ "DELETE FROM SYS_STATS WHERE INDEX_ID = :indexid;\n"
1939 /* Drop the field definitions of the index. */
1940 "DELETE FROM SYS_FIELDS WHERE INDEX_ID = :indexid;\n"
1941 /* Drop the index definition and the B-tree. */
1942diff -ruN a/storage/innobase/row/row0mysql.c b/storage/innobase/row/row0mysql.c
1943--- a/storage/innobase/row/row0mysql.c 2010-11-03 07:01:13.000000000 +0900
1944+++ b/storage/innobase/row/row0mysql.c 2010-12-03 17:19:24.918953476 +0900
1945@@ -921,6 +921,9 @@
1946
1947 table->stat_modified_counter = counter + 1;
1948
1949+ if (!srv_stats_auto_update)
1950+ return;
1951+
1952 /* Calculate new statistics if 1 / 16 of table has been modified
1953 since the last time a statistics batch was run, or if
1954 stat_modified_counter > 2 000 000 000 (to avoid wrap-around).
1955@@ -931,7 +934,7 @@
1956 || ((ib_int64_t)counter > 16 + table->stat_n_rows / 16)) {
1957
1958 dict_update_statistics(table, FALSE /* update even if stats
1959- are initialized */);
1960+ are initialized */, TRUE);
1961 }
1962 }
1963
11822e22 1964@@ -2105,6 +2108,71 @@
b4e1fa2c
AM
1965 }
1966
1967 /*********************************************************************//**
1968+*/
1969+UNIV_INTERN
1970+int
1971+row_insert_stats_for_mysql(
1972+/*=======================*/
1973+ dict_index_t* index,
1974+ trx_t* trx)
1975+{
1976+ ind_node_t* node;
1977+ mem_heap_t* heap;
1978+ que_thr_t* thr;
1979+ ulint err;
1980+
1981+ ut_ad(trx->mysql_thread_id == os_thread_get_curr_id());
1982+
1983+ trx->op_info = "try to insert rows to SYS_STATS";
1984+
1985+ trx_start_if_not_started(trx);
1986+ trx->error_state = DB_SUCCESS;
1987+
1988+ heap = mem_heap_create(512);
1989+
1990+ node = ind_insert_stats_graph_create(index, heap);
1991+
1992+ thr = pars_complete_graph_for_exec(node, trx, heap);
1993+
1994+ ut_a(thr == que_fork_start_command(que_node_get_parent(thr)));
1995+ que_run_threads(thr);
1996+
1997+ err = trx->error_state;
1998+
1999+ que_graph_free((que_t*) que_node_get_parent(thr));
2000+
2001+ trx->op_info = "";
2002+
2003+ return((int) err);
2004+}
2005+
11822e22
AM
2006+/*********************************************************************//**
2007+*/
2008+UNIV_INTERN
2009+int
2010+row_delete_stats_for_mysql(
2011+/*=============================*/
2012+ dict_index_t* index,
2013+ trx_t* trx)
2014+{
2015+ pars_info_t* info = pars_info_create();
2016+
2017+ trx->op_info = "delete rows from SYS_STATS";
2018+
2019+ trx_start_if_not_started(trx);
2020+ trx->error_state = DB_SUCCESS;
2021+
2022+ pars_info_add_ull_literal(info, "indexid", index->id);
2023+
2024+ return((int) que_eval_sql(info,
2025+ "PROCEDURE DELETE_STATISTICS_PROC () IS\n"
2026+ "BEGIN\n"
2027+ "DELETE FROM SYS_STATS WHERE INDEX_ID = :indexid;\n"
2028+ "END;\n"
2029+ , TRUE, trx));
2030+}
2031+
b4e1fa2c
AM
2032+/*********************************************************************//**
2033 Scans a table create SQL string and adds to the data dictionary
2034 the foreign key constraints declared in the string. This function
2035 should be called after the indexes for a table have been created.
11822e22 2036@@ -3024,7 +3092,7 @@
b4e1fa2c
AM
2037 dict_table_autoinc_initialize(table, 1);
2038 dict_table_autoinc_unlock(table);
2039 dict_update_statistics(table, FALSE /* update even if stats are
2040- initialized */);
2041+ initialized */, TRUE);
2042
2043 trx_commit_for_mysql(trx);
2044
11822e22 2045@@ -3326,6 +3394,8 @@
b4e1fa2c
AM
2046 " IF (SQL % NOTFOUND) THEN\n"
2047 " found := 0;\n"
2048 " ELSE\n"
2049+ " DELETE FROM SYS_STATS\n"
2050+ " WHERE INDEX_ID = index_id;\n"
2051 " DELETE FROM SYS_FIELDS\n"
2052 " WHERE INDEX_ID = index_id;\n"
2053 " DELETE FROM SYS_INDEXES\n"
11822e22
AM
2054diff -ruN a/storage/innobase/row/row0row.c b/storage/innobase/row/row0row.c
2055--- a/storage/innobase/row/row0row.c 2011-03-10 00:11:38.000000000 +0900
2056+++ b/storage/innobase/row/row0row.c 2011-03-30 11:44:58.000000000 +0900
2057@@ -347,6 +347,14 @@
2058
2059 rec_len = rec_offs_n_fields(offsets);
2060
2061+ if (srv_use_sys_stats_table
2062+ && index == UT_LIST_GET_FIRST(dict_sys->sys_stats->indexes)) {
2063+ if (rec_len < dict_index_get_n_fields(index)) {
2064+ /* the new record should be extended */
2065+ rec_len = dict_index_get_n_fields(index);
2066+ }
2067+ }
2068+
2069 entry = dtuple_create(heap, rec_len);
2070
2071 dtuple_set_n_fields_cmp(entry,
2072@@ -358,6 +366,14 @@
2073 for (i = 0; i < rec_len; i++) {
2074
2075 dfield = dtuple_get_nth_field(entry, i);
2076+
2077+ if (srv_use_sys_stats_table
2078+ && index == UT_LIST_GET_FIRST(dict_sys->sys_stats->indexes)
2079+ && i >= rec_offs_n_fields(offsets)) {
2080+ dfield_set_null(dfield);
2081+ continue;
2082+ }
2083+
2084 field = rec_get_nth_field(rec, offsets, i, &len);
2085
2086 dfield_set_data(dfield, field, len);
2087diff -ruN a/storage/innobase/row/row0upd.c b/storage/innobase/row/row0upd.c
2088--- a/storage/innobase/row/row0upd.c 2011-03-10 00:11:38.000000000 +0900
2089+++ b/storage/innobase/row/row0upd.c 2011-03-30 11:44:58.000000000 +0900
2090@@ -439,6 +439,12 @@
2091 0);
2092 }
2093
2094+ if (srv_use_sys_stats_table
2095+ && index == UT_LIST_GET_FIRST(dict_sys->sys_stats->indexes)
2096+ && upd_field->field_no >= rec_offs_n_fields(offsets)) {
2097+ return(TRUE);
2098+ }
2099+
2100 old_len = rec_offs_nth_size(offsets, upd_field->field_no);
2101
2102 if (rec_offs_comp(offsets)
2103@@ -879,6 +885,18 @@
2104
2105 for (i = 0; i < dtuple_get_n_fields(entry); i++) {
2106
2107+ if (srv_use_sys_stats_table
2108+ && index == UT_LIST_GET_FIRST(dict_sys->sys_stats->indexes)
2109+ && i >= rec_offs_n_fields(offsets)) {
2110+ dfield = dtuple_get_nth_field(entry, i);
2111+
2112+ upd_field = upd_get_nth_field(update, n_diff);
2113+ dfield_copy(&(upd_field->new_val), dfield);
2114+ upd_field_set_field_no(upd_field, i, index, trx);
2115+ n_diff++;
2116+ goto skip_compare;
2117+ }
2118+
2119 data = rec_get_nth_field(rec, offsets, i, &len);
2120
2121 dfield = dtuple_get_nth_field(entry, i);
b4e1fa2c
AM
2122diff -ruN a/storage/innobase/srv/srv0srv.c b/storage/innobase/srv/srv0srv.c
2123--- a/storage/innobase/srv/srv0srv.c 2010-12-03 15:53:54.625288512 +0900
2124+++ b/storage/innobase/srv/srv0srv.c 2010-12-03 17:19:24.922953561 +0900
11822e22 2125@@ -405,6 +405,9 @@
b4e1fa2c
AM
2126 /* When estimating number of different key values in an index, sample
2127 this many index pages */
2128 UNIV_INTERN unsigned long long srv_stats_sample_pages = 8;
b4e1fa2c
AM
2129+UNIV_INTERN ulint srv_stats_auto_update = 1;
2130+UNIV_INTERN ulint srv_stats_update_need_lock = 1;
2131+UNIV_INTERN ibool srv_use_sys_stats_table = FALSE;
2132
2133 UNIV_INTERN ibool srv_use_doublewrite_buf = TRUE;
2134 UNIV_INTERN ibool srv_use_checksums = TRUE;
11822e22
AM
2135diff -ruN a/storage/innobase/trx/trx0rec.c b/storage/innobase/trx/trx0rec.c
2136--- a/storage/innobase/trx/trx0rec.c 2011-03-10 00:11:38.000000000 +0900
2137+++ b/storage/innobase/trx/trx0rec.c 2011-03-30 11:44:58.000000000 +0900
2138@@ -665,14 +665,27 @@
2139 /* Save to the undo log the old values of the columns to be updated. */
2140
2141 if (update) {
2142+ ulint extended = 0;
2143+
2144 if (trx_undo_left(undo_page, ptr) < 5) {
2145
2146 return(0);
2147 }
2148
2149- ptr += mach_write_compressed(ptr, upd_get_n_fields(update));
2150+ if (srv_use_sys_stats_table
2151+ && index == UT_LIST_GET_FIRST(dict_sys->sys_stats->indexes)) {
2152+ for (i = 0; i < upd_get_n_fields(update); i++) {
2153+ ulint pos = upd_get_nth_field(update, i)->field_no;
2154+
2155+ if (pos >= rec_offs_n_fields(offsets)) {
2156+ extended++;
2157+ }
2158+ }
2159+ }
2160+
2161+ ptr += mach_write_compressed(ptr, upd_get_n_fields(update) - extended);
2162
2163- for (i = 0; i < upd_get_n_fields(update); i++) {
2164+ for (i = 0; i < upd_get_n_fields(update) - extended; i++) {
2165
2166 ulint pos = upd_get_nth_field(update, i)->field_no;
2167
This page took 0.346229 seconds and 4 git commands to generate.