Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Clarification of variable placeholders v's prepare_cached

by macPerl (Beadle)
on Feb 09, 2005 at 18:30 UTC ( [id://429459]=perlquestion: print w/replies, xml ) Need Help??

macPerl has asked for the wisdom of the Perl Monks concerning the following question:

If I use placeholders for creating sql statements with variable number of fields, (e.g. in post : Variable placeholders with DBI) does this mean that sql has to recompile the statement everytime (unlike the sample: What are placeholders in DBI, and why would I want to use them?)

I'm pretty sure it does, however, would like Monasterial Confirmation in case I am missing something.

  • Comment on Clarification of variable placeholders v's prepare_cached

Replies are listed 'Best First'.
Re: Clarification of variable placeholders v's prepare_cached
by grinder (Bishop) on Feb 09, 2005 at 18:55 UTC

    Consider a table t that has three columns, a, b and c, any of which may be null.

    You have some long-running code that every now and then, inserts a record into the table where all or some of the columns are referenced:

    my $sth = $db->prepare_cached( <<SQL ) or die $db->errstr; insert into t (a, c) values (?, ?) SQL $sth->execute( $foo, $bar ); # ... # sometime later # ... my $sth = $db->prepare_cached( <<SQL ) or die $db->errstr; insert into t (c) values (?) SQL $sth->execute( $quux );

    The first time DBI encounters an a-b-c tuple, it will parse the statement and generate an execution plan.

    The subsequent times that DBI encounters an insert tuple it has already seen, it will skip the compilation and return the prepared statement immediately, with the attendant speed gains.

    If you code could insert all possible single columns, pairs of columns and all three columns, you would wind with 3 + 3*2 + 1 = 10 3 + 3 + 1 = 7 cached insert statements.

    Assuming I've understood the question you're asking.

    In real life, the above scenario might occur when dealing with a hash, and depending on the keys that exist, you want to insert their values into columns of a table. Sometimes, (some of)? the keys are present, sometimes not.

    - another intruder with the mooring in the heart of the Perl

      Cheers, grinder.

      Actually the variability is introduced by different tables so (in this particular instance) "No of insert statements" = "no of tables"

      Your brief lesson above hits the spot.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://429459]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-25 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found