查询以查找表的rowcount

时间:2016-07-19 22:30:29

标签: mysql sql-server mongodb oracle11g database

我正在尝试查找查询以查找每个后端数据库中表的行和字节数:

I)MySQL
II)Oracle DB
III)SQL Server
IV)Mongo DB
V)Teradata

2 个答案:

答案 0 :(得分:2)

<强>的MySQL

Row count of single table

select count(*) from table_name;

Row Count of all tables

SELECT SUM(TABLE_ROWS) 
     FROM INFORMATION_SCHEMA.TABLES 
     WHERE TABLE_SCHEMA = 'database schema name';

Size
SELECT table_schema as `Database`, 
     table_name AS `Table`, 
     round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
ORDER BY (data_length + index_length) DESC;

尺寸的结果将是这样的

Database Table              Size in MB
sakai    sakai_realm_rl_fn  22.39
sonar    file_sources       8.56
sakai    sakai_site_tool    4.55
sakai    sakai_event        4.03
sonar    issues             3.75
sakai    sakai_site_page    3.03
sonar    project_measures   2.03

<强> MongoDB的 在mongoDB中,我们没有行和列,但一切都是文档,因此我们可以找到集合中的文档数量及其大小。

db.collection.stats() // example db.stackoverflow.stats(), where stackoverflow is my collection name

结果与此类似

{
        "ns" : "test.stackoverflow",
      "count" : 2,
        "size" : 224,
        "avgObjSize" : 112,
        "numExtents" : 1,
      "storageSize" : 8192,
        "lastExtentSize" : 8192,
        "paddingFactor" : 1,
        "paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
        "userFlags" : 1,
        "capped" : false,
        "nindexes" : 1,
        "totalIndexSize" : 8176,
        "indexSizes" : {
                "_id_" : 8176
        },
        "ok" : 1
}


db.stats(); // Will help us in finding the total documents in the database

Executing it in mongo shell, will a give a result like this

{
        "db" : "test",
        "collections" : 9,
        "objects" : 1000063,
        "avgObjSize" : 112.00058396321032,
        "dataSize" : 112007640,
        "storageSize" : 175837184,
        "numExtents" : 20,
        "indexes" : 12,
        "indexSize" : 169938160,
        "fileSize" : 2080374784,
        "nsSizeMB" : 16,
        "extentFreeList" : {
                "num" : 70,
                "totalSize" : 760217600
        },
        "dataFileVersion" : {
                "major" : 4,
                "minor" : 22
        },
        "ok" : 1
}

此处对象是跨集合的文档数

更多信息 - http://docs.mongodb.com/manual/reference/method/db.stats

<强>的Oracle

Row count of single table
select count(*) from table_name

Total Size
select * from dba_data_files;

select round((sum(bytes)/1048576/1024),2) from v$datafile;

To execute these queries you need to login as system user with all priveleges

答案 1 :(得分:1)

#1的答案是show table statusselect table_name, table_rows from information_schema.tables