Scheduling Statistics Maintenance in Azure SQL Data Warehouse
You can leverage your Azure SQL Data Warehouse to automate some of your maintenance. Creating a Runbook will let you schedule to your hearts content.
Join the DZone community and get the full member experience.Join For Free
the power and capabilities of azure sql data warehouse are pretty amazing. however, it’s still basically sql server under the covers. sql server still needs maintenance and one type of maintenance is keeping statistics up to date. statistics maintenance is doubly important within azure sql data warehouse because statistics are not created automatically, nor are they maintained automatically. it’s up to you to ensure your statistics are up to date.
statistics in azure sql data warehouse
it’s worth re-emphasizing the point that there is no automatic creation of statistics in azure sql data warehouse. this means you need to plan to add statistics to your tables. microsoft recommends an easy method is to add them to every column. while that does make things easy, it’s not like statistics are free. if you add them to every column, you’ll also have to maintain them, on every column. that could be costly. i’d rather see a targeted approach, but that will require additional work and a good understanding of how the data warehouse is being used. that’s not always possible, hence microsoft’s suggestion. your methods of data loading to your warehouse are also going to drive your statistics maintenance routines. it’s a complex dance that goes beyond the scope of this blog post. to read more about what microsoft has to say about statistics in azure sql data warehouse, read this article .
because we’ve moved into a platform as a service (paas) offering with azure sql data warehouse, we don’t have the types of automation mechanisms we’re accustomed to. by that i mean, there is no sql agent. instead, we have azure automation:
i have an azure sql data warehouse i’m using for teaching this topic while on sql cruise . let’s go ahead and add an automation account:
note : the most important habit you can start within azure is putting everything into discrete, planned, resource groups. these make management so much easier.
once the account is set, the first thing you need is to create a runbook. there is a collection of them for your use within azure. none of them are immediately applicable for what i need. i’m just writing a really simple powershell script to do what i want:
$sqlusername = 'grant' $sqlpwd = 'xxx' # define the connection to the sql database $conn = new-object system.data.sqlclient.sqlconnection("server=tcp:cruise2017.database.windows.net;database=cruisers;user id=$sqlusername;password=$sqlpwd;trusted_connection=false;encrypt=true;connection timeout=30;") # open the sql connection $conn.open() # define the sql command to run. in this case we are getting the number of rows in the table $cmd=new-object system.data.sqlclient.sqlcommand("update statistics dbo.dimreseller", $conn) # execute the sql command $cmd.executenonquery() # close the sql connection $conn.close()
nothing to it. yes, for a production system, you’ll want to be able to loop through all your tables, pick and choose which statistics get updated, etc. i’ll leave that to you to figure out for your system. it’s straightforward stuff.
note : thanks to denny cherry and mike fal for some pointers on troubleshooting setting this up. there’s a test pane that you need to make liberal use of along with write-output commands in order to identify where things are going south.
with the runbook complete, save it, and publish it:
once i’ve published my runbook, i can simply click on the schedule button in order to decide when i want it to run:
obviously, you can pretty easily set up the schedule in all the standard methods available to you in sql agent. once this is done, you’re all set. you’ve just successfully created a schedule for updating statistics in azure sql data warehouse.
while a long discussion can be had around when and where you need to create statistics inside azure sql data warehouse, there is no doubt that you will need to update those statistics. it can be just simply a part of your load process. however, if you have a trickle load, or nightly updates, you’ll want to explore using azure automation to make that happen.
Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.