ToB企服应用市场:ToB评测及商务社交产业平台

标题: 【MySQL】MySQL查数据库中每个表的数据量 [打印本页]

作者: 何小豆儿在此    时间: 2024-9-18 22:58
标题: 【MySQL】MySQL查数据库中每个表的数据量

一、方法1,数据字典表

  1. /* Mysql查数据库中每个表的数据量 */
  2. SELECT table_schema
  3.       ,table_name
  4.       ,table_rows
  5.   FROM information_schema.tables
  6. WHERE table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys');
复制代码
二、方法2,存过,循环count()每张表

  1. drop PROCEDURE if exists GetAllTableCounts;
  2. CREATE PROCEDURE GetAllTableCounts()
  3. BEGIN
  4.     DECLARE done INT DEFAULT FALSE;
  5.     DECLARE aDatabase CHAR(64);
  6.     DECLARE aTable CHAR(64);
  7.     DECLARE dateString VARCHAR(23);
  8.     DECLARE cur1 CURSOR FOR SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys');
  9.     DECLARE cur2 CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema = aDatabase;
  10.     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  11.     set @sqlString = 'select ''数据库名'' as dbname,''表名'' as tablename,''数量'' as count ';
  12.     OPEN cur1;
  13.     read_loop: LOOP
  14.         FETCH cur1 INTO aDatabase;
  15.         IF done THEN
  16.             LEAVE read_loop;
  17.         END IF;
  18.         OPEN cur2;
  19.         table_loop: LOOP
  20.             FETCH cur2 INTO aTable;
  21.             IF done THEN
  22.                 LEAVE table_loop;
  23.             END IF;
  24.             
  25.             set @sqlString = CONCAT(@sqlString ,' union all select ''',aDatabase,''' as dbname, ''',aTable,''' as tablename, count(*) from ',aDatabase,'.',aTable);
  26.             
  27.         END LOOP table_loop;
  28.         CLOSE cur2;
  29.         SET done = FALSE;
  30.     END LOOP read_loop;
  31.    
  32.     CLOSE cur1;
  33.     select @sqlString;
  34.    
  35.     PREPARE stmt FROM @sqlString;
  36.     execute stmt;
  37.     DEALLOCATE PREPARE stmt;
  38.    
  39. END;
  40. -- 调用方式1
  41. -- call GetAllTableCounts();
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4