Commit 94033fcf authored by Monty's avatar Monty Committed by Sergei Golubchik

MDEV-33151 Add more columns to TABLE_STATISTICS and USER STATS

Columns added to TABLE_STATISTICS
- ROWS_INSERTED, ROWS_DELETED, ROWS_UPDATED, KEY_READ_HITS and
  KEY_READ_MISSES.

Columns added to CLIENT_STATISTICS and USER_STATISTICS:
- KEY_READ_HITS and KEY_READ_MISSES.

User visible changes (except new columns):
- CLIENT_STATISTICS and USER_STATISTICS has columns KEY_READ_HITS and
  KEY_READ_MISSES added after column ROWS_UPDATED before SELECT_COMMANDS.

Other changes:
- Do not collect table statistics for system tables like index_stats
  table_stats, performance_schema, information_schema etc as the user
  has no control of these and the generate noice in the statistics.
- All row variables that are part of user_stats are moved to
  'struct rows_stats' to make it easy to clear all of them at once.
- ha_read_key_misses added to STATUS_VAR

Notes:
- userstat.result has a change of numbers of rows for handler_read_key.
  This is because use-stat-tables is now disabled for the test.
parent c9445186
......@@ -20,26 +20,35 @@ TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
test just_a_test IND_just_a_test_first_name_last_name 1 1
test just_a_test IND_just_a_test_state 2 1
select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
test just_a_test 18 5 5
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
test just_a_test 18 5 5 5 0 0 2 0
alter table just_a_test drop key IND_just_a_test_first_name_last_name;
select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
test just_a_test IND_just_a_test_state 2 1
select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
test just_a_test 23 5 5
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
test just_a_test 23 5 5 5 0 0 2 0
alter table just_a_test drop column state;
select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
select * from just_a_test force index(primary) where id between 1 and 2;
id first_name last_name address phone email
1 fa la china_a 11111111 fa_la@163.com
2 fb lb china_b 22222222 fb_lb@163.com
select * from just_a_test force index(primary) where id=8;
id first_name last_name address phone email
update just_a_test set first_name="unlucky" where id=5;
delete from just_a_test where id=5;
select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
test just_a_test 28 5 5
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
test just_a_test 32 7 7 5 1 1 5 1
select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
test just_a_test PRIMARY 4 3
drop table just_a_test;
select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
create table just_a_test(id int not null primary key,first_name varchar(10),last_name varchar(10),address varchar(100),phone bigint,email varchar(30), state varchar(30),key(first_name,last_name),key(state));
......@@ -63,11 +72,11 @@ test just_a_test PRIMARY 4 1
test just_a_test first_name 1 1
test just_a_test state 2 1
select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
test just_a_test 7 5 15
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
test just_a_test 7 5 15 5 0 0 3 0
drop table just_a_test;
select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test';
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
set global userstat=@save_userstat;
......@@ -10,6 +10,7 @@ insert into just_a_test values(1,'fa','la','china_a',11111111,'fa_la@163.com','C
(3,'fc','lc','china_c',33333333,'fc_lc@163.com','California'),
(4,'fd','ld','china_d',44444444,'fd_ld@163.com','Utah'),
(5,'fe','le','china_e',55555555,'fe_le@163.com','Arizona');
alter table just_a_test add primary key (id);
alter table just_a_test add key IND_just_a_test_first_name_last_name(first_name,last_name);
alter table just_a_test add key IND_just_a_test_state(state);
......@@ -23,6 +24,10 @@ select * from information_schema.index_statistics where table_schema='test' and
select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test';
alter table just_a_test drop column state;
select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test';
select * from just_a_test force index(primary) where id between 1 and 2;
select * from just_a_test force index(primary) where id=8;
update just_a_test set first_name="unlucky" where id=5;
delete from just_a_test where id=5;
select * from information_schema.table_statistics where table_schema='test' and table_name='just_a_test';
select * from information_schema.index_statistics where table_schema='test' and table_name='just_a_test';
drop table just_a_test;
......
......@@ -126,9 +126,9 @@ a
3
4
SELECT * FROM INFORMATION_SCHEMA.TABLE_STATISTICS ORDER BY BINARY TABLE_NAME;
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
test T1 4 4 4
test t1 4 4 4
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
test T1 4 4 4 4 0 0 0 0
test t1 4 4 4 4 0 0 0 0
SELECT * FROM INFORMATION_SCHEMA.INDEX_STATISTICS ORDER BY BINARY TABLE_NAME;
TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
test T1 a 4 1
......
......@@ -7629,7 +7629,7 @@ SELECT 1;
DROP PROCEDURE sp;
CREATE PROCEDURE sp() SET STATEMENT SQL_SELECT_LIMIT=0 FOR SHOW USER_STATISTICS;
CALL sp;
User Total_connections Concurrent_connections Connected_time Busy_time Cpu_time Bytes_received Bytes_sent Binlog_bytes_written Rows_read Rows_sent Rows_deleted Rows_inserted Rows_updated Select_commands Update_commands Other_commands Commit_transactions Rollback_transactions Denied_connections Lost_connections Access_denied Empty_queries Total_ssl_connections Max_statement_time_exceeded
User Total_connections Concurrent_connections Connected_time Busy_time Cpu_time Bytes_received Bytes_sent Binlog_bytes_written Rows_read Rows_sent Rows_deleted Rows_inserted Rows_updated Key_read_hits Key_read_misses Select_commands Update_commands Other_commands Commit_transactions Rollback_transactions Denied_connections Lost_connections Access_denied Empty_queries Total_ssl_connections Max_statement_time_exceeded
SELECT 1;
1
1
......
......@@ -7126,9 +7126,10 @@ MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes Rows_inserted Rows_updated Rows_deleted Key_read_hits Key_read_misses
test t1 2 0 0 0 0 0 0 0
test t2 3 0 0 0 0 0 1 0
test t3 0 0 0 0 0 0 0 1
show index_statistics;
Table_schema Table_name Index_name Rows_read Queries
test t2 b 1 1
......
......@@ -7128,9 +7128,10 @@ MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes Rows_inserted Rows_updated Rows_deleted Key_read_hits Key_read_misses
test t1 2 0 0 0 0 0 0 0
test t2 3 0 0 0 0 0 1 0
test t3 0 0 0 0 0 0 0 1
show index_statistics;
Table_schema Table_name Index_name Rows_read Queries
test t2 b 1 1
......
......@@ -7124,9 +7124,10 @@ MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes Rows_inserted Rows_updated Rows_deleted Key_read_hits Key_read_misses
test t1 2 0 0 0 0 0 0 0
test t2 3 0 0 0 0 0 1 0
test t3 0 0 0 0 0 0 0 1
show index_statistics;
Table_schema Table_name Index_name Rows_read Queries
test t2 b 1 1
......
......@@ -7121,9 +7121,10 @@ MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes Rows_inserted Rows_updated Rows_deleted Key_read_hits Key_read_misses
test t1 2 0 0 0 0 0 0 0
test t2 3 0 0 0 0 0 1 0
test t3 0 0 0 0 0 0 0 1
show index_statistics;
Table_schema Table_name Index_name Rows_read Queries
test t2 b 1 1
......
......@@ -7132,9 +7132,10 @@ MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes Rows_inserted Rows_updated Rows_deleted Key_read_hits Key_read_misses
test t1 2 0 0 0 0 0 0 0
test t2 3 0 0 0 0 0 1 0
test t3 0 0 0 0 0 0 0 1
show index_statistics;
Table_schema Table_name Index_name Rows_read Queries
test t2 b 1 1
......
......@@ -7121,9 +7121,10 @@ MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes Rows_inserted Rows_updated Rows_deleted Key_read_hits Key_read_misses
test t1 2 0 0 0 0 0 0 0
test t2 3 0 0 0 0 0 1 0
test t3 0 0 0 0 0 0 0 1
show index_statistics;
Table_schema Table_name Index_name Rows_read Queries
test t2 b 1 1
......
--disable-userstat
--disable-userstat --use-stat-tables=never
......@@ -15,6 +15,8 @@ ROWS_SENT bigint(21) NO NULL
ROWS_DELETED bigint(21) NO NULL
ROWS_INSERTED bigint(21) NO NULL
ROWS_UPDATED bigint(21) NO NULL
KEY_READ_HITS bigint(21) NO NULL
KEY_READ_MISSES bigint(21) NO NULL
SELECT_COMMANDS bigint(21) NO NULL
UPDATE_COMMANDS bigint(21) NO NULL
OTHER_COMMANDS bigint(21) NO NULL
......@@ -42,6 +44,8 @@ ROWS_SENT bigint(21) NO NULL
ROWS_DELETED bigint(21) NO NULL
ROWS_INSERTED bigint(21) NO NULL
ROWS_UPDATED bigint(21) NO NULL
KEY_READ_HITS bigint(21) NO NULL
KEY_READ_MISSES bigint(21) NO NULL
SELECT_COMMANDS bigint(21) NO NULL
UPDATE_COMMANDS bigint(21) NO NULL
OTHER_COMMANDS bigint(21) NO NULL
......@@ -67,6 +71,11 @@ TABLE_NAME varchar(192) NO NULL
ROWS_READ bigint(21) NO NULL
ROWS_CHANGED bigint(21) NO NULL
ROWS_CHANGED_X_INDEXES bigint(21) NO NULL
ROWS_INSERTED bigint(21) NO NULL
ROWS_UPDATED bigint(21) NO NULL
ROWS_DELETED bigint(21) NO NULL
KEY_READ_HITS bigint(21) NO NULL
KEY_READ_MISSES bigint(21) NO NULL
set @save_general_log=@@global.general_log;
set @@global.general_log=0;
set @@global.userstat=1;
......@@ -117,7 +126,7 @@ Handler_mrr_key_refills 0
Handler_mrr_rowid_refills 0
Handler_prepare 18
Handler_read_first 0
Handler_read_key 17
Handler_read_key 9
Handler_read_last 0
Handler_read_next 0
Handler_read_prev 0
......@@ -135,15 +144,15 @@ Handler_update 5
Handler_write 7
select variable_value - @global_read_key as "handler_read_key" from information_schema.global_status where variable_name="handler_read_key";
handler_read_key
17
9
disconnect ssl_con;
set @@global.userstat=0;
select * from information_schema.index_statistics;
TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
select * from information_schema.table_statistics;
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes Rows_inserted Rows_updated Rows_deleted Key_read_hits Key_read_misses
show index_statistics;
Table_schema Table_name Index_name Rows_read Queries
select TOTAL_CONNECTIONS, TOTAL_SSL_CONNECTIONS, CONCURRENT_CONNECTIONS, ROWS_READ, ROWS_SENT, ROWS_DELETED, ROWS_INSERTED, ROWS_UPDATED, SELECT_COMMANDS, UPDATE_COMMANDS, COMMIT_TRANSACTIONS, ROLLBACK_TRANSACTIONS, DENIED_CONNECTIONS, LOST_CONNECTIONS, ACCESS_DENIED, EMPTY_QUERIES from information_schema.client_statistics;;
......@@ -191,7 +200,7 @@ flush index_statistics;
select * from information_schema.index_statistics;
TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ QUERIES
select * from information_schema.table_statistics;
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
show status like "%generic%";
Variable_name Value
Com_show_generic 2
......
......@@ -34,12 +34,12 @@ DROP TABLE t1;
CREATE TABLE t2 (c1 INT UNSIGNED);
ALTER TABLE t2 MODIFY c1 FLOAT;
SELECT * FROM INFORMATION_SCHEMA.TABLE_STATISTICS WHERE TABLE_NAME='t2';
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
DROP TABLE t2;
CREATE TABLE t2 (c1 INT UNSIGNED);
ALTER TABLE t2 MODIFY c1 FLOAT;
SELECT * FROM INFORMATION_SCHEMA.TABLE_STATISTICS WHERE TABLE_NAME='t2';
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES
TABLE_SCHEMA TABLE_NAME ROWS_READ ROWS_CHANGED ROWS_CHANGED_X_INDEXES ROWS_INSERTED ROWS_UPDATED ROWS_DELETED KEY_READ_HITS KEY_READ_MISSES
DROP TABLE t2;
SET GLOBAL userstat= @userstat_old;
SET SESSION default_storage_engine = @default_storage_engine_old;
......@@ -16,6 +16,8 @@ static ST_FIELD_INFO client_stats_fields[]=
Column("ROWS_DELETED", SLonglong(), NOT_NULL, "Rows_deleted"),
Column("ROWS_INSERTED", SLonglong(), NOT_NULL, "Rows_inserted"),
Column("ROWS_UPDATED", SLonglong(), NOT_NULL, "Rows_updated"),
Column("KEY_READ_HITS", SLonglong(), NOT_NULL, "Key_read_hits"),
Column("KEY_READ_MISSES", SLonglong(), NOT_NULL, "Key_read_misses"),
Column("SELECT_COMMANDS", SLonglong(), NOT_NULL, "Select_commands"),
Column("UPDATE_COMMANDS", SLonglong(), NOT_NULL, "Update_commands"),
Column("OTHER_COMMANDS", SLonglong(), NOT_NULL, "Other_commands"),
......@@ -50,11 +52,13 @@ static int send_user_stats(THD* thd, HASH *all_user_stats, TABLE *table)
table->field[j++]->store((longlong)user_stats->bytes_received, TRUE);
table->field[j++]->store((longlong)user_stats->bytes_sent, TRUE);
table->field[j++]->store((longlong)user_stats->binlog_bytes_written, TRUE);
table->field[j++]->store((longlong)user_stats->rows_read, TRUE);
table->field[j++]->store((longlong)user_stats->rows_stats.read, TRUE);
table->field[j++]->store((longlong)user_stats->rows_sent, TRUE);
table->field[j++]->store((longlong)user_stats->rows_deleted, TRUE);
table->field[j++]->store((longlong)user_stats->rows_inserted, TRUE);
table->field[j++]->store((longlong)user_stats->rows_updated, TRUE);
table->field[j++]->store((longlong)user_stats->rows_stats.deleted, TRUE);
table->field[j++]->store((longlong)user_stats->rows_stats.inserted, TRUE);
table->field[j++]->store((longlong)user_stats->rows_stats.updated, TRUE);
table->field[j++]->store((longlong)user_stats->rows_stats.key_read_hit, TRUE);
table->field[j++]->store((longlong)user_stats->rows_stats.key_read_miss, TRUE);
table->field[j++]->store((longlong)user_stats->select_commands, TRUE);
table->field[j++]->store((longlong)user_stats->update_commands, TRUE);
table->field[j++]->store((longlong)user_stats->other_commands, TRUE);
......
......@@ -7,6 +7,11 @@ static ST_FIELD_INFO table_stats_fields[]=
Column("ROWS_READ", SLonglong(), NOT_NULL, "Rows_read"),
Column("ROWS_CHANGED", SLonglong(), NOT_NULL, "Rows_changed"),
Column("ROWS_CHANGED_X_INDEXES",SLonglong(), NOT_NULL, "Rows_changed_x_#indexes"),
Column("ROWS_INSERTED", SLonglong(), NOT_NULL, "Rows_inserted"),
Column("ROWS_UPDATED", SLonglong(), NOT_NULL, "Rows_updated"),
Column("ROWS_DELETED", SLonglong(), NOT_NULL, "Rows_deleted"),
Column("KEY_READ_HITS", SLonglong(), NOT_NULL, "Key_read_hits"),
Column("KEY_READ_MISSES", SLonglong(), NOT_NULL, "Key_read_misses"),
CEnd()
};
......@@ -24,6 +29,8 @@ static int table_stats_fill(THD *thd, TABLE_LIST *tables, COND *cond)
(TABLE_STATS*)my_hash_element(&global_table_stats, i);
TABLE_LIST tmp_table;
size_t schema_length, table_name_length;
struct rows_stats *rows_stats= &table_stats->rows_stats;
ulonglong rows_changed;
end_of_schema= strend(table_stats->table);
schema_length= (size_t) (end_of_schema - table_stats->table);
......@@ -40,14 +47,22 @@ static int table_stats_fill(THD *thd, TABLE_LIST *tables, COND *cond)
check_grant(thd, SELECT_ACL, &tmp_table, 1, 1, 1))
continue;
rows_changed= (rows_stats->inserted + rows_stats->updated +
rows_stats->updated);
table->field[0]->store(table_stats->table, schema_length,
system_charset_info);
table->field[1]->store(table_stats->table + schema_length+1,
table_name_length, system_charset_info);
table->field[2]->store((longlong)table_stats->rows_read, TRUE);
table->field[3]->store((longlong)table_stats->rows_changed, TRUE);
table->field[2]->store((longlong) rows_stats->read, TRUE);
table->field[3]->store((longlong) rows_changed, TRUE);
table->field[4]->store((longlong)table_stats->rows_changed_x_indexes,
TRUE);
table->field[5]->store((longlong) rows_stats->inserted, TRUE);
table->field[6]->store((longlong) rows_stats->updated, TRUE);
table->field[7]->store((longlong) rows_stats->deleted, TRUE);
table->field[8]->store((longlong) rows_stats->key_read_hit, TRUE);
table->field[9]->store((longlong) rows_stats->key_read_miss, TRUE);
if (schema_table_store_record(thd, table))
{
mysql_mutex_unlock(&LOCK_global_table_stats);
......
......@@ -16,6 +16,8 @@ static ST_FIELD_INFO user_stats_fields[]=
Column("ROWS_DELETED", SLonglong(), NOT_NULL, "Rows_deleted"),
Column("ROWS_INSERTED", SLonglong(), NOT_NULL, "Rows_inserted"),
Column("ROWS_UPDATED", SLonglong(), NOT_NULL, "Rows_updated"),
Column("KEY_READ_HITS", SLonglong(), NOT_NULL, "Key_read_hits"),
Column("KEY_READ_MISSES", SLonglong(), NOT_NULL, "Key_read_misses"),
Column("SELECT_COMMANDS", SLonglong(), NOT_NULL, "Select_commands"),
Column("UPDATE_COMMANDS", SLonglong(), NOT_NULL, "Update_commands"),
Column("OTHER_COMMANDS", SLonglong(), NOT_NULL, "Other_commands"),
......
......@@ -298,7 +298,7 @@ int ha_sequence::write_row(const uchar *buf)
Log_func *log_func= Write_rows_log_event::binlog_row_logging_function;
if (!sequence_locked)
sequence->copy(&tmp_seq);
rows_changed++;
rows_stats.updated++;
/* We have to do the logging while we hold the sequence mutex */
error= binlog_log_row(0, buf, log_func);
}
......
......@@ -3627,7 +3627,8 @@ int handler::ha_close(void)
In_use is 0 for tables that was closed from the table cache.
*/
if (table->in_use)
status_var_add(table->in_use->status_var.rows_tmp_read, rows_tmp_read);
status_var_add(table->in_use->status_var.rows_tmp_read,
rows_stats.tmp_read);
PSI_CALL_close_table(table_share, m_psi);
m_psi= NULL; /* instrumentation handle, invalid after close_table() */
DBUG_ASSERT(m_psi_batch_mode == PSI_BATCH_MODE_NONE);
......@@ -3738,10 +3739,16 @@ int handler::ha_index_read_map(uchar *buf, const uchar *key,
increment_statistics(&SSV::ha_read_key_count);
if (!result)
{
rows_stats.key_read_hit++; // For userstat
update_index_statistics();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(this, VCOL_UPDATE_FOR_READ);
}
else
{
status_var_increment(table->in_use->status_var.ha_read_key_miss);
rows_stats.key_read_miss++; // For userstat
}
table->status=result ? STATUS_NOT_FOUND: 0;
DBUG_RETURN(result);
}
......@@ -3766,11 +3773,17 @@ int handler::ha_index_read_idx_map(uchar *buf, uint index, const uchar *key,
increment_statistics(&SSV::ha_read_key_count);
if (!result)
{
rows_stats.key_read_hit++;
update_rows_read();
index_rows_read[index]++;
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(this, VCOL_UPDATE_FOR_READ);
}
else
{
status_var_increment(table->in_use->status_var.ha_read_key_miss);
rows_stats.key_read_miss++;
}
table->status=result ? STATUS_NOT_FOUND: 0;
return result;
}
......@@ -6006,19 +6019,19 @@ void handler::get_dynamic_partition_info(PARTITION_STATS *stat_info,
void handler::update_global_table_stats()
{
ulonglong changed;
TABLE_STATS * table_stats;
status_var_add(table->in_use->status_var.rows_read, rows_read);
DBUG_ASSERT(rows_tmp_read == 0);
status_var_add(table->in_use->status_var.rows_read, rows_stats.read);
DBUG_ASSERT(rows_stats.tmp_read == 0);
if (!table->in_use->userstat_running)
{
rows_read= rows_changed= 0;
return;
}
if (!table->in_use->userstat_running ||
table->s->table_category != TABLE_CATEGORY_USER)
goto reset;
if (rows_read + rows_changed == 0)
return; // Nothing to update.
if (rows_stats.read + rows_stats.updated + rows_stats.inserted +
rows_stats.deleted + rows_stats.key_read_miss == 0)
goto reset; // Nothing to update.
DBUG_ASSERT(table->s);
DBUG_ASSERT(table->s->table_cache_key.str);
......@@ -6051,14 +6064,20 @@ void handler::update_global_table_stats()
}
}
// Updates the global table stats.
table_stats->rows_read+= rows_read;
table_stats->rows_changed+= rows_changed;
table_stats->rows_changed_x_indexes+= (rows_changed *
table_stats->rows_stats.read+= rows_stats.read;
table_stats->rows_stats.updated+= rows_stats.updated;
table_stats->rows_stats.inserted+= rows_stats.inserted;
table_stats->rows_stats.deleted+= rows_stats.deleted;
table_stats->rows_stats.key_read_hit+= rows_stats.key_read_hit;
table_stats->rows_stats.key_read_miss+= rows_stats.key_read_miss;
changed= rows_stats.updated + rows_stats.inserted + rows_stats.deleted;
table_stats->rows_changed_x_indexes+= (changed *
(table->s->keys ? table->s->keys :
1));
rows_read= rows_changed= 0;
end:
mysql_mutex_unlock(&LOCK_global_table_stats);
reset:
bzero(&rows_stats, sizeof(rows_stats));
}
......@@ -8013,7 +8032,7 @@ int handler::ha_write_row(const uchar *buf)
MYSQL_INSERT_ROW_DONE(error);
if (likely(!error))
{
rows_changed++;
rows_stats.inserted++;
Log_func *log_func= Write_rows_log_event::binlog_row_logging_function;
error= binlog_log_row(0, buf, log_func);
......@@ -8064,7 +8083,7 @@ int handler::ha_update_row(const uchar *old_data, const uchar *new_data)
MYSQL_UPDATE_ROW_DONE(error);
if (likely(!error))
{
rows_changed++;
rows_stats.updated++;
Log_func *log_func= Update_rows_log_event::binlog_row_logging_function;
error= binlog_log_row(old_data, new_data, log_func);
......@@ -8140,7 +8159,7 @@ int handler::ha_delete_row(const uchar *buf)
MYSQL_DELETE_ROW_DONE(error);
if (likely(!error))
{
rows_changed++;
rows_stats.deleted++;
Log_func *log_func= Delete_rows_log_event::binlog_row_logging_function;
error= binlog_log_row(buf, 0, log_func);
......
......@@ -3325,9 +3325,7 @@ class handler :public Sql_alloc
inserter.
*/
/* Statistics variables */
ulonglong rows_read;
ulonglong rows_tmp_read;
ulonglong rows_changed;
struct rows_stats rows_stats;
/* One bigger than needed to avoid to test if key == MAX_KEY */
ulonglong index_rows_read[MAX_KEY+1];
ha_copy_info copy_info;
......@@ -3713,7 +3711,7 @@ class handler :public Sql_alloc
{ DBUG_ASSERT(false); return(false); }
void reset_statistics()
{
rows_read= rows_changed= rows_tmp_read= 0;
bzero(&rows_stats, sizeof(rows_stats));
bzero(index_rows_read, sizeof(index_rows_read));
bzero(&copy_info, sizeof(copy_info));
}
......@@ -4263,9 +4261,9 @@ class handler :public Sql_alloc
inline void update_rows_read()
{
if (likely(!internal_tmp_table))
rows_read++;
rows_stats.read++;
else
rows_tmp_read++;
rows_stats.tmp_read++;
}
inline void update_index_statistics()
{
......
......@@ -950,6 +950,7 @@ typedef struct system_status_var
ulong ha_read_first_count;
ulong ha_read_last_count;
ulong ha_read_key_count;
ulong ha_read_key_miss;
ulong ha_read_next_count;
ulong ha_read_prev_count;
ulong ha_read_retry_count;
......
......@@ -430,10 +430,7 @@ void init_user_stats(USER_STATS *user_stats,
ulonglong bytes_sent,
ulonglong binlog_bytes_written,
ha_rows rows_sent,
ha_rows rows_read,
ha_rows rows_inserted,
ha_rows rows_deleted,
ha_rows rows_updated,
rows_stats *rows_stats,
ulonglong select_commands,
ulonglong update_commands,
ulonglong other_commands,
......@@ -464,10 +461,7 @@ void init_user_stats(USER_STATS *user_stats,
user_stats->bytes_sent= bytes_sent;
user_stats->binlog_bytes_written= binlog_bytes_written;
user_stats->rows_sent= rows_sent;
user_stats->rows_read= rows_read;
user_stats->rows_inserted= rows_inserted;
user_stats->rows_deleted= rows_deleted;
user_stats->rows_updated= rows_updated;
user_stats->rows_stats= *rows_stats;
user_stats->select_commands= select_commands;
user_stats->update_commands= update_commands;
user_stats->other_commands= other_commands;
......@@ -576,6 +570,8 @@ static bool increment_count_by_name(const char *name, size_t name_length,
if (!(user_stats= (USER_STATS*) my_hash_search(users_or_clients, (uchar*) name,
name_length)))
{
struct rows_stats rows_stats;
bzero(&rows_stats, sizeof(rows_stats));
/* First connection for this user or client */
if (!(user_stats= ((USER_STATS*)
my_malloc(PSI_INSTRUMENT_ME, sizeof(USER_STATS),
......@@ -586,8 +582,8 @@ static bool increment_count_by_name(const char *name, size_t name_length,
0, 0, 0, // connections
0, 0, 0, // time
0, 0, 0, // bytes sent, received and written
0, 0, // rows sent and read
0, 0, 0, // rows inserted, deleted and updated
0,
&rows_stats,
0, 0, 0, // select, update and other commands
0, 0, // commit and rollback trans
thd->status_var.access_denied_errors,
......@@ -678,16 +674,22 @@ static void update_global_user_stats_with_user(THD *thd,
(thd->status_var.binlog_bytes_written -
thd->org_status_var.binlog_bytes_written);
/* We are not counting rows in internal temporary tables here ! */
user_stats->rows_read+= (thd->status_var.rows_read -
thd->org_status_var.rows_read);
user_stats->rows_sent+= (thd->status_var.rows_sent -
thd->org_status_var.rows_sent);
user_stats->rows_inserted+= (thd->status_var.ha_write_count -
thd->org_status_var.ha_write_count);
user_stats->rows_deleted+= (thd->status_var.ha_delete_count -
thd->org_status_var.ha_delete_count);
user_stats->rows_updated+= (thd->status_var.ha_update_count -
thd->org_status_var.ha_update_count);
user_stats->rows_sent+= (thd->status_var.rows_sent -
thd->org_status_var.rows_sent);
user_stats->rows_stats.read+= (thd->status_var.rows_read -
thd->org_status_var.rows_read);
user_stats->rows_stats.inserted+= (thd->status_var.ha_write_count -
thd->org_status_var.ha_write_count);
user_stats->rows_stats.deleted+= (thd->status_var.ha_delete_count -
thd->org_status_var.ha_delete_count);
user_stats->rows_stats.updated+= (thd->status_var.ha_update_count -
thd->org_status_var.ha_update_count);
user_stats->rows_stats.key_read_hit+= (thd->status_var.ha_read_key_count -
thd->org_status_var.ha_read_key_count -
(thd->status_var.ha_read_key_miss -
thd->org_status_var.ha_read_key_miss));
user_stats->rows_stats.key_read_miss+= (thd->status_var.ha_read_key_miss -
thd->org_status_var.ha_read_key_miss);
user_stats->select_commands+= thd->select_commands;
user_stats->update_commands+= thd->update_commands;
user_stats->other_commands+= thd->other_commands;
......
......@@ -337,6 +337,21 @@ typedef struct user_conn {
}
} USER_CONN;
/* Statistics used by user_stats */
struct rows_stats
{
ha_rows key_read_hit;
ha_rows key_read_miss;
ha_rows read;
ha_rows tmp_read;
ha_rows updated;
ha_rows inserted;
ha_rows deleted;
};
typedef struct st_user_stats
{
char user[MY_MAX(USERNAME_LENGTH, LIST_PROCESS_HOST_LEN) + 1];
......@@ -352,8 +367,8 @@ typedef struct st_user_stats
uint total_ssl_connections;
uint concurrent_connections;
time_t connected_time; // in seconds
ha_rows rows_read, rows_sent;
ha_rows rows_updated, rows_deleted, rows_inserted;
struct rows_stats rows_stats;
ha_rows rows_sent;
ulonglong bytes_received;
ulonglong bytes_sent;
ulonglong binlog_bytes_written;
......@@ -366,16 +381,18 @@ typedef struct st_user_stats
double cpu_time; // in seconds
} USER_STATS;
typedef struct st_table_stats
{
char table[NAME_LEN * 2 + 2]; // [db] + '\0' + [table] + '\0'
size_t table_name_length;
ulonglong rows_read, rows_changed;
struct rows_stats rows_stats;
ulonglong rows_changed_x_indexes;
/* Stores enum db_type, but forward declarations cannot be done */
int engine_type;
} TABLE_STATS;
typedef struct st_index_stats
{
// [db] + '\0' + [table] + '\0' + [index] + '\0'
......
......@@ -9,4 +9,9 @@ TABLE_NAME t1
ROWS_READ 1000
ROWS_CHANGED 1000
ROWS_CHANGED_X_INDEXES 1000
ROWS_INSERTED 1000
ROWS_UPDATED 0
ROWS_DELETED 0
KEY_READ_HITS 0
KEY_READ_MISSES 0
DROP TABLE t1;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment