Home » SQL & PL/SQL » SQL & PL/SQL » Faster Query
Faster Query [message #11060] Thu, 04 March 2004 05:11 Go to next message
Alexander Dumas
Messages: 3
Registered: January 2004
Junior Member
Hi... I would like to know if I can improve the execution time for the query below.

TableA is growing quickly and is currently 500,000 rows.

TableB is small, about 50 rows and is fairly static.

I have indexes on PersonSequenceNumber an B_SequenceNumber and Name.

Is there anything else I can do? Rework the query?

 

******************************************************

 select * from (
 select 
   a.B_SEQUENCENUMBER, 
   a.PERSONSEQUENCENUMBER, 
   a.Name
   b.LastName,
   b.FirstName
 from  TableA a,
   TableB b
 where a.PersonSequenceNumber = b.PersonSequenceNumber
 and  a.B_SEQUENCENUMBER = 2
 and  UPPER(ltrim(rtrim(a.Name))) like '%JONES%'
 order by BIRTHDATE desc
) b
where rownum < 7;
Re: Faster Query [message #11061 is a reply to message #11060] Thu, 04 March 2004 05:53 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
I don't have tables to test this query against, but if you are on at least version 8.1.6, you can use analytic functions to solve this kind of problem:
SELECT   rnkd.b_sequencenumber
,        rnkd.personsequencenumber
,        rnkd.name
,        rnkd.lastname
,        rnkd.firstname
FROM    (SELECT   a.b_sequencenumber
         ,        a.personsequencenumber
         ,        a.name
         ,        b.lastname
         ,        b.firstname
         ,        <a href="http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/functions35a.htm#1064413">DENSE_RANK</a>() OVER (ORDER BY birthdate DESC) age_rnk
         FROM     tablea    a
         ,        tableb    b
         WHERE    a.personsequencenumber = b.personsequencenumber
         AND      a.b_sequencenumber     = 2 
         AND      UPPER(LTRIM(RTRIM(a.name))) LIKE '%JONES%') rnkd
WHERE    rnkd.age_rnk <= 6
/
I'd be curious to learn how the addition of the analytic function affects the performance of this query.

I hope this helps,

Art.
Re: Faster Query [message #11062 is a reply to message #11061] Thu, 04 March 2004 06:06 Go to previous messageGo to next message
Alexander Dumas
Messages: 3
Registered: January 2004
Junior Member
Thanks for answering... I just tested it out and it didn't really make any difference. It executed in about the same about of time.
Re: Faster Query [message #11063 is a reply to message #11060] Thu, 04 March 2004 07:45 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
The indexes on Name (because you are wrapping the column with functions) and PersonSequenceNumber (not used for the join, on the TableA side) in TableA will do nothing for this query. An index on PersonSequenceNumber in TableB may or may not be helpful given its small size.

Out of the 500K rows in TableA, how many have a value for B_SequenceNumber = 2?
Re: Faster Query [message #11064 is a reply to message #11063] Thu, 04 March 2004 08:12 Go to previous messageGo to next message
Alexander
Messages: 109
Registered: May 2000
Senior Member
Most of the time, less than 50
Re: Faster Query [message #11065 is a reply to message #11064] Thu, 04 March 2004 08:50 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
If there are only 50 rows out of 500K, we need to look at the EXPLAIN PLAN for this query because it should be using the index on that column.

Are you familiar with how to obtain the query plan?

Once we get the query using the correct index, this should run very fast.
Re: Faster Query [message #11066 is a reply to message #11065] Thu, 04 March 2004 09:00 Go to previous messageGo to next message
Alexander
Messages: 109
Registered: May 2000
Senior Member
No... How can I get it?
Re: Faster Query [message #11068 is a reply to message #11066] Thu, 04 March 2004 10:17 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Here's one article (there are thousands of them out there):

Using EXPLAIN PLAN in Oracle
Re: Faster Query [message #11083 is a reply to message #11060] Fri, 05 March 2004 01:25 Go to previous message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
You might also add an index on TableB (PersonSequenceNumber).

Which table is birthdate in?

Have you analyzed your tables, indexes, and indexed columns, so that the optimizer has the statistics that it needs to choose the best execution plan? If not, then please run the following, after creating the index on TableB, then re-try your query:

ANALYZE TABLE TableA
COMPUTE STATISTICS
FOR TABLE
FOR ALL INDEXES
FOR ALL INDEXED COLUMNS;

ANALYZE TABLE TableB
COMPUTE STATISTICS
FOR TABLE
FOR ALL INDEXES
FOR ALL INDEXED COLUMNS;
Previous Topic: TTITLE
Next Topic: sql.pno
Goto Forum:
  


Current Time: Wed Apr 24 07:51:38 CDT 2024