Database management 一定会被问及很多事情,并且自己也得平时也的预先了解一些事情以防止某些可以预防的事情。今天就总结一些常用的query sql,方便应付各种 asking.
1 统计用户表中的字段,(被问及一个表有多少字段,应对有没有变态的字段类型或长度,那个有唯一主键等等)
SELECT
INFORMATION_SCHEMA.COLUMNS.TABLE_SCHEMA ,
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME,
INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME,
INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION,
INFORMATION_SCHEMA.COLUMNS.DATA_TYPE,
INFORMATION_SCHEMA.COLUMNS.COLUMN_KEY,
INFORMATION_SCHEMA.COLUMNS.EXTRA
FROM INFORMATION_SCHEMA.COLUMNS
WHERE INFORMATION_SCHEMA.COLUMNS.TABLE_SCHEMA not in ('information_schema','mysql','sys','performance_schema');
2 查看用户的数据库中是否有外键的使用
SELECT
INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_NAME,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE.COLUMN_NAME,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE.CONSTRAINT_NAME, INFORMATION_SCHEMA.KEY_COLUMN_USAGE.REFERENCED_TABLE_NAME,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE.REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where INFORMATION_SCHEMA.KEY_COLUMN_USAGE.REFERENCED_TABLE_NAME is not null
3 查看当前数据库instance 所有数据库的容量
SELECT table_schema "Database Name",
SUM( data_length + index_length)/1024/1024 "Database Size (MB)"
FROM information_schema.TABLES
group by table_schema;
4 查看 (employees) 数据库中所有的表的占用的空间
SELECT table_name "Table Name", table_rows "Rows Count",
round(((data_length + index_length)/1024/1024),2) "Table Size (MB)"
FROM information_schema.TABLES WHERE table_schema = "employees"
5 查看当前的客户端连接数据库情况
select host, current_connections,statements from sys.host_summary;
6 查看当前连接到数据库中,运行次数最多的SQL
select * from sys.statement_analysis order by exec_count desc limit 10;
7 查看当前系统I/O 中使用最量最大的前十文件
select * from sys.io_global_by_file_by_bytes limit 10;
8 查看平均延迟比较严重的
select * from sys.statement_analysis order by avg_latency desc limit 1
9 查看平均延迟比较严重的语句 TOP 10
select * from sys.statement_analysis order by avg_latency desc limit
10 查看系统中从未使用过的索引
select * from sys.schema_unused_indexes;
11 查看系统中冗余的索引
select table_schema,table_name,redundant_index_name,redundant_index_columns,dominant_index_name,dominant_index_columns from sys.schema_redundant_indexes;
12 那些表使用了临时表
select db, query, tmp_tables,tmp_disk_tables from sys.statement_analysis where tmp_tables>0 or tmp_disk_tables >0 order by(tmp_tables+tmp_disk_tables) desc limit 20;
13 那些表占用的buffer pool 最多
select * from sys.innodb_buffer_stats_by_table order by pages desc limit 10
14 查看每个链接占用的内存
select b.user, current_count_used,current_allocated, current_avg_alloc, current_max_alloc,total_allocated,current_statement from sys.memory_by_thread_by_current_bytes a,sys.session b where a.thread_id = b.thd_id;
15 查看MYSQL 内部的连接线程的数量
select user, count(*) from sys.processlist group by user;
16 查看各个表自增ID使用情况
select * from sys.schema_auto_increment_columns limit 10;
17 查看当前的数据库中是否有全表扫描的语句,并反映相关没有使用索引的具体情况
select query,db,exec_count,total_latency,no_index_used_count,no_good_index_used_count,last_seen from sys.statements_with_full_table_scans;
18 查看当前系统中最慢的10条执行的 SQL
select query,db,full_scan,exec_count,avg_latency,rows_sent from sys.statements_with_runtimes_in_95th_percentile;
19 系统中那些事件延迟并根据平均延迟时间排序
select * from sys.wait_classes_global_by_latency;
20 当前如果有锁的情况下,会在此表显示锁信息
select * from sys.schema_table_lock_waits;