Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: How to optimize this query?
Andy Hassall <andy_at_andyh.co.uk> wrote in message news:<1223jvomkc43md42gi5rem94mk32uvltkp_at_4ax.com>...
> On Wed, 06 Aug 2003 10:44:28 GMT, "Edward J. Prochak"
> <ed.prochak_at_magicinterface.com> wrote:
>
> >My understanding is that the optimizer can decide that if it can answer
> >COUNT(*) from table statistics, then it will do so. I hadn't thought about
> >using it as a wrapper around the actual query. In that case you are right,
> >cause there's no statistics for the optimizer to look at.
>
> But optimiser statistics (user_tables.num_rows) cannot be used as the basis
> for COUNT(*), since they are only refreshed periodically; so after the first
> insert/delete after gathering stats, it would give the wrong answer.
>
> It's going to have to scan _something_; most likely an index.
If you run performance test of queries in a production environment, then your stats will go "out of date" quickly. Lots of us do our performance testing on production instances right? 8^)
You only have to get burned ONCE to decide to play it safe. Besides transferring the data to the client can be a significant portion of query execution time in some instances (report generation for example).
And I thing the COUNT(*) wrapper suggestion is a great alternative. So, we have three ways:
option 1 Original query under test
select <columns> from <tables> where <conditions/joins>;
option 2 Query "without" data transfer
select count(*) from <tables> where <conditions/joins>;
option 3 wrapper query (embedding the original query as a view)
select count(*)
from ( select count(*) from <tables> where <conditions/joins> ) ;
in Option 3 it should be easy to sort out the extra work done for the count(*) from the explain plan and the trace should have reasonable info on data moves too (except for the actual data transfer to the client process). It looks like a win-win here. Received on Thu Aug 07 2003 - 15:22:22 CDT
![]() |
![]() |