8种数据库获取表行数的方法

数据库,获取,行数,方法 · 浏览次数 : 98

小编点评

**Oracle数据库** ```sql SELECT x.table_name AS 表名, x.表行数, x.表列数, y.表大小 AS 表大小单位MB FROM ( SELECT b.table_name, a.num_rows AS 表行数, b.count1 AS 表列数 FROM user_tables a INNER JOIN ( SELECT table_name, count( column_name ) AS count1 FROM user_tab_columns GROUP BY table_name ) b ON a.table_name = b.table_name ORDER BY b.table_name ) x LEFT outer JOIN ( SELECT sum( tablesize ) AS 表大小, tablename FROM ( SELECT sum( C.bytes ) / 1024 / 1024 AS tablesize, C.table_name AS tablename FROM ( SELECT sum( A.table_name, B.bytes FROM user_lobs A, user_extents B WHERE A.segment_name = B.segment_name ) C GROUP BY C.table_name UNION ALL SELECT sum( bytes ) / 1024 / 1024 AS tablesize, segment_name AS tablename FROM user_extents WHERE segment_type = 'TABLE' GROUP BY segment_name ) GROUP BY tablename ) GROUP BY tablename ) y ON x.table_name = y.tablename ORDER BY y.表大小 desc2. ``` **神通数据库** ```sql CREATE TABLE table_num ( table_name VARCHAR2(255), table_num NUMBER ); CREATE OR REPLACE PROCEDURE insert_table_num IS BEGIN FOR t IN (SELECT table_name FROM user_tables) LOOP EXECUTE IMMEDIATE 'INSERT INTO table_num (table_name, table_num) SELECT ''' || t.table_name || ''', COUNT(*) FROM ' || t.table_name; END LOOP; END;exec insert_table_num; SELECT * FROM table_num ORDER BY table_num DESC ``` ** 达梦数据库** ```sql SELECT T.TABLESPACE_NAME, T.OWNER, T.TABLE_NAME, TABLE_ROWCOUNT(T.OWNER, T.TABLE_NAME) AS TABLE_ROWCOUNT FROM DBA_TABLES T WHERE T.OWNER IN ('YourSchemas_name****') ORDER BY TABLE_ROWCOUNT DESC ``` ** MySQL 数据库** ```sql SELECT t.name AS TableName, SUM(p.rows) AS RowCounts FROM sys.tables t INNER JOIN sys.partitions p ON t.object_id = p.object_id WHERE t.is_ms_shipped = 0 AND p.index_id IN (0,1) GROUP BY t.name ORDER BY RowCounts DESC ``` ** PG 数据库** ```sql SELECT relname, reltuples FROM pg_class LEFT JOIN pg_namespace N ON ( N.oid = CLS.relnamespace ) WHERE nspname NOT IN ( 'pg_catalog', 'information_schema' ) AND relkind = 'r' ORDER BY reltuples DESC ``` ** SQL Server 数据库** ```sql SELECT t.name AS TableName, SUM(p.rows) AS RowCounts FROM sys.tables t INNER JOIN sys.partitions p ON t.object_id = p.object_id WHERE t.is_ms_shipped = 0 AND p.index_id IN (0,1) GROUP BY t.name ORDER BY RowCounts DESC ```

正文

1. Oracle数据库:

SELECT
    x.table_name AS 表名,
    x.表行数,
    x.表列数,
    y.表大小 AS 表大小单位MB 
FROM
    (
    SELECT
        b.table_name,
        a.num_rows AS 表行数,
        b.count1 AS 表列数 
    FROM
        user_tables a
        INNER JOIN ( SELECT table_name, count( column_name ) AS count1 FROM user_tab_columns GROUP BY table_name ) b ON a.table_name = b.table_name 
    ORDER BY
        b.table_name 
    ) x LEFT outer
    JOIN (
    SELECT
        sum( tablesize ) AS 表大小,
        tablename 
    FROM
        (
        SELECT
            sum( C.bytes ) / 1024 / 1024 AS tablesize,
            C.table_name AS tablename 
        FROM
            ( SELECT A.table_name, B.bytes FROM user_lobs A, user_extents B WHERE A.segment_name = B.segment_name ) C 
        GROUP BY
            C.table_name UNION ALL
        SELECT
            sum( bytes ) / 1024 / 1024 AS tablesize,
            segment_name AS tablename 
        FROM
            user_extents 
        WHERE
            segment_type = 'TABLE' 
        GROUP BY
            segment_name 
        ) 
    GROUP BY
        tablename 
    ORDER BY
        1 DESC 
    ) y ON x.table_name = y.tablename 
ORDER BY
    y.表大小 desc

2. 神通数据库:

CREATE TABLE table_num (
  table_name VARCHAR2(255),
  table_num NUMBER
);

CREATE OR REPLACE PROCEDURE insert_table_num IS
BEGIN
  FOR t IN (SELECT table_name FROM user_tables) LOOP
  BEGIN
    EXECUTE IMMEDIATE 'INSERT INTO table_num (table_name, table_num)
                      SELECT ''' || t.table_name || ''', COUNT(*) FROM ' || t.table_name;
   EXCEPTION
       when others then 
       null ;
   END;
  END LOOP;
END;

exec insert_table_num;

SELECT * FROM table_num ORDER BY table_num DESC 

3. 达梦数据库

SELECT T.TABLESPACE_NAME,
       T.OWNER,
       T.TABLE_NAME,
       TABLE_ROWCOUNT(T.OWNER, T.TABLE_NAME)                            AS TABLE_ROWCOUNT
FROM DBA_TABLES T
WHERE T.OWNER IN ('YourSchemas_name****') order by TABLE_ROWCOUNT desc


4. MySQL数据库

SELECT
	table_name,
	table_rows 
FROM
	information_schema.TABLES 
WHERE
	table_schema = 'YourSchemas_name' 
ORDER BY
	table_rows DESC

5. PG数据库

SELECT
    relname,
    reltuples
FROM
    pg_class
    CLS LEFT JOIN pg_namespace N ON ( N.oid = CLS.relnamespace )
WHERE
    nspname NOT IN ( 'pg_catalog', 'information_schema' )
    AND relkind = 'r' 
ORDER BY
reltuples DESC;

6. SQLSERVER数据库

SELECT 
    t.name AS TableName,
    SUM(p.rows) AS RowCounts
FROM 
    sys.tables t
INNER JOIN 
    sys.partitions p ON t.object_id = p.object_id
WHERE 
    t.is_ms_shipped = 0
    AND p.index_id IN (0,1)
GROUP BY 
    t.name
ORDER BY 
    RowCounts DESC;

7. 人大金仓数据库-瀚高数据库 应该都可以使用PG的语法

SELECT
    relname,
    reltuples
FROM
    pg_class
    CLS LEFT JOIN pg_namespace N ON ( N.oid = CLS.relnamespace )
WHERE
    nspname NOT IN ( 'pg_catalog', 'information_schema' )
    AND relkind = 'r' 
ORDER BY
reltuples DESC;

与8种数据库获取表行数的方法相似的内容:

8种数据库获取表行数的方法

## 1. Oracle数据库: ``` SELECT x.table_name AS 表名, x.表行数, x.表列数, y.表大小 AS 表大小单位MB FROM ( SELECT b.table_name, a.num_rows AS 表行数, b.count1 AS 表列数 FROM use

redis系列02---缓存过期、穿透、击穿、雪崩

一、缓存过期 问题产生的原由: 内存空间有限,给缓存设置过期时间,但有些键值运气比较好,每次都没有被我的随机算法选中,每次都能幸免于难,这可不行,这些长时间过期的数据一直霸占着不少的内存空间! 解决方案: redis提供8种策略供应用程序选择,用于我遇到内存不足时该如何决策: * noevictio

很多人讲不明白HTTPS,但是我能

很多人讲不明白HTTPS,但是我能 今天我们用问答的形式,来彻底弄明白HTTPS的过程 下面的问题都是 小明和小丽两个人通信为例 可以把小明想象成服务端,小丽想象成客户端 1. https是做什么用的? 答:数据安全传输用的。 2. 数据如何安全的传输? 答:把数据加密以后,再发送。 3. 用哪种加

[转帖]shell脚本字符串截取的8种方法

https://www.cnblogs.com/zwgblog/p/6031256.html 假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符。 1 echo ${var#*//} 其中 var 是变量名,# 号是运算符,*//

CSS 样式表引入的3种方式

CSS 样式表引入的3种方式

Libgdx游戏开发(4)——显示中文文字

原文: Libgdx游戏开发(4)——显示中文文字-Stars-One的杂货小窝 本文代码示例采用kotlin代码进行讲解,且需要有libgdx入门基础 这里主要介绍关于在Libgdx显示文字的2种方法 2种方法优缺点 BitmapFont 优势: 易于操作和使用,简单快速实现文本渲染。 资源消耗相

[转帖]【技术剖析】8. 相同版本 JVM 和 Java 应用,在 x86 和AArch64 平台性能相差30%,何故?

https://bbs.huaweicloud.com/forum/thread-168532-1-1.html 作者: 吴言 > 编者按:目前许多公司同时使用 x86 和 AArch64 2 种主流的服务器。这两种环境的算力相当,内存相同的情况下:相同版本的 JVM 和 Java 应用,相同的 J

OI-Wiki 学习笔记

算法基础 \(\text{Update: 2024 - 07 - 22}\) 复杂度 定义 衡量一个算法的快慢,一定要考虑数据规模的大小。 一般来说,数据规模越大,算法的用时就越长。 而在算法竞赛中,我们衡量一个算法的效率时,最重要的不是看它在某个数据规模下的用时,而是看它的用时随数据规模而增长的趋

【漏洞分析】Li.Fi攻击事件分析:缺乏关键参数检查的钻石协议

背景信息 2024 年 7 月 16日,Li.Fi 协议遭受黑客攻击,漏洞成因是钻石协议中 diamond 合约新添加的 facet 合约没有对参数进行检查,导致 call 函数任意执行。且 diamond 合约拥有用户的 approve,所以攻击者可以构造恶意参数对用户资金进行转移。 攻击交易ht

为什么StampedLock会导致CPU100%?

StampedLock 是 Java 8 引入的一种高级的锁机制,它位于 java.util.concurrent.locks 包中。与传统的读写锁(ReentrantReadWriteLock)相比,StampedLock 提供了更灵活和更高性能的锁解决方案,尤其适用于读操作远多于写操作的场景。