PostgreSQL-空表 [英] PostgreSQL - Empty table

查看:372
本文介绍了PostgreSQL-空表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PostgreSQL数据库模式上有一个名为EVENTS的表。

它是空的,即当我执行时

I have a table called EVENTS on my PostgreSQL DB schema.
It is empty, i.e. when I execute

SELECT * FROM EVENTS

我得到一个空结果集。

不过,该表占用5MB的磁盘空间。

我正在执行

Nonetheless, the table occupies 5MB of disk space.
I'm executing

SELECT round(pg_total_relation_size('events') / 1024.0 / 1024.0, 2)

我的内存为5.13MB。

And I'm getting 5.13MB.

我尝试显式运行VACUUM,但它没有任何改变。

I tried to explicitly run VACUUM, but it didn't change anything.

有什么想法吗?

推荐答案

截断表格:

truncate events;

来自文档:


TRUNCATE快速删除集合中的所有行表。它与在每个表上进行不限定DELETE的效果相同,但是由于它实际上并不扫描表,因此速度更快。此外,它可以立即回收磁盘空间,而不需要随后的VACUUM操作。这在大型表上最有用。

TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE on each table, but since it does not actually scan the tables it is faster. Furthermore, it reclaims disk space immediately, rather than requiring a subsequent VACUUM operation. This is most useful on large tables.

如果要立即回收磁盘空间以保留非空表的现有行,则可以可以使用真空:

If you want to immediately reclaim disk space keeping existing rows of a non-empty table, you can use vacuum:

vacuum full events;

此操作仅锁定表并将其重写(实际上,创建新副本并删除旧副本) )。这是一项昂贵的操作,通常不建议在较大的表上使用。

This locks exclusively the table and rewrite it (in fact, creates a new copy and drops the old one). It is an expensive operation and generally not recommended on larger tables.

在RDBMS中,磁盘空间的某些冗余使用是正常状态。如果您已正确配置 autovacuum守护程序,则未使用的空间将是在插入新行时使用。

In RDBMS some redundant usage of the disk space is a normal state. If you have a properly configured autovacuum daemon the unused space will be used when new rows are inserted.

这篇关于PostgreSQL-空表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆