經常使用SQL語句分享

前言: mysql

平常工做或學習過程當中,咱們可能會常常用到某些SQL,建議你們多多整理記錄下這些經常使用的SQL,這樣後續用到會方便不少。筆者在工做及學習過程當中也整理了下我的經常使用的SQL,如今分享給你!可能有些SQL你還不經常使用,但仍是但願對你有所幫助,說不定某日有需求就能夠用到。sql

注:下文分享的SQL適用於MySQL 5.7 版本,低版本可能稍許不一樣。有些SQL可能執行須要較高權限。數據庫

1.show相關語句

# 查看實例參數 例如:
show variables like '%innodb%';
show global variables like '%innodb%';

# 查看實例狀態,例如:
show status like 'uptime%';
show global status like 'connection%';

# 查看數據庫連接:
show processlist;
show full processlist;

# 查詢某個表的結構:
show create table tb_name;

# 查詢某個表的詳細字段信息:
show full columns from tb_name;

# 查詢某個表的所有索引信息:
show index from tb_name;

# 查詢某個庫以cd開頭的表:
show tables like 'cd%';

# 查詢某個庫中的全部視圖:
show table status where comment='view';

# 查詢某個用戶的權限:
show grants for 'test_user'@'%';

2.查看帳戶相關信息

# 這裏先介紹下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;

3.KILL數據庫連接

# 下面列舉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';

4.拼接建立數據庫或用戶語句

# 拼接建立數據庫語句(排除系統庫):
SELECT
    CONCAT(
        'create database ',
        '`',
    SCHEMA_NAME,
    '`',
    ' DEFAULT CHARACTER SET ',
    DEFAULT_CHARACTER_SET_NAME,
        ';'
    ) AS CreateDatabaseQuery
FROM
    information_schema.SCHEMATA
WHERE
    SCHEMA_NAME NOT IN (
        'information_schema',
        'performance_schema',
        'mysql',
        'sys'
    );

# 拼接建立用戶語句(排除系統用戶):
SELECT
    CONCAT(
        'create user \'',
    user,
    '\'@\'',
    Host,
    '\''
    ' IDENTIFIED BY PASSWORD \'',
    authentication_string,
        '\';'
    ) AS CreateUserQuery
FROM
    mysql.`user`
WHERE
    `User` NOT IN (
        'root',
        'mysql.session',
        'mysql.sys'
    );
# 有密碼字符串哦 在其餘實例執行 可直接建立出與本實例相同密碼的用戶。

5.查看庫或表大小

# 查看整個實例佔用空間大小:
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';

6.查看錶碎片及收縮語句

# 查看某個庫下全部表的碎片狀況:
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;

7.查找無主鍵表

# 查找某一個庫無主鍵表:
SELECT
table_schema,
table_name
FROM
    information_schema.`TABLES`
WHERE
    table_schema = 'test_db'
AND TABLE_NAME NOT IN (
    SELECT
        table_name
    FROM
        information_schema.table_constraints t
    JOIN information_schema.key_column_usage k USING (
        constraint_name,
        table_schema,
        table_name
    )
    WHERE
        t.constraint_type = 'PRIMARY KEY'
    AND t.table_schema = 'test_db'
);

# 查找除系統庫外 無主鍵表:
SELECT
    t1.table_schema,
    t1.table_name
FROM
    information_schema.`TABLES` t1
LEFT OUTER JOIN information_schema.TABLE_CONSTRAINTS t2 ON t1.table_schema = t2.TABLE_SCHEMA
AND t1.table_name = t2.TABLE_NAME
AND t2.CONSTRAINT_NAME IN ('PRIMARY')
WHERE
    t2.table_name IS NULL
AND t1.TABLE_SCHEMA NOT IN (
    'information_schema',
    'performance_schema',
    'mysql',
    'sys'
) ;

總結: session

但願這些SQL語句能對你有所幫助,能夠收藏一下,說不定某次就用到了呢!原創不易,感謝你們支持。ide

gongzhonghao

相關文章
相關標籤/搜索