| Author |
Topic |
|
Gyte
Starting Member
23 Posts |
Posted - 2007-02-12 : 08:08:13
|
| Hello, I'm migrating from SQL-Server 2000 to SQL-Server 2005 and one query seems very slow. The below query is fast in SQL-Server 2000, but in SQL-Server 2005 the same query is slow.SELECT tblContact.V8000 AS Companyname,tblContact.V91000 AS Companynumber,tblV.Vcyclusnumber AS cyclusnumber,tblReport.B231 AS SalesDataFROM tblContactINNER JOIN tblV ON tblV.Vclientnumber = tblContact.V91000LEFT JOIN tblReport ON tblV.Vcyclusnumber = tblReport.RcyclusnumberWHERE tblReport.ID_tblReport IS NULLOR tblReport.ID_tblReport IN (SELECT MAX(ID_tblReport) FROM tblReport GROUP BY Rcyclusnumber) |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-02-12 : 08:10:56
|
| What does the execution plan and IO statistics looks like on both SQL 2000 and 2005?Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-12 : 08:16:17
|
| Do you have the same indexes in the tables?Are there the same amount of records?Peter LarssonHelsingborg, Sweden |
 |
|
|
Gyte
Starting Member
23 Posts |
Posted - 2007-02-12 : 08:40:37
|
| Dear Harsh Athalye,I don't know where to find the execution plan and IO statistics.Dear Peter Larsson,All tables are indexed, but none of the tables has a primary key defined.I tried your query in SQL-Server 2005 and there was the following error message : "The TOP N WITH TIES clause is not allowed without a corresponding ORDER BY clause." |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-02-12 : 08:45:06
|
To see execution plan along with IO statistics run following commands:SET STATISTICS IO ONGOSET SHOWPLAN_TEXT ONGO-- Your query hereSELECT ....GOSET STATISTICS IO OFFGOSET SHOWPLAN_TEXT OFFGO Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-12 : 08:58:36
|
Try this, since you are using SQL Server 2005SELECT c.V8000 AS Companyname, c.V91000 AS Companynumber, v.Vcyclusnumber AS cyclusnumber, r.B231 AS SalesDataFROM tblContact AS cINNER JOIN tblV AS v ON v.Vclientnumber = c.V91000LEFT JOIN ( SELECT B231, Rcyclusnumber FROM ( SELECT B231, Rcyclusnumber, ROW_NUMBER() OVER (PARTITION BY Rcyclusnumber ORDER BY ID_tblReport DESC) AS rownum, ID_tblReport FROM tblReport ) AS x WHERE x.rownum = 1 OR x.ID_tblReport IS NULL ) AS r ON r.Rcyclusnumber = v.Vcyclusnumber Peter LarssonHelsingborg, Sweden |
 |
|
|
Gyte
Starting Member
23 Posts |
Posted - 2007-02-12 : 10:27:54
|
| Thank you Peter Larsson.The query you send me did the job. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-12 : 10:42:56
|
| What is the speed difference now?Peter LarssonHelsingborg, Sweden |
 |
|
|
Gyte
Starting Member
23 Posts |
Posted - 2007-02-12 : 10:48:41
|
| The speed difference is huge. The first query was very slow and sometimes generated a time out. The new query generates the results in about 10 seconds. This is quite an improvement. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-12 : 11:16:56
|
Try this one on the SQL 2000 server and compare speedSELECT c.V8000 AS Companyname, c.V91000 AS Companynumber, v.Vcyclusnumber AS cyclusnumber, r.B231 AS SalesDataFROM tblContact AS cINNER JOIN tblV AS v ON v.Vclientnumber = c.V91000LEFT JOIN ( SELECT x.B231, x.Rcyclusnumber FROM ( SELECT TOP 1 WITH TIES B231, Rcyclusnumber, ID_tblReport FROM tblReport ORDER BY ID_tblReport DESC UNION SELECT B231, Rcyclusnumber, NULL FROM tblReport WHERE ID_tblReport IS NULL ) AS x ) AS r ON r.Rcyclusnumber = v.Vcyclusnumber Peter LarssonHelsingborg, Sweden |
 |
|
|
|