简体   繁体   English

SQL Server Express的维护建议

[英]Maintenance suggestions for SQL Server Express

We operate a cloud version of our application that uses SQL Server 2012 Express as the DB engine. 我们运行应用程序的云版本,该版本使用SQL Server 2012 Express作为数据库引擎。 Each client has their own instance of SQL Server, allowing their instance to use the full 1GB of memory limited by Express, and it gives them 10GB of database size. 每个客户端都有自己的SQL Server实例,从而允许其实例使用Express限制的全部1GB内存,并为其提供10GB的数据库大小。

The challenge we have is keeping these databases optimized, yet allowing for maximum data storage for the clients and without disruption. 我们面临的挑战是使这些数据库保持最佳状态,同时又要为客户端提供最大的数据存储而又不会造成中断。 In other words, we're trying to give them nearly all 10GB of the Express db for their actual data, but we need to run maintenance to keep performance optimized and it grows the DB, sometimes so large that maintenance fails. 换句话说,我们试图为他们提供Express数据库的几乎所有10GB的实际数据,但是我们需要运行维护以保持性能的优化,并且数据库会不断增长,有时数据库太大,以至于维护失败。

Every Sunday night we run a SQL maintenance script like what's below. 每个星期天晚上,我们都会运行一个SQL维护脚本,如下所示。

BEGIN;
    FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag;

    IF @@FETCH_STATUS < 0 
         BREAK;

    SELECT @objectname = o.name, @schemaname = QUOTENAME(s.name)
    FROM sys.objects AS o
    JOIN sys.schemas as s ON s.schema_id = o.schema_id
    WHERE o.object_id = @objectid;

    SELECT @indexname = QUOTENAME(name)
    FROM sys.indexes
    WHERE object_id = @objectid AND index_id = @indexid;

    SELECT @partitioncount = count (*)
    FROM sys.partitions
    WHERE object_id = @objectid AND index_id = @indexid;

    -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.
    IF @frag < 30.0
        SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE';

    IF @frag >= 30.0
        SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON, STATISTICS_NORECOMPUTE = OFF);'

    IF @partitioncount > 1
        SET @command = @command + N' PARTITION=' + CAST(@partitionnum AS nvarchar(10));

    EXEC (@command);

    PRINT 'Executed: ' + @command;

    SET @COMMAND = 'USE DATA UPDATE STATISTICS Data.dbo.' +@objectName + ';'
    EXEC (@COMMAND)

    PRINT 'Executed: ' + @command;
    PRINT @objectname + 'Index Name: ' + @IndexName + 'Percent Fragmented: ' + CAST(@Frag AS VARCHAR(20))
END;

The maintenance works fine if space is available, the problem is that it swells the database and pushes the clients up to the 10GB limit. 如果有可用空间,维护工作会很好,问题是它会使数据库膨胀并使客户端推到10GB的限制。 This then causes issues because they may not be able to allocate space for tables they need to add values into. 然后,这会引起问题,因为它们可能无法为需要向其中添加值的表分配空间。 We then have to go back and try and shrink the DB, which from my understanding, then undoes some of the benefits of the index defragmentation / maintenance. 然后,我们不得不回过头来尝试缩小数据库,据我所知,这会消除索引碎片整理/维护的某些好处。

Given the 10GB limit on data files, and the need to run maintenance to keep the DB's operating sufficiently, what could/should we do to get benefits from maintenance without having to shrink the database afterwards? 鉴于数据文件的限制为10GB,并且需要进行维护以保持数据库的正常运行,我们该如何/应该做些什么才能从维护中获得好处而又不必随后缩小数据库?

We're using Simple recovery mode, no autoshrink, and autogrowth at 10% 我们使用的是简单恢复模式,没有自动收缩,并且自动增长为10%

Thank you in advance! 先感谢您!

after a lot of research it appears that reorganizing doesn't use as much space in the database files when run. 经过大量研究,看来重组在运行时并没有占用数据库文件中的太多空间。

I ended up using a combination of these instructions with the SQL maintenance script they referenced which is much better the way it's making the decision to reorganize or rebuild. 我最终将这些指令与它们所引用的SQL维护脚本结合使用,这比它决定重组或重建的方式要好得多。 https://www.brentozar.com/archive/2013/09/index-maintenance-sql-server-rebuild-reorganize/ https://www.brentozar.com/archive/2013/09/index-maintenance-sql-server-rebuild-reorganize/

Don't do a shrink database. 不要做收缩数据库。 There is no use in shrinking it just to grow it again contributing to fragmentation. 缩小它只是为了使其再次增长而导致碎片化是没有用的。 Rarely, would you need to perform a Shrinkfile as in after something unusual. 很少,您是否需要像执行异常操作后一样执行Shrinkfile。 Also, to reduce auto growth, you could go ahead and set the data file to something like 7GB. 另外,为减少自动增长,您可以继续将数据文件设置为7GB。 Since your recovery is simple then your log files should never really grow too large. 由于恢复很简单,因此日志文件绝对不应过大。 That being said here is a rather straightforward maintenance plan that you could run nightly. 话虽如此,这是一个非常简单的维护计划,您可以每晚运行。

•Check Database Integrity 
•Shrink the Log File – (This is ok because recovery mode is Simple)
•Reorganize Index
•Rebuild Index
•Update Statistics
•Clean Up History

You may also get a pretty clear answer over at dba exchange 您还可以在dba交易所获得一个非常明确的答案

Sort in TempDB You might want to see if the SORT_IN_TEMPDB option will help 在TempDB中排序您可能想看看SORT_IN_TEMPDB选项是否有帮助

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

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