mysql表类型查询示例详解
更新时间:2025年04月17日 11:06:47 作者:韶博雅
这篇文章主要介绍了mysql表类型查询示例详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
普通表
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;分区表
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;区分表
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;查出数据量
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;查出表行数
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;到此这篇关于mysql表类型查询的文章就介绍到这了,更多相关mysql表类型查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章
RedHat6.5/CentOS6.5安装Mysql5.7.20的教程详解
这篇文章主要介绍了RedHat6.5/CentOS6.5安装Mysql5.7.20的教程详解,非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-11-11
Windows10下mysql 8.0.16 安装配置方法图文教程
这篇文章主要为大家详细介绍了Windows10下mysql 8.0.16 安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-05-05
MySql 字符集不同导致 left join 慢查询的问题解决
当两个表的字符集不一样,在使用字符型字段进行表连接查询时,就需要特别注意下查询耗时是否符合预期,本文主要介绍了MySql 字符集不同导致 left join 慢查询的问题解决,感兴趣的可以了解一下2024-05-05
解决mysql报错:Data source rejected establishment of connect
这篇文章主要给大家介绍了关于如何解决mysql报错:Data source rejected establishment of connection, message from server: \"Too many connectio的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2023-02-02
Mysql外键设置中的CASCADE、NO ACTION、RESTRICT、SET NULL
本文主要介绍了Mysql外键设置中的CASCADE、NO ACTION、RESTRICT、SET NULL,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-07-07


最新评论