Subject | Re: Thanks...Help with extremely slow SQL |
---|---|
Author | nitaligavino <Dan.Crea@apropos.com> |
Post date | 2003-02-10T15:14:07Z |
Hello and thanks to Svein and Andrew:
I have replaced my original statement and based on your suggestions,
I'm now able to get a result set in a matter of milliseconds verses
minutes. Huge difference!
I agree with Svein's assessment about the subselects, I have seen the
same sort of behavior where it appears that the subselect is being
executed once for each potential row, very costly.
I do have one question however, Andrew, you suggested increasing the
number of buffers. Can you explain how to do this and what this will
do?
Thanks again for the help
Dan
--- In ib-support@yahoogroups.com, Svein Erling Tysvaer
<svein.erling.tysvaer@k...> wrote:
I have replaced my original statement and based on your suggestions,
I'm now able to get a result set in a matter of milliseconds verses
minutes. Huge difference!
I agree with Svein's assessment about the subselects, I have seen the
same sort of behavior where it appears that the subselect is being
executed once for each potential row, very costly.
I do have one question however, Andrew, you suggested increasing the
number of buffers. Can you explain how to do this and what this will
do?
Thanks again for the help
Dan
--- In ib-support@yahoogroups.com, Svein Erling Tysvaer
<svein.erling.tysvaer@k...> wrote:
> Hi Dan!being the
>
> In general, my impression (though I have no proof for it always
> case) is that subselects are executed once for every potentiallyreturned
> row. Or to say it another way, in your query the subselect 'SELECTMKey
> FROM MediaItem WHERE IState = ? GROUP BY MKey' is probably executed1321 times.
>avoid using
> IN is good when your set consists of fixed values, but I always
> IN with subselects. Andrews suggestion about using JOIN is goodwhen it is
> appropriate, but sometimes it can result in too many returned rows.If so,
> I rewrite the statement to using EXISTS, something which normallyis very
> fast in Fb/IB. E.g. I would useMI.
>
> SELECT * FROM Media M
> WHERE EXISTS (SELECT 1 FROM MediaItem MI where MI.MKey = M.MKey and
> IState = ?)result sets".
>
> to avoid having to create 1321 identical "temporary subselect
>references
> Having said that, as long as your subselect doesn't contain any
> to your original table I cannot understand why Fb/IB should have toissue
> the subselect for every potential row. Using EXISTS with subselectsis one
> of the pitfalls people repeatedly fall into and at least in theory(I have
> no knowledge of the source code, nor have I tried Fb 1.5 to see ifanything
> is changed in this regard) it should be possible to do things likethis
> much more efficiently.not have
>
> Your IState index may or may not be useful (it is useful if you do
> many records with the same value in that field), but it certainlydoesn't
> help much when using IN with subselects.SQL
>
> HTH,
> Set
>
> - I support Firebird, I am a FirebirdSQL Foundation member.
> - Join today at http://www.firebirdsql.org/ff/foundation
>
> At 15:10 07.02.2003 +0000, you wrote:
> >Hello all:
> >
> >I need some help understanding how an SQL statement is executed
> >within the FB engine. Specifically, I need to understand why an
> >statement such as:call?
> >
> >SELECT * FROM Media WHERE MKey IN (SELECT MKey FROM MediaItem WHERE
> >IState = ? GROUP BY MKey)
> >
> >takes approximately 3 minutes to execute per isc_dsql_fetch( )
> >is
> >In this SQL statement, Media.MKey is a primary key so it's auto
> >indexed, MediaItem.Meky is a foreign key and I have created an ASC
> >index on IState. What I'm seeing is extremely slow performance by
> >the ibserver.exe in executing this sort of query. When this query
> >executed the ibserver.exe will spike at 98% CPU utilization androws.
> >remain in this state for nearly 3 minutes before returning the
> >results to the client. Each table, Media and MediaItem has 1321
> >Plan: PLAN (MEDIAITEM ORDER RDB$FOREIGN11) PLAN (MEDIA NAUTRAL)