赞
踩
当数据库在有约束的情况下,无论进行删除或者更新操作,都会对相关表进行一个校验,判断相关表的相关记录是否被删除或者更新。
在删除大量数据时,可以先禁用触发器,然后再进行删除。最后再优化一下表空间
1.禁用触发器
ALTER TABLE table_name DISABLE TRIGGER ALL;
2.DELETE
DELETE FROM table_name WHERE ...
3.启用触发器
ALTER TABLE table_name ENABLE TRIGGER ALL;
4.优化表空间, 维护数据库磁盘,释放空间
VACUUM FULL table_name;
5.重建索引,替换查询效率
REINDEX TABLE table_name;
如果是直接清空数据的话。使用 TRUNCATE TABLE table_name
; 但试过清空一个200G的数据库,一天没反应。。
select pg_size_pretty(pg_database_size('postgres')) as size;
select datname, pg_size_pretty (pg_database_size(datname)) AS size from pg_database;
select pg_size_pretty(pg_relation_size('mytab')) as size;
select relname, pg_size_pretty(pg_relation_size(relid)) as size from pg_stat_user_tables;
select pg_size_pretty(pg_total_relation_size('tab')) as size;
select relname, pg_size_pretty(pg_total_relation_size(relid)) as size from pg_stat_user_tables;
select pg_size_pretty(pg_relation_size('myindex')) as size;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。