mysql表类型查询

普通表

bash 复制代码
SELECT 
    table_schema AS database_name,
    table_name
FROM 
    information_schema.tables
WHERE 
    table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
    AND table_type = 'BASE TABLE'
    AND table_name NOT IN (
        SELECT DISTINCT table_name 
        FROM information_schema.partitions 
        WHERE partition_name IS NOT NULL
    )
ORDER BY 
    table_schema, table_name;

分区表

bash 复制代码
SELECT 
    p.table_schema AS database_name,
    p.table_name,
    GROUP_CONCAT(p.partition_name ORDER BY p.partition_ordinal_position) AS partitions,
    p.partition_method,
    p.partition_expression
FROM 
    information_schema.partitions p
WHERE 
    p.table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
    AND p.partition_name IS NOT NULL
GROUP BY 
    p.table_schema, p.table_name, p.partition_method, p.partition_expression
ORDER BY 
    p.table_schema, p.table_name;

区分表

bash 复制代码
SELECT 
    t.table_schema AS database_name,
    t.table_name,
    CASE 
        WHEN p.table_name IS NULL THEN '普通表'
        ELSE '分区表'
    END AS table_type,
    p.partition_method,
    p.partition_expression
FROM 
    information_schema.tables t
LEFT JOIN (
    SELECT DISTINCT 
        table_schema, 
        table_name,
        partition_method,
        partition_expression
    FROM 
        information_schema.partitions 
    WHERE 
        partition_name IS NOT NULL
) p ON t.table_schema = p.table_schema AND t.table_name = p.table_name
WHERE 
    t.table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
    AND t.table_type = 'BASE TABLE'
ORDER BY 
    t.table_schema, t.table_name;
	

查出数据量

bash 复制代码
SELECT 
    t.table_schema AS '数据库名',
    t.table_name AS '表名',
    CASE 
        WHEN p.table_name IS NULL THEN '普通表'
        ELSE CONCAT('分区表(', p.partition_method, ')')
    END AS '表类型',
    t.table_rows AS '数据行数(估算)',
    CONCAT(ROUND(t.data_length / (1024 * 1024), 2), ' MB') AS '数据大小',
    CONCAT(ROUND(t.index_length / (1024 * 1024), 2), ' MB') AS '索引大小',
    CONCAT(ROUND((t.data_length + t.index_length) / (1024 * 1024), 2), ' MB') AS '总大小',
    p.partition_expression AS '分区键'
FROM 
    information_schema.tables t
LEFT JOIN (
    SELECT DISTINCT 
        table_schema, 
        table_name,
        partition_method,
        partition_expression
    FROM 
        information_schema.partitions 
    WHERE 
        partition_name IS NOT NULL
) p ON t.table_schema = p.table_schema AND t.table_name = p.table_name
WHERE 
    t.table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
    AND t.table_type = 'BASE TABLE'
ORDER BY 
    t.table_schema, 
    CASE WHEN p.table_name IS NULL THEN 0 ELSE 1 END,  -- 普通表在前
    t.table_name;



SELECT 
    t.table_schema AS '数据库',
    t.table_name AS '表名',
    CASE 
        WHEN p.partition_method IS NULL THEN '普通表'
        ELSE CONCAT('分区表(', p.partition_method, ')')
    END AS '表类型',
    t.table_rows AS '估算行数',
    CONCAT(ROUND(t.data_length/1024/1024, 2), ' MB') AS '数据大小',
    p.partition_expression AS '分区键'
FROM 
    information_schema.tables t
LEFT JOIN (
    SELECT 
        table_schema, 
        table_name,
        partition_method,
        partition_expression
    FROM 
        information_schema.partitions
    WHERE 
        partition_name IS NOT NULL
    GROUP BY 
        table_schema, table_name, partition_method, partition_expression
) p ON t.table_schema = p.table_schema AND t.table_name = p.table_name
WHERE 
    t.table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
    AND t.table_type = 'BASE TABLE'
ORDER BY 
    t.table_schema, t.table_name;

查出表行数

bash 复制代码
SELECT 
    t.table_schema AS '数据库',
    t.table_name AS '表名',
    CASE 
        WHEN p.partition_method IS NULL THEN '普通表'
        ELSE CONCAT('分区表(', p.partition_method, ')')
    END AS '表类型',
    t.table_rows AS '估算行数',
    p.partition_expression AS '分区键'
FROM 
    information_schema.tables t
LEFT JOIN (
    SELECT DISTINCT 
        table_schema, 
        table_name,
        partition_method,
        partition_expression
    FROM 
        information_schema.partitions
    WHERE 
        partition_name IS NOT NULL
) p ON t.table_schema = p.table_schema AND t.table_name = p.table_name
WHERE 
    t.table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
    AND t.table_type = 'BASE TABLE'
ORDER BY 
    t.table_schema, t.table_name;
相关推荐
东坡肘子1 天前
从开放平台到受控生态:谷歌宣布 Android 开发者验证政策 | 肘子的 Swift 周报 #0101
android·swiftui·swift
脚踏实地,坚持不懈!1 天前
ANDROID,Jetpack Compose, 贪吃蛇小游戏Demo
android
Just_Paranoid1 天前
【JobScheduler】Android 后台任务调度的核心组件指南
android·alarmmanager·jobscheduler·workmanager
小蒜学长1 天前
基于Spring Boot的火灾报警系统的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
我命由我123451 天前
Android 开发 - 一些画板第三方库(DrawBoard、FingerPaintView、PaletteLib)
android·java·java-ee·android studio·安卓·android-studio·android runtime
福赖1 天前
《MySQL基础——C 语言链接》
c语言·数据库·mysql
KIDAKN1 天前
Redis 分布式锁
数据库·redis·分布式
程序员的世界你不懂1 天前
【Flask】测试平台开发,工具模块开发 第二十二篇
android·python·flask
程序新视界1 天前
如何为MySQL中的JSON字段设置索引
数据库·mysql
Ultipa1 天前
查询语言的进化:SQL之后,为什么是GQL?数据世界正在改变
数据库·sql·图数据库·gql