Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two separate instances of SQL Server running a different explain plan

Tags:

sql-server

Here's one I need help from the SQL administrators out there. I have two separate SQL Server instances on Amazon EC2. One is our staging environment, and the other is our production environment, but they are configured exactly the same way (spawned from the same image).

We had a database that we copied from staging to our production environment last week. The way we copy a db to production is we take a backup of it on our staging site, and restore the backup in production. Anyways, we found that in production, one particular complex query was timing out after an hour, but that exact query in our staging environment completed in 10 minutes.

The explain plan on both were almost the same, except in one server it was doing a PK scan on a large table (8M rows), and on the other table it was doing an index seek. We're assuming this was the difference. So one server was doing a lot of disk IO, and the other was not.

So my question is, what are the reasons that one installation of SQL server would decide to use an index, while another one ignores it--assuming same versions of SQL server, and same data set? Even better, what are the best ways to find out why SQL is ignoring an index?

like image 810
Linus Avatar asked Apr 21 '11 12:04

Linus


People also ask

How do I run two instances of SQL Server?

You can install multiple instances of SQL Server, or install SQL Server on a computer where earlier SQL Server versions are already installed. The following SQL Server-related items are compatible with the installation of multiple instances on the same computer: Database Engine.

Can we create multiple instances of SQL Server on the same machine elaborate?

Yes, we can use multiple instance of SQL Server of same machine/server. All instances of the database engine other than the default instance are identified by an instance name specified during installation of the instance.

Can multiple SQL Server instances run on the same server?

SQL Server considerations for running multiple instances on the same server: As long as each SQL Server instance is installed as a unique named instance, there should be no conflict between them. Each SQL Server instance is installed to a separate directory.

Which of the following would be reasons for using more than one instance of an SQL Server?

Multiple instances are ideal for those pesky third party applications that require elevated SQL Server privileges where you do not want to commingle applications on the same instance.


2 Answers

SQL Server uses statistics to determine the query execution plan.

Normally, they should be the same on the same datasets, but there is a chance of outdated statistics on one of the machines.

Use sp_updatestats to update statistics on both machines.

Also, I'm not familiar with Amazon EC2, but there may be a chance that the machines running the two instances have different number of CPU installed (or made available for use by SQL Server). This is also taken into account by the optimizer.

like image 117
Quassnoi Avatar answered Sep 30 '22 14:09

Quassnoi


Parameter Sniffing?

An SP will use the query plan that was deemed most appropriate based on the parameters passed to it when it was executed (and so compiled) for the first time.

Restoring a database wipes the plan cache; if the SP on the copy of the database was run with parameters that favored an index seek, then that's what will subsequently be used.

You can check this by sp_recompile'ing both and running them again with identical parameters.

like image 36
Alex K. Avatar answered Sep 30 '22 14:09

Alex K.