473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do i Optimize this Query

22
I have 2 tables with a parent-child relationship. Parent table's primary key field is Entry_no and is of type VarChar(50). This parent table has about 50 fields. The child has 3 fields, Entry_No (varchar(50), Requestor char(3) and id (integer).

I need the following query

Expand|Select|Wrap|Line Numbers
  1. QUERY 1. select p.entry_no, p.reg_date from tparent p, tchild c where p.entry_no<>c.entry_no and c.requestor='KPA' order by p.reg_date, p.entry_no;
The above query takes a very long time (more than 30 min)

If i remove the ORDER BY clause, it runs very fast (milliseconds). The problem is that the query must be ordered for it to be of any help to me.

I have tried other options like

Expand|Select|Wrap|Line Numbers
  1. QUERY 2. select p.entry_no, p.reg_date from tparent p where p.entry_no not in (select entry_no from tchild where c.requestor='KPA') order by p.reg_date, p.entry_no;
This also takes forever. In fact this one hangs even when i remove the ORDER BY clause.

The interesting thing is that if i use = instead of <> in query 1 and 'in' in place of 'not in' in query 2, the query runs in milliseconds range whether or not the 'ORDER BY' clause is present.

How do i customize this query to run fast?

MORE INFORMATION:
- Oracle 10G
- Parent table is indexed on entry_no and sreg_date. Has approx 350,000 records.
- Child is indexed on entry_no and requestor. Has approx 500,000 records.
Mar 28 '08 #1
1 1765
amitpatel66
2,367 Expert 2GB
I have 2 tables with a parent-child relationship. Parent table's primary key field is Entry_no and is of type VarChar(50). This parent table has about 50 fields. The child has 3 fields, Entry_No (varchar(50), Requestor char(3) and id (integer).

I need the following query

Expand|Select|Wrap|Line Numbers
  1. QUERY 1. select p.entry_no, p.reg_date from tparent p, tchild c where p.entry_no<>c.entry_no and c.requestor='KPA' order by p.reg_date, p.entry_no;
The above query takes a very long time (more than 30 min)

If i remove the ORDER BY clause, it runs very fast (milliseconds). The problem is that the query must be ordered for it to be of any help to me.

I have tried other options like

Expand|Select|Wrap|Line Numbers
  1. QUERY 2. select p.entry_no, p.reg_date from tparent p where p.entry_no not in (select entry_no from tchild where c.requestor='KPA') order by p.reg_date, p.entry_no;
This also takes forever. In fact this one hangs even when i remove the ORDER BY clause.

The interesting thing is that if i use = instead of <> in query 1 and 'in' in place of 'not in' in query 2, the query runs in milliseconds range whether or not the 'ORDER BY' clause is present.

How do i customize this query to run fast?

MORE INFORMATION:
- Oracle 10G
- Parent table is indexed on entry_no and sreg_date. Has approx 350,000 records.
- Child is indexed on entry_no and requestor. Has approx 500,000 records.
Try This:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select p.entry_no, p.reg_date from tparent p where EXISTS (SELECT 'X' FROM tchild WHERE entry_no <> p.entry_no and requestor='KPA') order by p.reg_date, p.entry_no
  3.  
  4.  
Mar 28 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
6
by: Bruce D | last post by:
Could someone please help to explain why the following query isn't using the index... explain select id from kbm where state = 'MA' table type possible_keys key key_len ref rows Extra...
3
by: Paul Janssen | last post by:
Hello! Can anyone help me out with the following situation: (a) a single query with 550 id's in the IN-clause resulting into 800+ seconds; (b) 550 queries with a single id in the IN-clause...
5
by: xeqister | last post by:
Greetings all, We have a complicated statement in DB2 which takes long hour to complete and we have created most of the indexes. Does anybody knows how to tune the following statement to optimize...
3
by: Reddy | last post by:
The sql query for my datagrid returns 100, 000 records. But the datagrid should display 20 records per page. I am using datagrid paging, but it is taking too much time for the page to load. Is...
4
by: Huaer.XC | last post by:
>From the following MySQL command: EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON t3.name = t1.name WHERE t1.id IN(123, 124); which result is:...
13
by: Frank Swarbrick | last post by:
IBM has a product for the VSE operating system called the VSAM Redirector. It allows you to use VSAM to access RDBMS tables/views as if they were actual VSAM files. We're doing a comparison right...
11
by: bravo | last post by:
hi i need to optimize the join query, which joins three tables say table1 ,table2 , table3 each having huge volume of records... the query is as select table1.id,table2.time,table3.Status from...
1
by: acornejo | last post by:
Hi All I've the following code I need to optimize. Currently tblOutgoing is about 250K registers and growing at a rate of about 20k records per day. This code takes me over 5 secs to run on each...
3
zabsmarty
by: zabsmarty | last post by:
Can any one help me to make my query code optimize and load faster. Please help me or any example what steps should we use to optimize. Thank You
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.