获取总行数不正确

huangapple go评论77阅读模式
英文:

Get total rows incorrect

问题

我使用的是Firebird 2.5,以下是代码:

SELECT r.RDB$RELATION_NAME AS TABLE_NAME, COUNT(*) AS TOTAL_ROWS
FROM RDB$RELATIONS r
JOIN RDB$RELATION_FIELDS f ON r.RDB$RELATION_NAME = f.RDB$RELATION_NAME
GROUP BY r.RDB$RELATION_NAME;

有一张名为"customer"的表,上述代码返回总行数 = 332行。

如果我运行:

SELECT COUNT(*) FROM customer;

结果返回总行数 = 10000++ 行。

在第一个SQL语句中,我漏掉了什么?

英文:

I'm using Firebird 2.5, here's the code:

SELECT r.RDB$RELATION_NAME AS TABLE_NAME, COUNT(*) AS TOTAL_ROWS
FROM RDB$RELATIONS r
JOIN RDB$RELATION_FIELDS f ON r.RDB$RELATION_NAME = f.RDB$RELATION_NAME
GROUP BY r.RDB$RELATION_NAME;

There is a table called "customer", and above code returns total rows = 332 rows

If I run:

SELECT COUNT(*) FROM customer;

the result returns total rows = 10000++ rows

What have I missed in the first SQL statement?

答案1

得分: 1

The first statement returns the number of fields (columns) because RDB$RELATION_FIELDS stores the definitions of table and view columns.

第一条语句返回字段(列)的数量,因为RDB$RELATION_FIELDS存储了表和视图列的定义。

The second returns the number of rows (the data).

第二条语句返回行数(数据)的数量。

As I understand you want to get the number of rows (data) in all tables. If so you can use:

根据我理解,您想要获取所有表中的行数(数据)。如果是这样,您可以使用:

How to get record count for all tables?:

如何获取所有表的记录数?:

> Sometimes, when inspecting a database, you might want to get a quick glance over all the tables to see which ones actually have some data in them. There is no quick and easy way to get rough estimates, but you can always run brute-force "select count(*)" query on each table. Combining EXECUTE STATEMENT and EXECUTE BLOCK you can do this in a single SQL command:

> 有时,在检查数据库时,您可能希望快速查看所有表,以查看哪些实际上包含了一些数据。没有快速且简便的方式来获得粗略的估算,但您始终可以在每个表上运行“select count(*)”查询。通过结合使用EXECUTE STATEMENT和EXECUTE BLOCK,您可以在单个SQL命令中执行此操作:

set term !! ;

EXECUTE BLOCK
returns ( stm varchar(60), cnt integer )
as
BEGIN
for select cast('select count(*) from "'||trim(r.RDB$RELATION_NAME)||'"' as varchar(60))
from RDB$RELATIONS r
where (r.RDB$SYSTEM_FLAG is null or r.RDB$SYSTEM_FLAG = 0) and r.RDB$VIEW_BLR is null
order by 1
into :stm
DO
BEGIN
execute statement :stm into :cnt;
suspend;
END
END

set term !! ;
EXECUTE BLOCK
returns ( stm varchar(60), cnt integer )
as
BEGIN
for select cast('select count(*) from "'||trim(r.RDB$RELATION_NAME)||'"' as varchar(60))
from RDB$RELATIONS r
where (r.RDB$SYSTEM_FLAG is null or r.RDB$SYSTEM_FLAG = 0) and r.RDB$VIEW_BLR is null
order by 1
into :stm
DO
BEGIN
execute statement :stm into :cnt;
suspend;
END
END

英文:

The first statement returns the number of fields (columns) because
RDB$RELATION_FIELDS stores the definitions of table and view columns.

The second returns the number of rows (the data).

As I understand you want to get the number of rows (data) in all tables. If so you can use:

How to get record count for all tables?:

> Sometimes, when inspecting a database, you might want to get a quick
> glance over all the tables to see which ones actually have some data
> in them. There is no quick and easy way to get rough estimates, but
> you can always run brute-force "select count()" query on each table.
> Combining EXECUTE STATEMENT and EXECUTE BLOCK you can do this in a
> single SQL command:
>
> set term !! ;
> EXECUTE BLOCK
> returns ( stm varchar(60), cnt integer )
> as
> BEGIN
> for select cast('select count(
) from "'||trim(r.RDB$RELATION_NAME)||'"' as varchar(60))
> from RDB$RELATIONS r
> where (r.RDB$SYSTEM_FLAG is null or r.RDB$SYSTEM_FLAG = 0) and r.RDB$VIEW_BLR is null
> order by 1
> into :stm
> DO
> BEGIN
> execute statement :stm into :cnt;
> suspend;
> END
> END

答案2

得分: 0

你正在查询元数据表,该表提供有关数据库对象的结构和定义的信息。表RDB$RELATIONS包含表和视图,表RDB$RELATION_FIELDS包含表和视图列的定义。

如果第一个查询返回CUSTOMERS的列数为332,这意味着表CUSTOMERS有332个,这并不反映表CUSTOMERS中存储的行数。

英文:

You're querying the metadata tables, which provides information on the structure and definition of database objects. The table RDB$RELATIONS contains the tables and views, and the table RDB$RELATION_FIELDS contains the definitions of the columns of tables and views.

If the first query returns 332 for CUSTOMERS, it means the table CUSTOMERS has 332 columns, it does not in any way reflect the number of rows that are stored in the table CUSTOMERS.

huangapple
  • 本文由 发表于 2023年5月15日 13:05:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定