in reply to Re: Re: Re: How to Speed up MySQL w/ Perl
in thread How to Speed up MySQL w/ Perl

Well, what you really want is something like this (assuming you have transactions turned on (which you really, really want), and RaiseError as well):
eval { my $sth = $dbh -> prepare (<<' --'); update new_table set newData = ? where newID = ? -- while (my ($id, $data) = each %Data) { $sth -> execute ($data, $id) } }; if ($@) { $dbh -> rollback; die $@; } else { $dbh -> commit; }
Note that the data inserted doesn't get quotes tacked on - that's why you are using placeholders, quoting is being taken care of.

Abigail

Replies are listed 'Best First'.
Re: Re: How to Speed up MySQL w/ Perl
by dragonchild (Archbishop) on May 26, 2004 at 13:12 UTC
    assuming you have transactions turned on (which you really, really want), and RaiseError as well)

    I'm going to take issue with the transactions part of this statement. Transactions can be necessary in many cases. However, one of the benefits of MySQL over, say, Oracle is that you can choose whether or not you want transactions or not. In a majority of cases, you don't actually need transactions because your transactions are all one statement long.

    The benefit of ACID-compliant transactions is the ability to rollback multi-statement changes. If your one-statement transaction fails, nothing has actually occurred to change the state of the tables involved.

    By using a table type that doesn't have transactions (such as the default MyISAM), you can do many activities (like bulk inserts) 5-10x faster. If you don't need transactions, that's a huge benefit.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      If your one-statement transaction fails, nothing has actually occurred to change the state of the tables involved.
      It would only be a one-statement transaction if %Data contains less than two elements. It's unlikely that that is the common case.

      Transactions are a bit like "use strict" (except they prevent you from real harm). While there are cases you don't need "use strict", it's a good idea to get into the habit of using them, and only in special cases you don't use them.

      By using a table type that doesn't have transactions
      ... you have committed yourself to never be able to use transaction on modifications of that table. Why bother with a database in such a case? Might as well use a bunch of DBM files.
      If you don't need transactions
      Famous last words, and belongs in the series "we don't need backups", "we don't need redundancy", "we don't need to look around when crossing the street". And indeed, most of the time, you don't need them. You just never know when you do.

      Abigail

        It would only be a one-statement transaction if %Data contains less than two elements. It's unlikely that that is the common case.

        Each element of %Data would be a different transaction anyways. Unless, of course, you plan on having a 2-million+ insert transaction. That will strain any rollback segment. (This is actually one reason why I'm moving from Oracle to MySQL.)

        you have committed yourself to never be able to use transaction on modifications of that table.

        Nope. ALTER TABLE FOO.BAR TYPE=INNODB now means that the table is ACID-transaction capable.

        And, I can think of a specific case where I know I don't need transactions - a database that is loaded once and will never be written to. In that case, I don't want the overhead of a transaction on my SELECT statements.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Re: How to Speed up MySQL w/ Perl
by kiat (Vicar) on May 26, 2004 at 11:50 UTC
    Hey Abigail,

    Please enlighten me on the last bit of your code which I reproduced below:

    if ($@) { $dbh -> rollback; die $@; } else { $dbh -> commit; } # Can we leave out the 'else' and just say: if ($@) { $dbh -> rollback; die $@; } $dbh -> commit;
    Is there any difference?
      Sure, you can leave off the else, but that makes your code less flexible. Suppose you want to replace the die with a warn, then you shouldn't forget introduce the else. Again, if there's just a commit, not much of a problem, if there are a whole bunch of statements, you have to figure out which ones belong logically in the else, and which ones should be run regardless whether the eval block failed. If you start off the else to begin with - or rather, if you use that as a habit, you won't have such problems. Having the else block is more of an engineering issue than a semantic one.

      Abigail

        Great, thanks!

        Now I get a better idea as I sometimes leave out the 'else' but have no idea whether it's a good practice.