-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sql_statistics.cc
4107 lines (3444 loc) · 122 KB
/
sql_statistics.cc
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
/* Copyright (C) 2009 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
/**
@file
@brief
functions to update persitent statistical tables and to read from them
@defgroup Query_Optimizer Query Optimizer
@{
*/
#include "mariadb.h"
#include "sql_base.h"
#include "key.h"
#include "sql_statistics.h"
#include "opt_range.h"
#include "uniques.h"
#include "my_atomic.h"
#include "sql_show.h"
#include "sql_partition.h"
/*
The system variable 'use_stat_tables' can take one of the
following values:
"never", "complementary", "preferably".
If the values of the variable 'use_stat_tables' is set to
"never then any statistical data from the persistent statistical tables
is ignored by the optimizer.
If the value of the variable 'use_stat_tables' is set to
"complementary" then a particular statistical characteristic is used
by the optimizer only if the database engine does not provide similar
statistics. For example, 'nulls_ratio' for table columns currently
are not provided by any engine. So optimizer uses this statistical data
from the statistical tables. At the same time it does not use
'avg_frequency' for any index prefix from the statistical tables since
the a similar statistical characteristic 'records_per_key' can be
requested from the database engine.
If the value the variable 'use_stat_tables' is set to
"preferably" the optimizer uses a particular statistical data only if
it can't be found in the statistical data.
If an ANALYZE command is executed then it results in collecting
statistical data for the tables specified by the command and storing
the collected statistics in the persistent statistical tables only
when the value of the variable 'use_stat_tables' is not
equal to "never".
*/
/* Currently there are only 3 persistent statistical tables */
static const uint STATISTICS_TABLES= 3;
/*
The names of the statistical tables in this array must correspond the
definitions of the tables in the file ../scripts/mysql_system_tables.sql
*/
static const LEX_CSTRING stat_table_name[STATISTICS_TABLES]=
{
{ STRING_WITH_LEN("table_stats") },
{ STRING_WITH_LEN("column_stats") },
{ STRING_WITH_LEN("index_stats") }
};
/**
@details
The function builds a list of TABLE_LIST elements for system statistical
tables using array of TABLE_LIST passed as a parameter.
The lock type of each element is set to TL_READ if for_write = FALSE,
otherwise it is set to TL_WRITE.
*/
static
inline void init_table_list_for_stat_tables(TABLE_LIST *tables, bool for_write)
{
uint i;
memset((char *) &tables[0], 0, sizeof(TABLE_LIST) * STATISTICS_TABLES);
for (i= 0; i < STATISTICS_TABLES; i++)
{
tables[i].db= MYSQL_SCHEMA_NAME;
tables[i].table_name= stat_table_name[i];
tables[i].alias= stat_table_name[i];
tables[i].lock_type= for_write ? TL_WRITE : TL_READ;
if (i < STATISTICS_TABLES - 1)
tables[i].next_global= tables[i].next_local=
tables[i].next_name_resolution_table= &tables[i+1];
if (i != 0)
tables[i].prev_global= &tables[i-1].next_global;
}
}
/**
@details
The function builds a TABLE_LIST containing only one element 'tbl' for
the statistical table called 'stat_tab_name'.
The lock type of the element is set to TL_READ if for_write = FALSE,
otherwise it is set to TL_WRITE.
*/
static inline
void init_table_list_for_single_stat_table(TABLE_LIST *tbl,
const LEX_CSTRING *stat_tab_name,
bool for_write)
{
memset((char *) tbl, 0, sizeof(TABLE_LIST));
tbl->db= MYSQL_SCHEMA_NAME;
tbl->table_name= *stat_tab_name;
tbl->alias= *stat_tab_name;
tbl->lock_type= for_write ? TL_WRITE : TL_READ;
}
static Table_check_intact_log_error stat_table_intact;
static const
TABLE_FIELD_TYPE table_stat_fields[TABLE_STAT_N_FIELDS] =
{
{
{ STRING_WITH_LEN("db_name") },
{ STRING_WITH_LEN("varchar(64)") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("table_name") },
{ STRING_WITH_LEN("varchar(64)") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("cardinality") },
{ STRING_WITH_LEN("bigint(21)") },
{ NULL, 0 }
},
};
static const uint table_stat_pk_col[]= {0,1};
static const TABLE_FIELD_DEF
table_stat_def= {TABLE_STAT_N_FIELDS, table_stat_fields, 2, table_stat_pk_col };
static const
TABLE_FIELD_TYPE column_stat_fields[COLUMN_STAT_N_FIELDS] =
{
{
{ STRING_WITH_LEN("db_name") },
{ STRING_WITH_LEN("varchar(64)") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("table_name") },
{ STRING_WITH_LEN("varchar(64)") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("column_name") },
{ STRING_WITH_LEN("varchar(64)") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("min_value") },
{ STRING_WITH_LEN("varbinary(255)") },
{ NULL, 0 }
},
{
{ STRING_WITH_LEN("max_value") },
{ STRING_WITH_LEN("varbinary(255)") },
{ NULL, 0 }
},
{
{ STRING_WITH_LEN("nulls_ratio") },
{ STRING_WITH_LEN("decimal(12,4)") },
{ NULL, 0 }
},
{
{ STRING_WITH_LEN("avg_length") },
{ STRING_WITH_LEN("decimal(12,4)") },
{ NULL, 0 }
},
{
{ STRING_WITH_LEN("avg_frequency") },
{ STRING_WITH_LEN("decimal(12,4)") },
{ NULL, 0 }
},
{
{ STRING_WITH_LEN("hist_size") },
{ STRING_WITH_LEN("tinyint(3)") },
{ NULL, 0 }
},
{
{ STRING_WITH_LEN("hist_type") },
{ STRING_WITH_LEN("enum('SINGLE_PREC_HB','DOUBLE_PREC_HB')") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("histogram") },
{ STRING_WITH_LEN("varbinary(255)") },
{ NULL, 0 }
}
};
static const uint column_stat_pk_col[]= {0,1,2};
static const TABLE_FIELD_DEF
column_stat_def= {COLUMN_STAT_N_FIELDS, column_stat_fields, 3, column_stat_pk_col};
static const
TABLE_FIELD_TYPE index_stat_fields[INDEX_STAT_N_FIELDS] =
{
{
{ STRING_WITH_LEN("db_name") },
{ STRING_WITH_LEN("varchar(64)") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("table_name") },
{ STRING_WITH_LEN("varchar(64)") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("index") },
{ STRING_WITH_LEN("varchar(64)") },
{ STRING_WITH_LEN("utf8") }
},
{
{ STRING_WITH_LEN("prefix_arity") },
{ STRING_WITH_LEN("int(11)") },
{ NULL, 0 }
},
{
{ STRING_WITH_LEN("avg_frequency") },
{ STRING_WITH_LEN("decimal(12,4)") },
{ NULL, 0 }
}
};
static const uint index_stat_pk_col[]= {0,1,2,3};
static const TABLE_FIELD_DEF
index_stat_def= {INDEX_STAT_N_FIELDS, index_stat_fields, 4, index_stat_pk_col};
/**
@brief
Open all statistical tables and lock them
*/
static
inline int open_stat_tables(THD *thd, TABLE_LIST *tables,
Open_tables_backup *backup,
bool for_write)
{
int rc;
Dummy_error_handler deh; // suppress errors
thd->push_internal_handler(&deh);
init_table_list_for_stat_tables(tables, for_write);
init_mdl_requests(tables);
rc= open_system_tables_for_read(thd, tables, backup);
thd->pop_internal_handler();
/* If the number of tables changes, we should revise the check below. */
DBUG_ASSERT(STATISTICS_TABLES == 3);
if (!rc &&
(stat_table_intact.check(tables[TABLE_STAT].table, &table_stat_def) ||
stat_table_intact.check(tables[COLUMN_STAT].table, &column_stat_def) ||
stat_table_intact.check(tables[INDEX_STAT].table, &index_stat_def)))
{
close_system_tables(thd, backup);
rc= 1;
}
return rc;
}
/**
@brief
Open a statistical table and lock it
*/
static
inline int open_single_stat_table(THD *thd, TABLE_LIST *table,
const LEX_CSTRING *stat_tab_name,
Open_tables_backup *backup,
bool for_write)
{
init_table_list_for_single_stat_table(table, stat_tab_name, for_write);
init_mdl_requests(table);
return open_system_tables_for_read(thd, table, backup);
}
/*
The class Column_statistics_collected is a helper class used to collect
statistics on a table column. The class is derived directly from
the class Column_statistics, and, additionally to the fields of the
latter, it contains the fields to accumulate the results of aggregation
for the number of nulls in the column and for the size of the column
values. There is also a container for distinct column values used
to calculate the average number of records per distinct column value.
*/
class Column_statistics_collected :public Column_statistics
{
private:
Field *column; /* The column to collect statistics on */
ha_rows nulls; /* To accumulate the number of nulls in the column */
ulonglong column_total_length; /* To accumulate the size of column values */
Count_distinct_field *count_distinct; /* The container for distinct
column values */
bool is_single_pk_col; /* TRUE <-> the only column of the primary key */
public:
inline void init(THD *thd, Field * table_field);
inline bool add(ha_rows rowno);
inline void finish(ha_rows rows);
inline void cleanup();
};
/**
Stat_table is the base class for classes Table_stat, Column_stat and
Index_stat. The methods of these classes allow us to read statistical
data from statistical tables, write collected statistical data into
statistical tables and update statistical data in these tables
as well as update access fields belonging to the primary key and
delete records by prefixes of the primary key.
Objects of the classes Table_stat, Column_stat and Index stat are used
for reading/writing statistics from/into persistent tables table_stats,
column_stats and index_stats correspondingly. These tables are stored in
the system database 'mysql'.
Statistics is read and written always for a given database table t. When
an object of any of these classes is created a pointer to the TABLE
structure for this database table is passed as a parameter to the constructor
of the object. The other parameter is a pointer to the TABLE structure for
the corresponding statistical table st. So construction of an object to
read/write statistical data on table t from/into statistical table st
requires both table t and st to be opened.
In some cases the TABLE structure for table t may be undefined. Then
the objects of the classes Table_stat, Column_stat and Index stat are
created by the alternative constructor that require only the name
of the table t and the name of the database it belongs to. Currently the
alternative constructors are used only in the cases when some records
belonging to the table are to be deleted, or its keys are to be updated
Reading/writing statistical data from/into a statistical table is always
performed by a key. At the moment there is only one key defined for each
statistical table and this key is primary.
The primary key for the table table_stats is built as (db_name, table_name).
The primary key for the table column_stats is built as (db_name, table_name,
column_name).
The primary key for the table index_stats is built as (db_name, table_name,
index_name, prefix_arity).
Reading statistical data from a statistical table is performed by the
following pattern. First a table dependent method sets the values of the
the fields that comprise the lookup key. Then an implementation of the
method get_stat_values() declared in Stat_table as a pure virtual method
finds the row from the statistical table by the set key. If the row is
found the values of statistical fields are read from this row and are
distributed in the internal structures.
Let's assume the statistical data is read for table t from database db.
When statistical data is searched in the table table_stats first
Table_stat::set_key_fields() should set the fields of db_name and
table_name. Then get_stat_values looks for a row by the set key value,
and, if the row is found, reads the value from the column
table_stats.cardinality into the field read_stat.cardinality of the TABLE
structure for table t and sets the value of read_stat.cardinality_is_null
from this structure to FALSE. If the value of the 'cardinality' column
in the row is null or if no row is found read_stat.cardinality_is_null
is set to TRUE.
When statistical data is searched in the table column_stats first
Column_stat::set_key_fields() should set the fields of db_name, table_name
and column_name with column_name taken out of the only parameter f of the
Field* type passed to this method. After this get_stat_values looks
for a row by the set key value. If the row is found the values of statistical
data columns min_value, max_value, nulls_ratio, avg_length, avg_frequency,
hist_size, hist_type, histogram are read into internal structures. Values
of nulls_ratio, avg_length, avg_frequency, hist_size, hist_type, histogram
are read into the corresponding fields of the read_stat structure from
the Field object f, while values from min_value and max_value are copied
into the min_value and max_value record buffers attached to the TABLE
structure for table t.
If the value of a statistical column in the found row is null, then the
corresponding flag in the f->read_stat.column_stat_nulls bitmap is set off.
Otherwise the flag is set on. If no row is found for the column the all flags
in f->column_stat_nulls are set off.
When statistical data is searched in the table index_stats first
Index_stat::set_key_fields() has to be called to set the fields of db_name,
table_name, index_name and prefix_arity. The value of index_name is extracted
from the first parameter key_info of the KEY* type passed to the method.
This parameter specifies the index of interest idx. The second parameter
passed to the method specifies the arity k of the index prefix for which
statistical data is to be read. E.g. if the index idx consists of 3
components (p1,p2,p3) the table index_stats usually will contain 3 rows for
this index: the first - for the prefix (p1), the second - for the prefix
(p1,p2), and the third - for the the prefix (p1,p2,p3). After the key fields
has been set a call of get_stat_value looks for a row by the set key value.
If the row is found and the value of the avg_frequency column is not null
then this value is assigned to key_info->read_stat.avg_frequency[k].
Otherwise 0 is assigned to this element.
The method Stat_table::update_stat is used to write statistical data
collected in the internal structures into a statistical table st.
It is assumed that before any invocation of this method a call of the
function st.set_key_fields has set the values of the primary key fields
that serve to locate the row from the statistical table st where the
the collected statistical data from internal structures are to be written
to. The statistical data is written from the counterparts of the
statistical fields of internal structures into which it would be read
by the functions get_stat_values. The counterpart fields are used
only when statistics is collected
When updating/inserting a row from the statistical table st the method
Stat_table::update_stat calls the implementation of the pure virtual
method store_field_values to transfer statistical data from the fields
of internal structures to the fields of record buffer used for updates
of the statistical table st.
*/
class Stat_table
{
private:
/* Handler used for the retrieval of the statistical table stat_table */
handler *stat_file;
uint stat_key_length; /* Length of the key to access stat_table */
uchar *record[2]; /* Record buffers used to access/update stat_table */
uint stat_key_idx; /* The number of the key to access stat_table */
/* This is a helper function used only by the Stat_table constructors */
void common_init_stat_table()
{
stat_file= stat_table->file;
/* Currently any statistical table has only one key */
stat_key_idx= 0;
stat_key_info= &stat_table->key_info[stat_key_idx];
stat_key_length= stat_key_info->key_length;
record[0]= stat_table->record[0];
record[1]= stat_table->record[1];
}
protected:
/* Statistical table to read statistics from or to update/delete */
TABLE *stat_table;
KEY *stat_key_info; /* Structure for the index to access stat_table */
/* Table for which statistical data is read / updated */
TABLE *table;
TABLE_SHARE *table_share; /* Table share for 'table */
const LEX_CSTRING *db_name; /* Name of the database containing 'table' */
const LEX_CSTRING *table_name; /* Name of the table 'table' */
void store_record_for_update()
{
store_record(stat_table, record[1]);
}
void store_record_for_lookup()
{
DBUG_ASSERT(record[0] == stat_table->record[0]);
}
bool update_record()
{
int err;
if ((err= stat_file->ha_update_row(record[1], record[0])) &&
err != HA_ERR_RECORD_IS_THE_SAME)
return TRUE;
/* Make change permanent and avoid 'table is marked as crashed' errors */
stat_file->extra(HA_EXTRA_FLUSH);
return FALSE;
}
public:
/**
@details
This constructor has to be called by any constructor of the derived
classes. The constructor 'tunes' the private and protected members of
the constructed object to the statistical table 'stat_table' with the
statistical data of our interest and to the table 'tab' for which this
statistics has been collected.
*/
Stat_table(TABLE *stat, TABLE *tab)
:stat_table(stat), table(tab)
{
table_share= tab->s;
common_init_stat_table();
db_name= &table_share->db;
table_name= &table_share->table_name;
}
/**
@details
This constructor has to be called by any constructor of the derived
classes. The constructor 'tunes' the private and protected members of
the constructed object to the statistical table 'stat_table' with the
statistical data of our interest and to the table t for which this
statistics has been collected. The table t is uniquely specified
by the database name 'db' and the table name 'tab'.
*/
Stat_table(TABLE *stat, const LEX_CSTRING *db, const LEX_CSTRING *tab)
:stat_table(stat), table_share(NULL),db_name(db), table_name(tab)
{
common_init_stat_table();
}
virtual ~Stat_table() {}
/**
@brief
Store the given values of fields for database name and table name
@details
This is a purely virtual method.
The implementation for any derived class shall store the given
values of the database name and table name in the corresponding
fields of stat_table.
@note
The method is called by the update_table_name_key_parts function.
*/
virtual void change_full_table_name(const LEX_CSTRING *db, const LEX_CSTRING *tab)= 0;
/**
@brief
Store statistical data into fields of the statistical table
@details
This is a purely virtual method.
The implementation for any derived class shall put the appropriate
statistical data into the corresponding fields of stat_table.
@note
The method is called by the update_stat function.
*/
virtual void store_stat_fields()= 0;
/**
@brief
Read statistical data from fields of the statistical table
@details
This is a purely virtual method.
The implementation for any derived read shall read the appropriate
statistical data from the corresponding fields of stat_table.
*/
virtual void get_stat_values()= 0;
/**
@brief
Find a record in the statistical table by a primary key
@details
The function looks for a record in stat_table by its primary key.
It assumes that the key fields have been already stored in the record
buffer of stat_table.
@retval
FALSE the record is not found
@retval
TRUE the record is found
*/
bool find_stat()
{
uchar key[MAX_KEY_LENGTH];
key_copy(key, record[0], stat_key_info, stat_key_length);
return !stat_file->ha_index_read_idx_map(record[0], stat_key_idx, key,
HA_WHOLE_KEY, HA_READ_KEY_EXACT);
}
/**
@brief
Find a record in the statistical table by a key prefix value
@details
The function looks for a record in stat_table by the key value consisting
of 'prefix_parts' major components for the primary index.
It assumes that the key prefix fields have been already stored in the record
buffer of stat_table.
@retval
FALSE the record is not found
@retval
TRUE the record is found
*/
bool find_next_stat_for_prefix(uint prefix_parts)
{
uchar key[MAX_KEY_LENGTH];
uint prefix_key_length= 0;
for (uint i= 0; i < prefix_parts; i++)
prefix_key_length+= stat_key_info->key_part[i].store_length;
key_copy(key, record[0], stat_key_info, prefix_key_length);
key_part_map prefix_map= (key_part_map) ((1 << prefix_parts) - 1);
return !stat_file->ha_index_read_idx_map(record[0], stat_key_idx, key,
prefix_map, HA_READ_KEY_EXACT);
}
/**
@brief
Update/insert a record in the statistical table with new statistics
@details
The function first looks for a record by its primary key in the statistical
table stat_table. If the record is found the function updates statistical
fields of the records. The data for these fields are taken from internal
structures containing info on the table 'table'. If the record is not
found the function inserts a new record with the primary key set to the
search key and the statistical data taken from the internal structures.
The function assumes that the key fields have been already stored in
the record buffer of stat_table.
@retval
FALSE success with the update/insert of the record
@retval
TRUE failure with the update/insert of the record
@note
The function calls the virtual method store_stat_fields to populate the
statistical fields of the updated/inserted row with new statistics.
*/
bool update_stat()
{
if (find_stat())
{
bool res;
store_record_for_update();
store_stat_fields();
res= update_record();
DBUG_ASSERT(res == 0);
return res;
}
else
{
int err;
store_stat_fields();
if ((err= stat_file->ha_write_row(record[0])))
{
DBUG_ASSERT(0);
return TRUE;
}
/* Make change permanent and avoid 'table is marked as crashed' errors */
stat_file->extra(HA_EXTRA_FLUSH);
}
return FALSE;
}
/**
@brief
Update the table name fields in the current record of stat_table
@details
The function updates the fields containing database name and table name
for the last found record in the statistical table stat_table.
The corresponding names for update is taken from the parameters
db and tab.
@retval
FALSE success with the update of the record
@retval
TRUE failure with the update of the record
@note
The function calls the virtual method change_full_table_name
to store the new names in the record buffer used for updates.
*/
bool update_table_name_key_parts(const LEX_CSTRING *db, const LEX_CSTRING *tab)
{
store_record_for_update();
change_full_table_name(db, tab);
bool rc= update_record();
store_record_for_lookup();
return rc;
}
/**
@brief
Delete the current record of the statistical table stat_table
@details
The function deletes the last found record from the statistical
table stat_table.
@retval
FALSE success with the deletion of the record
@retval
TRUE failure with the deletion of the record
*/
bool delete_stat()
{
int err;
if ((err= stat_file->ha_delete_row(record[0])))
return TRUE;
/* Make change permanent and avoid 'table is marked as crashed' errors */
stat_file->extra(HA_EXTRA_FLUSH);
return FALSE;
}
friend class Stat_table_write_iter;
};
/*
An object of the class Table_stat is created to read statistical
data on tables from the statistical table table_stats, to update
table_stats with such statistical data, or to update columns
of the primary key, or to delete the record by its primary key or
its prefix.
Rows from the statistical table are read and updated always by
primary key.
*/
class Table_stat: public Stat_table
{
private:
Field *db_name_field; /* Field for the column table_stats.db_name */
Field *table_name_field; /* Field for the column table_stats.table_name */
void common_init_table_stat()
{
db_name_field= stat_table->field[TABLE_STAT_DB_NAME];
table_name_field= stat_table->field[TABLE_STAT_TABLE_NAME];
}
void change_full_table_name(const LEX_CSTRING *db, const LEX_CSTRING *tab)
{
db_name_field->store(db->str, db->length, system_charset_info);
table_name_field->store(tab->str, tab->length, system_charset_info);
}
public:
/**
@details
The constructor 'tunes' the private and protected members of the
constructed object for the statistical table table_stats to read/update
statistics on table 'tab'. The TABLE structure for the table table_stat
must be passed as a value for the parameter 'stat'.
*/
Table_stat(TABLE *stat, TABLE *tab) :Stat_table(stat, tab)
{
common_init_table_stat();
}
/**
@details
The constructor 'tunes' the private and protected members of the
object constructed for the statistical table table_stat for
the future updates/deletes of the record concerning the table 'tab'
from the database 'db'.
*/
Table_stat(TABLE *stat, const LEX_CSTRING *db, const LEX_CSTRING *tab)
:Stat_table(stat, db, tab)
{
common_init_table_stat();
}
/**
@brief
Set the key fields for the statistical table table_stat
@details
The function sets the values of the fields db_name and table_name
in the record buffer for the statistical table table_stat.
These fields comprise the primary key for the table.
@note
The function is supposed to be called before any use of the
method find_stat for an object of the Table_stat class.
*/
void set_key_fields()
{
db_name_field->store(db_name->str, db_name->length, system_charset_info);
table_name_field->store(table_name->str, table_name->length,
system_charset_info);
}
/**
@brief
Store statistical data into statistical fields of table_stat
@details
This implementation of a purely virtual method sets the value of the
column 'cardinality' of the statistical table table_stat according to
the value of the flag write_stat.cardinality_is_null and the value of
the field write_stat.cardinality' from the TABLE structure for 'table'.
*/
void store_stat_fields()
{
Field *stat_field= stat_table->field[TABLE_STAT_CARDINALITY];
if (table->collected_stats->cardinality_is_null)
stat_field->set_null();
else
{
stat_field->set_notnull();
stat_field->store(table->collected_stats->cardinality,true);
}
}
/**
@brief
Read statistical data from statistical fields of table_stat
@details
This implementation of a purely virtual method first looks for a record
the statistical table table_stat by its primary key set the record
buffer with the help of Table_stat::set_key_fields. Then, if the row is
found the function reads the value of the column 'cardinality' of the table
table_stat and sets the value of the flag read_stat.cardinality_is_null
and the value of the field read_stat.cardinality' from the TABLE structure
for 'table' accordingly.
*/
void get_stat_values()
{
Table_statistics *read_stats= table_share->stats_cb.table_stats;
read_stats->cardinality_is_null= TRUE;
read_stats->cardinality= 0;
if (find_stat())
{
Field *stat_field= stat_table->field[TABLE_STAT_CARDINALITY];
if (!stat_field->is_null())
{
read_stats->cardinality_is_null= FALSE;
read_stats->cardinality= stat_field->val_int();
}
}
}
};
/*
An object of the class Column_stat is created to read statistical data
on table columns from the statistical table column_stats, to update
column_stats with such statistical data, or to update columns
of the primary key, or to delete the record by its primary key or
its prefix.
Rows from the statistical table are read and updated always by
primary key.
*/
class Column_stat: public Stat_table
{
private:
Field *db_name_field; /* Field for the column column_stats.db_name */
Field *table_name_field; /* Field for the column column_stats.table_name */
Field *column_name_field; /* Field for the column column_stats.column_name */
Field *table_field; /* Field from 'table' to read /update statistics on */
void common_init_column_stat_table()
{
db_name_field= stat_table->field[COLUMN_STAT_DB_NAME];
table_name_field= stat_table->field[COLUMN_STAT_TABLE_NAME];
column_name_field= stat_table->field[COLUMN_STAT_COLUMN_NAME];
}
void change_full_table_name(const LEX_CSTRING *db, const LEX_CSTRING *tab)
{
db_name_field->store(db->str, db->length, system_charset_info);
table_name_field->store(tab->str, tab->length, system_charset_info);
}
public:
/**
@details
The constructor 'tunes' the private and protected members of the
constructed object for the statistical table column_stats to read/update
statistics on fields of the table 'tab'. The TABLE structure for the table
column_stats must be passed as a value for the parameter 'stat'.
*/
Column_stat(TABLE *stat, TABLE *tab) :Stat_table(stat, tab)
{
common_init_column_stat_table();
}
/**
@details
The constructor 'tunes' the private and protected members of the
object constructed for the statistical table column_stats for
the future updates/deletes of the record concerning the table 'tab'
from the database 'db'.
*/
Column_stat(TABLE *stat, const LEX_CSTRING *db, const LEX_CSTRING *tab)
:Stat_table(stat, db, tab)
{
common_init_column_stat_table();
}
/**
@brief
Set table name fields for the statistical table column_stats
@details
The function stores the values of the fields db_name and table_name
of the statistical table column_stats in the record buffer.
*/
void set_full_table_name()
{
db_name_field->store(db_name->str, db_name->length, system_charset_info);
table_name_field->store(table_name->str, table_name->length,
system_charset_info);
}
/**
@brief
Set the key fields for the statistical table column_stats
@param
col Field for the 'table' column to read/update statistics on
@details
The function stores the values of the fields db_name, table_name and
column_name in the record buffer for the statistical table column_stats.
These fields comprise the primary key for the table.
It also sets table_field to the passed parameter.
@note
The function is supposed to be called before any use of the
method find_stat for an object of the Column_stat class.
*/
void set_key_fields(Field *col)
{
set_full_table_name();
column_name_field->store(col->field_name.str, col->field_name.length,
system_charset_info);
table_field= col;
}
/**
@brief
Update the table name fields in the current record of stat_table
@details
The function updates the primary key fields containing database name,
table name, and column name for the last found record in the statistical
table column_stats.