Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Automating Azure SQL DB index and statistics maintenance using Azure Automation

To provide complete solution to maintain you Azure SQL DB statistics and maintenance we provide our maintenance script here

In this article, we will explain step by step how to automate this maintenance on Azure (You can also use that to automate your own T-SQL tasks)

General steps:

  1. Create Azure Automation account
  2. Import SQLServer module
  3. Add Credentials to access SQL DB
  4. Add a runbook to run the maintenance
  5. Schedule task

Step by step instructions:

  1. Create new automation account
  2. Login to your Azure portal and click "New" (the green plus sign)
  3. Type "automation" in the search box, and choose automation.

    Figure 1 – new automation account

  4. Click "create"
  5. Fill the form, choose a name for your automation account, and choose in which resource group it will be placed.

    make sure you choose "YES" for the Create Azure Run As account.

    Figure 2 – add automation account form.

  6. Click "create" and wait for the account to be created. The new automation configuration blade will be opened once the provision completed.
  7. Import SQLServer module
  8. Click on "Modules" at the left options panel, and then click on "Browse Gallery" and search for "SQLServer"

    Figure 3 – add module

  9. Choose "SqlServer" by matteot_msft

    Figure 4 – module name

  10. Then click on "import" and the "OK"
  11. Wait for the import to complete
  12. Add Credentials to access SQL DB
  13. This will use secure way to hold login name and password that will be used to access Azure SQL DB

    You can skip this and use it as clear text if you like to use clear text skip to the next step.

  14. Under "Shared Resources" click on credentials

    Figure 5 – Add new credential object

  15. Then click on "Add Credential"
  16. Type "SQLLogin" as the name of the credential.
  17. In the username field type the SQL Login that will be used for maintenance and its password.
  18. Click "Create"
  19. Add a runbook to run the maintenance
  20. Click on "runbooks" at the left panel and then click "add a runbook"

    Figure 6 – Add a runbook

  21. Choose "create a new runbook" and then give it a name and choose "Powershell" as the type of the runbook and then click on "create"

    Figure 7 – add new PowerShell runbook

  22. Copy and paste the following row to the new runbook.

    Make sure you change your database properties.

    </span>$AzureSQLServerName = "<ServerName>"
    $AzureSQLDatabaseName = "<DatabaseName>"
    
    $AzureSQLServerName = $AzureSQLServerName + ".database.windows.net"
    $Cred = Get-AutomationPSCredential -Name "SQLLogin"
    $SQLOutput = $(Invoke-Sqlcmd -ServerInstance $AzureSQLServerName -Username $Cred.UserName -Password $Cred.GetNetworkCredential().Password -Database $AzureSQLDatabaseName -Query "exec [dbo].[AzureSQLMaintenance] @Operation='all' ,@LogToTable=1" -Verbose) 4>&1
    
    Write-Output $SQLOutput<span style="font-family: Lucida Console; font-size: 9pt;" data-mce-style="font-family: Lucida Console; font-size: 9pt;"></span><span style="font-family: Lucida Console; font-size: 9pt;" data-mce-style="font-family: Lucida Console; font-size: 9pt;">

    TIP: to redirect the verbose output to the runbook log we use the technique as described here

  23. Click on Publish and confirm.
  24. Schedule task
  25. Click on Schedules

    Figure 8 – Schedules

  26. Click on "Add a schedule" and follow the instructions to choose existing schedule or create a new schedule.
  27. Choose a time when the application is in the idlest figure, as running the maintenance might impact on performance while it's executing.

    Figure 9 – Create new Schedule

More information:

Getting Started with Azure Automation

Create a standalone Azure Automation account

My first PowerShell runbook

Share the post

Automating Azure SQL DB index and statistics maintenance using Azure Automation

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×