Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: sql tuning question

Re: sql tuning question

From: Sybrand Bakker <postbus_at_sybrandb.demon.nl>
Date: Wed, 05 Jan 2000 18:52:21 GMT
Message-ID: <38739295.2559103@news.demon.nl>


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.&lt;some columns>, b.&lt;more columns> from a,b
><br>where a.mynumber = b.mynumber and a.method = :b1
><br>and to_char(a.mydate,'RRRRMMDD')&lt;=:b2
><br>and rtrim(a.cheque) is null
><p>union
><p>select c.&lt;some columns>, d.&lt;more columns> from c,d
><br>where c.id = d.id and c.method = :b3
><br>and to_char(c.mydate,'RRRRMMDD')&lt;=:b4
><br>and rtrim(c.cheque) is null
><p>order by &lt;something>
><br>;
><p>SELECT STATEMENT&nbsp;&nbsp; Cost = 66882
><br>&nbsp; SORT UNIQUE
><br>&nbsp;&nbsp;&nbsp; UNION-ALL
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HASH JOIN
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TABLE ACCESS FULL b
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TABLE ACCESS FULL a
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NESTED LOOPS
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TABLE ACCESS FULL c
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TABLE ACCESS BY ROWID d
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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.&lt;some columns>, b.&lt;more columns></tt>
><br><tt>from&nbsp; b,a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>-- make b the driving table</tt>
><br><tt>where a.mynumber = b.mynumber</tt>
><br><tt>and&nbsp;&nbsp; a.method&nbsp;&nbsp; = :b1</tt>
><br><tt>and&nbsp;&nbsp; a.mydate > to_date(:b2, 'RRRRMMDD') -- convert
>what is not known anyway, not what is</tt>
><br><tt>and&nbsp;&nbsp; a.cheque is null&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>-- 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.&lt;some columns>, b.&lt;more
>columns></tt>
><br><tt>from&nbsp; a,b</tt>
><br><tt>where a.mynumber = b.mynumber</tt>
><br><tt>and&nbsp;&nbsp; a.method&nbsp;&nbsp; = :b1</tt>
><br><tt>and&nbsp;&nbsp; a.mydate > to_date(:b2, 'RRRRMMDD')</tt>
><br><tt>and&nbsp;&nbsp; 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:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>Home:
><br>----------------------------------&nbsp;&nbsp; ----------------------------
><br>Inter Access V&amp;L&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>Hunzestraat 4
><br>Palatijn 3, 7521 PN Enschede&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>7555 WB Hengelo
><br>PoBox 545, 7500 AM Enschede&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>(31)074-2425046
><br>053-4341500
><br>&nbsp;</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

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US