Query much slower on 2005 compared to 2000

We have a database developed using SQL Server 2000. We are in the process of testing it on SQL Server 2005. So far i've not done much testing but it has become apparant quickly that quite a lot of queries are performing much slower in SQL Server 2005.

I am currently trying to figure out why a particular query is not performing well. One SQL Server 2000 it runs in around 6 seconds. On SQL Server 2005 it takes around 50 seconds. The query is this...

SELECT StartBase, StartPoint, PickupPoint, PickupInstructions, SingleJourney, Destination
FROM vwJobs
WHERE StartDateTime BETWEEN '2006-09-01' AND '2006-09-23'

vwJobs is a view in our database. The view includes many UDF's and also has a UNION which combines two seperate table structures.

Regardless of whether UDF's etc are a poor way of retrieving data, why is it that 2005 is so much slower than 2000?

Note: The tests are being performed on the same machine. I've also tried rebuilding the indexes on the 2005 database but I can't find any reason as to why it is so much slower.

[1088 byte] By [ChrisW] at [2007-12-25]
# 1
?

Did you set the compatability level to 90, in

addition to rebuilding the indexes?

I'm not surprised by queries that perform worse in

2005 than in 2000 -- the query optimizer has undergone a lot of changes and the

rules are a bit different. Your best bet is to look at the execution plan

and figure out how best to re-do the query. I've done a few upgrade

projects now, and each time have had to do some code rewrite to

maintain high performance.

By the way, you're testing on the same box -- did

you shut down SQL Server 2000 before trying the query? Are you sure the

2005 instance has enough RAM, etc, and that you're not seeing a resource

contention issue? Are the 2000 and 2005 DB files using the same physical

disks? Any differences between the configurations?

face=Arial size=2>
--
Adam

Machanic
Pro SQL Server 2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

<Chris href="mailto:W@discussions.microsoft.com">W@discussions.microsoft.com>

wrote in message href="news_3A77fd34f3-2da4-46af-84af-89dbf23d17b0_40discussions.microsoft.com">news:77fd34f3-2da4-46af-84af-89dbf23d17b0@discussions.microsoft.com...

We have a database developed using SQL Server 2000. We are in the process

of testing it on SQL Server 2005. So far i've not done much testing but it has

become apparant quickly that quite a lot of queries are performing much slower

in SQL Server 2005.

I am currently trying to figure out why a particular query is not

performing well. One SQL Server 2000 it runs in around 6 seconds. On SQL

Server 2005 it takes around 50 seconds. The query is this...

SELECT StartBase, StartPoint, PickupPoint, PickupInstructions,

SingleJourney, Destination
FROM vwJobs
WHERE StartDateTime BETWEEN

'2006-09-01' AND '2006-09-23'

vwJobs is a view in our database. The view includes many UDF's and also has

a UNION which combines two seperate table structures.

Regardless of whether UDF's etc are a poor way of retrieving data, why is

it that 2005 is so much slower than 2000?

Note: The tests are being performed on the same machine. I've also tried

rebuilding the indexes on the 2005 database but I can't find any reason as to

why it is so much slower.

MVPUser at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 2

Thanks for the reply. Yes I did change the compatibility level to 90.

RAM could be an issue if 2005 does require more memory, so yes I would ideally like to compare this on a high spec machine. However my machine is a reasonable specification and I have double the RAM than recommended for the edition of 2005 I am using. This specification of the machine is similar to what a lot of our customers have. Therefore unless I can make performance as good as SQL Server 2000 then users are not going to upgrade. This is a worrying issue bearing in mind Vista/Longhorn will not support SQL Server 2000 and from what I've seen so far quite a lot of our queries are slow and could take a long time to fix.

Both 2000 and 2005 were on the same disks and I don't think there are any differences in the configurations. Shutting down SQL Server 2000 made no difference.

NNTP User wrote:
?

Did you set the compatability level to 90, in addition to rebuilding the indexes?

I'm not surprised by queries that perform worse in 2005 than in 2000 -- the query optimizer has undergone a lot of changes and the rules are a bit different. Your best bet is to look at the execution plan and figure out how best to re-do the query. I've done a few upgrade projects now, and each time have had to do some code rewrite to maintain high performance.

By the way, you're testing on the same box -- did you shut down SQL Server 2000 before trying the query? Are you sure the 2005 instance has enough RAM, etc, and that you're not seeing a resource contention issue? Are the 2000 and 2005 DB files using the same physical disks? Any differences between the configurations?


--
Adam Machanic
Pro SQL Server 2005, available now
http://www..apress.com/book/bookDisplay.html?bID=457
ChrisW at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 3
?

So are you seeing differences in the query

plans? Can you share some additional details?


--
Adam Machanic
Pro SQL Server

2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

<Chris href="mailto:W@discussions.microsoft.com">W@discussions.microsoft.com>

wrote in message href="news_3A0226eb63-31ea-43d1-a5a9-48292eeda360_40discussions.microsoft.com">news:0226eb63-31ea-43d1-a5a9-48292eeda360@discussions.microsoft.com...

Thanks for the reply. Yes I did change the compatibility level to 90.

RAM could be an issue if 2005 does require more memory, so yes I would

ideally like to compare this on a high spec machine. However my machine is a

reasonable specification and I have double the RAM than recommended for the

edition of 2005 I am using. This specification of the machine is similar to

what a lot of our customers have. Therefore unless I can make performance as

good as SQL Server 2000 then users are not going to upgrade. This is a

worrying issue bearing in mind Vista/Longhorn will not support SQL Server 2000

and from what I've seen so far quite a lot of our queries are slow and could

take a long time to fix.

Both 2000 and 2005 were on the same disks and I don't think there are any

differences in the configurations. Shutting down SQL Server 2000 made no

difference.

[quote user="NNTP User"]?

Did you set the compatability level to 90, in

addition to rebuilding the indexes?

I'm not surprised by queries that perform worse

in 2005 than in 2000 -- the query optimizer has undergone a lot of changes and

the rules are a bit different. Your best bet is to look at the execution plan

and figure out how best to re-do the query. I've done a few upgrade projects

now, and each time have had to do some code rewrite to maintain high

performance.

By the way, you're testing on the same box -- did

you shut down SQL Server 2000 before trying the query? Are you sure the 2005

instance has enough RAM, etc, and that you're not seeing a resource contention

issue? Are the 2000 and 2005 DB files using the same physical disks? Any

differences between the configurations?

face=Arial size=2>
--
Adam

Machanic
Pro SQL Server 2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www...apress.com/book/bookDisplay.html?bID=457
[/quote]

MVPUser at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 4

I tried running the query plans against both 2000 and 2005. With 2000 it took 40 seconds to display the query plan. With 2005 I gave up after 39 minutes!

So I then decided to show the estimated query plan instead. With 2000 it showed one query as i've always been used to. With 2005 it was showed 27 seperate queries. I think they were all the seperate UDF's in my view. So I therefore decided that fixing the UDF's was the way to go.

I've managed to change all of the UDF's that involved queries to be a subquery instead. This left me with just a few UDF's that deal with string concatination etc. After these changes I now have these results...

Old Query in SQL 2000 - 23 seconds
Old Query in SQL 2005 - 39 seconds
New Query in SQL 2000 - 17 seconds
New Query in SQL 2005 - 18 seconds

So i've got it from a gap of 16 seconds with my old query to just one second. Therefore I can only assume 2005 can't handle UDF's anywhere near as well as 2000 (at least on my machines specification anyway).

Thanks.

NNTP User wrote:
?

So are you seeing differences in the query plans? Can you share some additional details?


--
Adam Machanic
Pro SQL Server 2005, available now
http://www..apress.com/book/bookDisplay.html?bID=457
ChrisW at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 5
Chris W wrote:

Thanks for the reply. Yes I did change the compatibility level to 90.

RAM could be an issue if 2005 does require more memory, so yes I would ideally like to compare this on a high spec machine. However my machine is a reasonable specification and I have double the RAM than recommended for the edition of 2005 I am using. This specification of the machine is similar to what a lot of our customers have. Therefore unless I can make performance as good as SQL Server 2000 then users are not going to upgrade. This is a worrying issue bearing in mind Vista/Longhorn will not support SQL Server 2000 and from what I've seen so far quite a lot of our queries are slow and could take a long time to fix.

Both 2000 and 2005 were on the same disks and I don't think there are any differences in the configurations. Shutting down SQL Server 2000 made no difference.

NNTP User wrote:

?

Did you set the compatability level to 90, in addition to rebuilding the indexes?

I'm not surprised by queries that perform worse in 2005 than in 2000 -- the query optimizer has undergone a lot of changes and the rules are a bit different. Your best bet is to look at the execution plan and figure out how best to re-do the query. I've done a few upgrade projects now, and each time have had to do some code rewrite to maintain high performance.

By the way, you're testing on the same box -- did you shut down SQL Server 2000 before trying the query? Are you sure the 2005 instance has enough RAM, etc, and that you're not seeing a resource contention issue? Are the 2000 and 2005 DB files using the same physical disks? Any differences between the configurations?


--
Adam Machanic
Pro SQL Server 2005, available now
http://www..apress.com/book/bookDisplay.html?bID=457

try to recompile all your Sp's and UDF's it could still be using procedure cache created by 2000

and needs to be updated in 2005 you can use sp_recompile for this matter

sp_recompile

Causes stored procedures and triggers to be recompiled the next time they are run.

joeydj at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 6
Thanks for the information. sp_recompile made a tiny difference but only a second at best.
ChrisW at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 7

try resolving the tables to two-part (schema.objectname) naming convention

for in-database queries, three-part (databasename.schema.objectname) naming convention

for cross database queries and

fourth-part naming convention for cross server queries

joeydj at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 8

The problem is getting worse for me. I've now tried running my fixed query on larger data files and it is now slower than the original query.

If I call a Sub-Query by doing:-

(SELECT VehicleID FROM dbo.VehicleAllocations WHERE StartDate = dbo.JobDates.StartDate AND AllocationID = dbo.Allocations.AllocationID) AS VehicleID

This is much slower than calling a UDF which effectively does the same thing:-

dbo.GetVehicleID(dbo.JobDates.StartDate, dbo.Allocations.AllocationID) AS VehicleID

The UDF looks like this...

CREATE FUNCTION [dbo].[GetVehicleID] (@StartDate datetime. @AllocationID int)
RETURNS varchar(10) AS
BEGIN
DECLARE @VehicleID varchar(10)
SELECT @VehicleID = VehicleID FROM dbo.VehicleAllocations
WHERE StartDate = @StartDate AND AllocationID = @AllocationID
RETURN @VehicleID
END


If I compare 2 queries, one which calls 3 UDF's similar to the above and another which calls 3 sub-queries and do a simple WHERE clause, the UDF query takes 2 seconds and the Sub-query method takes 13 seconds.

I can't figure out why something which on the face of it look as though it is doing something pretty identical is so different, and only on SQL Server 2005.

ChrisW at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 9

what's the database size? whats the tlog size?

whats the location of your OS? datafiles? and logfiles?

some wild guess

1. have you run a dbcc shrink database or dbcc shrinkfile

your db could be fragmented causing it to react very slow

joeydj at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 10
?

Chris,

Again, what are you seeing in the execution

plan? "Slower" doesn't mean much to me. Are you seeing a hash match

instead of a nested loop operation (which I'd assume you'd see with the

UDF)? What kind of indexes are in place?

Also, you say: size=3>"size=3>one which calls 3 UDF's similar to the above" -- why are you calling 3 of

the same UDF in the query? Why not merge the three calls into one?

Sounds like you're using a lot of extra resources in this query, either on 2000

or 2005 -- just that 2005 is highlighting the problem (which, as I mentioned

before, is not an uncommon situation; you just need to learn how to work with it

a bit differently.)


--
Adam Machanic
Pro SQL Server 2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

<Chris href="mailto:W@discussions.microsoft.com">W@discussions.microsoft.com>

wrote in message href="news_3Ad9dc4e20-4716-4f0f-81e0-25a12ea38a3d_WBRev1__40discussions.microsoft.com">news:d9dc4e20-4716-4f0f-81e0-25a12ea38a3d_WBRev1_@discussions..microsoft.com...

This

post has been edited either by the author or a moderator in the Microsoft

Forums: http://forums.microsoft.com

The problem is getting worse for me. I've now tried running my fixed

query on larger data files and it is now slower than the original query.

If I call a Sub-Query by doing:-

(SELECT VehicleID FROM dbo.VehicleAllocations WHERE StartDate =

dbo.JobDates.StartDate AND AllocationID = dbo.Allocations.AllocationID) AS

VehicleID

This is much slower than calling a UDF which effectively does the same

thing:-

dbo.GetVehicleID(dbo.JobDates.StartDate, dbo.Allocations.AllocationID) AS

VehicleID

The UDF looks like this...

CREATE FUNCTION [dbo].[GetVehicleID] (@StartDate datetime. @AllocationID

int)
RETURNS varchar(10) AS
BEGIN
DECLARE @VehicleID

varchar(10)
SELECT @VehicleID = VehicleID FROM dbo.VehicleAllocations


WHERE StartDate = @StartDate AND AllocationID = @AllocationID
RETURN

@VehicleID
END


If I compare 2 queries, one which calls 3 UDF's similar to the above

and another which calls 3 sub-queries and do a simple WHERE clause, the UDF

query takes 2 seconds and the Sub-query method takes 13 seconds.

I can't figure out why something which on the face of it look as though it

is doing something pretty identical is so different, and only on SQL Server

2005.

MVPUser at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 11

Sorry, for just saying slower. It's a confusing thing this when I am convinced i've tried something earlier and had different results. Time for a break I think and think about it fresh tomorrow!

I've now tried 3 different approaches.

Method 1 - Multiple sub queries
Method 2 - The original query with multiple UDF's
Method 3 - A derived table as a sub query joining to another query to get the values from all of the UDF's used in Method 2.

Method 3 has seen best results generally but not with large data files. Using these data files the query plan has the following results:-

Method 1 - Lots of nested loops plus clustered index scans
Method 2 - A hash match (34%), merge inner join (18%), clustered index scan 30%, + more index scans
Method 3 - A hash match (23%), hash match (16%), Distinct sort (10%) + mix of clustered index scans and merge joins

Method 1 takes 95% of the batch, Method 2 takes 1% and Method 3 takes 4% on this set of data.

NNTP User wrote:
?

Chris,

Again, what are you seeing in the execution plan? "Slower" doesn't mean much to me. Are you seeing a hash match instead of a nested loop operation (which I'd assume you'd see with the UDF)? What kind of indexes are in place?

Also, you say: "one which calls 3 UDF's similar to the above" -- why are you calling 3 of the same UDF in the query? Why not merge the three calls into one? Sounds like you're using a lot of extra resources in this query, either on 2000 or 2005 -- just that 2005 is highlighting the problem (which, as I mentioned before, is not an uncommon situation; you just need to learn how to work with it a bit differently.)


--
Adam Machanic
Pro SQL Server 2005, available now
http://www..apress.com/book/bookDisplay.html?bID=457
ChrisW at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 12

Hi Chris,

As you mentioned, Scalar Functions are poor performers in a SELECT statement so we should concentrate on seeing why the view with subqueries is doing poorly.

Can you post the following information:

JaredKo_Seattle at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 13
?

In addition to STATISTICS IO, it would be nice

to see XML showplan output --

Chris, before running your query do:

SET SHOWPLAN_XML ON

... then send us the XML it produces.


--


Adam Machanic
Pro SQL Server 2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

<Jared Ko _ href="mailto:Seattle@discussions.microsoft.com">Seattle@discussions.microsoft.com>

wrote in message href="news_3A45f76962-a042-4482-ae3d-82a62728e43f_40discussions.microsoft.com">news:45f76962-a042-4482-ae3d-82a62728e43f@discussions.microsoft.com...

Hi Chris,

As you mentioned, Scalar Functions are poor performers in a SELECT

statement so we should concentrate on seeing why the view with subqueries is

doing poorly.

Can you post the following information:

MVPUser at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 14

I will work on doing the table schema and sample data. Firstly here are the results on the SET STATISTICS IO ON...

Table 'Drivers'. Scan count 0, logical reads 1800, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ContractDriverAllocations'. Scan count 3, logical reads 12441, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'DriverTypes'. Scan count 1, logical reads 722, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Vehicles'. Scan count 0, logical reads 2958, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ContractVehicleAllocations'. Scan count 1, logical reads 6023, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'VehicleTypes'. Scan count 1, logical reads 714, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 1, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ContractDates'. Scan count 1, logical reads 3411, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ContractMovements'. Scan count 1, logical reads 306, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Clients'. Scan count 1, logical reads 8, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Contracts'. Scan count 1, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ContractDrivers'. Scan count 1, logical reads 95, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ContractVehicles'. Scan count 1, logical reads 61, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

ChrisW at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
SQL Server
SQL Server Database Engine

SQL Server Hot Topic

SQL Server New Topic

SQL Server

Site Classified