mysql-oracle查询所有表的记录数
此前做一个mysql到oracle的数据迁移,做完之后为了简单的判断数据是否有丢失,就使用了这个数据量的对比方法。但是发现mysql和oracle查出来的数据量竟然不一致,吓死了,还以自己写的程序有bug,后来仔细对比了一张表的记录,数据量又是一致的,反而用mysql的那个table_rows的方法查出来对应表的数据少那么几十条,然后我就想到还是别用mysql自带的东西了,用原生的语句来查一把,下面分享一下我的这段经历吧。
mysql数据量查询
首先贴出我的数据表。
在网上搜索了很多答案,基本上如出一辙。看下列结果,很明显,不正确。
select table_schema,table_name,table_rows
from information_schema.tables t
WHERE t.TABLE_SCHEMA = "test"
order by table_rows desc;
这里我就开始怀疑mysql提供的这个东西有问题,于是就用原始的方法:写一个存储过程,先查询出所有的表,然后使用count(*)查询这些表的记录,接着把这张表以及对应记录数存到一张temp表里面,最后再把这张temp表的数据全部查出来。
CREATE PROCEDURE `tables_row`()
BEGIN
DECLARE l_last_row INT DEFAULT 0;
declare tnames VARCHAR(200);# 临时表名
DECLARE count2 int;# 临时数据量
declare fetchSeqCursor cursor for select table_name from information_schema.TABLES where table_schema="test" order by table_name DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_last_row=1;
create temporary table if not exists tmpTable
(
tableName varchar(255) primary key,
tablerow int
);
truncate TABLE tmpTable;
OPEN fetchSeqCursor;
FETCH fetchSeqCursor into tnames;
while l_last_row != 1 do
set @sql1 = CONCAT("SELECT count(*) into @count_tmp from " ,tnames);
prepare stmt from @sql1;
EXECUTE stmt ;
set count2 = @count_tmp;
SET @sql2 = CONCAT("INSERT INTO tmpTable VALUES(",""",tnames,""",",",count2,")");
PREPARE stmt2 from @sql2;
EXECUTE stmt2;
FETCH fetchSeqCursor into tnames;
end WHILE;
CLOSE fetchSeqCursor;
SELECT * from tmpTable order by tableName DESC;
END
oracle数据量查询
oracle查询数据库的数据量就相对比较简单了,可以直接使用oracle内置的方法
select table_name,num_rows from user_tables
ORDER BY num_rows DESC;
如果这个语句报错,无法找到num_rows,可以添加如下函数
CREATE OR REPLACE
FUNCTION count_rows(table_name in varchar2) RETURN NUMBER
AS
num_rows number;
stmt varchar2(2000);
BEGIN
stmt := "select count(*) from "||table_name;
execute immediate stmt into num_rows;
return num_rows;
END;
然后再调用这个函数
select table_name,count_rows(table_name) from user_tables
ORDER BY num_rows DESC;
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了