The main patch for the MWL#248 back-ported from

lp:~igorb-seattle/mysql-server/mysql-azalea-wl4777.
This commit is contained in:
Igor Babaev 2012-01-04 17:51:53 -08:00
parent cd55894a52
commit e8497370b9
34 changed files with 2858 additions and 18 deletions

View file

@ -75,6 +75,7 @@ sqlsources = derror.cc field.cc field_conv.cc strfunc.cc filesort.cc \
sql_update.cc sql_yacc.cc table.cc thr_malloc.cc time.cc \
unireg.cc uniques.cc sql_union.cc hash_filo.cc \
spatial.cc gstream.cc sql_help.cc tztime.cc sql_cursor.cc \
sql_statistics.cc \
sp_head.cc sp_pcontext.cc sp.cc sp_cache.cc sp_rcontext.cc \
parse_file.cc sql_view.cc sql_trigger.cc my_decimal.cc \
rpl_filter.cc sql_partition.cc sql_builtin.cc sql_plugin.cc \

View file

@ -16,3 +16,6 @@ show create table proc;
show create table event;
show create table general_log;
show create table slow_log;
show create table table_stat;
show create table column_stat;
show create table index_stat;

View file

@ -0,0 +1,25 @@
CREATE TABLE Country (
Code char(3) NOT NULL default '',
Name char(52) NOT NULL default '',
SurfaceArea float(10,2) NOT NULL default '0.00',
Population int(11) NOT NULL default '0',
Capital int(11) default NULL,
PRIMARY KEY (Code),
UNIQUE INDEX (Name)
) CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE City (
ID int(11) NOT NULL auto_increment,
Name char(35) NOT NULL default '',
Country char(3) NOT NULL default '',
Population int(11) NOT NULL default '0',
PRIMARY KEY (ID),
INDEX (Population),
INDEX (Country)
) CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE CountryLanguage (
Country char(3) NOT NULL default '',
Language char(30) NOT NULL default '',
Percentage float(3,1) NOT NULL default '0.0',
PRIMARY KEY (Country, Language),
INDEX (Percentage)
) CHARACTER SET utf8 COLLATE utf8_bin;

View file

@ -6,6 +6,7 @@ mysql
test
show tables in mysql;
Tables_in_mysql
column_stat
columns_priv
db
event
@ -16,12 +17,14 @@ help_keyword
help_relation
help_topic
host
index_stat
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
table_stat
tables_priv
time_zone
time_zone_leap_second

View file

@ -1,6 +1,7 @@
drop table if exists t1,t2;
show tables;
Tables_in_mysql
column_stat
columns_priv
db
event
@ -11,12 +12,14 @@ help_keyword
help_relation
help_topic
host
index_stat
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
table_stat
tables_priv
time_zone
time_zone_leap_second
@ -34,6 +37,7 @@ grant ALL on *.* to test@localhost identified by "gambling";
grant ALL on *.* to test@127.0.0.1 identified by "gambling";
show tables;
Tables_in_mysql
column_stat
columns_priv
db
event
@ -44,12 +48,14 @@ help_keyword
help_relation
help_topic
host
index_stat
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
table_stat
tables_priv
time_zone
time_zone_leap_second
@ -75,6 +81,7 @@ ERROR HY000: Password hash should be a 41-digit hexadecimal number
set password=old_password('gambling3');
show tables;
Tables_in_mysql
column_stat
columns_priv
db
event
@ -85,12 +92,14 @@ help_keyword
help_relation
help_topic
host
index_stat
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
table_stat
tables_priv
time_zone
time_zone_leap_second

View file

@ -95,6 +95,7 @@ USER_STATISTICS
VIEWS
XTRADB_ADMIN_COMMAND
XTRADB_ENHANCEMENTS
column_stat
columns_priv
db
event
@ -105,6 +106,7 @@ help_keyword
help_relation
help_topic
host
index_stat
plugin
proc
procs_priv
@ -115,6 +117,7 @@ t2
t3
t4
t5
table_stat
tables_priv
time_zone
time_zone_leap_second
@ -132,6 +135,7 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS
TABLE_PRIVILEGES TABLE_PRIVILEGES
TABLE_STATISTICS TABLE_STATISTICS
TRIGGERS TRIGGERS
table_stat table_stat
tables_priv tables_priv
time_zone time_zone
time_zone_leap_second time_zone_leap_second
@ -152,6 +156,7 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS
TABLE_PRIVILEGES TABLE_PRIVILEGES
TABLE_STATISTICS TABLE_STATISTICS
TRIGGERS TRIGGERS
table_stat table_stat
tables_priv tables_priv
time_zone time_zone
time_zone_leap_second time_zone_leap_second
@ -172,6 +177,7 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS
TABLE_PRIVILEGES TABLE_PRIVILEGES
TABLE_STATISTICS TABLE_STATISTICS
TRIGGERS TRIGGERS
table_stat table_stat
tables_priv tables_priv
time_zone time_zone
time_zone_leap_second time_zone_leap_second

View file

@ -382,4 +382,4 @@ Wildcard: inf_rmation_schema
SELECT table_schema, count(*) FROM information_schema.TABLES WHERE table_schema IN ('mysql', 'INFORMATION_SCHEMA', 'test', 'mysqltest') AND table_name<>'ndb_binlog_index' AND table_name<>'ndb_apply_status' GROUP BY TABLE_SCHEMA;
table_schema count(*)
information_schema 52
mysql 22
mysql 25

View file

@ -19,6 +19,7 @@ mtr
mtr.global_suppressions OK
mtr.test_suppressions OK
mysql
mysql.column_stat OK
mysql.columns_priv OK
mysql.db OK
mysql.event OK
@ -28,12 +29,14 @@ mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.host OK
mysql.index_stat OK
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.renamed_general_log OK
mysql.servers OK
mysql.table_stat OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK

View file

@ -3,6 +3,7 @@ drop view if exists v1;
drop database if exists client_test_db;
mtr.global_suppressions OK
mtr.test_suppressions OK
mysql.column_stat OK
mysql.columns_priv OK
mysql.db OK
mysql.event OK
@ -12,11 +13,13 @@ mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.host OK
mysql.index_stat OK
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.servers OK
mysql.table_stat OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK
@ -24,6 +27,7 @@ mysql.time_zone_name OK
mysql.time_zone_transition OK
mysql.time_zone_transition_type OK
mysql.user OK
mysql.column_stat OK
mysql.columns_priv OK
mysql.db OK
mysql.event OK
@ -33,11 +37,13 @@ mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.host OK
mysql.index_stat OK
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.servers OK
mysql.table_stat OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK

View file

@ -0,0 +1,455 @@
drop table if exists t1,t2;
CREATE VIEW table_stat AS
SELECT * FROM mysql.table_stat;
CREATE VIEW column_stat AS
SELECT db_name, table_name, column_name,
min_value, max_value,
CAST(nulls_ratio AS decimal(12,4)) AS 'nulls_ratio',
CAST(avg_length AS decimal(12,4)) AS 'avg_length',
CAST(avg_frequency AS decimal(12,4)) AS 'avg_frequency'
FROM mysql.column_stat;
CREATE VIEW index_stat AS
SELECT db_name, table_name, index_name, prefix_arity,
CAST(avg_frequency AS decimal(12,4)) AS 'avg_frequency'
FROM mysql.index_stat;
DELETE FROM mysql.table_stat;
DELETE FROM mysql.column_stat;
DELETE FROM mysql.index_stat;
set optimizer_use_stat_tables='preferably';
CREATE TABLE t1 (
a int NOT NULL PRIMARY KEY,
b varchar(32),
c char(16),
d date,
e double,
f bit(3),
INDEX idx1 (b, e),
INDEX idx2(c, d),
INDEX idx3 (d),
INDEX idx4 (e, b, d)
);
INSERT INTO t1 VALUES
(0, NULL, NULL, NULL, NULL, NULL),
(7, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'dddddddd', '1990-05-15', 0.1, b'100'),
(17, 'vvvvvvvvvvvvv', 'aaaa', '1989-03-12', 0.01, b'101'),
(1, 'vvvvvvvvvvvvv', NULL, '1989-03-12', 0.01, b'100'),
(12, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1999-07-23', 0.112, b'001'),
(23, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.1, b'100'),
(8, 'vvvvvvvvvvvvv', 'aaaa', '1999-07-23', 0.1, b'100'),
(22, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'aaaa', '1989-03-12', 0.112, b'001'),
(31, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'aaaa', '1999-07-23', 0.01, b'001'),
(10, NULL, 'aaaa', NULL, 0.01, b'010'),
(5, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1999-07-23', 0.1, b'100'),
(15, 'vvvvvvvvvvvvv', 'ccccccccc', '1990-05-15', 0.1, b'010'),
(30, NULL, 'bbbbbb', NULL, NULL, b'100'),
(38, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', NULL, NULL, NULL),
(18, 'zzzzzzzzzzzzzzzzzz', 'ccccccccc', '1990-05-15', 0.01, b'010'),
(9, 'yyy', 'bbbbbb', '1998-08-28', 0.01, NULL),
(29, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.012, b'010'),
(3, 'yyy', 'dddddddd', '1990-05-15', 0.112, b'010'),
(39, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', NULL, 0.01, b'100'),
(14, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'ccccccccc', '1990-05-15', 0.1, b'100'),
(40, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', '1989-03-12', NULL, NULL),
(44, NULL, 'aaaa', '1989-03-12', NULL, b'010'),
(19, 'vvvvvvvvvvvvv', 'ccccccccc', '1990-05-15', 0.012, b'011'),
(21, 'zzzzzzzzzzzzzzzzzz', 'dddddddd', '1989-03-12', 0.112, b'100'),
(45, NULL, NULL, '1989-03-12', NULL, b'011'),
(2, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'ccccccccc', '1990-05-15', 0.1, b'001'),
(35, 'yyy', 'aaaa', '1990-05-15', 0.05, b'011'),
(4, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.01, b'101'),
(47, NULL, 'aaaa', '1990-05-15', 0.05, b'010'),
(42, NULL, 'ccccccccc', '1989-03-12', 0.01, b'010'),
(32, NULL, 'bbbbbb', '1990-05-15', 0.01, b'011'),
(49, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww' , 'aaaa', '1990-05-15', NULL, NULL),
(43, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww' , 'bbbbbb', '1990-05-15', NULL, b'100'),
(37, 'yyy', NULL, '1989-03-12', 0.05, b'011'),
(41, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'ccccccccc', '1990-05-15', 0.05, NULL),
(34, 'yyy', NULL, NULL, NULL, NULL),
(33, 'zzzzzzzzzzzzzzzzzz', 'dddddddd', '1989-03-12', 0.05, b'011'),
(24, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1990-05-15', 0.01, b'101'),
(11, 'yyy', 'ccccccccc', '1999-07-23', 0.1, NULL),
(25, 'zzzzzzzzzzzzzzzzzz', 'bbb', '1989-03-12', 0.01, b'101');
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
SELECT * FROM table_stat;
db_name table_name cardinality
test t1 40
SELECT * FROM column_stat;
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
test t1 a 0 49 0.0000 4.0000 1.0000
test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000
test t1 c aaaa dddddddd 0.1250 6.6571 7.0000
test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000
test t1 e 0.01 0.112 0.2250 8.0000 6.2000
test t1 f 1 5 0.2000 1.0000 6.4000
SELECT * FROM index_stat;
db_name table_name index_name prefix_arity avg_frequency
test t1 PRIMARY 1 1.0000
test t1 idx1 1 6.4000
test t1 idx1 2 1.6875
test t1 idx2 1 7.0000
test t1 idx2 2 2.3846
test t1 idx3 1 8.5000
test t1 idx4 1 6.2000
test t1 idx4 2 1.6875
test t1 idx4 3 1.1304
SELECT COUNT(*) FROM t1;
COUNT(*)
40
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='a';
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
test t1 a 0 49 0.0000 4.0000 1.0000
SELECT MIN(t1.a), MAX(t1.a),
(SELECT COUNT(*) FROM t1 WHERE t1.b IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.a)",
(SELECT COUNT(t1.a) FROM t1) /
(SELECT COUNT(DISTINCT t1.a) FROM t1) AS "AVG_FREQUENCY(t1.a)"
FROM t1;
MIN(t1.a) MAX(t1.a) NULLS_RATIO(t1.a) AVG_FREQUENCY(t1.a)
0 49 0.2000 1.0000
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='b';
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000
SELECT MIN(t1.b), MAX(t1.b),
(SELECT COUNT(*) FROM t1 WHERE t1.b IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.b)",
(SELECT COUNT(t1.b) FROM t1) /
(SELECT COUNT(DISTINCT t1.b) FROM t1) AS "AVG_FREQUENCY(t1.b)"
FROM t1;
MIN(t1.b) MAX(t1.b) NULLS_RATIO(t1.b) AVG_FREQUENCY(t1.b)
vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 6.4000
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='c';
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
test t1 c aaaa dddddddd 0.1250 6.6571 7.0000
SELECT MIN(t1.c), MAX(t1.c),
(SELECT COUNT(*) FROM t1 WHERE t1.c IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.c)",
(SELECT COUNT(t1.c) FROM t1) /
(SELECT COUNT(DISTINCT t1.c) FROM t1) AS "AVG_FREQUENCY(t1.c)"
FROM t1;
MIN(t1.c) MAX(t1.c) NULLS_RATIO(t1.c) AVG_FREQUENCY(t1.c)
aaaa dddddddd 0.1250 7.0000
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='d';
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000
SELECT MIN(t1.d), MAX(t1.d),
(SELECT COUNT(*) FROM t1 WHERE t1.d IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.d)",
(SELECT COUNT(t1.d) FROM t1) /
(SELECT COUNT(DISTINCT t1.d) FROM t1) AS "AVG_FREQUENCY(t1.d)"
FROM t1;
MIN(t1.d) MAX(t1.d) NULLS_RATIO(t1.d) AVG_FREQUENCY(t1.d)
1989-03-12 1999-07-23 0.1500 8.5000
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='e';
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
test t1 e 0.01 0.112 0.2250 8.0000 6.2000
SELECT MIN(t1.e), MAX(t1.e),
(SELECT COUNT(*) FROM t1 WHERE t1.e IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.e)",
(SELECT COUNT(t1.e) FROM t1) /
(SELECT COUNT(DISTINCT t1.e) FROM t1) AS "AVG_FREQUENCY(t1.e)"
FROM t1;
MIN(t1.e) MAX(t1.e) NULLS_RATIO(t1.e) AVG_FREQUENCY(t1.e)
0.01 0.112 0.2250 6.2000
SELECT * FROM index_stat
WHERE db_name='test' AND table_name='t1' AND index_name='idx1';
db_name table_name index_name prefix_arity avg_frequency
test t1 idx1 1 6.4000
test t1 idx1 2 1.6875
SELECT
(SELECT COUNT(*) FROM t1 WHERE t1.b IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.b) FROM t1 WHERE t1.b IS NOT NULL)
AS 'ARITY 1',
(SELECT COUNT(*) FROM t1 WHERE t1.b IS NOT NULL AND t1.e IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.b, t1.e) FROM t1
WHERE t1.b IS NOT NULL AND t1.e IS NOT NULL)
AS 'ARITY 2';
ARITY 1 ARITY 2
6.4000 1.6875
SELECT * FROM index_stat
WHERE db_name='test' AND table_name='t1' AND index_name='idx2';
db_name table_name index_name prefix_arity avg_frequency
test t1 idx2 1 7.0000
test t1 idx2 2 2.3846
SELECT
(SELECT COUNT(*) FROM t1 WHERE t1.c IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.c) FROM t1 WHERE t1.c IS NOT NULL)
AS 'ARITY 1',
(SELECT COUNT(*) FROM t1 WHERE t1.c IS NOT NULL AND t1.d IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.c, t1.d) FROM t1
WHERE t1.c IS NOT NULL AND t1.d IS NOT NULL)
AS 'ARITY 2';
ARITY 1 ARITY 2
7.0000 2.3846
SELECT * FROM index_stat
WHERE db_name='test' AND table_name='t1' AND index_name='idx3';
db_name table_name index_name prefix_arity avg_frequency
test t1 idx3 1 8.5000
SELECT
(SELECT COUNT(*) FROM t1 WHERE t1.d IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.d) FROM t1 WHERE t1.d IS NOT NULL)
AS 'ARITY 1';
ARITY 1
8.5000
SELECT * FROM index_stat
WHERE db_name='test' AND table_name='t1' AND index_name='idx4';
db_name table_name index_name prefix_arity avg_frequency
test t1 idx4 1 6.2000
test t1 idx4 2 1.6875
test t1 idx4 3 1.1304
SELECT
(SELECT COUNT(*) FROM t1 WHERE t1.e IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.e) FROM t1 WHERE t1.e IS NOT NULL)
AS 'ARITY 1',
(SELECT COUNT(*) FROM t1 WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.e, t1.b) FROM t1
WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL)
AS 'ARITY 2',
(SELECT COUNT(*) FROM t1
WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL AND t1.d IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.e, t1.b, t1.d) FROM t1
WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL AND t1.d IS NOT NULL)
AS 'ARITY 3';
ARITY 1 ARITY 2 ARITY 3
6.2000 1.6875 1.1304
CREATE TABLE t2 LIKE t1;
ALTER TABLE t2 ENGINE=InnoDB;
INSERT INTO t2 SELECT * FROM t1;
ANALYZE TABLE t2;
Table Op Msg_type Msg_text
test.t2 analyze status OK
SELECT * FROM table_stat;
db_name table_name cardinality
test t1 40
test t2 40
SELECT * FROM column_stat ORDER BY column_name;
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
test t1 a 0 49 0.0000 4.0000 1.0000
test t2 a 0 49 0.0000 4.0000 1.0000
test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000
test t2 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000
test t1 c aaaa dddddddd 0.1250 6.6571 7.0000
test t2 c aaaa dddddddd 0.1250 6.6571 7.0000
test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000
test t2 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000
test t1 e 0.01 0.112 0.2250 8.0000 6.2000
test t2 e 0.01 0.112 0.2250 8.0000 6.2000
test t1 f 1 5 0.2000 1.0000 6.4000
test t2 f 1 5 0.2000 1.0000 6.4000
SELECT * FROM index_stat ORDER BY index_name, prefix_arity, table_name;
db_name table_name index_name prefix_arity avg_frequency
test t1 PRIMARY 1 1.0000
test t2 PRIMARY 1 1.0000
test t1 idx1 1 6.4000
test t2 idx1 1 6.4000
test t1 idx1 2 1.6875
test t2 idx1 2 1.6875
test t1 idx2 1 7.0000
test t2 idx2 1 7.0000
test t1 idx2 2 2.3846
test t2 idx2 2 2.3846
test t1 idx3 1 8.5000
test t2 idx3 1 8.5000
test t1 idx4 1 6.2000
test t2 idx4 1 6.2000
test t1 idx4 2 1.6875
test t2 idx4 2 1.6875
test t1 idx4 3 1.1304
test t2 idx4 3 1.1304
DELETE FROM mysql.table_stat;
DELETE FROM mysql.column_stat;
DELETE FROM mysql.index_stat;
ALTER TABLE t1
DROP INDEX idx1,
DROP INDEX idx4;
ALTER TABLE t1
MODIFY COLUMN b text,
ADD INDEX idx1 (b(4), e),
ADD INDEX idx4 (e, b(4), d);
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
SELECT * FROM column_stat;
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
test t1 a 0 49 0.0000 4.0000 1.0000
test t1 b NULL NULL 0.2000 17.1250 NULL
test t1 c aaaa dddddddd 0.1250 6.6571 7.0000
test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000
test t1 e 0.01 0.112 0.2250 8.0000 6.2000
test t1 f 1 5 0.2000 1.0000 6.4000
SELECT * FROM index_stat;
db_name table_name index_name prefix_arity avg_frequency
test t1 PRIMARY 1 1.0000
test t1 idx2 1 7.0000
test t1 idx2 2 2.3846
test t1 idx3 1 8.5000
test t1 idx1 1 NULL
test t1 idx1 2 NULL
test t1 idx4 1 6.2000
test t1 idx4 2 NULL
test t1 idx4 3 NULL
DROP TABLE t1,t2;
DELETE FROM mysql.table_stat;
DELETE FROM mysql.column_stat;
DELETE FROM mysql.index_stat;
set optimizer_use_stat_tables='never';
set names utf8;
CREATE DATABASE world;
use world;
CREATE TABLE Country (
Code char(3) NOT NULL default '',
Name char(52) NOT NULL default '',
SurfaceArea float(10,2) NOT NULL default '0.00',
Population int(11) NOT NULL default '0',
Capital int(11) default NULL,
PRIMARY KEY (Code),
UNIQUE INDEX (Name)
) CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE City (
ID int(11) NOT NULL auto_increment,
Name char(35) NOT NULL default '',
Country char(3) NOT NULL default '',
Population int(11) NOT NULL default '0',
PRIMARY KEY (ID),
INDEX (Population),
INDEX (Country)
) CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE CountryLanguage (
Country char(3) NOT NULL default '',
Language char(30) NOT NULL default '',
Percentage float(3,1) NOT NULL default '0.0',
PRIMARY KEY (Country, Language),
INDEX (Percentage)
) CHARACTER SET utf8 COLLATE utf8_bin;
set optimizer_use_stat_tables='preferably';
ANALYZE TABLE Country, City, CountryLanguage;
SELECT * FROM test.table_stat;
db_name table_name cardinality
world Country 239
world City 4079
world CountryLanguage 984
SELECT * FROM test.column_stat;
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
world Country Code ABW ZWE 0.0000 3.0000 1.0000
world Country Name Afghanistan Zimbabwe 0.0000 10.1088 1.0000
world Country SurfaceArea 0.40 17075400.00 0.0000 4.0000 1.0042
world Country Population 0 1277558000 0.0000 4.0000 1.0575
world Country Capital 1 4074 0.0293 4.0000 1.0000
world City ID 1 4079 0.0000 4.0000 1.0000
world City Name A Coruña (La Coruña) Ürgenc 0.0000 8.6416 1.0195
world City Country ABW ZWE 0.0000 3.0000 17.5819
world City Population 42 10500000 0.0000 4.0000 1.0467
world CountryLanguage Country ABW ZWE 0.0000 3.0000 4.2232
world CountryLanguage Language Abhyasi [South]Mande 0.0000 7.1778 2.1532
world CountryLanguage Percentage 0.0 99.9 0.0000 4.0000 2.7640
SELECT * FROM test.index_stat;
db_name table_name index_name prefix_arity avg_frequency
world Country PRIMARY 1 1.0000
world Country Name 1 1.0000
world City PRIMARY 1 1.0000
world City Population 1 1.0467
world City Country 1 17.5819
world CountryLanguage PRIMARY 1 4.2232
world CountryLanguage PRIMARY 2 1.0000
world CountryLanguage Percentage 1 2.7640
use test;
set optimizer_use_stat_tables='never';
CREATE DATABASE world_innodb;
use world_innodb;
CREATE TABLE Country (
Code char(3) NOT NULL default '',
Name char(52) NOT NULL default '',
SurfaceArea float(10,2) NOT NULL default '0.00',
Population int(11) NOT NULL default '0',
Capital int(11) default NULL,
PRIMARY KEY (Code),
UNIQUE INDEX (Name)
) CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE City (
ID int(11) NOT NULL auto_increment,
Name char(35) NOT NULL default '',
Country char(3) NOT NULL default '',
Population int(11) NOT NULL default '0',
PRIMARY KEY (ID),
INDEX (Population),
INDEX (Country)
) CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE CountryLanguage (
Country char(3) NOT NULL default '',
Language char(30) NOT NULL default '',
Percentage float(3,1) NOT NULL default '0.0',
PRIMARY KEY (Country, Language),
INDEX (Percentage)
) CHARACTER SET utf8 COLLATE utf8_bin;
ALTER TABLE Country ENGINE=InnoDB;
ALTER TABLE City ENGINE=InnoDB;
ALTER TABLE CountryLanguage ENGINE=InnoDB;
set optimizer_use_stat_tables='preferably';
ANALYZE TABLE Country, City, CountryLanguage;
SELECT * FROM test.table_stat;
db_name table_name cardinality
world Country 239
world City 4079
world CountryLanguage 984
world_innodb Country 239
world_innodb City 4079
world_innodb CountryLanguage 984
SELECT * FROM test.column_stat;
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency
world Country Code ABW ZWE 0.0000 3.0000 1.0000
world Country Name Afghanistan Zimbabwe 0.0000 10.1088 1.0000
world Country SurfaceArea 0.40 17075400.00 0.0000 4.0000 1.0042
world Country Population 0 1277558000 0.0000 4.0000 1.0575
world Country Capital 1 4074 0.0293 4.0000 1.0000
world City ID 1 4079 0.0000 4.0000 1.0000
world City Name A Coruña (La Coruña) Ürgenc 0.0000 8.6416 1.0195
world City Country ABW ZWE 0.0000 3.0000 17.5819
world City Population 42 10500000 0.0000 4.0000 1.0467
world CountryLanguage Country ABW ZWE 0.0000 3.0000 4.2232
world CountryLanguage Language Abhyasi [South]Mande 0.0000 7.1778 2.1532
world CountryLanguage Percentage 0.0 99.9 0.0000 4.0000 2.7640
world_innodb Country Code ABW ZWE 0.0000 3.0000 1.0000
world_innodb Country Name Afghanistan Zimbabwe 0.0000 10.1088 1.0000
world_innodb Country SurfaceArea 0.40 17075400.00 0.0000 4.0000 1.0042
world_innodb Country Population 0 1277558000 0.0000 4.0000 1.0575
world_innodb Country Capital 1 4074 0.0293 4.0000 1.0000
world_innodb City ID 1 4079 0.0000 4.0000 1.0000
world_innodb City Name A Coruña (La Coruña) Ürgenc 0.0000 8.6416 1.0195
world_innodb City Country ABW ZWE 0.0000 3.0000 17.5819
world_innodb City Population 42 10500000 0.0000 4.0000 1.0467
world_innodb CountryLanguage Country ABW ZWE 0.0000 3.0000 4.2232
world_innodb CountryLanguage Language Abhyasi [South]Mande 0.0000 7.1778 2.1532
world_innodb CountryLanguage Percentage 0.0 99.9 0.0000 4.0000 2.7640
SELECT * FROM test.index_stat;
db_name table_name index_name prefix_arity avg_frequency
world Country PRIMARY 1 1.0000
world Country Name 1 1.0000
world City PRIMARY 1 1.0000
world City Population 1 1.0467
world City Country 1 17.5819
world CountryLanguage PRIMARY 1 4.2232
world CountryLanguage PRIMARY 2 1.0000
world CountryLanguage Percentage 1 2.7640
world_innodb Country PRIMARY 1 1.0000
world_innodb Country Name 1 1.0000
world_innodb City PRIMARY 1 1.0000
world_innodb City Population 1 1.0467
world_innodb City Country 1 17.5819
world_innodb CountryLanguage PRIMARY 1 4.2232
world_innodb CountryLanguage PRIMARY 2 1.0000
world_innodb CountryLanguage Percentage 1 2.7640
use test;
DROP DATABASE world;
DROP DATABASE world_innodb;
DELETE FROM mysql.table_stat;
DELETE FROM mysql.column_stat;
DELETE FROM mysql.index_stat;
DROP VIEW test.table_stat;
DROP VIEW test.column_stat;
DROP VIEW test.index_stat;

View file

@ -1,5 +1,6 @@
show tables;
Tables_in_db
column_stat
columns_priv
db
event
@ -10,12 +11,14 @@ help_keyword
help_relation
help_topic
host
index_stat
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
table_stat
tables_priv
time_zone
time_zone_leap_second
@ -262,5 +265,36 @@ slow_log CREATE TABLE `slow_log` (
`server_id` int(10) unsigned NOT NULL,
`sql_text` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'
show create table table_stat;
Table Create Table
table_stat CREATE TABLE `table_stat` (
`db_name` varchar(64) COLLATE utf8_bin NOT NULL,
`table_name` varchar(64) COLLATE utf8_bin NOT NULL,
`cardinality` bigint(21) unsigned DEFAULT NULL,
PRIMARY KEY (`db_name`,`table_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Tables'
show create table column_stat;
Table Create Table
column_stat CREATE TABLE `column_stat` (
`db_name` varchar(64) COLLATE utf8_bin NOT NULL,
`table_name` varchar(64) COLLATE utf8_bin NOT NULL,
`column_name` varchar(64) COLLATE utf8_bin NOT NULL,
`min_value` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`max_value` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`nulls_ratio` double DEFAULT NULL,
`avg_length` double DEFAULT NULL,
`avg_frequency` double DEFAULT NULL,
PRIMARY KEY (`db_name`,`table_name`,`column_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Columns'
show create table index_stat;
Table Create Table
index_stat CREATE TABLE `index_stat` (
`db_name` varchar(64) COLLATE utf8_bin NOT NULL,
`table_name` varchar(64) COLLATE utf8_bin NOT NULL,
`index_name` varchar(64) COLLATE utf8_bin NOT NULL,
`prefix_arity` int(11) unsigned NOT NULL,
`avg_frequency` double DEFAULT NULL,
PRIMARY KEY (`db_name`,`table_name`,`index_name`,`prefix_arity`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Indexes'
show tables;
Tables_in_test

View file

@ -1,6 +1,7 @@
drop table if exists t1,t2;
show tables;
Tables_in_mysql
column_stat
columns_priv
db
event
@ -11,12 +12,14 @@ help_keyword
help_relation
help_topic
host
index_stat
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
table_stat
tables_priv
time_zone
time_zone_leap_second
@ -34,6 +37,7 @@ grant ALL on *.* to test@localhost identified by "gambling";
grant ALL on *.* to test@127.0.0.1 identified by "gambling";
show tables;
Tables_in_mysql
column_stat
columns_priv
db
event
@ -44,12 +48,14 @@ help_keyword
help_relation
help_topic
host
index_stat
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
table_stat
tables_priv
time_zone
time_zone_leap_second
@ -75,6 +81,7 @@ ERROR HY000: Password hash should be a 41-digit hexadecimal number
set password=old_password('gambling3');
show tables;
Tables_in_mysql
column_stat
columns_priv
db
event
@ -85,12 +92,14 @@ help_keyword
help_relation
help_topic
host
index_stat
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
table_stat
tables_priv
time_zone
time_zone_leap_second

View file

@ -0,0 +1,290 @@
--source include/have_innodb.inc
--disable_warnings
drop table if exists t1,t2;
--enable_warnings
CREATE VIEW table_stat AS
SELECT * FROM mysql.table_stat;
CREATE VIEW column_stat AS
SELECT db_name, table_name, column_name,
min_value, max_value,
CAST(nulls_ratio AS decimal(12,4)) AS 'nulls_ratio',
CAST(avg_length AS decimal(12,4)) AS 'avg_length',
CAST(avg_frequency AS decimal(12,4)) AS 'avg_frequency'
FROM mysql.column_stat;
CREATE VIEW index_stat AS
SELECT db_name, table_name, index_name, prefix_arity,
CAST(avg_frequency AS decimal(12,4)) AS 'avg_frequency'
FROM mysql.index_stat;
DELETE FROM mysql.table_stat;
DELETE FROM mysql.column_stat;
DELETE FROM mysql.index_stat;
set optimizer_use_stat_tables='preferably';
CREATE TABLE t1 (
a int NOT NULL PRIMARY KEY,
b varchar(32),
c char(16),
d date,
e double,
f bit(3),
INDEX idx1 (b, e),
INDEX idx2(c, d),
INDEX idx3 (d),
INDEX idx4 (e, b, d)
);
INSERT INTO t1 VALUES
(0, NULL, NULL, NULL, NULL, NULL),
(7, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'dddddddd', '1990-05-15', 0.1, b'100'),
(17, 'vvvvvvvvvvvvv', 'aaaa', '1989-03-12', 0.01, b'101'),
(1, 'vvvvvvvvvvvvv', NULL, '1989-03-12', 0.01, b'100'),
(12, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1999-07-23', 0.112, b'001'),
(23, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.1, b'100'),
(8, 'vvvvvvvvvvvvv', 'aaaa', '1999-07-23', 0.1, b'100'),
(22, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'aaaa', '1989-03-12', 0.112, b'001'),
(31, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'aaaa', '1999-07-23', 0.01, b'001'),
(10, NULL, 'aaaa', NULL, 0.01, b'010'),
(5, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1999-07-23', 0.1, b'100'),
(15, 'vvvvvvvvvvvvv', 'ccccccccc', '1990-05-15', 0.1, b'010'),
(30, NULL, 'bbbbbb', NULL, NULL, b'100'),
(38, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', NULL, NULL, NULL),
(18, 'zzzzzzzzzzzzzzzzzz', 'ccccccccc', '1990-05-15', 0.01, b'010'),
(9, 'yyy', 'bbbbbb', '1998-08-28', 0.01, NULL),
(29, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.012, b'010'),
(3, 'yyy', 'dddddddd', '1990-05-15', 0.112, b'010'),
(39, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', NULL, 0.01, b'100'),
(14, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'ccccccccc', '1990-05-15', 0.1, b'100'),
(40, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', '1989-03-12', NULL, NULL),
(44, NULL, 'aaaa', '1989-03-12', NULL, b'010'),
(19, 'vvvvvvvvvvvvv', 'ccccccccc', '1990-05-15', 0.012, b'011'),
(21, 'zzzzzzzzzzzzzzzzzz', 'dddddddd', '1989-03-12', 0.112, b'100'),
(45, NULL, NULL, '1989-03-12', NULL, b'011'),
(2, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'ccccccccc', '1990-05-15', 0.1, b'001'),
(35, 'yyy', 'aaaa', '1990-05-15', 0.05, b'011'),
(4, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.01, b'101'),
(47, NULL, 'aaaa', '1990-05-15', 0.05, b'010'),
(42, NULL, 'ccccccccc', '1989-03-12', 0.01, b'010'),
(32, NULL, 'bbbbbb', '1990-05-15', 0.01, b'011'),
(49, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww' , 'aaaa', '1990-05-15', NULL, NULL),
(43, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww' , 'bbbbbb', '1990-05-15', NULL, b'100'),
(37, 'yyy', NULL, '1989-03-12', 0.05, b'011'),
(41, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'ccccccccc', '1990-05-15', 0.05, NULL),
(34, 'yyy', NULL, NULL, NULL, NULL),
(33, 'zzzzzzzzzzzzzzzzzz', 'dddddddd', '1989-03-12', 0.05, b'011'),
(24, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1990-05-15', 0.01, b'101'),
(11, 'yyy', 'ccccccccc', '1999-07-23', 0.1, NULL),
(25, 'zzzzzzzzzzzzzzzzzz', 'bbb', '1989-03-12', 0.01, b'101');
ANALYZE TABLE t1;
SELECT * FROM table_stat;
SELECT * FROM column_stat;
SELECT * FROM index_stat;
SELECT COUNT(*) FROM t1;
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='a';
SELECT MIN(t1.a), MAX(t1.a),
(SELECT COUNT(*) FROM t1 WHERE t1.b IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.a)",
(SELECT COUNT(t1.a) FROM t1) /
(SELECT COUNT(DISTINCT t1.a) FROM t1) AS "AVG_FREQUENCY(t1.a)"
FROM t1;
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='b';
SELECT MIN(t1.b), MAX(t1.b),
(SELECT COUNT(*) FROM t1 WHERE t1.b IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.b)",
(SELECT COUNT(t1.b) FROM t1) /
(SELECT COUNT(DISTINCT t1.b) FROM t1) AS "AVG_FREQUENCY(t1.b)"
FROM t1;
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='c';
SELECT MIN(t1.c), MAX(t1.c),
(SELECT COUNT(*) FROM t1 WHERE t1.c IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.c)",
(SELECT COUNT(t1.c) FROM t1) /
(SELECT COUNT(DISTINCT t1.c) FROM t1) AS "AVG_FREQUENCY(t1.c)"
FROM t1;
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='d';
SELECT MIN(t1.d), MAX(t1.d),
(SELECT COUNT(*) FROM t1 WHERE t1.d IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.d)",
(SELECT COUNT(t1.d) FROM t1) /
(SELECT COUNT(DISTINCT t1.d) FROM t1) AS "AVG_FREQUENCY(t1.d)"
FROM t1;
SELECT * FROM column_stat
WHERE db_name='test' AND table_name='t1' AND column_name='e';
SELECT MIN(t1.e), MAX(t1.e),
(SELECT COUNT(*) FROM t1 WHERE t1.e IS NULL) /
(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.e)",
(SELECT COUNT(t1.e) FROM t1) /
(SELECT COUNT(DISTINCT t1.e) FROM t1) AS "AVG_FREQUENCY(t1.e)"
FROM t1;
SELECT * FROM index_stat
WHERE db_name='test' AND table_name='t1' AND index_name='idx1';
SELECT
(SELECT COUNT(*) FROM t1 WHERE t1.b IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.b) FROM t1 WHERE t1.b IS NOT NULL)
AS 'ARITY 1',
(SELECT COUNT(*) FROM t1 WHERE t1.b IS NOT NULL AND t1.e IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.b, t1.e) FROM t1
WHERE t1.b IS NOT NULL AND t1.e IS NOT NULL)
AS 'ARITY 2';
SELECT * FROM index_stat
WHERE db_name='test' AND table_name='t1' AND index_name='idx2';
SELECT
(SELECT COUNT(*) FROM t1 WHERE t1.c IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.c) FROM t1 WHERE t1.c IS NOT NULL)
AS 'ARITY 1',
(SELECT COUNT(*) FROM t1 WHERE t1.c IS NOT NULL AND t1.d IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.c, t1.d) FROM t1
WHERE t1.c IS NOT NULL AND t1.d IS NOT NULL)
AS 'ARITY 2';
SELECT * FROM index_stat
WHERE db_name='test' AND table_name='t1' AND index_name='idx3';
SELECT
(SELECT COUNT(*) FROM t1 WHERE t1.d IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.d) FROM t1 WHERE t1.d IS NOT NULL)
AS 'ARITY 1';
SELECT * FROM index_stat
WHERE db_name='test' AND table_name='t1' AND index_name='idx4';
SELECT
(SELECT COUNT(*) FROM t1 WHERE t1.e IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.e) FROM t1 WHERE t1.e IS NOT NULL)
AS 'ARITY 1',
(SELECT COUNT(*) FROM t1 WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.e, t1.b) FROM t1
WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL)
AS 'ARITY 2',
(SELECT COUNT(*) FROM t1
WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL AND t1.d IS NOT NULL) /
(SELECT COUNT(DISTINCT t1.e, t1.b, t1.d) FROM t1
WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL AND t1.d IS NOT NULL)
AS 'ARITY 3';
CREATE TABLE t2 LIKE t1;
ALTER TABLE t2 ENGINE=InnoDB;
INSERT INTO t2 SELECT * FROM t1;
ANALYZE TABLE t2;
SELECT * FROM table_stat;
SELECT * FROM column_stat ORDER BY column_name;
SELECT * FROM index_stat ORDER BY index_name, prefix_arity, table_name;
DELETE FROM mysql.table_stat;
DELETE FROM mysql.column_stat;
DELETE FROM mysql.index_stat;
ALTER TABLE t1
DROP INDEX idx1,
DROP INDEX idx4;
ALTER TABLE t1
MODIFY COLUMN b text,
ADD INDEX idx1 (b(4), e),
ADD INDEX idx4 (e, b(4), d);
ANALYZE TABLE t1;
SELECT * FROM column_stat;
SELECT * FROM index_stat;
DROP TABLE t1,t2;
DELETE FROM mysql.table_stat;
DELETE FROM mysql.column_stat;
DELETE FROM mysql.index_stat;
set optimizer_use_stat_tables='never';
set names utf8;
CREATE DATABASE world;
use world;
--source include/world_schema_utf8.inc
--disable_query_log
--disable_result_log
--disable_warnings
--source include/world.inc
--enable_warnings
--enable_result_log
--enable_query_log
set optimizer_use_stat_tables='preferably';
--disable_result_log
ANALYZE TABLE Country, City, CountryLanguage;
--enable_result_log
SELECT * FROM test.table_stat;
SELECT * FROM test.column_stat;
SELECT * FROM test.index_stat;
use test;
set optimizer_use_stat_tables='never';
CREATE DATABASE world_innodb;
use world_innodb;
--source include/world_schema_utf8.inc
ALTER TABLE Country ENGINE=InnoDB;
ALTER TABLE City ENGINE=InnoDB;
ALTER TABLE CountryLanguage ENGINE=InnoDB;
--disable_query_log
--disable_result_log
--disable_warnings
--source include/world.inc
--enable_warnings
--enable_result_log
--enable_query_log
set optimizer_use_stat_tables='preferably';
--disable_result_log
ANALYZE TABLE Country, City, CountryLanguage;
--enable_result_log
SELECT * FROM test.table_stat;
SELECT * FROM test.column_stat;
SELECT * FROM test.index_stat;
use test;
DROP DATABASE world;
DROP DATABASE world_innodb;
DELETE FROM mysql.table_stat;
DELETE FROM mysql.column_stat;
DELETE FROM mysql.index_stat;
DROP VIEW test.table_stat;
DROP VIEW test.column_stat;
DROP VIEW test.index_stat;

View file

@ -97,7 +97,8 @@ INSERT INTO user VALUES ('localhost','', '','N','N','N','N','N','N','N','N','
DROP TABLE db, host, user, func, plugin, tables_priv, columns_priv,
procs_priv, servers, help_category, help_keyword, help_relation, help_topic, proc,
time_zone, time_zone_leap_second, time_zone_name, time_zone_transition,
time_zone_transition_type, general_log, slow_log, event, ndb_binlog_index;
time_zone_transition_type, general_log, slow_log, event, ndb_binlog_index,
table_stat, column_stat, index_stat;
-- enable_query_log

View file

@ -59,6 +59,11 @@ CREATE TABLE time_zone_transition_type ( Time_zone_id int unsigned NOT NULL,
CREATE TABLE time_zone_leap_second ( Transition_time bigint signed NOT NULL, Correction int signed NOT NULL, PRIMARY KEY TranTime (Transition_time) ) engine=MyISAM CHARACTER SET utf8 comment='Leap seconds information for time zones';
CREATE TABLE table_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, cardinality bigint(21) unsigned DEFAULT NULL, PRIMARY KEY (db_name,table_name) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Tables';
CREATE TABLE column_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, column_name varchar(64) NOT NULL, min_value varchar(255) DEFAULT NULL, max_value varchar(255) DEFAULT NULL, nulls_ratio double DEFAULT NULL, avg_length double DEFAULT NULL, avg_frequency double DEFAULT NULL, PRIMARY KEY (db_name,table_name,column_name) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Columns';
CREATE TABLE index_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, index_name varchar(64) NOT NULL, prefix_arity int(11) unsigned NOT NULL, avg_frequency double DEFAULT NULL, PRIMARY KEY (db_name,table_name,index_name,prefix_arity) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Indexes';
# Run the mysql_fix_privilege_tables.sql using "mysql --force"
--exec $MYSQL --force test < $MYSQL_FIX_PRIVILEGE_TABLES > $MYSQLTEST_VARDIR/log/system_mysql_db_fix40123.log 2>&1
@ -72,7 +77,7 @@ CREATE TABLE time_zone_leap_second ( Transition_time bigint signed NOT NULL,
-- disable_query_log
# Drop all tables created by this test
DROP TABLE db, host, user, func, plugin, tables_priv, columns_priv, procs_priv, servers, help_category, help_keyword, help_relation, help_topic, proc, time_zone, time_zone_leap_second, time_zone_name, time_zone_transition, time_zone_transition_type, general_log, slow_log, event, ndb_binlog_index;
DROP TABLE db, host, user, func, plugin, tables_priv, columns_priv, procs_priv, servers, help_category, help_keyword, help_relation, help_topic, proc, time_zone, time_zone_leap_second, time_zone_name, time_zone_transition, time_zone_transition_type, general_log, slow_log, event, ndb_binlog_index, table_stat, column_stat, index_stat;
-- enable_query_log

View file

@ -66,6 +66,12 @@ CREATE TABLE servers ( Server_name char(64) NOT NULL DEFAULT '', Host char(64) N
INSERT INTO servers VALUES ('test','localhost','test','root','', 0,'','mysql','root');
CREATE TABLE table_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, cardinality bigint(21) unsigned DEFAULT NULL, PRIMARY KEY (db_name,table_name) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Tables';
CREATE TABLE column_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, column_name varchar(64) NOT NULL, min_value varchar(255) DEFAULT NULL, max_value varchar(255) DEFAULT NULL, nulls_ratio double DEFAULT NULL, avg_length double DEFAULT NULL, avg_frequency double DEFAULT NULL, PRIMARY KEY (db_name,table_name,column_name) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Columns';
CREATE TABLE index_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, index_name varchar(64) NOT NULL, prefix_arity int(11) unsigned NOT NULL, avg_frequency double DEFAULT NULL, PRIMARY KEY (db_name,table_name,index_name,prefix_arity) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Indexes';
# Run the mysql_fix_privilege_tables.sql using "mysql --force"
--exec $MYSQL --force test < $MYSQL_FIX_PRIVILEGE_TABLES > $MYSQLTEST_VARDIR/log/system_mysql_db_fix50030.log 2>&1
@ -78,7 +84,7 @@ INSERT INTO servers VALUES ('test','localhost','test','root','', 0,'','mysql','r
-- disable_query_log
# Drop all tables created by this test
DROP TABLE db, host, user, func, plugin, tables_priv, columns_priv, procs_priv, servers, help_category, help_keyword, help_relation, help_topic, proc, time_zone, time_zone_leap_second, time_zone_name, time_zone_transition, time_zone_transition_type, general_log, slow_log, event, ndb_binlog_index;
DROP TABLE db, host, user, func, plugin, tables_priv, columns_priv, procs_priv, servers, help_category, help_keyword, help_relation, help_topic, proc, time_zone, time_zone_leap_second, time_zone_name, time_zone_transition, time_zone_transition_type, general_log, slow_log, event, ndb_binlog_index, table_stat, column_stat, index_stat;
-- enable_query_log

View file

@ -85,6 +85,12 @@ CREATE TABLE IF NOT EXISTS event ( db char(64) CHARACTER SET utf8 COLLATE utf8_b
CREATE TABLE IF NOT EXISTS ndb_binlog_index (Position BIGINT UNSIGNED NOT NULL, File VARCHAR(255) NOT NULL, epoch BIGINT UNSIGNED NOT NULL, inserts BIGINT UNSIGNED NOT NULL, updates BIGINT UNSIGNED NOT NULL, deletes BIGINT UNSIGNED NOT NULL, schemaops BIGINT UNSIGNED NOT NULL, PRIMARY KEY(epoch)) ENGINE=MYISAM;
CREATE TABLE table_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, cardinality bigint(21) unsigned DEFAULT NULL, PRIMARY KEY (db_name,table_name) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Tables';
CREATE TABLE column_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, column_name varchar(64) NOT NULL, min_value varchar(255) DEFAULT NULL, max_value varchar(255) DEFAULT NULL, nulls_ratio double DEFAULT NULL, avg_length double DEFAULT NULL, avg_frequency double DEFAULT NULL, PRIMARY KEY (db_name,table_name,column_name) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Columns';
CREATE TABLE index_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, index_name varchar(64) NOT NULL, prefix_arity int(11) unsigned NOT NULL, avg_frequency double DEFAULT NULL, PRIMARY KEY (db_name,table_name,index_name,prefix_arity) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Indexes';
# Run the mysql_fix_privilege_tables.sql using "mysql --force"
--exec $MYSQL --force test < $MYSQL_FIX_PRIVILEGE_TABLES > $MYSQLTEST_VARDIR/log/system_mysql_db_fix50117.log 2>&1
@ -97,7 +103,7 @@ CREATE TABLE IF NOT EXISTS ndb_binlog_index (Position BIGINT UNSIGNED NOT NULL,
-- disable_query_log
# Drop all tables created by this test
DROP TABLE db, host, user, func, plugin, tables_priv, columns_priv, procs_priv, servers, help_category, help_keyword, help_relation, help_topic, proc, time_zone, time_zone_leap_second, time_zone_name, time_zone_transition, time_zone_transition_type, general_log, slow_log, event, ndb_binlog_index;
DROP TABLE db, host, user, func, plugin, tables_priv, columns_priv, procs_priv, servers, help_category, help_keyword, help_relation, help_topic, proc, time_zone, time_zone_leap_second, time_zone_name, time_zone_transition, time_zone_transition_type, general_log, slow_log, event, ndb_binlog_index, table_stat, column_stat, index_stat;
-- enable_query_log

View file

@ -102,3 +102,8 @@ CREATE TABLE IF NOT EXISTS event ( db char(64) CHARACTER SET utf8 COLLATE utf8_b
CREATE TABLE IF NOT EXISTS ndb_binlog_index (Position BIGINT UNSIGNED NOT NULL, File VARCHAR(255) NOT NULL, epoch BIGINT UNSIGNED NOT NULL, inserts BIGINT UNSIGNED NOT NULL, updates BIGINT UNSIGNED NOT NULL, deletes BIGINT UNSIGNED NOT NULL, schemaops BIGINT UNSIGNED NOT NULL, PRIMARY KEY(epoch)) ENGINE=MYISAM;
CREATE TABLE IF NOT EXISTS table_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, cardinality bigint(21) unsigned DEFAULT NULL, PRIMARY KEY (db_name,table_name) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Tables';
CREATE TABLE IF NOT EXISTS column_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, column_name varchar(64) NOT NULL, min_value varchar(255) DEFAULT NULL, max_value varchar(255) DEFAULT NULL, nulls_ratio double DEFAULT NULL, avg_length double DEFAULT NULL, avg_frequency double DEFAULT NULL, PRIMARY KEY (db_name,table_name,column_name) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Columns';
CREATE TABLE IF NOT EXISTS index_stat (db_name varchar(64) NOT NULL, table_name varchar(64) NOT NULL, index_name varchar(64) NOT NULL, prefix_arity int(11) unsigned NOT NULL, avg_frequency double DEFAULT NULL, PRIMARY KEY (db_name,table_name,index_name,prefix_arity) ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Statistics on Indexes';

View file

@ -69,7 +69,8 @@ SET (SQL_SOURCE
sql_map.cc sql_parse.cc sql_partition.cc sql_plugin.cc
sql_prepare.cc sql_rename.cc
debug_sync.cc debug_sync.h
sql_repl.cc sql_select.cc sql_show.cc sql_state.c sql_string.cc
sql_repl.cc sql_select.cc sql_show.cc sql_state.c
sql_statistics.cc sql_string.cc
sql_table.cc sql_test.cc sql_trigger.cc sql_udf.cc sql_union.cc
sql_update.cc sql_view.cc strfunc.cc table.cc thr_malloc.cc
time.cc tztime.cc uniques.cc unireg.cc item_xmlfunc.cc

View file

@ -73,7 +73,7 @@ noinst_HEADERS = item.h item_func.h item_sum.h item_cmpfunc.h \
log_event_old.h rpl_record_old.h \
sql_sort.h sql_cache.h set_var.h \
spatial.h gstream.h client_settings.h tzfile.h \
tztime.h my_decimal.h\
tztime.h my_decimal.h sql_statistics.h \
sp_head.h sp_pcontext.h sp_rcontext.h sp.h sp_cache.h \
parse_file.h sql_view.h sql_trigger.h \
sql_array.h sql_cursor.h events.h scheduler.h \
@ -127,7 +127,7 @@ mysqld_SOURCES = sql_lex.cc sql_handler.cc sql_partition.cc \
repl_failsafe.h repl_failsafe.cc \
sql_olap.cc sql_view.cc \
gstream.cc spatial.cc sql_help.cc sql_cursor.cc \
tztime.cc my_decimal.cc\
tztime.cc my_decimal.cc sql_statistics.cc \
sp_head.cc sp_pcontext.cc sp_rcontext.cc sp.cc \
sp_cache.cc parse_file.cc sql_trigger.cc \
event_scheduler.cc event_data_objects.cc \

View file

@ -70,7 +70,7 @@ const char field_separator=',';
((ulong) ((LL(1) << min(arg, 4) * 8) - LL(1)))
#define ASSERT_COLUMN_MARKED_FOR_READ DBUG_ASSERT(!table || (!table->read_set || bitmap_is_set(table->read_set, field_index)))
#define ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED DBUG_ASSERT(!table || (!table->write_set || bitmap_is_set(table->write_set, field_index) || bitmap_is_set(table->vcol_set, field_index)))
#define ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED DBUG_ASSERT(is_stat_field || !table || (!table->write_set || bitmap_is_set(table->write_set, field_index) || bitmap_is_set(table->vcol_set, field_index)))
/*
Rules for merging different types of fields in UNION
@ -1330,7 +1330,8 @@ Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
flags=null_ptr ? 0: NOT_NULL_FLAG;
comment.str= (char*) "";
comment.length=0;
field_index= 0;
field_index= 0;
is_stat_field= FALSE;
}
@ -1817,6 +1818,21 @@ Field *Field::clone(MEM_ROOT *root, struct st_table *new_table)
}
Field *Field::clone(MEM_ROOT *root, TABLE *new_table, my_ptrdiff_t diff,
bool stat_flag)
{
Field *tmp;
if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
{
tmp->init(new_table);
tmp->move_field_offset(diff);
}
tmp->is_stat_field= stat_flag;
return tmp;
}
/****************************************************************************
Field_null, a field that always return NULL
****************************************************************************/

View file

@ -33,6 +33,7 @@ class Send_field;
class Protocol;
class Create_field;
class Relay_log_info;
class Count_distinct_field;
struct ha_field_option_struct;
struct st_cache_field;
@ -183,6 +184,57 @@ public:
*/
bool is_created_from_null_item;
bool is_stat_field; /* TRUE in Field objects created for column min/max values */
/* Statistical data on a column */
class Column_statistics
{
public:
/*
Bitmap indicating what statistical characteristics
are available for the column
*/
uint32 column_stat_nulls;
/* Minimum value for the column */
Field *min_value;
/* Maximum value for the column */
Field *max_value;
/*
The ratio Z/N, where N is the total number of rows,
Z is the number of nulls in the column
*/
double nulls_ratio;
/*
Average number of bytes occupied by the representation of a
value of the column in memory buffers such as join buffer.
CHAR values are stripped of trailing spaces.
Flexible values are stripped of their length prefixes.
*/
double avg_length;
/*
The ratio N/D, where N is the number of rows with null value
in the column, D the number of distinct values among them
*/
double avg_frequency;
};
/*
This structure is used for statistical data on the column
that has been read from the statistical table column_stat
*/
Column_statistics read_stat;
/*
This structure is used for statistical data on the column that
is collected by the function collect_statistics_for_table
*/
Column_statistics write_stat;
/* These members are used only when collecting statistics on the column */
ha_rows nulls;
ulonglong column_total_length;
Count_distinct_field *count_distinct;
/*
This is additional data provided for any computed(virtual) field.
In particular it includes a pointer to the item by which this field
@ -284,6 +336,26 @@ public:
virtual uint32 data_length() { return pack_length(); }
virtual uint32 sort_length() const { return pack_length(); }
/*
Get the number bytes occupied by the value in the field.
CHAR values are stripped of trailing spaces.
Flexible values are stripped of their length.
*/
virtual uint32 value_length()
{
uint len;
if (!zero_pack() &&
(type() == MYSQL_TYPE_STRING &&
(len= pack_length()) >= 4 && len < 256))
{
uchar *str, *end;
for (str= ptr, end= str+len; end > str && end[-1] == ' '; end--);
len=(uint) (end-str);
return len;
}
return data_length();
}
/**
Get the maximum size of the data in packed format.
@ -325,6 +397,36 @@ public:
{ return cmp(a, b); }
virtual int key_cmp(const uchar *str, uint length)
{ return cmp(ptr,str); }
/*
Update the value m of the 'min_val' field with the current value v
of this field if force_update is set to TRUE or if v < m.
Return TRUE if the value has been updated.
*/
virtual bool update_min(Field *min_val, bool force_update)
{
bool update_fl= force_update || cmp(ptr, min_val->ptr) < 0;
if (update_fl)
{
min_val->set_notnull();
memcpy(min_val->ptr, ptr, pack_length());
}
return update_fl;
}
/*
Update the value m of the 'max_val' field with the current value v
of this field if force_update is set to TRUE or if v > m.
Return TRUE if the value has been updated.
*/
virtual bool update_max(Field *max_val, bool force_update)
{
bool update_fl= force_update || cmp(ptr, max_val->ptr) > 0;
if (update_fl)
{
max_val->set_notnull();
memcpy(max_val->ptr, ptr, pack_length());
}
return update_fl;
}
virtual uint decimals() const { return 0; }
/*
Caller beware: sql_type can change str.Ptr, so check
@ -396,6 +498,8 @@ public:
uchar *new_ptr, uchar *new_null_ptr,
uint new_null_bit);
Field *clone(MEM_ROOT *mem_root, struct st_table *new_table);
Field *clone(MEM_ROOT *mem_root, TABLE *new_table, my_ptrdiff_t diff,
bool stat_flag= FALSE);
inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
{
ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
@ -1785,6 +1889,10 @@ public:
int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
int key_cmp(const uchar *,const uchar*);
int key_cmp(const uchar *str, uint length);
/* Never update the value of min_val for a blob field */
bool update_min(Field *min_val, bool force_update) { return FALSE; }
/* Never update the value of max_val for a blob field */
bool update_max(Field *max_val, bool force_update) { return FALSE; }
uint32 key_length() const { return 0; }
void sort_string(uchar *buff,uint length);
uint32 pack_length() const
@ -1802,6 +1910,7 @@ public:
{ return (uint32) (packlength); }
uint row_pack_length() { return pack_length_no_ptr(); }
uint32 sort_length() const;
uint32 value_length() { return get_length(); }
virtual uint32 max_data_length() const
{
return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
@ -2059,6 +2168,28 @@ public:
{ return cmp_binary((uchar *) a, (uchar *) b); }
int key_cmp(const uchar *str, uint length);
int cmp_offset(uint row_offset);
bool update_min(Field *min_val, bool force_update)
{
longlong val= val_int();
bool update_fl= force_update || val < min_val->val_int();
if (update_fl)
{
min_val->set_notnull();
min_val->store(val, FALSE);
}
return update_fl;
}
bool update_max(Field *max_val, bool force_update)
{
longlong val= val_int();
bool update_fl= force_update || val > max_val->val_int();
if (update_fl)
{
max_val->set_notnull();
max_val->store(val, FALSE);
}
return update_fl;
}
void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
{ get_key_image(buff, length, itRAW); }
void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)

View file

@ -887,14 +887,14 @@ C_MODE_START
/* Declarations for auxilary C-callbacks */
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
{
return memcmp(key1, key2, *(uint *) arg);
}
static int item_sum_distinct_walk(void *element, element_count num_of_dups,
void *item)
int item_sum_distinct_walk(void *element, element_count num_of_dups,
void *item)
{
return ((Item_sum_distinct*) (item))->unique_walk_function(element);
}
@ -2362,7 +2362,7 @@ int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
C_MODE_START
static int count_distinct_walk(void *elem, element_count count, void *arg)
int count_distinct_walk(void *elem, element_count count, void *arg)
{
(*((ulonglong*)arg))++;
return 0;

View file

@ -1519,6 +1519,12 @@ find_field_in_table_sef(TABLE *table, const char *name);
int update_virtual_fields(THD *thd, TABLE *table, bool ignore_stored= FALSE);
int dynamic_column_error_message(enum_dyncol_func_result rc);
int read_statistics_for_table(THD *thd, TABLE *table);
int collect_statistics_for_table(THD *thd, TABLE *table);
int update_statistics_for_table(THD *thd, TABLE *table);
extern TYPELIB optimizer_use_stat_tables_typelib;
#endif /* MYSQL_SERVER */
#ifdef HAVE_OPENSSL
@ -1935,6 +1941,9 @@ void mysql_wait_completed_table(ALTER_PARTITION_PARAM_TYPE *lpt, TABLE *my_table
/* Functions to work with system tables. */
bool open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
Open_tables_state *backup);
bool unlock_tables_n_open_system_tables_for_write(THD *thd,
TABLE_LIST *table_list,
Open_tables_state *backup);
void close_system_tables(THD *thd, Open_tables_state *backup);
TABLE *open_system_table_for_update(THD *thd, TABLE_LIST *one_table);
@ -2589,6 +2598,13 @@ Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
LEX_STRING component);
int get_var_with_binlog(THD *thd, enum_sql_command sql_command,
LEX_STRING &name, user_var_entry **out_entry);
/* item_sum.cc */
extern "C" int simple_raw_key_cmp(void* arg, const void* key1,
const void* key2);
extern "C" int count_distinct_walk(void *elem, element_count count, void *arg);
int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2);
/* log.cc */
bool flush_error_log(void);

View file

@ -3759,6 +3759,8 @@ static int init_common_variables(const char *conf_file_name, int argc,
global_system_variables.character_set_results= default_charset_info;
global_system_variables.character_set_client= default_charset_info;
global_system_variables.optimizer_use_stat_tables= 0;
if (!(character_set_filesystem=
get_charset_by_csname(character_set_filesystem_name,
MY_CS_PRIMARY, MYF(MY_WME))))

View file

@ -537,6 +537,12 @@ static sys_var_thd_ulong sys_progress_report_time(&vars,
"progress_report_time",
&SV::progress_report_time);
static sys_var_thd_enum sys_optimizer_use_stat_tables(&vars,
"optimizer_use_stat_tables",
&SV::optimizer_use_stat_tables,
&optimizer_use_stat_tables_typelib,
NULL);
static sys_var_const sys_pid_file(&vars, "pid_file",
OPT_GLOBAL, SHOW_CHAR,
(uchar*) pidfile_name);

View file

@ -4855,6 +4855,13 @@ int open_tables(THD *thd, TABLE_LIST **start, uint *counter, uint flags)
parent_l->next_global= *parent_l->table->child_last_l;
}
if (thd->variables.optimizer_use_stat_tables > 0)
{
if (tables->table && tables->table->s &&
tables->table->s->table_category != TABLE_CATEGORY_SYSTEM)
(void) read_statistics_for_table(thd, tables->table);
}
if (refresh) // Refresh in progress
{
/*
@ -9495,6 +9502,72 @@ error:
}
/*
Unlock opened tables and open and lock system tables for write.
SYNOPSIS
open_system_tables_for_read()
thd Thread context.
table_list List of tables to open.
backup Pointer to Open_tables_state instance where
information about currently open tables will be
saved, and from which will be restored when we will
end work with system tables.
DESCRIPTION
The function first unlocks the opened tables, but do not close them.
Then it opens and locks for write the specified system tables.
NOTE
The system tables cannot be locked for write without unlocking
the current opened tables. Yet in some cases we still need valid TABLE
structures for these tables to be able to extract data that is to be
written into the system tables.
This function is used when updating the statistical tables.
RETURN
FALSE Success
TRUE Error
*/
bool
unlock_tables_n_open_system_tables_for_write(THD *thd,
TABLE_LIST *table_list,
Open_tables_state *backup)
{
Query_tables_list query_tables_list_backup;
LEX *lex= thd->lex;
DBUG_ENTER("unlock_tables_n_open_system_tables_for_write");
mysql_unlock_tables(thd, thd->lock);
thd->lock= 0;
lex->reset_n_backup_query_tables_list(&query_tables_list_backup);
thd->reset_n_backup_open_tables_state(backup);
if (open_and_lock_tables_derived(thd, table_list, FALSE))
{
lex->restore_backup_query_tables_list(&query_tables_list_backup);
goto error;
}
for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
{
DBUG_ASSERT(tables->table->s->table_category == TABLE_CATEGORY_SYSTEM);
tables->table->use_all_columns();
}
lex->restore_backup_query_tables_list(&query_tables_list_backup);
DBUG_RETURN(FALSE);
error:
close_system_tables(thd, backup);
DBUG_RETURN(TRUE);
}
/*
Close system tables, opened with open_system_tables_for_read().

View file

@ -458,6 +458,7 @@ struct system_variables
ulong optimizer_search_depth;
/* A bitmap for switching optimizations on/off */
ulong optimizer_switch;
ulong optimizer_use_stat_tables;
ulong preload_buff_size;
ulong profiling_history_size;
ulong query_cache_type;

1587
sql/sql_statistics.cc Normal file

File diff suppressed because it is too large Load diff

63
sql/sql_statistics.h Normal file
View file

@ -0,0 +1,63 @@
/* Copyright 2006-2008 MySQL AB, 2008 Sun Microsystems, Inc.
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 St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef SQL_STATISTICS_H
#define SQL_STATISTICS_H
/*
These enumeration types comprise the dictionary of three
statistical tables table_stat, column_stat and index_stat
as they defined in ../scripts/mysql_system_tables.sql.
It would be nice if the declarations of these types were
generated automatically by the table definitions.
*/
enum enum_stat_tables
{
TABLE_STAT,
COLUMN_STAT,
INDEX_STAT,
};
enum enum_table_stat_col
{
TABLE_STAT_DB_NAME,
TABLE_STAT_TABLE_NAME,
TABLE_STAT_CARDINALITY
};
enum enum_column_stat_col
{
COLUMN_STAT_DB_NAME,
COLUMN_STAT_TABLE_NAME,
COLUMN_STAT_COLUMN_NAME,
COLUMN_STAT_MIN_VALUE,
COLUMN_STAT_MAX_VALUE,
COLUMN_STAT_NULLS_RATIO,
COLUMN_STAT_AVG_LENGTH,
COLUMN_STAT_AVG_FREQUENCY
};
enum enum_index_stat_col
{
INDEX_STAT_DB_NAME,
INDEX_STAT_TABLE_NAME,
INDEX_STAT_INDEX_NAME,
INDEX_STAT_PREFIX_ARITY,
INDEX_STAT_AVG_FREQUENCY
};
#endif /* SQL_STATISTICS_H */

View file

@ -4692,6 +4692,7 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
Protocol *protocol= thd->protocol;
LEX *lex= thd->lex;
int result_code;
int compl_result_code;
bool need_repair_or_alter= 0;
DBUG_ENTER("mysql_admin_table");
DBUG_PRINT("enter", ("extra_open_options: %u", extra_open_options));
@ -4946,9 +4947,22 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
}
}
DBUG_PRINT("admin", ("calling operator_func '%s'", operator_name));
result_code = (table->table->file->*operator_func)(thd, check_opt);
DBUG_PRINT("admin", ("operator_func returned: %d", result_code));
result_code= compl_result_code= 0;
if (operator_func != &handler::ha_analyze ||
thd->variables.optimizer_use_stat_tables < 3)
{
DBUG_PRINT("admin", ("calling operator_func '%s'", operator_name));
result_code = (table->table->file->*operator_func)(thd, check_opt);
DBUG_PRINT("admin", ("operator_func returned: %d", result_code));
}
if (operator_func == &handler::ha_analyze &&
thd->variables.optimizer_use_stat_tables > 0)
{
if (!(compl_result_code=
collect_statistics_for_table(thd, table->table)))
compl_result_code= update_statistics_for_table(thd, table->table);
}
if (result_code == HA_ADMIN_NOT_IMPLEMENTED && need_repair_or_alter)
{
@ -4958,6 +4972,7 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
*/
result_code= admin_recreate_table(thd, table);
}
send_result:
lex->cleanup_after_one_table_open();

View file

@ -102,6 +102,31 @@ typedef struct st_key {
For temporary heap tables this member is NULL.
*/
ulong *rec_per_key;
/* Statistical data on an index prefixes */
class Index_statistics
{
public:
/*
The k-th element of this array contains the ratio N/D,
where N is the number of index entries without nulls
in the first k components, and D is the number of distinct
k-component prefixes among them
*/
double *avg_frequency;
};
/*
This structure is used for statistical data on the index
that has been read from the statistical table index_stat
*/
Index_statistics read_stat;
/*
This structure is used for statistical data on the index that
is collected by the function collect_statistics_for_table
*/
Index_statistics write_stat;
union {
int bdb_return_if_eq;
} handler;

View file

@ -489,6 +489,12 @@ inline bool is_system_table_name(const char *name, uint length)
my_tolower(ci, name[2]) == 'm' &&
my_tolower(ci, name[3]) == 'e') ||
/* one of mysql.*_stat tables */
(my_tolower(ci, name[length-4]) == 's' &&
my_tolower(ci, name[length-3]) == 't' &&
my_tolower(ci, name[length-2]) == 'a' &&
my_tolower(ci, name[length-1]) == 't') ||
/* mysql.event table */
(my_tolower(ci, name[0]) == 'e' &&
my_tolower(ci, name[1]) == 'v' &&
@ -698,6 +704,8 @@ static int open_binary_frm(THD *thd, TABLE_SHARE *share, uchar *head,
uchar *disk_buff, *strpos, *null_flags, *null_pos, *options;
uchar *buff= 0;
ulong pos, record_offset, *rec_per_key, rec_buff_length;
double *read_avg_frequency= 0;
double *write_avg_frequency= 0;
handler *handler_file= 0;
KEY *keyinfo;
KEY_PART_INFO *key_part;
@ -812,7 +820,13 @@ static int open_binary_frm(THD *thd, TABLE_SHARE *share, uchar *head,
if (!(rec_per_key= (ulong*) alloc_root(&share->mem_root,
sizeof(ulong)*key_parts)))
goto err;
if (!(read_avg_frequency= (double*) alloc_root(&share->mem_root,
sizeof(double)*key_parts)))
goto err;
if (!(write_avg_frequency= (double*) alloc_root(&share->mem_root,
sizeof(double)*key_parts)))
goto err;
for (i=0 ; i < keys ; i++, keyinfo++)
{
if (new_frm_ver >= 3)
@ -835,9 +849,13 @@ static int open_binary_frm(THD *thd, TABLE_SHARE *share, uchar *head,
keyinfo->key_part= key_part;
keyinfo->rec_per_key= rec_per_key;
keyinfo->read_stat.avg_frequency= read_avg_frequency;
keyinfo->write_stat.avg_frequency= write_avg_frequency;
for (j=keyinfo->key_parts ; j-- ; key_part++)
{
*rec_per_key++=0;
*read_avg_frequency++= 0;
*write_avg_frequency++= 0;
key_part->fieldnr= (uint16) (uint2korr(strpos) & FIELD_NR_MASK);
key_part->offset= (uint) uint2korr(strpos+2)-1;
key_part->key_type= (uint) uint2korr(strpos+5);

View file

@ -747,6 +747,25 @@ struct st_table {
*/
query_id_t query_id;
/* Statistical data on a table */
class Table_statistics
{
public:
my_bool cardinality_is_null; /* TRUE if the cardinality is unknown */
ha_rows cardinality; /* Number of rows in the table */
};
/*
This structure is used for statistical data on the table
that has been read from the statistical table table_stat
*/
Table_statistics read_stat;
/*
This structure is used for statistical data on the table that
is collected by the function collect_statistics_for_table
*/
Table_statistics write_stat;
/*
For each key that has quick_keys.is_set(key) == TRUE: estimate of #records
and max #key parts that range access would use.