8. Configuring Database Auto Options
Five database-configuration options determine the automatic behaviors of SQL Server databases (see Table 7). In Management Studio, they are all set in the Options tab of the Database Properties page .
Table 7 Index-Configuration Properties
Auto Close
Auto close directs SQL Server to
release all database resources (cached data pages, compiled stored
procedures, and saved query execution plans) when all users exit the
database and all processes are complete. This frees memory for other
databases. Although this option can slightly improve performance for
other databases, reloading the database can take longer, as will
recompiling the procedures and recalculating the query execution plans,
after the database is again opened by a SQL Server when a user accesses
the database again.
If the database is used regularly, do not enable
auto close. If the database is used occasionally, then auto close might
be appropriate to save memory.
Caution
Many front-end client applications repeatedly open and close a connection to SQL Server. Setting auto close ON in this type of environment is a sure way to kill SQL Server performance.
Use the following to set auto close ON for the AdventureWorks2012 sample database in code:
ALTER DATABASE AdventureWorks2012 SET AUTO_CLOSE ON;
Best Practice
Do not set auto_close ON
for production SQL Server databases. There may be situations in which
you may have hundreds or even thousands of databases with archived data
that are never all used at the same time. For these situations you may
benefit from having auto_close on for some or even all databases. By default, auto_close is on only for SQL Server Express Edition and is off by default for all other editions.
Auto Shrink
If the database has more than 25
percent free space, then this option causes SQL Server to perform a
data and log file shrink operation. This option also causes the
transaction log to shrink after it's backed up.
Performing a file shrink is a costly operation
because several pages must be moved within the file. Plus, it's
probable that the files have to grow again later (another costly
operation), causing file fragmentation at the OS level. This option
also regularly checks the status of the data pages to determine whether
they can be shrunk.
To set the auto shrink option off for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET AUTO_SHRINK OFF;
Best Practice
Use of auto_shrink
can cause severe performance issues as it can cause index fragmentation
as well disk fragmentation at the operating system level. It's
generally best practice that you do not have this enabled on your
databases.
Auto Create Statistics
Data-distribution statistics are a key
factor in how the SQL Server Query Optimizer creates query execution
plans. This option directs SQL Server to automatically create
statistics for any columns for which statistics could be useful. The
default for this option is set to ON.
To set auto create statistics on for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET AUTO_CREATE_STATISTICS ON;
Auto Update Statistics
SQL Server's cost-based query optimizer
uses statistics to choose the most efficient plan for retrieving or
updating data. Hence, out-of-date data-distribution statistics aren't
very useful. The AUTO_UPDATE_STATISTICS
database option causes statistics to be recomputed every time a certain
number of rows in the table changes. The default for this option is set
to on, which is best practice and works for most environments. Based on
the row changes, sometimes the statistics may be updated too
frequently, other times too infrequently, and sometimes automatically
updating the statistics may cause a delay just when you don't want it.
To avoid these situations, you can disable the AUTO_UPDATE_STATISTICS
database option and schedule jobs to recompute statistics during a low
traffic or maintenance window. In some environments, DBAs schedule jobs
to manually compute the statistics and keep the AUTO_UPDATE_STATISTICS
option on as a failsafe measure in case many more rows change than they
normally do. To set the auto update statistics option on for
AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET AUTO_UPDATE_STATISTICS ON;
Auto Update Statistics Asynchronously
When a query triggers an AUTO_UPDATE_STATISTICS
event, the query waits until the updated statistics can be used. This
can cause unpredictable query response times. Starting from SQL Server
2005, there is a database option called AUTO_UPDATE_STATISTICS_ASYNC that you can enable to asynchronously update the statistics. By default, this option is off. If the AUTO_UPDATE_STATISTICS_ASYNC
database option is turned on, then SQL Server performs the automatic
update of statistics in the background. The query that causes the
automatic statistics does not need to wait for the statistics to be
updated and proceeds with the old statistics. This may result in a
less-efficient query plan, but the query response times are
predictable. Queries that start after the statistics are updated use
those statistics.
To set the auto update statistics asynchronous option on for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET AUTO_UPDATE_STATISTICS_ASYNC ON;
Note
The AUTO_UPDATE_STATISTICS_ASYNC database option is dependent on the AUTO_UPDATE_STATISTICS option. Therefore, you need to ensure that the AUTO_UPDATE_STATISTICS option is ON and then enable the AUTO_UPDATE_STATISTICS_ASYNC
option. Like any SQL Server configuration, you need to thoroughly test
this option to see if your SQL Server applications benefit from this
option.