Ask the Expert

Insert in a cursor takes hours to complete

We run an insert from Oracle SQL tool that selects 200,000 rows from various tables and inserts them in another table. This ends in 10 minutes. We try the same query inside a stored procedure in Oracle, and it runs for hours. Oracle version is 8.1.5 and runs on Windows NT.

    Requires Free Membership to View

Are you running the INSERT statement in the stored proc by using an explicit CURSOR? Are you looping through the cursor and performing a COMMIT every so often? These type of things can make your INSERT statement slow to a crawl.

Let's suppose that I have a simple INSERT statment, selecting data from a table, that I can run in SQL*Plus similar to the following:

INSERT INTO new_table SELECT * FROM old_table;
To run this statement in a PL/SQL block, just do the same:
   INSERT INTO new_table SELECT * FROM old_table;
Now this PL/SQL block can be put into a stored procedure for quick recall. There is no need to use a cursor for this. Oracle will create an implicit cursor for you.

For More Information

This was first published in August 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: