平常工做或學習過程當中,會經常使用到某些SQL語句,又不太容易記憶的。建議你們多多整理記錄下這些經常使用的SQL,這樣後續用到會方便不少。我在工做及學習過程當中也整理了下我的經常使用的SQL,如今借雲棲社區這個平臺分享給你們。可能有些SQL你還不經常使用,但仍是但願有所幫助,說不定未來哪天有需求就能用到。mysql
原文:https://www.toutiao.com/a6799...sql
注:下文分享的SQL適用於MySQL 5.7 版本,低版本可能稍許不一樣。有些SQL可能執行須要較高權限。都在阿里雲RDS數據庫中使用過,沒問題了。數據庫
查看實例參數 例如:函數
show variables like '%innodb%'; show global variables like '%innodb%';
查看實例狀態,例如:學習
show status like 'uptime%'; show global status like 'connection%';
查看數據庫連接:阿里雲
show processlist; show full processlist;
查詢某個表的結構:code
show create table tb_name;
查詢某個表的詳細字段信息:orm
show full columns from tb_name;
查詢某個表的所有索引信息:索引
show index from tb_name;
查詢某個庫以cd開頭的表:ssl
show tables like 'cd%';
查詢某個庫中的全部視圖:
show table status where comment='view';
查詢某個用戶的權限:
show grants for 'test_user'@'%';
這裏先介紹下CONCAT函數:在MySQL中 CONCAT()函數用於將多個字符串鏈接成一個字符串,
利用此函數咱們能夠將原來一步沒法獲得的sql拼接出來,後面部分語句有用到該函數。
當拼接字符串中出現''時 需使用轉義符
查看全部用戶名:
SELECT DISTINCT CONCAT( 'User: '', user, ''@'', host, '';' ) AS QUERY FROM mysql.user;
查看用戶詳細信息:
SELECT user, host, authentication_string, password_expired, password_lifetime, password_last_changed, account_locked FROM mysql.user;
下面列舉SQL只是拼接出kill 連接的語句,若想執行 直接將結果複製執行便可。
殺掉空閒時間大於2000s的連接:
SELECT concat( 'KILL ', id, ';' ) FROM information_schema.PROCESSLIST WHERE Command = 'Sleep' AND TIME > 2000;
殺掉處於某狀態的連接:
SELECT concat( 'KILL ', id, ';' ) FROM information_schema.PROCESSLIST WHERE STATE LIKE 'Creating sort index';
殺掉某個用戶的連接:
SELECT concat( 'KILL ', id, ';' ) FROM information_schema.PROCESSLIST WHERE where user='root';
下面列舉SQL只是拼接出kill 連接的語句,若想執行 直接將結果複製執行便可。
殺掉空閒時間大於2000s的連接:
SELECT concat( 'KILL ', id, ';' ) FROM information_schema.PROCESSLIST WHERE Command = 'Sleep' AND TIME > 2000;
殺掉處於某狀態的連接:
SELECT concat( 'KILL ', id, ';' ) FROM information_schema.PROCESSLIST WHERE STATE LIKE 'Creating sort index';
殺掉某個用戶的連接:
SELECT concat( 'KILL ', id, ';' ) FROM information_schema.PROCESSLIST WHERE where user='root';
查看整個實例佔用空間大小:
SELECT concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'MB' ) AS data_length_MB, concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'MB' ) AS index_length_MB FROM information_schema.TABLES;
查看各個庫佔用大小:
SELECT TABLE_SCHEMA, concat( TRUNCATE ( sum( data_length )/ 1024 / 1024, 2 ), ' MB' ) AS data_size, concat( TRUNCATE ( sum( index_length )/ 1024 / 1024, 2 ), 'MB' ) AS index_size FROM information_schema.TABLES GROUP BY TABLE_SCHEMA;
查看單個庫佔用空間大小:
SELECT concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'MB' ) AS data_length_MB, concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'MB' ) AS index_length_MB FROM information_schema.TABLES WHERE table_schema = 'test_db';
查看單個表佔用空間大小:
SELECT concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'MB' ) AS data_length_MB, concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'MB' ) AS index_length_MB FROM information_schema.TABLES WHERE table_schema = 'test_db' AND table_name = 'tbname';
查看某個庫下全部表的碎片狀況:
SELECT t.TABLE_SCHEMA, t.TABLE_NAME, t.TABLE_ROWS, concat( round( t.DATA_LENGTH / 1024 / 1024, 2 ), 'M' ) AS size, t.INDEX_LENGTH, concat( round( t.DATA_FREE / 1024 / 1024, 2 ), 'M' ) AS datafree FROM information_schema.TABLES t WHERE t.TABLE_SCHEMA = 'test_db' ORDER BY datafree DESC;
收縮表,減小碎片:
alter table tb_name engine = innodb; optimize table tb_name;
查看某個庫下全部表的碎片狀況:
SELECT t.TABLE_SCHEMA, t.TABLE_NAME, t.TABLE_ROWS, concat( round( t.DATA_LENGTH / 1024 / 1024, 2 ), 'M' ) AS size, t.INDEX_LENGTH, concat( round( t.DATA_FREE / 1024 / 1024, 2 ), 'M' ) AS datafree FROM information_schema.TABLES t WHERE t.TABLE_SCHEMA = 'test_db' ORDER BY datafree DESC;
收縮表,減小碎片:
alter table tb_name engine = innodb; optimize table tb_name;