Solved

What is Good...CURSOR or PLAIN SELECT Statement...

Posted on 2004-10-13
9
776 Views
Last Modified: 2008-02-01
Hi...
I am writing a procedure. I am starting with opening a cursor and looping through each record. Now for each record I have to get other details from 5 different tables. I am plannig to create five different parametric cursors for all five tables. Also, I am planning to pass PK to these cursors to fetch data for the record fetched by first cursor.
First cursor is must...I know that all the five tables are going to return me one single record for the passed PK.
Is cursor approach is good ??? or should I just get the data using simple SELECT statement with WHERE clause ??? I am looking for SPEED ...I am expecting, first cursor will return 2000 rows. That  means if I am using CURSOR approach I am using five tables cursor 2000 times. Where as using SELECT statement I am hitting DB table directly each time for getting the record.
 
0
Comment
Question by:ushelke
9 Comments
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 20 total points
ID: 12304389
Cursors are record-based operations, and SELECT's are set-based operations. Set-based is almost always far faster than record-based.
0
 
LVL 3

Expert Comment

by:jaramill
ID: 12304527
Well if you want to take data from these records and do some processing before inserting, then you use a CURSOR with the SQL statement being one big SQL with sub-queries.  If you're doing just a straight insert then jdlambert1's suggestion is correct.  INSERT INTO SELECT * FROM ... is always faster.
0
 

Author Comment

by:ushelke
ID: 12304545
I am taking this data and then going to use this data for processing purpose. I am also thinking of writing one single cursor for fetching data from all five tables rather than writing five different cursors. But not sure which is good approach. Five different cursor or one single cursor based on complicated SELECT statement with one parameter i.e my PK key from first cursor.  Please comment...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:jaramill
ID: 12304548
If you read my last post I say what you should do, and that is one single cursor based on a complex SQL statement.
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12304553
Avoiding 4 cursors would be very beneficial. Use the single cursor, regardless of how complex that makes the SELECT statement.

Also, you can test the SELECT statement by itself. If it has performance problems, you should be able to improve that by adding appropriate indexes.
0
 
LVL 47

Expert Comment

by:schwertner
ID: 12305734
SELECT is best. But you can use it only if it selects ONE row.
If your select statement selects more then one row you have to use
cursor. This is so because PL/SQL is not able to store a set of rows.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Number Format 1 45
Adding a blank row when using union all 4 49
PL/SQL LOOP CURSOR 3 42
query in Oracle forms Builder 2 27
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example, show how to take different types of Oracle backups using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now