简体   繁体   English

自动管理Sql Server数据库大小

[英]Automatically manage Sql Server database size

There are 2 .NET services which use 2 SQL Server databases. 有2个.NET服务使用2个SQL Server数据库。 I am currently using SQL Express so the maximum database size is an issue. 我当前正在使用SQL Express,因此最大数据库大小是一个问题。

When the size approaches the 10GB limit (or some record limit), I would like to automatically delete the oldest X amount of records to free up some space. 当大小接近10GB限制(或某些记录限制)时,我想自动删除最早的X记录量以释放一些空间。

This is not a production environment and I REALLY don't need the old data, i just want to keep the data "fresh". 这不是生产环境,我真的不需要旧数据,我只想保持数据“新鲜”。

SHould this be done at the service level? 这应该在服务级别上完成吗? I can modify my services to periodically check spaceused and execute a manual "clean up" (Whether it's a delete, archive, etc.). 我可以修改我的服务以定期检查已用空间并执行手动“清理”(是否是删除,存档等)。 I'm not sure how do this on the SQL level however. 我不确定如何在SQL级别上执行此操作。

Thanks 谢谢

Since you are using SQL Express, you will need to do this at the service level on some schedule. 由于您使用的是SQL Express,因此需要按计划在服务级别上执行此操作。 You will first need to delete the rows out of the table(s) that you want to purge the data from. 首先,您需要从表中删除要从中清除数据的行。 Something like: 就像是:

delete BigOleTable where LoggedDate < dateadd(yy,-1,getdate())

that will get rid of stuff older than a year. 将会淘汰一年以上的东西。

Then, you will need to shrink the database.. so, this depends on your recovery model. 然后,您将需要收缩数据库..因此,这取决于您的恢复模型。 If you're in full recovery, you'll need to backup the transaction log. 如果您要进行完全恢复,则需要备份事务日志。 and then issue a shrinkdatabase as Tanner alluded to above. 然后发布Tanner在上面提到的收缩数据库。

You can create a job that does this or better use SSIS (see this: http://technet.microsoft.com/en-us/library/ms181153%28v=sql.105%29.aspx ). 您可以创建执行此操作或更好地使用SSIS的作业(请参阅: http : //technet.microsoft.com/zh-cn/library/ms181153%28v=sql.105%29.aspx )。

You can use this http://technet.microsoft.com/en-us/library/ms188776.aspx procedure to query the space used and if it exceeds a thesshold you could delete data. 您可以使用此http://technet.microsoft.com/zh-cn/library/ms188776.aspx过程查询所使用的空间,如果空间超过阈值,则可以删除数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM