PostgreSQL truncate table会释放索引的空间
apple=# create table test(id integer, info text);
CREATE TABLE
apple=# insert into test select generate_series(1, 100000), md5('test');
INSERT 0 100000
apple=# select * from test limit 1;
id | info
----+----------------------------------
1 | 098f6bcd4621d373cade4e832627b4f6
(1 row) apple=# select pg_size_pretty(pg_relation_size('test'));
pg_size_pretty
----------------
6672 kB
(1 row) apple=# create index idx_test_id on test(id);
CREATE INDEX
apple=# select * from pg_indexes_size('test');
pg_indexes_size
-----------------
2260992
(1 row) apple=# select * from pg_size_pretty(pg_indexes_size('test'));
pg_size_pretty
----------------
2208 kB
(1 row) apple=# truncate test;
TRUNCATE TABLE
apple=# select * from pg_size_pretty(pg_indexes_size('test'));
pg_size_pretty
----------------
8192 bytes
(1 row) execute query success, query is select * from pg_size_pretty(pg_indexes_size('test'));
apple=# select pg_size_pretty(pg_relation_size('test'));
pg_size_pretty
----------------
0 bytes
(1 row)
1.delete,表占用空间不变,这个众所周知,所谓的water-mark不变;表上索引占用空间也不会变;
2.默认不会重置表对应的sequence,除非在后面加上 RESTART IDENTITY。
3.truncate table tb_name,表与表上索引的空间占用均回到建立索引或表时的initial参数。
4.定期rebuild索引是一个好习惯,一是提高索引效率二是释放存贮区。
5.cascade慎用。
TRUNCATE TRUNCATE — empty a table or set of tables Synopsis TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
Description 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. Parameters name
The name (optionally schema-qualified) of a table to truncate. If ONLY is specified before the table name, only that table is truncated. If ONLY is not specified, the table and all its descendant tables (if any) are truncated. Optionally, * can be specified after the table name to explicitly indicate that descendant tables are included. RESTART IDENTITY
Automatically restart sequences owned by columns of the truncated table(s). CONTINUE IDENTITY
Do not change the values of sequences. This is the default. CASCADE
Automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to CASCADE. RESTRICT
Refuse to truncate if any of the tables have foreign-key references from tables that are not listed in the command. This is the default. Notes You must have the TRUNCATE privilege on a table to truncate it. TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates on, which blocks all other concurrent operations on the table. When RESTART IDENTITY is specified, any sequences that are to be restarted are likewise locked exclusively. If concurrent access to a table is required, then the DELETE command should be used instead. TRUNCATE cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command. Checking validity in such cases would require table scans, and the whole point is not to do one. The CASCADE option can be used to automatically include all dependent tables — but be very careful when using this option, or else you might lose data you did not intend to! TRUNCATE will not fire any ON DELETE triggers that might exist for the tables. But it will fire ON TRUNCATE triggers. If ON TRUNCATE triggers are defined for any of the tables, then all BEFORE TRUNCATE triggers are fired before any truncation happens, and all AFTER TRUNCATE triggers are fired after the last truncation is performed and any sequences are reset. The triggers will fire in the order that the tables are to be processed (first those listed in the command, and then any that were added due to cascading). TRUNCATE is not MVCC-safe. After truncation, the table will appear empty to concurrent transactions, if they are using a snapshot taken before the truncation occurred. See Section 13.5 for more details. TRUNCATE is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not commit. When RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART operations are also done transactionally; that is, they will be rolled back if the surrounding transaction does not commit. This is unlike the normal behavior of ALTER SEQUENCE RESTART. Be aware that if any additional sequence operations are done on the restarted sequences before the transaction rolls back, the effects of these operations on the sequences will be rolled back, but not their effects on currval(); that is, after the transaction currval() will continue to reflect the last sequence value obtained inside the failed transaction, even though the sequence itself may no longer be consistent with that. This is similar to the usual behavior of currval() after a failed transaction. TRUNCATE is not currently supported for foreign tables. This implies that if a specified table has any descendant tables that are foreign, the command will fail.
PostgreSQL truncate table会释放索引的空间的更多相关文章
- TRUNCATE TABLE (Transact-SQL)
删除表中的所有行,而不记录单个行删除操作. TRUNCATE TABLE 与没有 WHERE 子句的 DELETE 语句类似:但是,TRUNCATE TABLE 速度更快,使用的系统资源和事务日志资源 ...
- Truncate table、Delete与Drop table的区别
Truncate table.Delete与Drop table的区别 TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNC ...
- Truncate Table 用法
TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作. 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 注释 TRUNC ...
- Truncate Table user
Truncate Table 百科名片 Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段,做计数重置归零重新计算的作用. 目录 语法 参数 注释 示例 ...
- SQL中truncate table和delete的区别
truncate table table_name delete from table_name drop table table_name truncate table在功能上与不带where子句的 ...
- Oracle 使用TRUNCATE TABLE删除所有行
若要删除表中的所有行,则 TRUNCATE TABLE 语句是一种快速.有效的方法.TRUNCATE TABLE 与不含 WHERE 子句的 DELETE 语句类似.但是,TRUNCATE TABLE ...
- TRUNCATE TABLE 与 DELETE (转)
TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作.TRUNCATE TABLE 与没有 WHERE 子句的 DELETE 语句类似:但是,TRUNCATE TABLE 速度更快, ...
- truncate table,drop,delete的区别
TRUNCATE TABLE 在功能上与不带 Where 子句的 Delete 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 Delete 速度快,且使用的系统和事务日志资源 ...
- truncate table (tablename )表明
Truncate是SQL中的一个删除数据表内容的语句,用法是: 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 下面是对Truncate语 ...
随机推荐
- Redhat 7改动默认执行级别方法 --RHEL7使用systemd创建符号链接指向默认执行级别
今天装了下正式版的RHEL7,发现熟悉的inittab中没有了改动默认执行级别,打开inittab例如以下 [root@localhost init.d]# vi /etc/inittab # ini ...
- java 多线程 day18 ThreadPoolExecutor
http://blog.csdn.net/lipc_/article/details/52025993 https://www.cnblogs.com/trust-freedom/p/6681948. ...
- python基础之if语句
python之if语句 通用格式 if <test1>: <do something> elif: <do something> else: <do some ...
- 4.5 Routing -- Setting Up A Controller
1. 改变URL可能也会改变屏幕显示哪个模板.然而,如果它们有信息去显示,模板才是有用的. 2. 在Ember.js中,一个模板从一个controller检索信息去显示. 3. 为了告诉control ...
- Rails的HashWithIndifferentAccess
ruby 2.0 引入了keyword arguments,方法的参数可以这么声明 def foo(bar: 'default') puts bar end foo # => 'default' ...
- 【运维技术】CentOS7上从零开始安装LAMP安装织梦DedeCMS教程
前期准备数据 centos7 系统 安装 appache httpd # 更新httpd yum update httpd # 安装httpd yum install -y httpd # 启动服务 ...
- bzoj1628 [Usaco2007 Demo]City skyline(单调栈)
Description Input 第一行给出N,W 第二行到第N+1行:每行给出二个整数x,y,输入的x严格递增,并且第一个x总是1 Output 输出一个整数,表示城市中最少包含的建筑物数量 Sa ...
- 20172305 2018-2019-1 《Java软件结构与数据结构》第四周学习总结
20172305 2018-2019-1 <Java软件结构与数据结构>第四周学习总结 教材学习内容总结 本周内容主要为书第六章内容: 列表 有序列表(元素按照元素内在特性进行排序) 无序 ...
- 封装JS实现Ajax
这两天仔细理解了一下Ajax,然后整理封装了一下,如果有什么不对的地方,请指教,谢谢! AJAX AJAX = Asynchronous JavaScript and XML(异步的 JavaScri ...
- 从0开始学习 GITHUB 系列之「初识 GITHUB」【转】
本文转载自:http://stormzhang.com/github/2016/05/25/learn-github-from-zero1/ 版权声明:本文为 stormzhang 原创文章,可以随意 ...