© Pranab Mazumdar, Sourabh Agarwal, Amit Banerjee 2016

Pranab Mazumdar, Sourabh Agarwal and Amit Banerjee, Pro SQL Server on Microsoft Azure , 10.1007/978-1-4842-2083-2_10

10. Azure SQL Database: Performance and Monitoring

Pranab Mazumdar, Sourabh Agarwal1 and Amit Banerjee1

(1)Bangalore, Karnataka, India

Microsoft Azure SQL Database can be provisioned in different service tiers—Basic, Standard, and Premium—each with multiple performance levels—Basic, S1, S2, P1, P2, etc. Each performance level provides an increasing set of resources, which are designed to deliver increasingly higher transaction throughput.

The resources and transactional power in each service tier and performance level are expressed in terms of Database Throughput Units (DTUs) . DTUs provide a way to describe the relative transaction processing capacity of a performance level based on a mix of CPU, memory, or IO rates. In essence, when migration from the S0 to S1 performance level of the standard tier (i.e., increasing the DTUs from 10 to 20) equates to doubling the processing power of the database.

In this chapter, we will explain what a DTU is and why choosing the right performance level is very important. We will also look at the various performance optimization and performance monitoring features available in Microsoft Azure SQL Database.

What Is a DTU?

A DTU is a logical representation to create a liaison between a specific and defined workload and the Azure SQL DB SKU/Performance levels. As mentioned in Chapter 8, in a very simplified description, one DTU equates to the resources required to achieve a transactional rate of ∼1 transaction/sec on the database. Measuring performance in terms of DTUs provides a way to guarantee predictable performance for Azure SQL databases. For example, a database running with performance level P1 (125 DTUs) will provide a predictable transaction throughput of ∼125 transactions/second.

Choosing a Performance Level

When architecting/designing for on-premises (or Azure VM) deployments of SQL Server, users have traditionally used the machine hardware specs to determine the power available to their database workloads. However, this approach doesn’t work in the platform as a service world, where the hardware details are abstracted.

On-premises deployments are constrained by the fact that a lot of deliberation and calculations need to be put in before choosing the hardware specs, since scaling up the deployment might require fresh hardware investments.

Choosing the correct performance level in Azure SQL Database environment boils down to understanding the transaction throughput requirements for the database and then choosing the appropriate performance level for the database. If the chosen performance level does not meet the requirements, it’s very easy to scale up (or down) the database to a different performance level, which provides a higher (or lower) throughput. Microsoft has published performance benchmarks for the different performance levels, whereby the throughput of each performance level has been summarized in terms of transaction rates per hour, per minute, and per second. The performance benchmark numbers and their summarizations can be used to determine the approximate performance levels required for the database.

Changing the Performance Level

As mentioned earlier, SQL Database service tiers or performance levels can be changed very easily using either the Azure Portal or PowerShell. This ability to change performance levels comes in handy when the user has chosen an incorrect service tier or performance level to start with or when there is a need to reduce/increase the performance level because the business is expecting a decrease/increase in the database operations.

Changing service tiers is an online operation, which means the database remains online while the change is taking effect.

PowerShell to Change the Service Tier or Performance Level

PowerShell can be utilized to configure the service tier or performance Level for a SQL database. Listing 10-1 changes the service tier of an existing database to Standard Edition, S0 performance level.

Listing 10-1. PowerShell Script to Change the Service Tier and Performance Level for an Azure SQL Database
#login into the Azure Account
Add-AzureRmAccount


#Select the Subscription
$subscriptions = Get-AzureRmSubscription
$SubscriptionId = $subscriptions[0].SubscriptionId
Select-AzureRmSubscription -SubscriptionId $SubscriptionId


#Select the Resource Group
$ResourceGroupName = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -notlike "Default*"}
$ResourceGroup= $ResourceGroupName[1].ResourceGroupName


#Select the Azure SQL Server and Database Name
$ServerName = (Get-AzureRmSqlServer -ResourceGroupName $ResourceGroup)[1].ServerName
$DatabaseName = (Get-AzureRmSqlDatabase -ServerName $ServerName -ResourceGroupName $ResourceGroup | Where-Object {$_.DatabaseName -ne "Master"}).DatabaseName


#select the new Service Tier and Performance Level
$NewEdition = "Standard"
$NewPerformanceLevel = "S0"


# Change the Service Tier/Performance Level
$ScaleRequest = Set-AzureRmSqlDatabase -DatabaseName $DatabaseName -ServerName $ServerName -ResourceGroupName $ResourceGroup -Edition $NewEdition -RequestedServiceObjectiveName $NewPerformanceLevel

Using Azure Portal to Change the Service Tier or Performance Level

Azure Management Portal provides a single-click mechanism to change the service tiers or performance levels for your database. Figures 10-1 and 10-2 show how the service tier and the performance level of an Azure SQL Database can be changed using the Azure Management Portal.

A371128_1_En_10_Fig1_HTML.jpg
Figure 10-1. Changing database service tiers using the Azure Portal
A371128_1_En_10_Fig2_HTML.jpg
Figure 10-2. Selecting the new performance level

Azure SQL Performance Optimization Features

Azure SQL Database provides a few useful features that can be used to optimize performance of SQL Database. The most important of these features are:

  • In-Memory Optimizations

  • Query Performance Insights

  • SQL Database Index Advisor

In-Memory Optimizations

In-Memory Optimization features present in Azure SQL Database are very similar to the ones in Microsoft SQL Server 2014 (and the upcoming SQL 2016). In-memory optimization (in-memory tables and natively compiled SPs) and ColumnStore indexes can be used to increase the performance of both OLTP and analytics workloads. Moreover, the combination of the two can be used to provides near real-time analytics.

SQL Database Index Advisor

Azure SQL Database Index Advisor provides recommendations for indexes to be created (only non-clustered indexes) or dropped (only duplicate indexes at the time of writing) on the SQL Database. Index Advisor can be configured to automatically apply the index recommendations to the database. If the recommendations do not help performance, they can be easily rolled back. Automating index recommendations requires that query store be enabled on the Azure SQL Database.

Index recommendations can be accessed on the Azure Portal, as shown in Figure 10-3.

A371128_1_En_10_Fig3_HTML.jpg
Figure 10-3. Access the Index Advisor on Azure Portal

To get index recommendations, a database needs a substantial amount of consistent usage and activity . Index Advisor provides better recommendations when there is consistent activity rather than inconsistent one-time burst activities. As shown in Figure 10-4, advisor settings can be configured to automatically create or drop the indexes suggested. Note that this would be an online operation and may impact queries running against the database.

A371128_1_En_10_Fig4_HTML.jpg
Figure 10-4. Configuring Index Advisor settings

If there are index recommendations available, they will be displayed on the page. If there are none, the advisor provides a reason as to why that’s so. For example, in Figure 10-4, the reason for not having any recommendations is the absence of activity on the database.

SQL Database Query Performance Insights

Query Performance Insights provide a very simplified way to monitor and troubleshoot performance of an Azure SQL Database. Query Performance Insights requires query store to function and can provide detailed information about query performance and DTU consumption.

Query Performance Insights can be accessed from the Settings page of the Azure SQL Database, as illustrated in Figure 10-5.

A371128_1_En_10_Fig5_HTML.jpg
Figure 10-5. Query Performance Insights

Query Performance Insights can be used to determine the overall DTU utilization and the utilization per query. Execution details of each query can be accessed from the portal. For example, as illustrated in Figure 10-6, you can see the query text and the corresponding CPU and DTU usage by the query. Query Performance Insights settings can be tweaked to display statistics for different time periods and for different number of queries, as illustrated in Figure 10-7.

A371128_1_En_10_Fig6_HTML.jpg
Figure 10-6. Detailed Query Performance details
A371128_1_En_10_Fig7_HTML.jpg
Figure 10-7. Query Performance Insights settings

Query Performance Insights are based on query store, and it’s possible that query store can run out of storage space. When query store runs out of storage space, it will go into a read-only mode, thereby not allowing storage of any more query performance data. It is important to set up correct retention and clean-up policies for query store. Clean-up can be set to AUTO (SQL will run cleanup whenever the max size limit is reached) or a time-based retention.

ALTER DATABASE [YourDB]
SET QUERY_STORE (SIZE_BASED_CLEANUP_MODE = AUTO);


ALTER DATABASE [YourDB]
SET QUERY_STORE (CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30));

Query Performance Insights provides a great method to figure out problematic queries and optimize them. For example, you can get the top CPU consuming queries and tune them before they have any major performance impacts on the database.

Monitoring SQL Database

Azure provides multiple ways to monitor performance, resource utilization, and security for the SQL databases. This can be done using either Azure Portal or using DMVs (Dynamic Management View) exposed at both the database and logical server level or by using extended events configurable on the SQL database.

Using Azure Portal

Azure Portal provides a very convenient way to monitor resource utilization for SQL Database. Counters like CPU percentage, DTU percentage, and so on, can be monitored from the Azure Portal. This information can be utilized to ensure optimal health of the database. The monitoring tab on the Azure Portal (under the database details) can be utilized to monitor the resource utilization for a database (see Figure 10-8). Also, the metrics and the time period settings can be configured using the Management Portal, as per the requirements (see Figure 10-9).

A371128_1_En_10_Fig8_HTML.jpg
Figure 10-8. Monitoring SQL Database resource utilization
A371128_1_En_10_Fig9_HTML.jpg
Figure 10-9. Configuring monitoring metrics

The monitoring graphs can be edited to add multiple other counters, as shown in Figure 10-9.

Once the metrics have been configured, the resource utilization can be monitored on the Azure Portal, as illustrated in Figure 10-10.

A371128_1_En_10_Fig10_HTML.jpg
Figure 10-10. Monitoring metrics on theportal

Azure Portal also allows users to configure alerts against any of the available metrics (see Figure 10-11). For example, you can set up an e-mail based alert to indicate if the database crosses 80% of the maximum allowed size for the performance level, or if the DTU utilization percentage crosses 80% of the DTUs for that service tier. This information can be used to determine if the database performance level needs to be changed.

A371128_1_En_10_Fig11_HTML.jpg
Figure 10-11. Configure alerts for resource utilization metrics

The alerts can be configured with the desired threshold values and can be configured to send e-mails to admins or other users. These settings can be changed using the Add an Alert Rule page (see Figure 10-12) .

A371128_1_En_10_Fig12_HTML.jpg
Figure 10-12. Configuring alert properties

Using DMV and Catalog Views

DMVs and catalog views exposed for both the database and logical servers can be used to report and/or monitor the performance, resource utilization, database object details, query execution information, and so on, of a SQL database and the logical server.

Running queries against the catalog view and DMVs requires View Server State and View Database State permissions. Some of the most commonly used DMVs are mentioned next.

Resource Utilization

Detailed information about the resource utilization (both at the logical server level and the database level) can also be obtained using the catalog views available with Azure SQL Database.

  • Sys.resource_stats. Provides resource utilization information at the logical server level. The information is collected every five minutes for all the databases running on the logical server.

  • sys.dm_db_resource_stats. Provides resource utilization information for the database. This information is collected every 15 seconds.

  • sys.event_log. Provides information about connectivity, throttling, and deadlock events on the databases.

Database Related Information

As with resource related DMVs and catalog views, Azure SQL Database also provides a number of DMVs and catalog view to provide database-related information like the space usage, wait statistics, etc.

  • sys.dm_db_file_space_usage. Provides information about the space usage in the database.

  • sys.dm_db_wait_stats. Provides wait statistics information for all the operations in the database level. This DMV can be utilized to diagnose performance issues with the database and other query execution.

  • sys.dm_database_copies. Provides information about the geo-replication copies for the database. Details like maximum_lag and replication_state_desc can be used to determine the latency and status of replication.

Execution Related Information

Most of the execution related DMVs available with Microsoft SQL Server are also available with Azure SQL Databases and can be used to determine the performance issues, execution related issues, etc. The following are some of the commonly used DMVs:

  • sys.dm_exec_requests/sys.dm_exec_sessions/sys.dm_exec_connections. Provides information about the sessions connected to (or active requests executing on) the Azure SQL Database.

  • sys.dm_exec_query_stats, sys.dm_exec_function_stats,sys.dm_exec_procedure_stats, sys.dm_exec_trigger_stats. Provide aggregated execution statistics for queries, procedures, functions, and triggers, respectively. These DMVs can be utilized to determine the top duration/CPU consuming/read/write queries executing on the server. Entries from these DMVs are removed when the corresponding queries or objects are removed from the cache.

  • sys.dm_exec_query_memory_grants. Provides information about memory grants (pending or granted) for each query running on the database.

Other DMVs are available with Azure SQL Database to monitor various details like database indexes, security of the database or the logical server, etc. A complete list of all the DMVs is available on MSDN.

Using Extended Events

Extended Events or (XEvents) are not new. They have been available with Microsoft SQL Server since the SQL Server 2008 days. There has been a lot of optimization and improvements done to this feature over the last several years. The amount of information available through XEvents is enormous, which can be determined by the fact that the upcoming release of SQL Server, SQL Server 2016, has almost 1,200 events exposed.

Extended Events available on Azure SQL Database is only a subset of the feature available with SQL Server. These extended events are scoped to a single Azure SQL database, meaning that an extended event running against one Azure SQL database cannot be used to monitor events against another database on the same logical server.

Listing 10-2 provides a sample script that can be used to determine the events/actions available with Extended Events on Azure SQL Database.

Listing 10-2. T-SQL Script to Determine the Events or Actions Available with Azure SQL Database
SELECT
        o.object_type,
        p.name         AS [package_name],
        o.name         AS [db_object_name],
        o.description  AS [db_obj_description]
    FROM
                   sys.dm_xe_objects  AS o
        INNER JOIN sys.dm_xe_packages AS p  ON p.guid = o.package_guid
    WHERE
        o.object_type in
            (
            'event','action'
            )
    ORDER BY
        o.object_type,
        p.name,
        o.name;

The easiest way to configure Extended Events for Azure SQL Database is to use SQL Server Management Studio, as illustrated in Figure 10-13. In SSMs, Extended Events can be accessed by expanding the database node.

A371128_1_En_10_Fig13_HTML.jpg
Figure 10-13. Accessing Extended Events

A new session can be configured by right-clicking on the session node and choosing either New Session Wizard or New Session. Either of the options are fairly straightforward to use.

Following is a sample T-SQL script to create an XEvent to monitor blocking and deadlock:

CREATE EVENT SESSION [Sample_XEvents] ON DATABASE

ADD EVENT sqlos.wait_info(
    ACTION(sqlserver.database_name,sqlserver.session_id,sqlserver.sql_text)),
ADD EVENT sqlserver.blocked_process_report(
    ACTION(sqlserver.database_name,sqlserver.session_id,sqlserver.sql_text)),
ADD EVENT sqlserver.database_xml:deadlock_report(
    ACTION(sqlserver.database_name,sqlserver.session_id,sqlserver.sql_text)),
ADD EVENT sqlserver.lock_deadlock(
    ACTION(sqlserver.database_name,sqlserver.session_id,sqlserver.sql_text)),
ADD EVENT sqlserver.sp_statement_completed(
    ACTION(sqlserver.database_name,sqlserver.session_id,sqlserver.sql_text)),
ADD EVENT sqlserver.sql_batch_completed(
    ACTION(sqlserver.database_name,sqlserver.session_id,sqlserver.sql_text))


ADD TARGET package0.event_file(
                 SET filename=N'https://YourStorageAccountName.blob.core.windows.net/ContainerName/OutputFile.xel'),
ADD TARGET package0.ring_buffer


WITH (STARTUP_STATE=ON)
GO

Similar sessions can be created to monitor other events on the SQL Database.

Control permission is required to create an Extended Events. This permission is available by default to the DBO user.

Information about currently running XEvent sessions and other information regarding the events, actions, targets, etc. exposed through XEvents can be derived from the several DMVs and catalog views exposed by Azure SQL Database. Following are some of the most commonly used DMVs and catalog views :

  • sys.dm_xe_database_session_events. Exposes information about the events configured for the currently active sessions.

  • sys.dm_xe_database_sessions. Exposes information about the currently active Extended Event sessions.

  • sys.database_event_sessions. Exposes information about all the XEvents sessions configured on the SQL database.

  • sys.dm_xe_objects. Provides information about the events, actions, targets, etc. exposed by XEvents on Azure SQL Database. This DMV is similar to the one available with Microsoft SQL Server.

Extended Events can be useful in determining performance issues or other issues with the Azure SQL Database. But take care not to overload the database with too many sessions or events being captured. Doing so can cause over-committing memory and ultimately lead to database performance issues.

Summary

This chapter discussed the Database Throughput Unit (DTU) and how choosing the right performance level is of paramount importance. We discussed how the services tier and the performance level of an Azure SQL database can be changed and then talked about the various performance optimization and monitoring features available with Azure SQL Database and on the Azure Management Portal.

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

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