阿⾥云数据库常⽤SQL语句⼤全
⽇常⼯作或学习过程中,会常⽤到某些SQL语句,⼜不太容易记忆的。建议⼤家多多整理记录下这些常⽤的SQL,这样后续⽤到会⽅便很多。⽼魏在⼯作及学习过程中也整理了下个⼈常⽤的SQL,现在借云栖社区这个平台分享给⼤家。可能有些SQL你还不常⽤,但还是希望有所帮助,说不定将来哪天有需求就能⽤到。
注:下⽂分享的SQL适⽤于MySQL 5.7 版本,低版本可能稍许不同。有些SQL可能执⾏需要较⾼权限。都在阿⾥云RDS数据库中使⽤过没问题。
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.拼接创建数据库或⽤户语句
下⾯列举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’;
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;
查看单个库占⽤空间⼤⼩:
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
sql查询语句实例大全WHERE
t.TABLE_SCHEMA = ‘test_db’
ORDER BY
datafree DESC;
收缩表,减少碎⽚:
alter table tb_name engine = innodb;
optimize table tb_name;
7.查⽆主键表
查看某个库下所有表的碎⽚情况:
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;
更多参阅
希望这些SQL语句能对你有所帮助,可以收藏⼀下,说不定某次就⽤到了呢!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论