mysql一些常用查询语句

本文介绍了如何通过SQL查询在MySQL中获取执行中的进程列表,筛选特定操作,以及分析库和表的空间占用情况,包括数据、索引和总容量。

MYSQL

版本:8.0

查询正在执行表或者使用者的执行信息

select t1.* from (select * from information_schema.`PROCESSLIST` ) t1 
WHERE `INFO` like "%sp_aqc_org_patent_m%" 
ORDER BY `TIME` DESC;

select t1.* from (select * from information_schema.`PROCESSLIST` ) t1 
WHERE `HOST` like "%.43:%" 
ORDER BY `TIME` DESC;

查询使用空间

#查询mysql所有库的占用磁盘大小
SELECT 
TABLE_SCHEMA as '数据库',
concat(data_size,' GB') as '数据容量(GB)',
concat(index_size,' GB') as '索引容量(GB)',
concat(data_sum,' GB') as '合计'
FROM (
	SELECT 
	*,
	truncate(data_size + index_size, 2) as 'data_sum'
	from 
	(
		select 
		TABLE_SCHEMA,
		truncate(sum(data_length)/1024/1024/1024,2) as 'data_size',
		truncate(sum(index_length)/1024/1024/1024,2) as 'index_size'
		from information_schema.tables
		group by TABLE_SCHEMA
	) t1
	ORDER BY data_sum desc
) t2;


#查询mysql指定库下表的占用磁盘空间大小
SELECT 
data_schema as '库名',
data_table as '表名',
concat(data_size,' GB') as '数据容量(GB)',
concat(index_size,' GB') as '索引容量(GB)',
concat(data_sum,' GB') as '合计(GB)' 
FROM (
	SELECT 
	 data_schema,
	 data_table,
	 data_size,
	 index_size,
	 round(data_size + index_size, 2) as 'data_sum'
	FROM (
		SELECT 
				table_schema as `data_schema`, 
				table_name AS `data_table`, 
				round(((data_length) / 1024 / 1024 / 1024), 2) as `data_size`, 
				round(((index_length) / 1024 / 1024 / 1024), 2) as `index_size`
		FROM information_schema.tables 
		WHERE table_schema = 'qhdata_support_credit_baidu_m'
	) t1
	ORDER BY data_sum DESC
) t2;

;
#查询库表对应的信息总条数
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024/1024, 2)) as '数据容量(GB)',
sum(truncate(index_length/1024/1024/1024, 2)) as '索引容量(GB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;

CTE迭代计算股权穿透

投资关系表

 CREATE TABLE if NOT EXISTS db_test.tmp_org_invest_dtl (
 `rid` int NOT NULL AUTO_INCREMENT COMMENT '行ID',
 `org_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '投资单位id',
 `inv_org_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '被投资主体单位id',
 `invest` decimal(20, 8) DEFAULT NULL COMMENT '投资比重',
 `source_table` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '数据来源表',
 PRIMARY KEY (`rid`) USING BTREE,
 KEY `idx_01` (`org_id`) USING BTREE,
 KEY `idx_02` (`inv_org_id`) USING BTREE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='单位对外投资关系明细表';

设置CTE参数

SET group_concat_max_len = 100000;  -- 设置最大长度为10000字符
SET SESSION cte_max_recursion_depth = 1000; -- 最大递归深度,默认1000

CTE语句

WITH RECURSIVE Tree AS (
    -- 锚点成员:选择根节点
    SELECT 
        t1.org_id, 
        t1.inv_org_id, 
        t1.invest as invest, 
        t1.invest as path_invest,
        0 AS path_level, 
        CONCAT(
        CAST(t1.org_id AS CHAR(10000)), ';', CAST(t1.inv_org_id AS CHAR(10000)), 
        CONCAT('[', ifnull(t1.invest, ''), ',', t1.source_table COLLATE utf8mb4_general_ci, ']')
        ) as path
    FROM db_test.tmp_org_invest_dtl t1
    WHERE t1.org_id = "{org_id}"
        AND not isnull(t1.inv_org_id)
    UNION ALL
    -- 递归成员:加入子节点
    SELECT 
        t1.org_id, 
        t1.inv_org_id, 
        t1.invest as invest,
        ROUND(p.path_invest * t1.invest, 8) as path_invest,
        p.path_level + 1, 
        CONCAT(
        p.path, ';', t1.inv_org_id,
        CONCAT('[', ifnull(t1.invest, ''), ',', t1.source_table COLLATE utf8mb4_general_ci, ']')
        ) AS path
    FROM db_test.tmp_org_invest_dtl t1
    JOIN Tree p ON t1.org_id = p.inv_org_id
        WHERE not isnull(t1.inv_org_id)
        AND (
            ROUND(p.path_invest * t1.invest, 8) >= 0.00001 
            or isnull(p.path_invest) or isnull(t1.invest)
            )  -- 筛选有效数据
        AND not instr(p.path, t1.inv_org_id)  -- 防止循环处理
        AND p.path_level <= 9  -- 限制层级数量
)
SELECT org_id, inv_org_id, ROUND(invest * 100, 2) as invest_percent, ROUND(path_invest * 100, 2) as path_invest_percent, path_level, path
FROM Tree
ORDER BY path_level, org_id;

达梦数据库

版本:未知

查询模式列表

SELECT DISTINCT OWNER, OBJECT_NAME FROM ALL_OBJECTS WHERE OBJECT_TYPE = 'SCH'

创建用户

CREATE USER {user} IDENTIFIED BY "{password}";

创建模式

CREATE  SCHEMA {schema}  AUTHORIZATION {user_name};

提权

GRANT DBA TO {user};
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值