Re: simpleQuery method causes too many mysql change db commands
| From: | Justin Patrin | Date: | Thu, 11 Aug 2005 16:51:14 +0000 |
| Subject: | Re: simpleQuery method causes too many mysql change db commands | ||
| References: | 1 2 3 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-20382@lists.php.net to get a copy of this message | ||
On 8/11/05, Ubaldo Huerta / gmail <ubaldo@gmail.com> wrote:
> Justin
>
> Thanks for the reply. Can you suggest a way to measure the performance
> impact of the avalanche of use database commands? My feeling is that
> having a round trip to the server before every select statement should
> noticeably affect performance (even if the command does virtually
> nothing, a round trip is still a round trip to be avoided if possible.
> See below the stats of a server that runs a web app that has a little
> bit a Pear DB code in it (only 15% of change db commands)
>
> Since I never have more than one open connection to different
> databases, it should be OK for me to comment out the code. However, I
> would prefer to see a long term solution (don't want to always have to
> patch the Pear DB code after a pear update). Can the code be patched
> to keep track whether there are concurrent connections to more than
> one database and only issue select database in such cases?
>
Well....here are the possible solutions I see.
1) Just remove that line from DB every time you update. This is hard
to deal with and will make your code fail if you ever connect to 2
DBs.
2) Fix the DB backends in PHP to actually use *seperate* connections
for different DBs or to keep track of and switch the DB on its own.
Yeah, like this is likely to happen.
3) Patch PEAR::DB to only chaneg the DB when necessary.
This last solution is probably the best but it's also problematic. Solutions:
a) Have a simple static var which holds the last-accessed-database and
change DBs if the current connection is on a different database. This
would mean that databases on different servers and on different
databases would all count together. Not the best solution, but it
should work cleanly and be perfect when only using one DB connection
in a script.
b) Have a static array which keeps track of the last DB accessed for
each DB type. This also would work like a) but would be better when
access multiple types of DBs. Still not 100% right.
c) Have a static array which keeps track of the DB for each DB type
and Host. This sounds like the right solution, but if you connect to
two DBs, one with "localhost" and one with "127.0.0.1" (or one server
by 2 different domain names) this solution would think that they're
seperate connections and not change the DB which could cause errors.
This problem could possibly me mitigated by resolving domain names to
IPs.
>
>
> On 8/10/05, Justin Patrin <papercrane@gmail.com> wrote:
> > On 8/10/05, Ubaldo Huerta / gmail <ubaldo@gmail.com> wrote:
> > > See in output of 'mytop' below that in my production server, 60% of all
> > > commands are change db. So I track it down and noticed that in simpleQuery
> > > there is database select even though in ::connect method the database is already
> > > selected.
> > >
> > > Can anybody explain why is done this way? Should passing the connection
> > > param to mysql_query call be sufficient?
> > >
> >
> > AFAIR this is done because the various DB extensions (such as mysql)
> > tend to set the database for *all* connections to a single server at
> > once. This means if you have two "connections" to different databases
> > on the same DB server you have to make sure the correct DB is selecte
> > don each one *every* time you make a query.
> >
> > This could possibly be "fixed" in one of a few ways, but before going
> > into that are you sure that this is affecting performance that much?
> > 60% of the queries may be "change db queries" but I doubt 60% of the
> > server's time is spent on it. It is, after all, only a simple context
> > change and doesn't return any data or do any real querying.
> >
> > --
> > Justin Patrin
> >
>
--
Justin Patrin