Home All Groups Group Topic Archive Search About

Newbie: Timeout On IN statement

Author
2 Dec 2005 4:25 PM
Chuck
I have a query which returns the Top 100 selling products for each of the 14
categories that we sell.  It takes 10 minutes to run, however.  Does anyone
have a suggestion on speeding this up.

SELECT     ProductID, CategoryID, Sales, Qty
FROM         Sales s1
WHERE   productid in
                          (SELECT     TOP 100 productid
                            FROM          Sales s2
                            WHERE      (s1.productID = s2.productid)
                            ORDER BY s2.Sales DESC)

I have tried to sort the View "Sales" that it queries by the column Sales to
speed it up, to no effect.  If I run the nested select on its own without the
WHERE clause, it runs in 2 seconds.

Author
2 Dec 2005 4:31 PM
Chuck
Sorry,  Wrong WHERE 

SELECT     ProductID, CategoryID, Sales, Qty
FROM         Sales s1
WHERE   productid in
                          (SELECT     TOP 100 productid
                            FROM          Sales s2
                            WHERE      (s1.categoryID = s2.categoryid)
                            ORDER BY s2.Sales DESC)
Author
2 Dec 2005 4:32 PM
Adam Machanic
I think your query is written incorrectly.  Don't you really mean:

SELECT     ProductID, CategoryID, Sales, Qty
FROM         Sales s1
WHERE   productid in
                          (SELECT     TOP 100 productid
                            FROM          Sales s2
                            WHERE      (s1.categoryID =
s2.categoryID) --Changed product to category here
                            ORDER BY s2.Sales DESC)

Aside from that, can you show the view definition?  Is there a way to query
less tables than the view touches?


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


Show quote
"Chuck" <Ch***@discussions.microsoft.com> wrote in message
news:2B25C9C7-A42D-46F7-B0FF-A3E4B06EDD31@microsoft.com...
>I have a query which returns the Top 100 selling products for each of the
>14
> categories that we sell.  It takes 10 minutes to run, however.  Does
> anyone
> have a suggestion on speeding this up.
>
> SELECT     ProductID, CategoryID, Sales, Qty
> FROM         Sales s1
> WHERE   productid in
>                          (SELECT     TOP 100 productid
>                            FROM          Sales s2
>                            WHERE      (s1.productID = s2.productid)
>                            ORDER BY s2.Sales DESC)
>
> I have tried to sort the View "Sales" that it queries by the column Sales
> to
> speed it up, to no effect.  If I run the nested select on its own without
> the
> WHERE clause, it runs in 2 seconds.
>
>
>
Author
2 Dec 2005 4:33 PM
Dean
SELECT     ProductID, CategoryID, Sales, Qty
FROM         Sales s1
WHERE   EXISTS
                          (SELECT     *
                            FROM          Sales s2
                            WHERE      s1.productID = s2.productid)

dean

Show quote
"Chuck" <Ch***@discussions.microsoft.com> wrote in message
news:2B25C9C7-A42D-46F7-B0FF-A3E4B06EDD31@microsoft.com...
>I have a query which returns the Top 100 selling products for each of the
>14
> categories that we sell.  It takes 10 minutes to run, however.  Does
> anyone
> have a suggestion on speeding this up.
>
> SELECT     ProductID, CategoryID, Sales, Qty
> FROM         Sales s1
> WHERE   productid in
>                          (SELECT     TOP 100 productid
>                            FROM          Sales s2
>                            WHERE      (s1.productID = s2.productid)
>                            ORDER BY s2.Sales DESC)
>
> I have tried to sort the View "Sales" that it queries by the column Sales
> to
> speed it up, to no effect.  If I run the nested select on its own without
> the
> WHERE clause, it runs in 2 seconds.
>
>
>
Author
2 Dec 2005 4:45 PM
Adam Machanic
Dean,

Won't that query simply return every row in the Sales table?


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


Show quote
"Dean" <dvitner@nospam.gmail.com> wrote in message
news:O6lrF419FHA.1148@tk2msftngp13.phx.gbl...
> SELECT     ProductID, CategoryID, Sales, Qty
> FROM         Sales s1
> WHERE   EXISTS
>                          (SELECT     *
>                            FROM          Sales s2
>                            WHERE      s1.productID = s2.productid)
>
> dean
>
> "Chuck" <Ch***@discussions.microsoft.com> wrote in message
> news:2B25C9C7-A42D-46F7-B0FF-A3E4B06EDD31@microsoft.com...
>>I have a query which returns the Top 100 selling products for each of the
>>14
>> categories that we sell.  It takes 10 minutes to run, however.  Does
>> anyone
>> have a suggestion on speeding this up.
>>
>> SELECT     ProductID, CategoryID, Sales, Qty
>> FROM         Sales s1
>> WHERE   productid in
>>                          (SELECT     TOP 100 productid
>>                            FROM          Sales s2
>>                            WHERE      (s1.productID = s2.productid)
>>                            ORDER BY s2.Sales DESC)
>>
>> I have tried to sort the View "Sales" that it queries by the column Sales
>> to
>> speed it up, to no effect.  If I run the nested select on its own without
>> the
>> WHERE clause, it runs in 2 seconds.
>>
>>
>>
>
>
Author
2 Dec 2005 5:00 PM
Chuck
SEE POST BELOW -- Chuck

Show quote
"Adam Machanic" wrote:

> Dean,
>
> Won't that query simply return every row in the Sales table?
>
>
> --
> Adam Machanic
> Pro SQL Server 2005, available now
> http://www.apress.com/book/bookDisplay.html?bID=457
> --
>

> "Dean" <dvitner@nospam.gmail.com> wrote in message
> news:O6lrF419FHA.1148@tk2msftngp13.phx.gbl...
> > SELECT     ProductID, CategoryID, Sales, Qty
> > FROM         Sales s1
> > WHERE   EXISTS
> >                          (SELECT     *
> >                            FROM          Sales s2
> >                            WHERE      s1.productID = s2.productid)
> >
> > dean
> >
> > "Chuck" <Ch***@discussions.microsoft.com> wrote in message
> > news:2B25C9C7-A42D-46F7-B0FF-A3E4B06EDD31@microsoft.com...
> >>I have a query which returns the Top 100 selling products for each of the
> >>14
> >> categories that we sell.  It takes 10 minutes to run, however.  Does
> >> anyone
> >> have a suggestion on speeding this up.
> >>
> >> SELECT     ProductID, CategoryID, Sales, Qty
> >> FROM         Sales s1
> >> WHERE   productid in
> >>                          (SELECT     TOP 100 productid
> >>                            FROM          Sales s2
> >>                            WHERE      (s1.productID = s2.productid)
> >>                            ORDER BY s2.Sales DESC)
> >>
> >> I have tried to sort the View "Sales" that it queries by the column Sales
> >> to
> >> speed it up, to no effect.  If I run the nested select on its own without
> >> the
> >> WHERE clause, it runs in 2 seconds.
> >>
> >>
> >>
> >
> >
>
Adam, Dean,

Dean's query did just that, but, I altered it to include the TOP 100 * ...
ORDER BY "Sales" and it returned the results in 9 seconds.  (after changing
the where to reflect my mistake in the original post.  Thanks to both of you,
what a relief !

Does any one have an explanation of why EXISTS worked so much better than IN?

Chuck Ghastin
Author
2 Dec 2005 5:07 PM
Alexander Kuznetsov
> Does any one have an explanation of why EXISTS worked so much better than IN?

when you post both plans, you'll have a better chance of getting a
useful explanation
Author
2 Dec 2005 5:13 PM
Adam Machanic
"Chuck" <Ch***@discussions.microsoft.com> wrote in message
news:7FD2E304-00AF-452B-823A-94C1BB34BAA0@microsoft.com...
>
> Dean's query did just that, but, I altered it to include the TOP 100 * ...
> ORDER BY "Sales" and it returned the results in 9 seconds.  (after
> changing
> the where to reflect my mistake in the original post.  Thanks to both of
> you,
> what a relief !

    Can you show the query you ended up with?  I'm not certain that what
you're describing is logically equivalent to your original query.


--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
Author
2 Dec 2005 5:35 PM
Dean
missread it, sorry..

dean

Show quote
"Adam Machanic" <amachanic@hotmail._removetoemail_.com> wrote in message
news:OnukR$19FHA.3608@TK2MSFTNGP09.phx.gbl...
> Dean,
>
> Won't that query simply return every row in the Sales table?
>
>
> --
> Adam Machanic
> Pro SQL Server 2005, available now
> http://www.apress.com/book/bookDisplay.html?bID=457
> --
>
>
> "Dean" <dvitner@nospam.gmail.com> wrote in message
> news:O6lrF419FHA.1148@tk2msftngp13.phx.gbl...
>> SELECT     ProductID, CategoryID, Sales, Qty
>> FROM         Sales s1
>> WHERE   EXISTS
>>                          (SELECT     *
>>                            FROM          Sales s2
>>                            WHERE      s1.productID = s2.productid)
>>
>> dean
>>
>> "Chuck" <Ch***@discussions.microsoft.com> wrote in message
>> news:2B25C9C7-A42D-46F7-B0FF-A3E4B06EDD31@microsoft.com...
>>>I have a query which returns the Top 100 selling products for each of the
>>>14
>>> categories that we sell.  It takes 10 minutes to run, however.  Does
>>> anyone
>>> have a suggestion on speeding this up.
>>>
>>> SELECT     ProductID, CategoryID, Sales, Qty
>>> FROM         Sales s1
>>> WHERE   productid in
>>>                          (SELECT     TOP 100 productid
>>>                            FROM          Sales s2
>>>                            WHERE      (s1.productID = s2.productid)
>>>                            ORDER BY s2.Sales DESC)
>>>
>>> I have tried to sort the View "Sales" that it queries by the column
>>> Sales to
>>> speed it up, to no effect.  If I run the nested select on its own
>>> without the
>>> WHERE clause, it runs in 2 seconds.
>>>
>>>
>>>
>>
>>
>
>
Author
2 Dec 2005 4:58 PM
Alexander Kuznetsov
an index on (CategoryID, Sales DESC, productid) might help
Author
2 Dec 2005 6:18 PM
Chuck
> >>>Does any one have an explanation of why EXISTS worked so much better than IN?

"Alexander Kuznetsov" wrote:
>when you post both plans, you'll have a better chance of getting a
useful explanation
> an index on (CategoryID, Sales DESC, productid) might help
>


The original attempt with timeout was...
SELECT     ProductID, CategoryID, Sales, Qty
FROM         Sales s1
WHERE   productid in
                          (SELECT     TOP 100 productid
                            FROM          Sales s2
                            WHERE      (s1.categoryid = s2.categoryid)
                            ORDER BY s2.Sales DESC)

Changed to the following with results in 9 seconds...
SELECT     ProductID, CategoryID, Sales, Qty
FROM         Sales s1
WHERE   EXISTS
                          (SELECT     TOP 100 *
                            FROM          Sales s2
                            WHERE      (s1.categoryid = s2.categoryid)
                            ORDER BY s2.Sales DESC)
Author
2 Dec 2005 6:25 PM
Adam Machanic
Chuck,

Those two queries are not logically equivalent.  The old query, essentially,
said:

"Give me all products where the product is in the top 100 for its category."

The new query says:

"Give me all products where my category exists."

Check your results.


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


Show quote
"Chuck" <Ch***@discussions.microsoft.com> wrote in message
news:C5E91415-3F01-40D9-8D27-77F69F67EA80@microsoft.com...
>
>> >>>Does any one have an explanation of why EXISTS worked so much better
>> >>>than IN?
>
> "Alexander Kuznetsov" wrote:
>>when you post both plans, you'll have a better chance of getting a
> useful explanation
>> an index on (CategoryID, Sales DESC, productid) might help
>>
>
>
> The original attempt with timeout was...
> SELECT     ProductID, CategoryID, Sales, Qty
> FROM         Sales s1
> WHERE   productid in
>                          (SELECT     TOP 100 productid
>                            FROM          Sales s2
>                            WHERE      (s1.categoryid = s2.categoryid)
>                            ORDER BY s2.Sales DESC)
>
> Changed to the following with results in 9 seconds...
> SELECT     ProductID, CategoryID, Sales, Qty
> FROM         Sales s1
> WHERE   EXISTS
>                          (SELECT     TOP 100 *
>                            FROM          Sales s2
>                            WHERE      (s1.categoryid = s2.categoryid)
>                            ORDER BY s2.Sales DESC)
>

AddThis Social Bookmark Button