SQL SERVER – Copy Statistics from One Server to Another Server

I was recently working on a performance tuning project in Dubai (yeah I was able to see the tallest tower from the window of my work place). I had a very interesting learning experience there. There was a situation where we wanted to receive the schema of original database from a certain client. However, the client was not able to provide us any data due to privacy issues. The schema was very important because without having an access to underlying data, it was a bit difficult to judge the queries etc. For example, without any primary data, all the queries are running in 0 (zero) milliseconds and all were using nested loop as there were no data to be returned. Even though we had CPU offending queries, they were not doing anything without the data in the tables. This was really a challenge as I did not have access to production server data and I could not recreate the scenarios as production without data.

Well, I was confused but Ruben from Solid Quality Mentors, Spain taught me new tricks. He suggested that when table schema is generated, we can create the statistics consequently. Here is how we had done that:

Once statistics is created along with the schema, without data in the table, all the queries will work as how they will work on production server. This way, without access to the data, we were able to recreate the same scenario as production server on development server.

SQL SERVER - Copy Statistics from One Server to Another Server statsscript1

SQL SERVER - Copy Statistics from One Server to Another Server statsscript2

SQL SERVER - Copy Statistics from One Server to Another Server statsscript3

SQL SERVER - Copy Statistics from One Server to Another Server statsscript4

SQL SERVER - Copy Statistics from One Server to Another Server statsscript5

SQL SERVER - Copy Statistics from One Server to Another Server statsscript6

SQL SERVER - Copy Statistics from One Server to Another Server statsscript7

When observed at the script, you will find that the statistics were also generated along with the query.

You will find statistics included in WITH STATS_STREAM clause.

SQL SERVER - Copy Statistics from One Server to Another Server statsscript8

What a very simple and effective script.

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Statistics
Previous Post
SQL SERVER – Reduce the Virtual Log Files (VLFs) from LDF file
Next Post
SQL SERVER – Beginning SQL Server: One Step at a Time – SQL Server Magazine

Related Posts

Leave a Reply