Q

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.

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:
BEGIN
   INSERT INTO new_table SELECT * FROM old_table;
END;
/
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

Dig deeper on Oracle database design and architecture

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close