前面实现了在数据库中通过编写函数(存储过程)和触发器来清理创建时间七天以上的数据,用的触发策略为insert,即每次插入数据都会触发该清除历史数据的函数,性能上考虑太差了,所以优化为通过shell脚本的形式来定时在每天凌晨两点清除。
shell脚本如下:
#!/bin/sh
# 自动定期删除创建时间大于七天的记录
/home/cms/postgresql/bin/psql -h 127.0.01 -p 5432 -d yourdb -U username --command "delete from user where create_time < (now() - interval '7 day')"
这样可能会有问题,因为连接p