Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

alainb:

It looks like you should be able to reduce the number of database calls. The method will likely differ based on the database, but you should be able to reduce the number of operations for each set, possibly to a single transaction. Examples:

MySQL

MySQL allows you to insert multiple records with a single statement, like so:

insert into FooBar (ID, col1, col2, col3) values ((select max(ID)+1 from FooBar), 'a', 1, 'flort'), ((select max(ID)+2 from FooBar), 'b', 2, 'zap'), ((select max(ID)+3 from FooBar), 'c', 3, 'kazoo')

This would let you use a single communication for each table type.

MS SQL Server / Sybase / Oracle / etc.

insert into FooBar (ID, col1, col2, col3) select ID, col1, col2, col3 from ( select max(ID)+1 from FooBar) ID, 'a' col1, 1 col2, 'flort' col3 union select max(ID)+1 from FooBar), 'b', 2, 'zap' union select max(ID)+1 from FooBar), 'c', 3, 'kazoo' )

Oracle

Some database drivers, such as Oracle, let you send a block of SQL to execute, so you can build blocks and do all your inserts with one swell foop:

declare v_ID number; begin -- First table select max(ID) into v_ID from FooBar; insert into FooBar (ID, col1, col2, col3) values (v_ID+1, 'a', 1, 'flort'); insert into FooBar (ID, col1, col2, col3) values (v_ID+2, 'b', 2, 'zap'); insert into FooBar (ID, col1, col2, col3) values (v_ID+3, 'c', 3, 'kazoo'); -- Second table select max(ID) into v_ID from BarBaz; insert into BarBaz (ID, col1) values (v_ID+1, 'etcetera'); end;

Using a work table

Another option, that I frequently find useful, is to create some temporary work tables and bulk load the data into them. The bulk loader should be about the fastest way to get data into the database. Then create a stored procedure that integrates the data in the work tables into the real tables. For adding large sets of data to a database, this is pretty hard to beat.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re^5: Fetchrow question by roboticus
in thread Fetchrow question by alainb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found