Chapter 4. Configuring and Tuning Microsoft SQL Server

As with SQL Server 2000, SQL Server 2005 is designed to balance the workload dynamically and to self-tune configuration settings. For example, SQL Server can increase or decrease memory usage dynamically based on overall system memory requirements. SQL Server also manages memory efficiently, especially when it comes to queries and user connections—and memory is just one of dozens of areas in which the configuration is automatically adjusted.

Although the SQL Server self-tuning feature works well, there are times when you will need to configure SQL Server settings manually. For example, if you are running a large database with special constraints and the database is not working the way you expect it to perform, you may want to customize the configuration. You may also need to modify configuration settings for SQL Server accounts, authentication, and auditing. Key tools you will use to configure and tune SQL Server include the following:

  • System Catalog Queries. Provide a direct way to determine database configuration characteristics and their related settings.

  • Stored Procedures. Lets you view and manage configuration settings through stored procedures, such as sp_configure and sp_dboption. Note that you can change some options of sp_configure only when Show Advanced Options is set to 1, as in the following example:

    exec sp_configure "show advanced options", 1
  • SQL Server Management Studio. Provides an easy-to-use interface that updates the database and registry settings for you.

  • SQLServr.exe. Starts SQL Server from the command line, and you can use it to set configuration parameters at startup.

In this chapter, we will examine the structures available for configuring and tuning SQL Server. We start with a look at the SQL Server 2005 system catalog, and then continue with a discussion of catalog queries and stored procedures. This discussion provides the essential background for understanding how to configure and tune SQL Server 2005. The next chapter, Chapter 5, provides details about using SQL Server Management Studio and SQLServr.exe.

Accessing SQL Server Configuration Data

SQL Server 2005 uses an object-based approach to representing servers, databases, and all of their configuration characteristics and data contents. At the heart of this object-based structure is the system catalog, which describes the objects in a particular instance of SQL Server along with their attributes. For example, attributes of a database describe:

  • The number and names of the tables and views.

  • The number and names of columns in a table or view.

  • The column data type, scale, and precision.

  • The triggers and constraints that are defined on a table.

  • The indexes and keys that are defined for a table.

  • The statistics used by query optimizer for generating query plans.

In queries, you can access this and other system catalog information using:

  • Catalog views. Provide access to metadata stored in a database, which includes database attributes and their values. Catalog views can be used to access all user-available metadata, except for replication, backup, database maintenance plan, and SQL Agent metadata.

  • Compatibility views. Provide access to many of the system tables from earlier releases of SQL Server using SQL Server 2005 views. These views are meant for backward compatibility only, and they expose the same metadata that was available in the SQL Server 2000. They do not expose metadata for new SQL Server 2005 features, such as database partitioning and mirroring.

  • Information Schema views. Provide access to a subset of metadata stored in a database, which includes database attributes and their values. Information Schema views are based on catalog view definitions in the SQL-92 standard and do not contain metadata specific to SQL Server 2005. Applications that use these views are portable between heterogeneous SQL-92-compliant database systems.

  • ODBC catalog functions. Provide an interface that open database connectivity (ODBC) drivers can use to return result sets containing system catalog information. The result sets present catalog information in a way that is independent of the structure of the underlying catalog tables.

  • OLE DB schema rowsets. Provide an IDBSchemaRowset interface that OLE DB providers can use to access system catalog information. The rowsets present catalog information independently from the structure of the underlying catalog tables.

  • System stored procedures and functions. Provide Transact-SQL stored procedures and functions that return catalog information.

Catalog views and stored procedures are the methods recommended to access a database’s metadata. This is primarily because catalog views present metadata in a format that is independent of any catalog table implementation, which means that the views are not affected by changes in the underlying catalog tables. When you want to configure or manage a server, you will typically use stored procedures to help you perform the necessary tasks. Stored procedures provide the necessary functionality to view and manage the configuration of SQL Server and related databases with ease.

Working with the System Catalog and Catalog Views

Catalog views contain information used by the SQL Server 2005 Database Engine. They provide the most general interface to the catalog metadata and are the most direct way to access and work with this information. All user-available metadata in the system catalog is exposed through catalog views. Catalog views do not contain information about replication, backup, database maintenance plan, or SQL Agent.

Like all structures in SQL Server 2005 databases, catalog views follow an object-based hierarchy in which lower-level objects inherit attributes of higher-level objects. Some catalog views inherit rows from other catalog views. For example, the Tables catalog view inherits all the columns of the Objects catalog view. Thus, in addition to columns that are specific to the Tables catalog view itself, the Tables catalog view has all the columns from the Objects catalog view. Table 4-1 summarizes the SQL Server 2005 catalog views and their uses.

Table 4-1. SQL Server 2005 Catalog Views

View Type

Description

Key Catalog Views

CLR Assembly

Catalog views

Describe Common Language

Runtime (CLR) assemblies.

sys.assemblies sys.assembly_files

sys.assembly_references

Databases and Files Catalog views

Describe databases, database files, and backup devices associated with a SQL Server instance.

sys.backup_devices sys.database_files

sys.databases sys.master_files

Database Mirroring Catalog views

Describe witness roles that a server plays as a database mirroring partner.

sys.database_mirroring_witnesses

Data Spaces and Full-Text Catalog views

Describe filegroups, partition schemes, and full-text catalogs.

sys.data_spaces

sys.destination_data_spaces sys.filegroups

sys.fulltext_catalogs sys.partition_schemes

Endpoints Catalog views

Describe endpoints used for mirroring, service broker messaging, and Web services.

sys.database_mirroring_endpoints

sys.endpoint_webmethods sys.endpoints

sys.http_endpoints

sys.service_broker_endpoints

sys.soap_endpoints sys.tcp_endpoints

sys.via_endpoints

Extended Properties Catalog views

Describe extended properties and the class of objects from which they originate.

sys.extended_properties

Linked Servers Catalog views

Describe linked or remote servers and their related logins.

sys.linked_logins sys.remote_logins

sys.servers

Messages (for Errors) Catalog views

Describe system-defined and user-defined error messages.

sys.messages

Objects Catalog views

Describe top-level database objects.

sys.allocation_units sys.assembly_modules

sys.check_constraints sys.columns

sys.computed_columns

sys.default_constraints

sys.event_notifications sys.events

sys.extended_procedures sys.stats

sys.foreign_key_columns

sys.foreign_keys

sys.fulltext_index_columns

sys.fulltext_indexes

sys.identity_columns

sys.index_columns sys.indexes

sys.key_constraints

sys.numbered_procedures

sys.numbered_procedure_parameters

sys.objects sys.parameters

sys.partitions sys.procedures

sys.service_queues

sys.sql_dependencies

sys.sql_modules sys.stats_columns

sys.synonyms

sys.tables sys.traces

sys.trigger_events

sys.triggers sys.views

Partition Function Catalog views

Describe partition functions, parameters, and range values.

sys.partition_functions

sys.partition_parameters

sys.partition_range_values

Scalar Types Catalog views

Describe user-defined scalar types for CLR assemblies as well as other system- and user-defined scalar types.

sys.assembly_types sys.types

Schemas Catalog views

Describe database schemas.

sys.schemas

Security Catalog views

Describe server-level, database-level, and encryption security attributes and values.

Database-level views

(sys.database_permissions,

sys.database_principals,

sys.database_role_members)

Server-level views (sys.server_permissions,

sys.server_principals,

sys.server_role_members, sys.sql_logins)

Encryption views (sys.asymmetric_keys,

sys.certificates, sys.credentials,

sys.crypt_properties, sys.key_encryptions,

sys.symmetric_keys)

Service Broker Catalog views

Describe Service Broker endpoints and messaging components.

sys.conversation_endpoints

sys.conversation_groups

sys.remote_service_bindings

sys.service_contract_message_usages

sys.service_contract_usages sys.routes

sys.service_contracts

sys.service_message_types sys.services

sys.transmission_queue

Server-Wide Configuration Catalog views

Describe server-wide configuration option values.

sys.configurations sys.fulltext_languages

sys.trace_categories sys.trace_columns

sys.trace_event_bindings sys.trace_events

sys.traces sys.trace_subclass_values

XML Schemas (XML Type System) Catalog views

Describe XML Schema components and values.

sys.xml_indexes

sys.xml_schema_attributes

sys.xml_schema_collections

sys.xml_schema_component_placements

sys.xml_schema_components

sys.xml_schema_elements

sys.xml_schema_facets

sys.xml_schema_model_groups

sys.xml_schema_namespaces

sys.xml_schema_types

sys.xml_schema_wildcard_namespaces

sys.xml_schema_wildcards

Table 4-2 provides mapping between SQL Server 2000 system tables and SQL Server 2005 system views. The entries are organized by database and view type. Mappings for the master database are followed by mappings for all databases.

Table 4-2. Mapping SQL Server 2000 System Tables to SQL Server 2005 System Views

SQL Server 2000 System Table

SQL Server 2005 System View

SQL Server 2005 View Type

Master Database

  

sysaltfiles

sys.master_files

Catalog view

syscacheobjects

sys.dm_exec_cached_plans

Dynamic management view

syscharsets

sys.syscharsets

Compatibility view

sysconfigures

sys.configurations

Catalog view

syscurconfigs

sys.configurations

Catalog view

sysdatabases

sys.databases

Catalog view

sysdevices

sys.backup_devices

Catalog view

syslanguages

sys.languages

Compatibility view

syslockinfo

sys.dm_tran_locks

Dynamic management view

syslocks

sys.dm_tran_locks

Dynamic management view

syslogins

sys.server_principals

Catalog view

sysmessages

sys.messages

Catalog view

sysoledbusers

sys.linked_logins

Catalog view

sysopentapes

sys.dm_io_backup_tapes

Dynamic management view

sysperfinfo

sys.dm_os_performance_counters

Dynamic management view

sysprocesses

sys.dm_exec_connections

sys.dm_evec_sessions

sys.dm_exec_requests

Dynamic management views

sysremotelogins

sys.remote_logins

Catalog view

sysservers

sys.servers

Catalog view

All Databases

  

syscolumns

sys.columns

Catalog view

syscomments

sys.sql_modules

Catalog view

sysconstraints

sys.check_constraints

sys.default_constraints

sys.key_constraints

sys.foreign_keys

Catalog views

sysdepends

sys.sql_dependencies

Catalog view

sysfilegroups

sys.filegroups

Catalog view

sysfiles

sys.database_files

Catalog view

sysforeignkeys

sys.foreign_keys

Catalog view

sysfulltextcatalogs

sys.fulltext_catalogs

Catalog view

sysindexes

sys.indexes

Catalog view

sysindexkeys

sys.index_columns

Catalog view

sysmembers

sys.databases_role_members

Catalog view

sysobjects

sys.objects

Catalog view

syspermissions

sys.database_permissions,

sys.server_permissions

Catalog views

sysprotects

sys.database_permissions,

sys.server_permissions

Catalog views

sysreferences

sys.foreign_keys

Catalog view

systypes

sys.types

Catalog view

sysusers

sys.database_principals

Catalog view

Working with System Stored Procedures

You can use system stored procedures to view SQL Server configuration details and to perform general administration. SQL Server 2005 has two main categories of system stored procedures:

  • Those meant for administrators

  • Those used to implement functionality for database application programming interfaces (APIs)

Naturally, you will want to work with system stored procedures meant for administration and not those that implement database API functions. System stored procedures are written using Transact-SQL (T-SQL). Most return a value of 0 to indicate success and a nonzero value to indicate failure. As an example, sp_dboption is a stored procedure for managing the configuration options of SQL Server databases (except for the master and tempdb databases). When you use sp_dboption to set a database configuration value, a return code of 0 indicates that the option was set as expected. A return code of 1 indicates that the stored procedure failed and the option was not set as expected.

The following example takes the Personnel database offline if there are no current users:

USE master;

GO

EXEC sp_dboption "Personnel", "offline", "TRUE";

GO

If the stored procedure returns 0, then the database was successfully taken offline. A return value of 1 indicates that there was a problem taking the database offline, which means that the database is still online. For more information on using stored procedures, see the section titled "Configuring SQL Server with Stored Procedures" later in this chapter.

Table 4-3 provides a summary of stored procedures for administration. The table entries are organized by the type of administration activity for which the stored procedure is designed.

Table 4-3. Key System Stored Procedures by Type

Stored Procedure Type

Description

Related System Stored Procedures

Active Directory Stored Procedures

Register instances of SQL Server and SQL Server databases in Active Directory.

sp_ActiveDirectory_Obj, sp_ActiveDirectory_SCP

Catalog Stored Procedures

Implement ODBC data dictionary functions.

sp_column_privileges, sp_columns,

sp_databases, sp_fkeys, sp_pkeys,

sp_server_info, sp_special_columns,

sp_sproc_columns, sp_statistics,

sp_stored_procedures, sp_table_privileges,

sp_tables

Cursor Stored Procedures

Implement cursor variable functionality.

sp_cursor_list, sp_describe_cursor,

sp_describe_cursor_columns,

sp_describe_cursor_tables

Database Engine Stored Procedures

Maintain SQL Server instances and perform general administration activities.

sp_add_data_file_recover_suspect_db,

sp_add_log_file_recover_suspect_db,

sp_addextendedproc, sp_addextendedproperty,

sp_addmessage, sp_addtype,

sp_addumpdevice, sp_altermessage,

sp_attach_db, sp_attach_single_file_db,

sp_autostats, sp_bindefault, sp_bindrule,

sp_bindsession, sp_certify_removable,

sp_configure, sp_create_removable,

sp_createstats, sp_cycle_errorlog,

sp_datatype_info, sp_dbcmptlevel, sp_dboption,

sp_dbremove, sp_delete_backuphistory,

sp_depends, sp_detach_db, sp_dropdevice,

sp_dropextendedproc,

sp_dropextendedproperty, sp_dropmessage,

sp_droptype, sp_executesql, sp_getapplock,

sp_getbindtoken, sp_help, sp_helpconstraint,

sp_helpdb, sp_helpdevice,

sp_helpextendedproc, sp_helpfile,

sp_helpfilegroup, sp_helpindex,

sp_helplanguage, sp_helpserver, sp_helpsort,

sp_helpstats, sp_helptext, sp_helptrigger,

sp_indexoption, sp_invalidate_textptr, sp_lock,

sp_monitor, sp_procoption, sp_recompile,

sp_refreshview, sp_releaseapplock, sp_rename,

sp_renamedb, sp_resetstatus, sp_serveroption,

sp_setnetname, sp_settriggerorder,

sp_spaceused, sp_tableoption, sp_unbindefault,

sp_unbindrule, sp_updateextendedproperty,

sp_updatestats, sp_validname, sp_who

Database Mail Stored Procedures

Perform e-mail operations from within SQL Server.

sp_send_dbmail, sysmail_add_account_sp,

sysmail_add_principalprofile_sp,

sysmail_add_profile_sp,

sysmail_add_profileaccount_sp,

sysmail_configure_sp,

sysmail_delete_account_sp,

sysmail_delete_principalprofile_sp,

sysmail_delete_profile_sp,

sysmail_delete_profileaccount_sp,

sysmail_help_account_sp,

sysmail_help_configure_sp,

sysmail_help_principalprofile_sp,

sysmail_help_profile_sp,

sysmail_help_profileaccount_sp,

sysmail_start_sp, sysmail_stop_sp,

sysmail_update_account_sp,

sysmail_update_principalprofile_sp,

sysmail_update_profile_sp,

sysmail_update_profileaccount_sp

Database Maintenance Plan Stored Procedures

Configure and manage database maintenance plans and related tasks.

sp_add_maintenance_plan,

sp_add_maintenance_plan_db,

sp_add_maintenance_plan_job,

sp_delete_maintenance_plan,

sp_delete_maintenance_plan_db,

sp_delete_maintenance_plan_job,

sp_help_maintenance_plan

Distributed Queries Stored Procedures

Implement and manage Distributed Queries.

sp_addlinkedserver, sp_addlinkedsrvlogin,

sp_catalogs, sp_column_privileges_ex,

sp_columns_ex, sp_droplinkedsrvlogin,

sp_foreignkeys, sp_indexes, sp_linkedservers,

sp_primarykeys, sp_serveroption,

sp_table_privileges_ex, sp_tables_ex,

sp_testlinkedserver

Full-Text Search Stored Procedures

Implement and query full-text indexes.

sp_fulltext_catalog, sp_fulltext_column,

sp_fulltext_database, sp_fulltext_service,

sp_fulltext_table, sp_help_fulltext_catalogs,

sp_help_fulltext_catalogs_cursor,

sp_help_fulltext_columns,

sp_help_fulltext_columns_cursor,

sp_help_fulltext_tables,

sp_help_fulltext_tables_cursor

General Extended Stored Procedures

Provide an interface from SQL Server to external programs, primarily for server maintenance.

xp_cmdshell, xp_enumgroups, xp_findnextmsg,

xp_grantlogin, xp_logevent, xp_loginconfig,

xp_logininfo, xp_msver, xp_revokelogin,

xp_sprintf, xp_sqlmaint, xp_sscanf

Log Shipping Stored Procedures

Implement, manage, and monitor log shipping configurations.

sp_add_log_shipping_alert_job,

sp_add_log_shipping_primary_database,

sp_add_log_shipping_primary_secondary,

sp_add_log_shipping_secondary_database,

sp_add_log_shipping_secondary_primary,

sp_change_log_shipping_primary_database,

sp_change_log_shipping_secondary_database,

sp_change_log_shipping_secondary_primary,

sp_cleanup_log_shipping_history,

sp_delete_log_shipping_alert_job,

sp_delete_log_shipping_primary_database,

sp_delete_log_shipping_primary_secondary,

sp_delete_log_shipping_secondary_database,

sp_delete_log_shipping_secondary_primary,

sp_help_log_shipping_alert_job,

sp_help_log_shipping_monitor_primary,

sp_help_log_shipping_monitor_secondary,

sp_help_log_shipping_primary_database,

sp_help_log_shipping_primary_secondary,

sp_help_log_shipping_secondary_database,

sp_help_log_shipping_secondary_primary,

sp_refresh_log_shipping_monitor,

sp_resolve_logins

Notification Services Stored Procedures

Manage, debug, and troubleshoot Microsoft SQL Server 2005 Notification Services.

NSAdministrationHistory,

NSDiagnosticDeliveryChannel,

NSDiagnosticEventClass,

NSDiagnosticEventProvider,

NSDiagnosticFailedNotifications,

NSDiagnosticNotificationClass,

NSDiagnosticSubscriptionClass,

NSEventBatchDetails,

NSEventBeginBatch<EventClassName>,

NSEventFlushBatch<EventClassName>,

NSEventSubmitBatch<EventClassName>,

NSEventWrite<EventClassName>,

NSExecuteRuleFiring,

NSNotificationBatchDetails,

NSNotificationBatchDetails,

NSNotificationBatchList, NSPrepareRuleFiring,

NSQuantumDetails,

NSQuantumExecutionTime,

NSQuantumFailures, NSQuantumList,

NSQuantumPerformance,

NSQuantumsSkipped,

NSScheduledSubscriptionDetails,

NSScheduledSubscriptionList,

NSSetQuantumClock,

NSSetQuantumClockDate,

NSSnapshotApplications,

NSSnapshotDeliveryChannels,

NSSnapshotEvents, NSSnapshotProviders,

NSSnapshotSubscriptions,

NSSubscriptionConditionInformation,

NSVacuum

OLE Automation Stored Procedures

Create and manage OLE automation objects.

sp_OACreate, sp_OADestroy,

sp_OAGetErrorInfo, sp_OAGetProperty,

sp_OAMethod, sp_OASetProperty, sp_OAStop

Security Stored Procedures

Manage server and database security.

sp_addalias, sp_addapprole, sp_addgroup,

sp_addlinkedsrvlogin, sp_addlogin,

sp_addremotelogin, sp_addrole,

sp_addrolemember, sp_addserver,

sp_addsrvrolemember, sp_adduser,

sp_approlepassword, sp_change_users_login,

sp_changedbowner, sp_changegroup,

sp_changeobjectowner,

sp_dbfixedrolepermission, sp_defaultdb,

sp_defaultlanguage, sp_denylogin,

sp_dropalias, sp_dropapprole, sp_dropgroup,

sp_droplinkedsrvlogin, sp_droplogin,

sp_dropremotelogin, sp_droprolemember,

sp_dropserver, sp_dropsrvrolemember,

sp_dropuser, sp_grantdbaccess, sp_grantlogin,

sp_helpdbfixedrole, sp_helpgroup,

sp_helplinkedsrvlogin, sp_helplogins,

sp_helpntgroup, sp_helpremotelogin,

sp_helprole, sp_helprolemember, sp_helprotect,

sp_helpsrvrole, sp_helpsrvrolemember,

sp_helpuser, sp_MShasdbaccess, sp_password,

sp_remoteoption, sp_revokedbaccess,

sp_revokelogin, sp_setapprole,

sp_srvrolepermission, sp_validatelogins

SQL Mail Stored Procedures

Perform e-mail operations from within SQL Server. (In SQL Server 2005, Database Mail is preferred over SQL Mail.)

sp_processmail, xp_deletemail, xp_findnextmsg,

xp_readmail, xp_sendmail, xp_startmail,

xp_stopmail

SQL Server Profiler Stored Procedures

Used by SQL Profiler to monitor performance and activity.

sp_trace_create, sp_trace_generateevent,

sp_trace_setevent, sp_trace_setfilter,

sp_trace_setstatus

SQL Server Agent Stored Procedures

Manage scheduled alerts and other SQL Server Agent activities.

sp_add_alert, sp_add_category, sp_add_job,

sp_add_jobschedule, sp_add_jobserver,

sp_add_jobstep, sp_add_notification,

sp_add_operator, sp_add_proxy,

sp_add_schedule, sp_add_targetservergroup,

sp_add_targetsvrgrp_member,

sp_apply_job_to_targets, sp_attach_schedule,

sp_cycle_agent_errorlog, sp_cycle_errorlog,

sp_delete_alert, sp_delete_category,

sp_delete_job, sp_delete_jobschedule,

sp_delete_jobserver, sp_delete_jobstep,

sp_delete_jobsteplog, sp_delete_notification,

sp_delete_operator, sp_delete_proxy,

sp_delete_schedule, sp_delete_targetserver,

sp_delete_targetservergroup,

sp_delete_targetsvrgrp_member,

sp_detach_schedule, sp_enum_login_for_proxy,

sp_enum_proxy_for_subsystem,

sp_enum_sqlagent_subsystems, sp_grant_login_to_proxy, sp_grant_proxy_to_subsystem,

sp_help_alert, sp_help_category,

sp_help_downloadlist, sp_help_job,

sp_help_jobactivity, sp_help_jobcount,

sp_help_jobhistory, sp_help_jobs_in_schedule,

sp_help_jobschedule, sp_help_jobserver,

sp_help_jobstep, sp_help_jobsteplog,

sp_help_notification, sp_help_operator,

sp_help_proxy, sp_help_schedule,

sp_help_targetserver, sp_help_targetservergroup,

sp_manage_jobs_by_login, sp_msx_defect,

sp_msx_enlist, sp_msx_get_account,

sp_msx_set_account, sp_notify_operator,

sp_post_msx_operation, sp_purge_jobhistory,

sp_remove_job_from_targets,

sp_resync_targetserver,

sp_revoke_login_from_proxy,

sp_revoke_proxy_from_subsystem, sp_start_job,

sp_stop_job, sp_update_alert,

sp_update_category, sp_update_job,

sp_update_jobschedule, sp_update_jobstep,

sp_update_notification, sp_update_operator,

sp_update_proxy, sp_update_schedule,

sp_update_targetservergroup

XML Stored Procedures

Manage Extensible

Markup Language (XML) text.

sp_xml_preparedocument,

sp_xml_removedocument

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset