如何计算mysql中的列数和行数

时间:2018-05-15 10:08:15

标签: mysql

我有一个要求,我需要计算给定数据库的列数,行数和每个表的大小。

下面的查询没有给出确切的结果,

SELECT t1.table_name, t1. table_rows,COUNT(t2.table_name) AS table_colums, t1. data_length 
FROM INFORMATION_SCHEMA.TABLES t1 
JOIN (SELECT table_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='my_db_name') t2 
ON t1.table_name=t2.table_name 
WHERE t1.TABLE_SCHEMA = 'my_db_name';

任何线索?

3 个答案:

答案 0 :(得分:2)

计算行数:

SELECT COUNT(*) FROM yourtable;

计算列:

SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'yourtable'

如果你的意思是每个表的大小大小,以MB为单位,那么:

SELECT 
    table_name AS `Table`, 
    round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
WHERE table_schema = "yourdatabase"
    AND table_name = "yourtable";

答案 1 :(得分:0)

此查询解决了我的问题。

\n54766392632990,178.32.243.13,wfsdsfsdfs23432,\n54766393632990,178.32.243.13,

答案 2 :(得分:0)

这可能会为您提供所需的结果

SELECT t1.table_name
,round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB`
,(select count(*) from information_schema.columns c where c.table_schema = t1.table_schema and c.table_name = t1.table_name) as cols
,t1.table_rows as rows
FROM INFORMATION_SCHEMA.TABLES t1 
WHERE t1.table_schema = 'my_db_name'