~percona-dev/percona-xtradb/extensions-1.0.6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
diff -ruN innodb_plugin-1.0.6_orig/btr/btr0cur.c innodb_plugin-1.0.6_tmp/btr/btr0cur.c
--- innodb_plugin-1.0.6_orig/btr/btr0cur.c	2009-12-04 12:13:50.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/btr/btr0cur.c	2009-12-04 12:16:38.000000000 +0900
@@ -886,6 +886,108 @@
 	}
 }
 
+/**********************************************************************//**
+Positions a cursor at a randomly chosen position within a B-tree
+after the given path
+@return TRUE if the position is at the first page, and cursor must point
+        the first record for used by the caller.*/
+UNIV_INTERN
+ibool
+btr_cur_open_at_rnd_pos_after_path(
+/*====================*/
+	dict_index_t*	index,		/*!< in: index */
+	ulint		latch_mode,	/*!< in: BTR_SEARCH_LEAF, ... */
+	btr_path_t*	first_rec_path,
+	btr_cur_t*	cursor,		/*!< in/out: B-tree cursor */
+	mtr_t*		mtr)		/*!< in: mtr */
+{
+	page_cur_t*	page_cursor;
+	btr_path_t*	slot;
+	ibool		is_first_rec	= TRUE;
+	ulint		page_no;
+	ulint		space;
+	ulint		zip_size;
+	ulint		height;
+	rec_t*		node_ptr;
+	mem_heap_t*	heap		= NULL;
+	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
+	ulint*		offsets		= offsets_;
+	rec_offs_init(offsets_);
+
+	if (latch_mode == BTR_MODIFY_TREE) {
+		mtr_x_lock(dict_index_get_lock(index), mtr);
+	} else {
+		mtr_s_lock(dict_index_get_lock(index), mtr);
+	}
+
+	page_cursor = btr_cur_get_page_cur(cursor);
+	cursor->index = index;
+
+	space = dict_index_get_space(index);
+	zip_size = dict_table_zip_size(index->table);
+	page_no = dict_index_get_page(index);
+
+	height = ULINT_UNDEFINED;
+	slot = first_rec_path;
+
+	for (;;) {
+		buf_block_t*	block;
+		page_t*		page;
+
+		block = buf_page_get_gen(space, zip_size, page_no,
+					 RW_NO_LATCH, NULL, BUF_GET,
+					 __FILE__, __LINE__, mtr);
+		page = buf_block_get_frame(block);
+		ut_ad(0 == ut_dulint_cmp(index->id,
+					 btr_page_get_index_id(page)));
+
+		if (height == ULINT_UNDEFINED) {
+			/* We are in the root node */
+
+			height = btr_page_get_level(page, mtr);
+		}
+
+		if (height == 0) {
+			btr_cur_latch_leaves(page, space, zip_size, page_no,
+					     latch_mode, cursor, mtr);
+		}
+
+		if (is_first_rec && slot->nth_rec != ULINT_UNDEFINED) {
+			if (height == 0) {
+				/* must open the first rec */
+				page_cur_open_on_nth_user_rec(block, page_cursor, slot->nth_rec);
+			} else {
+				is_first_rec = page_cur_open_on_rnd_user_rec_after_nth(block,
+								page_cursor, slot->nth_rec);
+			}
+		} else {
+			is_first_rec = FALSE;
+			page_cur_open_on_rnd_user_rec(block, page_cursor);
+		}
+
+		if (height == 0) {
+			break;
+		}
+
+		ut_ad(height > 0);
+
+		height--;
+		slot++;
+
+		node_ptr = page_cur_get_rec(page_cursor);
+		offsets = rec_get_offsets(node_ptr, cursor->index, offsets,
+					  ULINT_UNDEFINED, &heap);
+		/* Go to the child node */
+		page_no = btr_node_ptr_get_child_page_no(node_ptr, offsets);
+	}
+
+	if (UNIV_LIKELY_NULL(heap)) {
+		mem_heap_free(heap);
+	}
+
+	return (is_first_rec);
+}
+
 /*==================== B-TREE INSERT =========================*/
 
 /*************************************************************//**
@@ -3216,6 +3318,154 @@
 }
 
 /*******************************************************************//**
+Estimates the number of pages which have not null value of the key of n_cols.
+@return	estimated number of pages */
+UNIV_INTERN
+ulint
+btr_estimate_n_pages_not_null(
+/*=========================*/
+	dict_index_t*	index,	/*!< in: index */
+	ulint		n_cols,	/*!< in: The cols should be not null */
+	btr_path_t*	path1)	/*!< in: path1[BTR_PATH_ARRAY_N_SLOTS] */
+{
+	dtuple_t*	tuple1;
+	btr_path_t	path2[BTR_PATH_ARRAY_N_SLOTS];
+	btr_cur_t	cursor;
+	btr_path_t*	slot1;
+	btr_path_t*	slot2;
+	ibool		diverged;
+	ibool		diverged_lot;
+	ulint		divergence_level;
+	ulint		n_pages;
+	ulint		i,j;
+	mtr_t		mtr;
+	mem_heap_t*	heap;
+
+	heap = mem_heap_create(n_cols * sizeof(dfield_t)
+				+ sizeof(dtuple_t));
+
+	/* make tuple1 (NULL,NULL,,,) from n_cols */
+	tuple1 = dtuple_create(heap, n_cols);
+	dict_index_copy_types(tuple1, index, n_cols);
+
+	for (i = 0; i < n_cols; i++) {
+		dfield_set_null(dtuple_get_nth_field(tuple1, i));
+	}
+
+	mtr_start(&mtr);
+
+	cursor.path_arr = path1;
+
+	btr_cur_search_to_nth_level(index, 0, tuple1, PAGE_CUR_G,
+				    BTR_SEARCH_LEAF | BTR_ESTIMATE,
+				    &cursor, 0, &mtr);
+
+	mtr_commit(&mtr);
+
+
+
+	mtr_start(&mtr);
+
+	cursor.path_arr = path2;
+
+	btr_cur_open_at_index_side(FALSE, index,
+				   BTR_SEARCH_LEAF | BTR_ESTIMATE,
+				   &cursor, &mtr);
+
+	mtr_commit(&mtr);
+
+	mem_heap_free(heap);
+
+	/* We have the path information for the range in path1 and path2 */
+
+	n_pages = 1;
+	diverged = FALSE;	    /* This becomes true when the path is not
+				    the same any more */
+	diverged_lot = FALSE;	    /* This becomes true when the paths are
+				    not the same or adjacent any more */
+	divergence_level = 1000000; /* This is the level where paths diverged
+				    a lot */
+	for (i = 0; ; i++) {
+		ut_ad(i < BTR_PATH_ARRAY_N_SLOTS);
+
+		slot1 = path1 + i;
+		slot2 = path2 + i;
+
+		if ((slot1 + 1)->nth_rec == ULINT_UNDEFINED
+		    || (slot2 + 1)->nth_rec == ULINT_UNDEFINED) {
+
+			if (i > divergence_level + 1) {
+				/* In trees whose height is > 1 our algorithm
+				tends to underestimate: multiply the estimate
+				by 2: */
+
+				n_pages = n_pages * 2;
+			}
+
+			/* Do not estimate the number of rows in the range
+			to over 1 / 2 of the estimated rows in the whole
+			table */
+
+			if (n_pages > index->stat_n_leaf_pages / 2) {
+				n_pages = index->stat_n_leaf_pages / 2;
+
+				/* If there are just 0 or 1 rows in the table,
+				then we estimate all rows are in the range */
+
+				if (n_pages == 0) {
+					n_pages = index->stat_n_leaf_pages;
+				}
+			}
+
+			return(n_pages);
+		}
+
+		if (!diverged && slot1->nth_rec != slot2->nth_rec) {
+
+			diverged = TRUE;
+
+			if (slot1->nth_rec < slot2->nth_rec) {
+				n_pages = slot2->nth_rec - slot1->nth_rec;
+
+				if (n_pages > 1) {
+					diverged_lot = TRUE;
+					divergence_level = i;
+				}
+			} else {
+				/* Maybe the tree has changed between
+				searches */
+
+				return(10);
+			}
+
+		} else if (diverged && !diverged_lot) {
+
+			if (slot1->nth_rec < slot1->n_recs
+			    || slot2->nth_rec > 1) {
+
+				diverged_lot = TRUE;
+				divergence_level = i;
+
+				n_pages = 0;
+
+				if (slot1->nth_rec < slot1->n_recs) {
+					n_pages += slot1->n_recs
+						- slot1->nth_rec;
+				}
+
+				if (slot2->nth_rec > 1) {
+					n_pages += slot2->nth_rec - 1;
+				}
+			}
+		} else if (diverged_lot) {
+
+			n_pages = (n_pages * (slot1->n_recs + slot2->n_recs))
+				/ 2;
+		}
+	}
+}
+
+/*******************************************************************//**
 Estimates the number of different key values in a given index, for
 each n-column prefix of the index where n <= dict_index_get_n_unique(index).
 The estimates are stored in the array index->stat_n_diff_key_vals. */
@@ -3244,18 +3494,38 @@
 	ulint		offsets_next_rec_[REC_OFFS_NORMAL_SIZE];
 	ulint*		offsets_rec	= offsets_rec_;
 	ulint*		offsets_next_rec= offsets_next_rec_;
+	ulint		stats_method	= srv_stats_method;
+	btr_path_t	first_rec_path[BTR_PATH_ARRAY_N_SLOTS];
+	ulint		effective_pages; /* effective leaf pages */
 	rec_offs_init(offsets_rec_);
 	rec_offs_init(offsets_next_rec_);
 
 	n_cols = dict_index_get_n_unique(index);
 
+	if (stats_method == SRV_STATS_METHOD_IGNORE_NULLS) {
+		/* estimate effective pages and path for the first effective record */
+		/* TODO: make it work also for n_cols > 1. */
+		effective_pages = btr_estimate_n_pages_not_null(index, 1 /*k*/, first_rec_path);
+
+		if (!effective_pages) {
+			for (j = 0; j <= n_cols; j++) {
+				index->stat_n_diff_key_vals[j] = (ib_int64_t)index->stat_n_leaf_pages;
+			}
+			return;
+		} else if (effective_pages > index->stat_n_leaf_pages) {
+			effective_pages = index->stat_n_leaf_pages;
+		}
+	} else {
+		effective_pages = index->stat_n_leaf_pages;
+	}
+
 	n_diff = mem_zalloc((n_cols + 1) * sizeof(ib_int64_t));
 
 	/* It makes no sense to test more pages than are contained
 	in the index, thus we lower the number if it is too high */
-	if (srv_stats_sample_pages > index->stat_index_size) {
-		if (index->stat_index_size > 0) {
-			n_sample_pages = index->stat_index_size;
+	if (srv_stats_sample_pages > effective_pages) {
+		if (effective_pages > 0) {
+			n_sample_pages = effective_pages;
 		} else {
 			n_sample_pages = 1;
 		}
@@ -3267,9 +3537,15 @@
 
 	for (i = 0; i < n_sample_pages; i++) {
 		rec_t*	supremum;
+		ibool	is_first_page = TRUE;
 		mtr_start(&mtr);
 
+		if (stats_method == SRV_STATS_METHOD_IGNORE_NULLS) {
+			is_first_page = btr_cur_open_at_rnd_pos_after_path(index, BTR_SEARCH_LEAF,
+									first_rec_path, &cursor, &mtr);
+		} else {
 		btr_cur_open_at_rnd_pos(index, BTR_SEARCH_LEAF, &cursor, &mtr);
+		}
 
 		/* Count the number of different key values for each prefix of
 		the key on this index page. If the prefix does not determine
@@ -3280,7 +3556,13 @@
 		page = btr_cur_get_page(&cursor);
 
 		supremum = page_get_supremum_rec(page);
+		if (stats_method == SRV_STATS_METHOD_IGNORE_NULLS && is_first_page) {
+			/* the cursor should be the first record of the page. */
+			/* Counting should be started from here. */
+			rec = btr_cur_get_rec(&cursor);
+		} else {
 		rec = page_rec_get_next(page_get_infimum_rec(page));
+		}
 
 		if (rec != supremum) {
 			not_empty_flag = 1;
@@ -3289,7 +3571,8 @@
 		}
 
 		while (rec != supremum) {
-			rec_t*	next_rec = page_rec_get_next(rec);
+			rec_t*  next_rec;
+			next_rec = page_rec_get_next(rec);
 			if (next_rec == supremum) {
 				break;
 			}
@@ -3303,7 +3586,10 @@
 			cmp_rec_rec_with_match(rec, next_rec,
 					       offsets_rec, offsets_next_rec,
 					       index, &matched_fields,
-					       &matched_bytes);
+					       &matched_bytes,
+				(stats_method==SRV_STATS_METHOD_NULLS_NOT_EQUAL) ?
+				SRV_STATS_METHOD_NULLS_NOT_EQUAL :
+				SRV_STATS_METHOD_NULLS_EQUAL);
 
 			for (j = matched_fields + 1; j <= n_cols; j++) {
 				/* We add one if this index record has
@@ -3364,7 +3650,7 @@
 	for (j = 0; j <= n_cols; j++) {
 		index->stat_n_diff_key_vals[j]
 			= ((n_diff[j]
-			    * (ib_int64_t)index->stat_n_leaf_pages
+			    * (ib_int64_t)effective_pages
 			    + n_sample_pages - 1
 			    + total_external_size
 			    + not_empty_flag)
@@ -3379,7 +3665,7 @@
 		different key values, or even more. Let us try to approximate
 		that: */
 
-		add_on = index->stat_n_leaf_pages
+		add_on = effective_pages
 			/ (10 * (n_sample_pages
 				 + total_external_size));
 
@@ -3388,9 +3674,19 @@
 		}
 
 		index->stat_n_diff_key_vals[j] += add_on;
+
+		if (stats_method == SRV_STATS_METHOD_IGNORE_NULLS) {
+			/* index->stat_n_diff_key_vals[k] is used for calc rec_per_key,
+			as "stats.records / index->stat_n_diff_key_vals[x]".
+			So it should be adjusted to the value which is based on whole of the index. */
+			index->stat_n_diff_key_vals[j] =
+				index->stat_n_diff_key_vals[j] * (ib_int64_t)index->stat_n_leaf_pages
+					/ (ib_int64_t)effective_pages;
+		}
 	}
 
 	mem_free(n_diff);
+
 	if (UNIV_LIKELY_NULL(heap)) {
 		mem_heap_free(heap);
 	}
diff -ruN innodb_plugin-1.0.6_orig/dict/dict0dict.c innodb_plugin-1.0.6_tmp/dict/dict0dict.c
--- innodb_plugin-1.0.6_orig/dict/dict0dict.c	2009-12-04 12:08:15.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/dict/dict0dict.c	2009-12-04 12:16:38.000000000 +0900
@@ -4339,7 +4339,8 @@
 
 	ut_ad(mutex_own(&(dict_sys->mutex)));
 
-	dict_update_statistics_low(table, TRUE);
+	if (srv_stats_auto_update)
+		dict_update_statistics_low(table, TRUE);
 
 	fprintf(stderr,
 		"--------------------------------------\n"
diff -ruN innodb_plugin-1.0.6_orig/dict/dict0load.c innodb_plugin-1.0.6_tmp/dict/dict0load.c
--- innodb_plugin-1.0.6_orig/dict/dict0load.c	2009-05-25 19:52:29.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/dict/dict0load.c	2009-12-04 12:16:38.000000000 +0900
@@ -221,7 +221,7 @@
 			/* The table definition was corrupt if there
 			is no index */
 
-			if (dict_table_get_first_index(table)) {
+			if (srv_stats_auto_update && dict_table_get_first_index(table)) {
 				dict_update_statistics_low(table, TRUE);
 			}
 
diff -ruN innodb_plugin-1.0.6_orig/handler/ha_innodb.cc innodb_plugin-1.0.6_tmp/handler/ha_innodb.cc
--- innodb_plugin-1.0.6_orig/handler/ha_innodb.cc	2009-12-04 12:15:57.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/handler/ha_innodb.cc	2009-12-04 12:16:38.000000000 +0900
@@ -4644,6 +4644,10 @@
 
 	error = row_insert_for_mysql((byte*) record, prebuilt);
 
+#ifdef EXTENDED_FOR_USERSTAT
+	if (error == DB_SUCCESS) rows_changed++;
+#endif
+
 	/* Handle duplicate key errors */
 	if (auto_inc_used) {
 		ulint		err;
@@ -4975,6 +4979,10 @@
 		}
 	}
 
+#ifdef EXTENDED_FOR_USERSTAT
+	if (error == DB_SUCCESS) rows_changed++;
+#endif
+
 	innodb_srv_conc_exit_innodb(trx);
 
 	error = convert_error_code_to_mysql(error,
@@ -5028,6 +5036,10 @@
 
 	error = row_update_for_mysql((byte*) record, prebuilt);
 
+#ifdef EXTENDED_FOR_USERSTAT
+	if (error == DB_SUCCESS) rows_changed++;
+#endif
+
 	innodb_srv_conc_exit_innodb(trx);
 
 	error = convert_error_code_to_mysql(
@@ -5536,6 +5548,11 @@
 	case DB_SUCCESS:
 		error = 0;
 		table->status = 0;
+#ifdef EXTENDED_FOR_USERSTAT
+		rows_read++;
+		if (active_index >= 0 && active_index < MAX_KEY)
+			index_rows_read[active_index]++;
+#endif
 		break;
 	case DB_RECORD_NOT_FOUND:
 		error = HA_ERR_END_OF_FILE;
@@ -7312,7 +7329,9 @@
 	ib_table = prebuilt->table;
 
 	if (flag & HA_STATUS_TIME) {
-		if (innobase_stats_on_metadata) {
+		if (innobase_stats_on_metadata
+		    || thd_sql_command(user_thd) == SQLCOM_ANALYZE
+		    ) {
 			/* In sql_show we call with this flag: update
 			then statistics so that they are up-to-date */
 
@@ -7394,7 +7413,7 @@
 		We do not update delete_length if no locking is requested
 		so the "old" value can remain. delete_length is initialized
 		to 0 in the ha_statistics' constructor. */
-		if (!(flag & HA_STATUS_NO_LOCK)) {
+		if (!(flag & HA_STATUS_NO_LOCK) && srv_stats_update_need_lock) {
 
 			/* lock the data dictionary to avoid races with
 			ibd_file_missing and tablespace_discarded */
@@ -10274,6 +10293,37 @@
   "The number of index pages to sample when calculating statistics (default 8)",
   NULL, NULL, 8, 1, ~0ULL, 0);
 
+const char *innobase_stats_method_names[]=
+{
+  "nulls_equal",
+  "nulls_unequal",
+  "nulls_ignored",
+  NullS
+};
+TYPELIB innobase_stats_method_typelib=
+{
+  array_elements(innobase_stats_method_names) - 1, "innobase_stats_method_typelib",
+  innobase_stats_method_names, NULL
+};
+static MYSQL_SYSVAR_ENUM(stats_method, srv_stats_method,
+  PLUGIN_VAR_RQCMDARG,
+  "Specifies how InnoDB index statistics collection code should threat NULLs. "
+  "Possible values of name are same to for 'myisam_stats_method'. "
+  "This is startup parameter.",
+  NULL, NULL, 0, &innobase_stats_method_typelib);
+
+static MYSQL_SYSVAR_ULONG(stats_auto_update, srv_stats_auto_update,
+  PLUGIN_VAR_RQCMDARG,
+  "Enable/Disable InnoDB's auto update statistics of indexes. "
+  "(except for ANALYZE TABLE command) 0:disable 1:enable",
+  NULL, NULL, 1, 0, 1, 0);
+
+static MYSQL_SYSVAR_ULONG(stats_update_need_lock, srv_stats_update_need_lock,
+  PLUGIN_VAR_RQCMDARG,
+  "Enable/Disable InnoDB's update statistics which needs to lock dictionary. "
+  "e.g. Data_free.",
+  NULL, NULL, 1, 0, 1, 0);
+
 static MYSQL_SYSVAR_BOOL(adaptive_hash_index, btr_search_enabled,
   PLUGIN_VAR_OPCMDARG,
   "Enable InnoDB adaptive hash index (enabled by default).  "
@@ -10583,6 +10633,9 @@
   MYSQL_SYSVAR(overwrite_relay_log_info),
   MYSQL_SYSVAR(rollback_on_timeout),
   MYSQL_SYSVAR(stats_on_metadata),
+  MYSQL_SYSVAR(stats_method),
+  MYSQL_SYSVAR(stats_auto_update),
+  MYSQL_SYSVAR(stats_update_need_lock),
   MYSQL_SYSVAR(stats_sample_pages),
   MYSQL_SYSVAR(adaptive_hash_index),
   MYSQL_SYSVAR(replication_delay),
@@ -10643,6 +10696,8 @@
 i_s_innodb_cmp_reset,
 i_s_innodb_cmpmem,
 i_s_innodb_cmpmem_reset,
+i_s_innodb_table_stats,
+i_s_innodb_index_stats,
 i_s_innodb_patches
 mysql_declare_plugin_end;
 
diff -ruN innodb_plugin-1.0.6_orig/handler/i_s.cc innodb_plugin-1.0.6_tmp/handler/i_s.cc
--- innodb_plugin-1.0.6_orig/handler/i_s.cc	2009-12-04 12:13:50.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/handler/i_s.cc	2009-12-04 12:16:38.000000000 +0900
@@ -46,6 +46,7 @@
 #include "dict0dict.h" /* for dict_index_get_if_in_cache */
 #include "trx0rseg.h" /* for trx_rseg_struct */
 #include "trx0sys.h" /* for trx_sys */
+#include "dict0dict.h" /* for dict_sys */
 /* from buf0buf.c */
 struct buf_chunk_struct{
 	ulint		mem_size;	/* allocated size of the chunk */
@@ -2657,3 +2658,342 @@
 	/* void* */
 	STRUCT_FLD(__reserved1, NULL)
 };
+
+/***********************************************************************
+*/
+static ST_FIELD_INFO	i_s_innodb_table_stats_info[] =
+{
+	{STRUCT_FLD(field_name,		"table_schema"),
+	 STRUCT_FLD(field_length,	NAME_LEN),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_STRING),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	0),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"table_name"),
+	 STRUCT_FLD(field_length,	NAME_LEN),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_STRING),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	0),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"rows"),
+	 STRUCT_FLD(field_length,	MY_INT64_NUM_DECIMAL_DIGITS),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_LONGLONG),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	MY_I_S_UNSIGNED),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"clust_size"),
+	 STRUCT_FLD(field_length,	MY_INT64_NUM_DECIMAL_DIGITS),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_LONGLONG),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	MY_I_S_UNSIGNED),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"other_size"),
+	 STRUCT_FLD(field_length,	MY_INT64_NUM_DECIMAL_DIGITS),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_LONGLONG),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	MY_I_S_UNSIGNED),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"modified"),
+	 STRUCT_FLD(field_length,	MY_INT64_NUM_DECIMAL_DIGITS),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_LONGLONG),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	MY_I_S_UNSIGNED),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	END_OF_ST_FIELD_INFO
+};
+
+static ST_FIELD_INFO	i_s_innodb_index_stats_info[] =
+{
+	{STRUCT_FLD(field_name,		"table_schema"),
+	 STRUCT_FLD(field_length,	NAME_LEN),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_STRING),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	0),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"table_name"),
+	 STRUCT_FLD(field_length,	NAME_LEN),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_STRING),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	0),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"index_name"),
+	 STRUCT_FLD(field_length,	NAME_LEN),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_STRING),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	0),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"fields"),
+	 STRUCT_FLD(field_length,	MY_INT64_NUM_DECIMAL_DIGITS),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_LONGLONG),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	MY_I_S_UNSIGNED),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"row_per_keys"),
+	 STRUCT_FLD(field_length,	256),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_STRING),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	0),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"index_size"),
+	 STRUCT_FLD(field_length,	MY_INT64_NUM_DECIMAL_DIGITS),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_LONGLONG),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	MY_I_S_UNSIGNED),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	{STRUCT_FLD(field_name,		"leaf_pages"),
+	 STRUCT_FLD(field_length,	MY_INT64_NUM_DECIMAL_DIGITS),
+	 STRUCT_FLD(field_type,		MYSQL_TYPE_LONGLONG),
+	 STRUCT_FLD(value,		0),
+	 STRUCT_FLD(field_flags,	MY_I_S_UNSIGNED),
+	 STRUCT_FLD(old_name,		""),
+	 STRUCT_FLD(open_method,	SKIP_OPEN_TABLE)},
+
+	END_OF_ST_FIELD_INFO
+};
+
+static
+int
+i_s_innodb_table_stats_fill(
+/*========================*/
+	THD*		thd,
+	TABLE_LIST*	tables,
+	COND*		cond)
+{
+	TABLE*	i_s_table	= (TABLE *) tables->table;
+	int	status	= 0;
+	dict_table_t*	table;
+
+	DBUG_ENTER("i_s_innodb_table_stats_fill");
+
+	/* deny access to non-superusers */
+	if (check_global_access(thd, PROCESS_ACL)) {
+		DBUG_RETURN(0);
+	}
+
+	mutex_enter(&(dict_sys->mutex));
+
+	table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
+
+	while (table) {
+		char	buf[NAME_LEN * 2 + 2];
+		char*	ptr;
+
+		if (table->stat_clustered_index_size == 0) {
+			table = UT_LIST_GET_NEXT(table_LRU, table);
+			continue;
+		}
+
+		buf[NAME_LEN * 2 + 1] = 0;
+		strncpy(buf, table->name, NAME_LEN * 2 + 1);
+		ptr = strchr(buf, '/');
+		if (ptr) {
+			*ptr = '\0';
+			++ptr;
+		} else {
+			ptr = buf;
+		}
+
+		field_store_string(i_s_table->field[0], buf);
+		field_store_string(i_s_table->field[1], ptr);
+		i_s_table->field[2]->store(table->stat_n_rows);
+		i_s_table->field[3]->store(table->stat_clustered_index_size);
+		i_s_table->field[4]->store(table->stat_sum_of_other_index_sizes);
+		i_s_table->field[5]->store(table->stat_modified_counter);
+
+		if (schema_table_store_record(thd, i_s_table)) {
+			status = 1;
+			break;
+		}
+
+		table = UT_LIST_GET_NEXT(table_LRU, table);
+	}
+
+	mutex_exit(&(dict_sys->mutex));
+
+	DBUG_RETURN(status);
+}
+
+static
+int
+i_s_innodb_index_stats_fill(
+/*========================*/
+	THD*		thd,
+	TABLE_LIST*	tables,
+	COND*		cond)
+{
+	TABLE*	i_s_table	= (TABLE *) tables->table;
+	int	status	= 0;
+	dict_table_t*	table;
+	dict_index_t*	index;
+
+	DBUG_ENTER("i_s_innodb_index_stats_fill");
+
+	/* deny access to non-superusers */
+	if (check_global_access(thd, PROCESS_ACL)) {
+		DBUG_RETURN(0);
+	}
+
+	mutex_enter(&(dict_sys->mutex));
+
+	table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
+
+	while (table) {
+		if (table->stat_clustered_index_size == 0) {
+			table = UT_LIST_GET_NEXT(table_LRU, table);
+			continue;
+		}
+
+		ib_int64_t	n_rows = table->stat_n_rows;
+
+		if (n_rows < 0) {
+			n_rows = 0;
+		}
+
+		index = dict_table_get_first_index(table);
+
+		while (index) {
+			char	buff[256+1];
+			char	row_per_keys[256+1];
+			char	buf[NAME_LEN * 2 + 2];
+			char*	ptr;
+			ulint	i;
+
+			buf[NAME_LEN * 2 + 1] = 0;
+			strncpy(buf, table->name, NAME_LEN * 2 + 1);
+			ptr = strchr(buf, '/');
+			if (ptr) {
+				*ptr = '\0';
+				++ptr;
+			} else {
+				ptr = buf;
+			}
+
+			field_store_string(i_s_table->field[0], buf);
+			field_store_string(i_s_table->field[1], ptr);
+			field_store_string(i_s_table->field[2], index->name);
+			i_s_table->field[3]->store(index->n_uniq);
+
+			row_per_keys[0] = '\0';
+			if (index->stat_n_diff_key_vals) {
+				for (i = 1; i <= index->n_uniq; i++) {
+					ib_int64_t	rec_per_key;
+					if (index->stat_n_diff_key_vals[i]) {
+						rec_per_key = n_rows / index->stat_n_diff_key_vals[i];
+					} else {
+						rec_per_key = n_rows;
+					}
+					ut_snprintf(buff, 256, (i == index->n_uniq)?"%llu":"%llu, ",
+						 rec_per_key);
+					strncat(row_per_keys, buff, 256 - strlen(row_per_keys));
+				}
+			}
+			field_store_string(i_s_table->field[4], row_per_keys);
+
+			i_s_table->field[5]->store(index->stat_index_size);
+			i_s_table->field[6]->store(index->stat_n_leaf_pages);
+
+			if (schema_table_store_record(thd, i_s_table)) {
+				status = 1;
+				break;
+			}
+
+			index = dict_table_get_next_index(index);
+		}
+
+		if (status == 1) {
+			break;
+		}
+
+		table = UT_LIST_GET_NEXT(table_LRU, table);
+	}
+
+	mutex_exit(&(dict_sys->mutex));
+
+	DBUG_RETURN(status);
+}
+
+static
+int
+i_s_innodb_table_stats_init(
+/*========================*/
+	void*   p)
+{
+	DBUG_ENTER("i_s_innodb_table_stats_init");
+	ST_SCHEMA_TABLE* schema = (ST_SCHEMA_TABLE*) p;
+
+	schema->fields_info = i_s_innodb_table_stats_info;
+	schema->fill_table = i_s_innodb_table_stats_fill;
+
+	DBUG_RETURN(0);
+}
+
+static
+int
+i_s_innodb_index_stats_init(
+/*========================*/
+	void*	p)
+{
+	DBUG_ENTER("i_s_innodb_index_stats_init");
+	ST_SCHEMA_TABLE* schema = (ST_SCHEMA_TABLE*) p;
+
+	schema->fields_info = i_s_innodb_index_stats_info;
+	schema->fill_table = i_s_innodb_index_stats_fill;
+
+	DBUG_RETURN(0);
+}
+
+UNIV_INTERN struct st_mysql_plugin	i_s_innodb_table_stats =
+{
+	STRUCT_FLD(type, MYSQL_INFORMATION_SCHEMA_PLUGIN),
+	STRUCT_FLD(info, &i_s_info),
+	STRUCT_FLD(name, "INNODB_TABLE_STATS"),
+	STRUCT_FLD(author, plugin_author),
+	STRUCT_FLD(descr, "InnoDB table statistics in memory"),
+	STRUCT_FLD(license, PLUGIN_LICENSE_GPL),
+	STRUCT_FLD(init, i_s_innodb_table_stats_init),
+	STRUCT_FLD(deinit, i_s_common_deinit),
+	STRUCT_FLD(version, 0x0100 /* 1.0 */),
+	STRUCT_FLD(status_vars, NULL),
+	STRUCT_FLD(system_vars, NULL),
+	STRUCT_FLD(__reserved1, NULL)
+};
+
+UNIV_INTERN struct st_mysql_plugin	i_s_innodb_index_stats =
+{
+	STRUCT_FLD(type, MYSQL_INFORMATION_SCHEMA_PLUGIN),
+	STRUCT_FLD(info, &i_s_info),
+	STRUCT_FLD(name, "INNODB_INDEX_STATS"),
+	STRUCT_FLD(author, plugin_author),
+	STRUCT_FLD(descr, "InnoDB index statistics in memory"),
+	STRUCT_FLD(license, PLUGIN_LICENSE_GPL),
+	STRUCT_FLD(init, i_s_innodb_index_stats_init),
+	STRUCT_FLD(deinit, i_s_common_deinit),
+	STRUCT_FLD(version, 0x0100 /* 1.0 */),
+	STRUCT_FLD(status_vars, NULL),
+	STRUCT_FLD(system_vars, NULL),
+	STRUCT_FLD(__reserved1, NULL)
+};
diff -ruN innodb_plugin-1.0.6_orig/handler/i_s.h innodb_plugin-1.0.6_tmp/handler/i_s.h
--- innodb_plugin-1.0.6_orig/handler/i_s.h	2009-12-04 11:57:09.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/handler/i_s.h	2009-12-04 12:16:38.000000000 +0900
@@ -38,5 +38,7 @@
 extern struct st_mysql_plugin	i_s_innodb_cmpmem_reset;
 extern struct st_mysql_plugin	i_s_innodb_patches;
 extern struct st_mysql_plugin	i_s_innodb_rseg;
+extern struct st_mysql_plugin	i_s_innodb_table_stats;
+extern struct st_mysql_plugin	i_s_innodb_index_stats;
 
 #endif /* i_s_h */
diff -ruN innodb_plugin-1.0.6_orig/handler/innodb_patch_info.h innodb_plugin-1.0.6_tmp/handler/innodb_patch_info.h
--- innodb_plugin-1.0.6_orig/handler/innodb_patch_info.h	2009-12-04 12:15:57.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/handler/innodb_patch_info.h	2009-12-04 12:16:38.000000000 +0900
@@ -35,5 +35,6 @@
 {"innodb_expand_import","convert .ibd file automatically when import tablespace","the files are generated by xtrabackup export mode.","http://www.percona.com/docs/wiki/percona-xtradb"},
 {"innodb_dict_size_limit","Limit dictionary cache size","Variable innodb_dict_size_limit in bytes","http://www.percona.com/docs/wiki/percona-xtradb"},
 {"innodb_split_buf_pool_mutex","More fix of buffer_pool mutex","Spliting buf_pool_mutex and optimizing based on innodb_opt_lru_count","http://www.percona.com/docs/wiki/percona-xtradb"},
+{"innodb_stats","Additional features about InnoDB statistics/optimizer","","http://www.percona.com/docs/wiki/percona-xtradb"},
 {NULL, NULL, NULL, NULL}
 };
diff -ruN innodb_plugin-1.0.6_orig/include/page0cur.h innodb_plugin-1.0.6_tmp/include/page0cur.h
--- innodb_plugin-1.0.6_orig/include/page0cur.h	2009-05-26 22:28:49.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/include/page0cur.h	2010-03-01 11:43:10.000000000 +0900
@@ -293,6 +293,22 @@
 /*==========================*/
 	buf_block_t*	block,	/*!< in: page */
 	page_cur_t*	cursor);/*!< out: page cursor */
+
+UNIV_INTERN
+void
+page_cur_open_on_nth_user_rec(
+/*==========================*/
+	buf_block_t*	block,	/*!< in: page */
+	page_cur_t*	cursor,	/*!< out: page cursor */
+	ulint		nth);
+
+UNIV_INTERN
+ibool
+page_cur_open_on_rnd_user_rec_after_nth(
+/*==========================*/
+	buf_block_t*	block,	/*!< in: page */
+	page_cur_t*	cursor,	/*!< out: page cursor */
+	ulint		nth);
 #endif /* !UNIV_HOTBACKUP */
 /***********************************************************//**
 Parses a log record of a record insert on a page.
diff -ruN innodb_plugin-1.0.6_orig/include/rem0cmp.h innodb_plugin-1.0.6_tmp/include/rem0cmp.h
--- innodb_plugin-1.0.6_orig/include/rem0cmp.h	2009-08-07 08:04:03.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/include/rem0cmp.h	2009-12-04 12:16:38.000000000 +0900
@@ -167,10 +167,11 @@
 				matched fields; when the function returns,
 				contains the value the for current
 				comparison */
-	ulint*		matched_bytes);/*!< in/out: number of already matched
+	ulint*		matched_bytes, /*!< in/out: number of already matched
 				bytes within the first field not completely
 				matched; when the function returns, contains
 				the value for the current comparison */
+	ulint		stats_method);
 /*************************************************************//**
 This function is used to compare two physical records. Only the common
 first fields are compared.
diff -ruN innodb_plugin-1.0.6_orig/include/rem0cmp.ic innodb_plugin-1.0.6_tmp/include/rem0cmp.ic
--- innodb_plugin-1.0.6_orig/include/rem0cmp.ic	2009-05-25 19:52:29.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/include/rem0cmp.ic	2009-12-04 12:16:38.000000000 +0900
@@ -87,5 +87,5 @@
 	ulint	match_b		= 0;
 
 	return(cmp_rec_rec_with_match(rec1, rec2, offsets1, offsets2, index,
-				      &match_f, &match_b));
+				      &match_f, &match_b, 0));
 }
diff -ruN innodb_plugin-1.0.6_orig/include/srv0srv.h innodb_plugin-1.0.6_tmp/include/srv0srv.h
--- innodb_plugin-1.0.6_orig/include/srv0srv.h	2009-12-04 12:15:57.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/include/srv0srv.h	2009-12-04 12:16:38.000000000 +0900
@@ -210,6 +210,12 @@
 extern ibool	srv_innodb_status;
 
 extern unsigned long long	srv_stats_sample_pages;
+extern ulint	srv_stats_method;
+#define SRV_STATS_METHOD_NULLS_EQUAL     0
+#define SRV_STATS_METHOD_NULLS_NOT_EQUAL 1
+#define SRV_STATS_METHOD_IGNORE_NULLS    2
+extern ulint	srv_stats_auto_update;
+extern ulint	srv_stats_update_need_lock;
 
 extern ibool	srv_use_doublewrite_buf;
 extern ibool	srv_use_checksums;
diff -ruN innodb_plugin-1.0.6_orig/page/page0cur.c innodb_plugin-1.0.6_tmp/page/page0cur.c
--- innodb_plugin-1.0.6_orig/page/page0cur.c	2009-09-28 17:52:25.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/page/page0cur.c	2010-03-01 11:43:10.000000000 +0900
@@ -564,6 +564,74 @@
 	} while (rnd--);
 }
 
+UNIV_INTERN
+void
+page_cur_open_on_nth_user_rec(
+/*==========================*/
+	buf_block_t*	block,	/*!< in: page */
+	page_cur_t*	cursor,	/*!< out: page cursor */
+	ulint		nth)
+{
+	ulint	n_recs = page_get_n_recs(buf_block_get_frame(block));
+
+	page_cur_set_before_first(block, cursor);
+
+	if (UNIV_UNLIKELY(n_recs == 0)) {
+
+		return;
+	}
+
+	nth--;
+
+	if (nth >= n_recs) {
+		nth = n_recs - 1;
+	}
+
+	do {
+		page_cur_move_to_next(cursor);
+	} while (nth--);
+}
+
+UNIV_INTERN
+ibool
+page_cur_open_on_rnd_user_rec_after_nth(
+/*==========================*/
+	buf_block_t*	block,	/*!< in: page */
+	page_cur_t*	cursor,	/*!< out: page cursor */
+	ulint		nth)
+{
+	ulint	rnd;
+	ulint	n_recs = page_get_n_recs(buf_block_get_frame(block));
+	ibool	ret;
+
+	page_cur_set_before_first(block, cursor);
+
+	if (UNIV_UNLIKELY(n_recs == 0)) {
+
+		return (FALSE);
+	}
+
+	nth--;
+
+	if (nth >= n_recs) {
+		nth = n_recs - 1;
+	}
+
+	rnd = (ulint) (nth + page_cur_lcg_prng() % (n_recs - nth));
+
+	if (rnd == nth) {
+		ret = TRUE;
+	} else {
+		ret = FALSE;
+	}
+
+	do {
+		page_cur_move_to_next(cursor);
+	} while (rnd--);
+
+	return (ret);
+}
+
 /***********************************************************//**
 Writes the log record of a record insert on a page. */
 static
diff -ruN innodb_plugin-1.0.6_orig/rem/rem0cmp.c innodb_plugin-1.0.6_tmp/rem/rem0cmp.c
--- innodb_plugin-1.0.6_orig/rem/rem0cmp.c	2009-08-07 08:04:03.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/rem/rem0cmp.c	2009-12-04 12:16:38.000000000 +0900
@@ -861,10 +861,11 @@
 				matched fields; when the function returns,
 				contains the value the for current
 				comparison */
-	ulint*		matched_bytes) /*!< in/out: number of already matched
+	ulint*		matched_bytes, /*!< in/out: number of already matched
 				bytes within the first field not completely
 				matched; when the function returns, contains
 				the value for the current comparison */
+	ulint		stats_method)
 {
 	ulint		rec1_n_fields;	/* the number of fields in rec */
 	ulint		rec1_f_len;	/* length of current field in rec */
@@ -957,7 +958,11 @@
 
 				if (rec1_f_len == rec2_f_len) {
 
-					goto next_field;
+					if (stats_method == SRV_STATS_METHOD_NULLS_EQUAL) {
+						goto next_field;
+					} else {
+						ret = -1;
+					}
 
 				} else if (rec2_f_len == UNIV_SQL_NULL) {
 
diff -ruN innodb_plugin-1.0.6_orig/row/row0mysql.c innodb_plugin-1.0.6_tmp/row/row0mysql.c
--- innodb_plugin-1.0.6_orig/row/row0mysql.c	2009-11-17 23:44:20.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/row/row0mysql.c	2009-12-04 12:16:38.000000000 +0900
@@ -852,6 +852,9 @@
 
 	table->stat_modified_counter = counter + 1;
 
+	if (!srv_stats_auto_update)
+		return;
+
 	/* Calculate new statistics if 1 / 16 of table has been modified
 	since the last time a statistics batch was run, or if
 	stat_modified_counter > 2 000 000 000 (to avoid wrap-around).
diff -ruN innodb_plugin-1.0.6_orig/scripts/install_innodb_plugins.sql innodb_plugin-1.0.6_tmp/scripts/install_innodb_plugins.sql
--- innodb_plugin-1.0.6_orig/scripts/install_innodb_plugins.sql	2009-12-04 11:57:09.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/scripts/install_innodb_plugins.sql	2009-12-04 12:17:33.000000000 +0900
@@ -12,3 +12,5 @@
 INSTALL PLUGIN INNODB_BUFFER_POOL_PAGES_BLOB SONAME 'ha_innodb.so';
 INSTALL PLUGIN INNODB_BUFFER_POOL_PAGES_INDEX SONAME 'ha_innodb.so';
 INSTALL PLUGIN innodb_rseg SONAME 'ha_innodb.so';
+INSTALL PLUGIN innodb_table_stats SONAME 'ha_innodb.so';
+INSTALL PLUGIN innodb_index_stats SONAME 'ha_innodb.so';
diff -ruN innodb_plugin-1.0.6_orig/scripts/install_innodb_plugins_win.sql innodb_plugin-1.0.6_tmp/scripts/install_innodb_plugins_win.sql
--- innodb_plugin-1.0.6_orig/scripts/install_innodb_plugins_win.sql	2009-12-04 11:57:09.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/scripts/install_innodb_plugins_win.sql	2009-12-04 12:18:00.000000000 +0900
@@ -12,3 +12,5 @@
 INSTALL PLUGIN INNODB_BUFFER_POOL_PAGES_BLOB SONAME 'ha_innodb.dll';
 INSTALL PLUGIN INNODB_BUFFER_POOL_PAGES_INDEX SONAME 'ha_innodb.dll';
 INSTALL PLUGIN innodb_rseg SONAME 'ha_innodb.dll';
+INSTALL PLUGIN innodb_table_stats SONAME 'ha_innodb.dll';
+INSTALL PLUGIN innodb_index_stats SONAME 'ha_innodb.dll';
diff -ruN innodb_plugin-1.0.6_orig/srv/srv0srv.c innodb_plugin-1.0.6_tmp/srv/srv0srv.c
--- innodb_plugin-1.0.6_orig/srv/srv0srv.c	2009-12-04 12:15:57.000000000 +0900
+++ innodb_plugin-1.0.6_tmp/srv/srv0srv.c	2009-12-04 12:16:38.000000000 +0900
@@ -372,6 +372,9 @@
 /* When estimating number of different key values in an index, sample
 this many index pages */
 UNIV_INTERN unsigned long long	srv_stats_sample_pages = 8;
+UNIV_INTERN ulint	srv_stats_method = 0;
+UNIV_INTERN ulint	srv_stats_auto_update = 1;
+UNIV_INTERN ulint	srv_stats_update_need_lock = 1;
 
 UNIV_INTERN ibool	srv_use_doublewrite_buf	= TRUE;
 UNIV_INTERN ibool	srv_use_checksums = TRUE;