Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: sql tuning question
On Wed, 05 Jan 2000 11:00:52 -0800, Frank van Bortel
<f.van.bortel_at_vnl.nl> wrote:
><!doctype html public "-//w3c//dtd html 4.0 transitional//en">
><html>
>kshave_at_health.gov.mb.ca wrote:
><blockquote TYPE=CITE>I am having some trouble tuning a particular sql
>statement.
><p>explain plan for
><br>select a.<some columns>, b.<more columns> from a,b
><br>where a.mynumber = b.mynumber and a.method = :b1
><br>and to_char(a.mydate,'RRRRMMDD')<=:b2
><br>and rtrim(a.cheque) is null
><p>union
><p>select c.<some columns>, d.<more columns> from c,d
><br>where c.id = d.id and c.method = :b3
><br>and to_char(c.mydate,'RRRRMMDD')<=:b4
><br>and rtrim(c.cheque) is null
><p>order by <something>
><br>;
><p>SELECT STATEMENT Cost = 66882
><br> SORT UNIQUE
><br> UNION-ALL
><br> HASH JOIN
><br> TABLE ACCESS FULL b
><br> TABLE ACCESS FULL a
><br> NESTED LOOPS
><br> TABLE ACCESS FULL c
><br> TABLE ACCESS BY ROWID d
><br> INDEX UNIQUE
>SCAN PK_d
><p>A - 3.5 million rows
><br>B - 700 rows
><br>C - 77 rows
><br>D - 700 rows
><p>The bottom portion of this query is fine. The top portion is what I
><br>have a problem with.
><p>Table a has a non-unique index set up on the 'mynumber' column (which
><br>is not being used, as you can see from the explain plan output). Table
><br>b has a primary key (and unique index) set up on the 'mynumber' column.
><p>Is there anyway to use an index on table a?
><p>--
><br>-Keith
><p>Sent via Deja.com http://www.deja.com/
><br>Before you buy.</blockquote>
>
><p><br>Do you want that? Using an index is not always the most effecient
>way to access data.
><br>As a rule of thumb: when you retrieve more than 15% of a table, a full
>scan will be
><br>more efficient than an index scan, followed by a tale access by rowid.
><br>AFAIK, the optimizer uses this rule of thumb too, but sets my 15% to
>5% (!).
><br>You might try:<tt></tt>
><p><tt>select /*+ ORDERED */ a.<some columns>, b.<more columns></tt>
><br><tt>from b,a
>-- make b the driving table</tt>
><br><tt>where a.mynumber = b.mynumber</tt>
><br><tt>and a.method = :b1</tt>
><br><tt>and a.mydate > to_date(:b2, 'RRRRMMDD') -- convert
>what is not known anyway, not what is</tt>
><br><tt>and a.cheque is null
>-- why rtrim and check for null?</tt><tt></tt>
><p><tt>union</tt><tt></tt>
><p>...
><br>Or, hinting to use the index:
><p><tt>select /*+ INDEX(a index_name_of_a) */ a.<some columns>, b.<more
>columns></tt>
><br><tt>from a,b</tt>
><br><tt>where a.mynumber = b.mynumber</tt>
><br><tt>and a.method = :b1</tt>
><br><tt>and a.mydate > to_date(:b2, 'RRRRMMDD')</tt>
><br><tt>and a.cheque is null</tt><tt></tt>
><p><tt>union</tt><tt></tt>
><p>...
><p>--
><br>Met vriendelijke groet/kind regards,
><p>Frank van Bortel
><br>Technical consultant Oracle
><p>Work:
>Home:
><br>---------------------------------- ----------------------------
><br>Inter Access V&L
>Hunzestraat 4
><br>Palatijn 3, 7521 PN Enschede
>7555 WB Hengelo
><br>PoBox 545, 7500 AM Enschede
>(31)074-2425046
><br>053-4341500
><br> </html>
>
Your contributions are always worthwile, but please remember there are people out there who can't read html posts (all forte free agent users)
Hth,
Sybrand Bakker, Oracle DBA Received on Wed Jan 05 2000 - 12:52:21 CST