![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a table on a database that contains 18million records. I need to design a system that queries this table to produce fast counts. I have got counts for multiple criteria down to only a few seconds. Most take under a second however I have a few queries that seam to take longer which I am working on reducing the time. I have found some strange behavour in the way SQL Server works. Take the following two queries which produce exactly the same result: --------------------------------- select count(*) from dbo.table where column1='value1' and column2='value2' -------------------------------- and -------------------------------- select count(*) from (select id from table where column1 = 'value1') as value1 join (select id from table where column2 = 'value2') as value2 on value1.id = value2.id --------------------------------- I would assume that the first query should run faster then the second query. When I look at the query plans, they are almost identical cost wise. The first takes about 53% of the cost and the second takes 47%. Yet, the first query takes about 25 seconds to run and the second takes only 5 seconds. Does anyone know of a reason why there would be such a difference in query speed? |
#3
| |||
| |||
|
|
I have a table on a database that contains 18million records. I need to design a system that queries this table to produce fast counts. I have got counts for multiple criteria down to only a few seconds. Most take under a second however I have a few queries that seam to take longer which I am working on reducing the time. I have found some strange behavour in the way SQL Server works. Take the following two queries which produce exactly the same result: --------------------------------- select count(*) from dbo.table where column1='value1' and column2='value2' -------------------------------- and -------------------------------- select count(*) from (select id from table where column1 = 'value1') as value1 join (select id from table where column2 = 'value2') as value2 on value1.id = value2.id --------------------------------- I would assume that the first query should run faster then the second query. When I look at the query plans, they are almost identical cost wise. The first takes about 53% of the cost and the second takes 47%. Yet, the first query takes about 25 seconds to run and the second takes only 5 seconds. Does anyone know of a reason why there would be such a difference in query speed? |
#4
| |||
| |||
|
|
Does anyone know of a reason why there would be such a difference in query speed? |
#5
| |||
| |||
|
|
On Wed, 24 Oct 2007 22:13:11 -0700, DBMonitor wrote: Does anyone know of a reason why there would be such a difference in query speed? Hi DBMonitor, In addition to the suggestion posted by Roy, have you considered that this may be caused by cachhing? In other words, if you run the second query first and the first query last, or if you run both queries repeatedly, does that change anything to the execution time? -- Hugo Kornelis, SQL Server MVP My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis |
#6
| |||
| |||
|
|
Both query plans use indexes on for both the tables though. The plans are almost identical. |
#7
| |||
| |||
|
|
DBMonitor (spamawa... (AT) yahoo (DOT) com.au) writes: Both query plans use indexes on for both the tables though. The plans are almost identical. "Almost". Apparently, there is a subtle, but important difference. Would it be posible for you to post the query plans? If you are on SQL 2005 you can save the graphical execution plan in a file and post that in an attachment. (Or put it on a web site with a link to it.) |
#8
| |||
| |||
|
|
The query plans are as follows: ----------------- Subquery Query (Total cost 41.52%) SELECT (0%)<-CS (0%)<-HM IJ (68%)<-IS Col1 (11%) .................................<-IS Col2 (20%) Standard Query (Total Cost 58.48%) SELECT (0%)<-CS (0%)<-HM IJ (78%)<-IS Col1 (8%) .................................<-IS Col2 (14%) KEY: CS - Comput Scalar HM IJ - Hash Match/Inner Join IS - Index Seek |
|
When I run it on a dedicated server, the times to run the queries are almost identical and the lastwaittypes change to CXPACKET and uses multiple threads. |
![]() |
| Thread Tools | |
| Display Modes | |
| |