Re: Use of SELECT TOP x (Are you there, Kalen...heheh)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Steve Kass (skass_at_drew.edu)
Date: 02/01/05


Date: Tue, 01 Feb 2005 18:40:58 -0500

Joe,

  My guess is that the stored procedure is using a plan
inappropriate for the particular parameters you are passing it.
This sometimes happens because of something called "parameter
sniffing" (search for that for some ideas). It can also happen when
the data is not well-described by statistics. In either case, it can
also be worth looking at your indexing. I see this more often
when there are a bunch of one-column indexes on a table,
but no multi-column indexes, which would make a big improvement.

  In any case, removing a WHERE clause and adding TOP 100
will tell you pretty much nothing useful about what's going on.
Without the WHERE clause, or with the TOP, or with both
changes, more than likely an entirely different approach is
used to return rows. You are certainly unlikely to be seeing
rows that happen to be produced in the same order as any kind
of intermediate result in your sp.

You can see what's going on by comparing the rowcounts in the
actual and estimated execution plans. In query analyzer, type
CTRL-K and then run your troublesome procedure. The hover-over
popups in the graphical execution plan you see will show row counts
(third row or so) and estimated rowcounts (bottom row). That, as
well as reading the plan through, are ways to get a feel for what's
going wrong.

Look for a bookmark operator as the culprit in the query plan.
If it's the one with the bad rowcount, one of the suggestions
I made is more likely to be an effective road to follow.

Steve Kass
Drew University.

J. M. De Moor wrote:

>(I hope I don't have to provide a bunch of DDL, as it is a general
>question...but if I need to I will.)
>
>We have a rather complex SELECT statement in a STORED PROC that suddenly
>started to perform poorly. Suffice to say it builds a summary resultset
>from several tables, generally returning less than 6 or 7 rows. A couple of
>the base tables have 200,000 rows...not too big, anyway.
>
>In troubleshooting the sudden drop in performance (queries went from
>subsecond to 20 second response times), I removed the WHERE clause and added
>a SELECT TOP 100 just to see what the unfiltered data looked like. The
>response time was dropped to subsecond. When I added the WHERE clause back
>in, the response time remained subsecond. Once I removed the SELECT TOP
>100, the query again responded in around 20 seconds.
>
>Given that the resultset is certainly never more than 10 rows, there is
>obviously something I don't understand about the workings of SELECT TOP x.
>Can someone enlighten me?
>
>Thanks.
>
>Joe
>
>PS. I still believe we have to fix something, and we will as soon as we
>find it, we were just surprised that TOP x had this impact.
>
>
>
>



Relevant Pages

  • Re: Udf inside Where clause possible?
    ... I meant that if the stored procedure were compiled using parameters for ... We still might not get an optimal plan if we ... Your repro uses the parameter in the WHERE clause. ... > create proc p ( ...
    (microsoft.public.sqlserver.programming)
  • Re: "LA Times" says hurricanes impact was a certainty . . .
    ... Plans for handling disaster ... So you think there would have been no realistic plan that would have ... improved our response time and effectiveness? ... governing body learn from. ...
    (alt.guitar.bass)
  • Re: "LA Times" says hurricanes impact was a certainty . . .
    ... > So you think there would have been no realistic plan that would have ... > improved our response time and effectiveness? ... the lower part of the city would have been the best, ... no shit the government performs poorly, and everybody is aware of that. ...
    (alt.guitar.bass)
  • Re: Question about HSQLDB database synchronization
    ... the response time of my application is very ... And how do you plan to keep 10GB of data /in-memory/? ... memory and every server has 8GB memory and I can using 6GB for my ... server application and it is enough for 2GB data. ...
    (comp.lang.java.programmer)
  • Re: O9i: general index question
    ... clause is the only additional clause and the conection column is a part ... read for the index leaf block ... plan results. ... no experienced Oracle developer is using that crap. ...
    (comp.databases.oracle.misc)