Re: Re: General Mysql Connect

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2008-10-29 at 18:32 -0700, Waynn Lue wrote:
> >
> > Waynn Lue wrote:
> >
> >> Yeah, it's the same user, same everything (for now). But I wonder why
> >> we're seeing these "lost connection" errors and I'm trying to fix
> >> it--this was one of the things I was investigating.
> >>
> >
> > Random guess :)
> >
> > You're overwriting a result or connection variable?
> >
> > $query = "select * from db1.table1";
> > $result = mysql_query($query, $conn1);
> >
> > while ($row = mysql_fetch_assoc($result)) {
> >  ...
> >  $result = mysql_query("select * from db2.table2", $conn2);
> > }
> >
> It's possible, but seems unlikely because the problem only comes up
> periodically, not deterministically.

Here's the answer you want:

Switching database over the same connection is MUCH cheaper than opening
a brand new connection each time. HOWEVER, if you are constantly
switching between the databases within the same request, then it is more
efficient to open two connections. WHY? Because it costs you a network
(or socket) query every time you switch databases. This is cheaper than
opening a new connection, but over time if you continuously switch
within the same request, it will be more expensive than opening a new
connection. Most likely, from what I've read, it is cheaper for you to
switch over the same connection. Now, someone else mentioned that in
MySQL you can do select foo from db2.table where blah blah blah. This is
the fastest since no extra query needs to go out. However, and this may
or may not matter, I think it is less portable.

Sooo.....

<?php

   $db1 = new Connection( 'db1' );
   $db2 = new Connection( 'db2' );

   for( $i = 0; $i < 1000000; $++ )
   {
       $db1->query();
       $db2->query();
   }

?>

2 expensive opens, 2 million cheap queries.

And...

<?php

   $db = new Connection();

   for( $i = 0; $i < 1000000; $++ )
   {
       $db->selectDatabase( 'db1' );
       $db->query();

       $db->selectDatabase( 'db2' );
       $db->query();
   }

?>

1 expensive open, 4 million cheap queries.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux