Is select_db necessary?
am 12.08.2009 05:23:21 von Allen McCabe
--001636426c1d58b7fc0470e9578c
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
I have seen different scripts for working with SQL, and most follow the same
method with on difference.
Variables are defined (host, password, etc.)
mysql_connect command
//then, the difference
mysql_select_db command
//back to common
$sql = "SELECT ... "
$result = mysql_query($ql)
Is the database selection necessary, or is that implied with a SELECT or
other SQL command?
--001636426c1d58b7fc0470e9578c--
Re: Is select_db necessary?
am 12.08.2009 05:56:18 von Paul M Foster
On Tue, Aug 11, 2009 at 08:23:21PM -0700, Allen McCabe wrote:
> I have seen different scripts for working with SQL, and most follow the same
> method with on difference.
>
> Variables are defined (host, password, etc.)
> mysql_connect command
>
> //then, the difference
>
> mysql_select_db command
>
> //back to common
>
> $sql = "SELECT ... "
> $result = mysql_query($ql)
>
> Is the database selection necessary, or is that implied with a SELECT or
> other SQL command?
All major SQL DBMSes can have multiple databases available. A given
database may contain a variety of tables. If you simply start firing SQL
commands at a DBMS, it won't know which database to look in unless you
tell it. By contrast, the connection process in PostgreSQL must include
a database; there is no separate database selection function call.
Paul
--
Paul M. Foster
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 12.08.2009 09:59:08 von Tony Marston
"Paul M Foster" wrote in message
news:20090812035618.GD2555@quillandmouse.com...
> On Tue, Aug 11, 2009 at 08:23:21PM -0700, Allen McCabe wrote:
>
>> I have seen different scripts for working with SQL, and most follow the
>> same
>> method with on difference.
>>
>> Variables are defined (host, password, etc.)
>> mysql_connect command
>>
>> //then, the difference
>>
>> mysql_select_db command
>>
>> //back to common
>>
>> $sql = "SELECT ... "
>> $result = mysql_query($ql)
>>
>> Is the database selection necessary, or is that implied with a SELECT or
>> other SQL command?
>
> All major SQL DBMSes can have multiple databases available. A given
> database may contain a variety of tables. If you simply start firing SQL
> commands at a DBMS, it won't know which database to look in unless you
> tell it. By contrast, the connection process in PostgreSQL must include
> a database; there is no separate database selection function call.
This is not totally accurate. With MySQL you connect to a server which is a
container for one or more databases, so you need select_db in order to
identify the current database name.
With PostgreSQL you connect to a database which is a container for one or
more schemas, so you need to issue the "SET search_path TO " command
in order to identify the current schema.
Oracle is the same in that you connect to a server which is a container for
one or more databases, and unless you give every table a public synonym you
must use the "ALTER SESSION SET CURRENT_SCHEMA = " command to
identify the current schema.
In all these cases this will allow you to issue an sql query which contains
table names which do not have to be qualified with their database/schema
names. If you wish to refer to a table which is not in the current
database/schema then you must include the database/schema name.
I consider the use of the term "schema", as used by PostgreSQL and Oracle,
to be inaccurate in that a database table is subordinate to a database, not
a schema. That is why it is called a "database" table and not a "schema"
table.
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 12.08.2009 13:41:12 von PJ
Paul M Foster wrote:
> On Tue, Aug 11, 2009 at 08:23:21PM -0700, Allen McCabe wrote:
>
>
>> I have seen different scripts for working with SQL, and most follow the same
>> method with on difference.
>>
>> Variables are defined (host, password, etc.)
>> mysql_connect command
>>
>> //then, the difference
>>
>> mysql_select_db command
>>
>> //back to common
>>
>> $sql = "SELECT ... "
>> $result = mysql_query($ql)
>>
>> Is the database selection necessary, or is that implied with a SELECT or
>> other SQL command?
>>
>
> All major SQL DBMSes can have multiple databases available. A given
> database may contain a variety of tables. If you simply start firing SQL
> commands at a DBMS, it won't know which database to look in unless you
> tell it. By contrast, the connection process in PostgreSQL must include
> a database; there is no separate database selection function call.
>
> Paul
>
I seem to recall from the manual, that once you have done mysql_connect
, any subsequent queries will be directed at the same db until another
mysql_connect points to another db. I repeat the call with every query
as a precaution.
I'm not expert, but I thought I'd offer my observation and am open to
correction. ;-)
PJ
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 12.08.2009 14:06:51 von Tony Marston
"PJ" wrote in message
news:4A82AA58.2020806@videotron.ca...
> Paul M Foster wrote:
>> On Tue, Aug 11, 2009 at 08:23:21PM -0700, Allen McCabe wrote:
>>
>>
>>> I have seen different scripts for working with SQL, and most follow the
>>> same
>>> method with on difference.
>>>
>>> Variables are defined (host, password, etc.)
>>> mysql_connect command
>>>
>>> //then, the difference
>>>
>>> mysql_select_db command
>>>
>>> //back to common
>>>
>>> $sql = "SELECT ... "
>>> $result = mysql_query($ql)
>>>
>>> Is the database selection necessary, or is that implied with a SELECT or
>>> other SQL command?
>>>
>>
>> All major SQL DBMSes can have multiple databases available. A given
>> database may contain a variety of tables. If you simply start firing SQL
>> commands at a DBMS, it won't know which database to look in unless you
>> tell it. By contrast, the connection process in PostgreSQL must include
>> a database; there is no separate database selection function call.
>>
>> Paul
>>
> I seem to recall from the manual, that once you have done mysql_connect
> , any subsequent queries will be directed at the same db until another
> mysql_connect points to another db. I repeat the call with every query
> as a precaution.
> I'm not expert, but I thought I'd offer my observation and am open to
> correction. ;-)
> PJ
This only works if you specify a database name in the call to
mysqli_connect(). If provided (it is optional) it identifies the default
database name for all subsequent queries. If, like me, you have multiple
databases available, then you need to use select_db in order to switch the
default database from one to another.
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Is select_db necessary?
am 12.08.2009 14:07:46 von Jay Blanchard
[snip]
Is the database selection necessary, or is that implied with a SELECT or
other SQL command?
[/snip]
It depends on the database (as you have seen in many of the responses),
but there is a way to keep from doing this if the database is ANSI
compliant using proper SQL syntax;
SELECT a.foo, a.bar
FROM myDatabase.myTable a
WHERE you set other conditions here
All that is required is that you establish a connection to a server.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 12.08.2009 14:25:41 von Floyd Resler
mysql_select_db is not necessary but it does make writing queries
easier since you don't have to specify which database in each query.
Take care,
Floyd
On Aug 11, 2009, at 11:23 PM, Allen McCabe wrote:
> I have seen different scripts for working with SQL, and most follow
> the same
> method with on difference.
>
> Variables are defined (host, password, etc.)
> mysql_connect command
>
> //then, the difference
>
> mysql_select_db command
>
> //back to common
>
> $sql = "SELECT ... "
> $result = mysql_query($ql)
>
> Is the database selection necessary, or is that implied with a
> SELECT or
> other SQL command?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 12.08.2009 14:47:37 von Robert Cummings
Jay Blanchard wrote:
> [snip]
> Is the database selection necessary, or is that implied with a SELECT or
> other SQL command?
> [/snip]
>
> It depends on the database (as you have seen in many of the responses),
> but there is a way to keep from doing this if the database is ANSI
> compliant using proper SQL syntax;
>
> SELECT a.foo, a.bar
> FROM myDatabase.myTable a
> WHERE you set other conditions here
>
> All that is required is that you establish a connection to a server.
If I recall correctly, this will cause issues with replication in
MySQL... insofar as you perform amodifying query.
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
RE: Is select_db necessary?
am 12.08.2009 14:53:29 von Jay Blanchard
[snip]
Jay Blanchard wrote:
> [snip]
> Is the database selection necessary, or is that implied with a SELECT
or
> other SQL command?
> [/snip]
>=20
> It depends on the database (as you have seen in many of the
responses),
> but there is a way to keep from doing this if the database is ANSI
> compliant using proper SQL syntax;
>=20
> SELECT a.foo, a.bar
> FROM myDatabase.myTable a
> WHERE you set other conditions here
>=20
> All that is required is that you establish a connection to a server.
If I recall correctly, this will cause issues with replication in=20
MySQL... insofar as you perform amodifying query.
[/snip]
You're correct with regards to queries that modify on replicated
systems. If all you're doing is gathering data then this will work just
fine, is somewhat self-documenting (especially in lengthier code
containers), and very flexible. It also leaves the selection in the
database's hands, and as we almost always say, "let the database do the
work when it can".
If you are not using replicated systems you can set up your modifying
queries the same way.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 12.08.2009 15:28:11 von Ralph Deffke
here a basic background to this question.
all databases are build from various module bases. one module is the
database itself prosessing eg. the sql's another module is the database
connectivity. e.g. mySQL has a ability to connect thru ADO, .NET and an
server via IP.
MySQL supports unlimited databases comtaining tables. so from the point of
the database u have always to selct the database and then to the table.
however, if u study the various Database extensions u will find functions
(eg. mysql_db_query() ) where u point the database in the function call
while others don't (eg. mysql_query() ) on those u have to do a db select
first because the function itself doesn't do it, while mysql_db_connect()
does.
so if we know that now, we are coming to the question, why are database
extensions do have those two types of processing a sql statement?
the answer is: speed ! while those commands with a pointing out the database
do internally a select of the database they do it every time on each call.
if u have a application which does a lot of stuff at the same time other
then just select statement, this comes into consideration. it saves time to
do one select_db first and then 50 just raw sql's to that database.
now after dumping that much stuff on u, it depends on ur design if u need a
select_db first or not.
hope that helps
Ralph
ralph_deffke@yahoo.de
"Allen McCabe" wrote in message
news:657acef20908112023y222de6f4q63e64cd1e2785d35@mail.gmail .com...
> I have seen different scripts for working with SQL, and most follow the
same
> method with on difference.
>
> Variables are defined (host, password, etc.)
> mysql_connect command
>
> //then, the difference
>
> mysql_select_db command
>
> //back to common
>
> $sql = "SELECT ... "
> $result = mysql_query($ql)
>
> Is the database selection necessary, or is that implied with a SELECT or
> other SQL command?
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 12.08.2009 15:28:15 von Colin Guthrie
'Twas brillig, and Jay Blanchard at 12/08/09 13:53 did gyre and gimble:
> Jay Blanchard wrote:
>> SELECT a.foo, a.bar
>> FROM myDatabase.myTable a
>> WHERE you set other conditions here
>>
>> All that is required is that you establish a connection to a server.
>
> If I recall correctly, this will cause issues with replication in
> MySQL... insofar as you perform amodifying query.
> [/snip]
>
> You're correct with regards to queries that modify on replicated
> systems. If all you're doing is gathering data then this will work just
> fine, is somewhat self-documenting (especially in lengthier code
> containers), and very flexible. It also leaves the selection in the
> database's hands, and as we almost always say, "let the database do the
> work when it can".
I'm interested to know why you consider this to be very flexible and how
this leaves the selection in the database's hands?
If I were to implement this and they try some destructive testing/demo
on a sacrificial database, I'd have to use a whole other server instance
(as all the queries would hardcode in the db name).
Is it not more flexible if you omit the table name in every single query
and specify it once in your bootstrap/connection code? Thus doing tests
on other dbs etc. is a pretty simple switch of the connection code.
Also telling the db engine what database you want to use in every query
is not, IMO, leaving the selection in the the database's hands.
Just curious as to the rationale here :)
Col
--
Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/
Day Job:
Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
Mandriva Linux Contributor [http://www.mandriva.com/]
PulseAudio Hacker [http://www.pulseaudio.org/]
Trac Hacker [http://trac.edgewall.org/]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 12.08.2009 15:37:54 von Ralph Deffke
I agree totally, are we not dicussing speed issues all the time? and then we
recommend a code doing an unessesary job on every call?
an ANSI selct db in the sql forces any database to run the internal select
db because there would be no check if the databse is the current one.
because, databasedevelopers can espext some smartness of us, the
programmers. its a lot off stuff to do for the database to select a
database. for shure, the database leafs that IN OUR hand to avoid to force
time consuming server resources.
ralph
ralph_deffke@yahoo.de
"Colin Guthrie" wrote in message
news:h5ug1h$tjb$1@ger.gmane.org...
> 'Twas brillig, and Jay Blanchard at 12/08/09 13:53 did gyre and gimble:
> > Jay Blanchard wrote:
> >> SELECT a.foo, a.bar
> >> FROM myDatabase.myTable a
> >> WHERE you set other conditions here
> >>
> >> All that is required is that you establish a connection to a server.
> >
> > If I recall correctly, this will cause issues with replication in
> > MySQL... insofar as you perform amodifying query.
> > [/snip]
> >
> > You're correct with regards to queries that modify on replicated
> > systems. If all you're doing is gathering data then this will work just
> > fine, is somewhat self-documenting (especially in lengthier code
> > containers), and very flexible. It also leaves the selection in the
> > database's hands, and as we almost always say, "let the database do the
> > work when it can".
>
> I'm interested to know why you consider this to be very flexible and how
> this leaves the selection in the database's hands?
>
> If I were to implement this and they try some destructive testing/demo
> on a sacrificial database, I'd have to use a whole other server instance
> (as all the queries would hardcode in the db name).
>
> Is it not more flexible if you omit the table name in every single query
> and specify it once in your bootstrap/connection code? Thus doing tests
> on other dbs etc. is a pretty simple switch of the connection code.
>
> Also telling the db engine what database you want to use in every query
> is not, IMO, leaving the selection in the the database's hands.
>
> Just curious as to the rationale here :)
>
> Col
>
>
>
>
> --
>
> Colin Guthrie
> gmane(at)colin.guthr.ie
> http://colin.guthr.ie/
>
> Day Job:
> Tribalogic Limited [http://www.tribalogic.net/]
> Open Source:
> Mandriva Linux Contributor [http://www.mandriva.com/]
> PulseAudio Hacker [http://www.pulseaudio.org/]
> Trac Hacker [http://trac.edgewall.org/]
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: Is select_db necessary?
am 12.08.2009 16:18:01 von Martin Scotta
--0016e64697429a17bf0470f27c59
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Wed, Aug 12, 2009 at 10:37 AM, Ralph Deffke wrote:
> I agree totally, are we not dicussing speed issues all the time? and then
> we
> recommend a code doing an unessesary job on every call?
>
> an ANSI selct db in the sql forces any database to run the internal select
> db because there would be no check if the databse is the current one.
> because, databasedevelopers can espext some smartness of us, the
> programmers. its a lot off stuff to do for the database to select a
> database. for shure, the database leafs that IN OUR hand to avoid to force
> time consuming server resources.
>
> ralph
> ralph_deffke@yahoo.de
>
> "Colin Guthrie" wrote in message
> news:h5ug1h$tjb$1@ger.gmane.org...
> > 'Twas brillig, and Jay Blanchard at 12/08/09 13:53 did gyre and gimble:
> > > Jay Blanchard wrote:
> > >> SELECT a.foo, a.bar
> > >> FROM myDatabase.myTable a
> > >> WHERE you set other conditions here
> > >>
> > >> All that is required is that you establish a connection to a server.
> > >
> > > If I recall correctly, this will cause issues with replication in
> > > MySQL... insofar as you perform amodifying query.
> > > [/snip]
> > >
> > > You're correct with regards to queries that modify on replicated
> > > systems. If all you're doing is gathering data then this will work just
> > > fine, is somewhat self-documenting (especially in lengthier code
> > > containers), and very flexible. It also leaves the selection in the
> > > database's hands, and as we almost always say, "let the database do the
> > > work when it can".
> >
> > I'm interested to know why you consider this to be very flexible and how
> > this leaves the selection in the database's hands?
> >
> > If I were to implement this and they try some destructive testing/demo
> > on a sacrificial database, I'd have to use a whole other server instance
> > (as all the queries would hardcode in the db name).
> >
> > Is it not more flexible if you omit the table name in every single query
> > and specify it once in your bootstrap/connection code? Thus doing tests
> > on other dbs etc. is a pretty simple switch of the connection code.
> >
> > Also telling the db engine what database you want to use in every query
> > is not, IMO, leaving the selection in the the database's hands.
> >
> > Just curious as to the rationale here :)
> >
> > Col
> >
> >
> >
> >
> > --
> >
> > Colin Guthrie
> > gmane(at)colin.guthr.ie
> > http://colin.guthr.ie/
> >
> > Day Job:
> > Tribalogic Limited [http://www.tribalogic.net/]
> > Open Source:
> > Mandriva Linux Contributor [http://www.mandriva.com/]
> > PulseAudio Hacker [http://www.pulseaudio.org/]
> > Trac Hacker [http://trac.edgewall.org/]
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
$link = mysql_connect( /* settings */);
mysql_select_db( 'database', $link);
$result = mysql_query( 'SELECT * FROM ', $link );
What SQL was sent to the database?
Looking at bin logs I've found this.
1. use database => mysql_select_db
2. use database: SELECT * FROM => mysql_query
The DB is usually a common bottle-neck for most applications.
You can have several webservers, but can't do that with the DB... of course,
you can have multiples slaves but just 1 master.
is this the best way to send queries?
What's the better and faster way?
--
Martin Scotta
--0016e64697429a17bf0470f27c59--
Re: Re: Is select_db necessary?
am 12.08.2009 16:30:12 von Ralph Deffke
as i said earlier: on db level there is always al select db done, doing this
on higer level layers (the database extension) consumes time. or why do
extension have the two ways of functions? to make our live more difficult?
on a ANSI sql the sql interpreter time is increased! unnessarylie
ralph_deffke@yahoo.de
"Martin Scotta" wrote in message
news:6445d94e0908120718g6c5bf368tacf8bbad127b5c59@mail.gmail .com...
> Wed, Aug 12, 2009 at 10:37 AM, Ralph Deffke wrote:
>
> > I agree totally, are we not dicussing speed issues all the time? and
then
> > we
> > recommend a code doing an unessesary job on every call?
> >
> > an ANSI selct db in the sql forces any database to run the internal
select
> > db because there would be no check if the databse is the current one.
> > because, databasedevelopers can espext some smartness of us, the
> > programmers. its a lot off stuff to do for the database to select a
> > database. for shure, the database leafs that IN OUR hand to avoid to
force
> > time consuming server resources.
> >
> > ralph
> > ralph_deffke@yahoo.de
> >
> > "Colin Guthrie" wrote in message
> > news:h5ug1h$tjb$1@ger.gmane.org...
> > > 'Twas brillig, and Jay Blanchard at 12/08/09 13:53 did gyre and
gimble:
> > > > Jay Blanchard wrote:
> > > >> SELECT a.foo, a.bar
> > > >> FROM myDatabase.myTable a
> > > >> WHERE you set other conditions here
> > > >>
> > > >> All that is required is that you establish a connection to a
server.
> > > >
> > > > If I recall correctly, this will cause issues with replication in
> > > > MySQL... insofar as you perform amodifying query.
> > > > [/snip]
> > > >
> > > > You're correct with regards to queries that modify on replicated
> > > > systems. If all you're doing is gathering data then this will work
just
> > > > fine, is somewhat self-documenting (especially in lengthier code
> > > > containers), and very flexible. It also leaves the selection in the
> > > > database's hands, and as we almost always say, "let the database do
the
> > > > work when it can".
> > >
> > > I'm interested to know why you consider this to be very flexible and
how
> > > this leaves the selection in the database's hands?
> > >
> > > If I were to implement this and they try some destructive testing/demo
> > > on a sacrificial database, I'd have to use a whole other server
instance
> > > (as all the queries would hardcode in the db name).
> > >
> > > Is it not more flexible if you omit the table name in every single
query
> > > and specify it once in your bootstrap/connection code? Thus doing
tests
> > > on other dbs etc. is a pretty simple switch of the connection code.
> > >
> > > Also telling the db engine what database you want to use in every
query
> > > is not, IMO, leaving the selection in the the database's hands.
> > >
> > > Just curious as to the rationale here :)
> > >
> > > Col
> > >
> > >
> > >
> > >
> > > --
> > >
> > > Colin Guthrie
> > > gmane(at)colin.guthr.ie
> > > http://colin.guthr.ie/
> > >
> > > Day Job:
> > > Tribalogic Limited [http://www.tribalogic.net/]
> > > Open Source:
> > > Mandriva Linux Contributor [http://www.mandriva.com/]
> > > PulseAudio Hacker [http://www.pulseaudio.org/]
> > > Trac Hacker [http://trac.edgewall.org/]
> > >
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
>
> $link = mysql_connect( /* settings */);
> mysql_select_db( 'database', $link);
> $result = mysql_query( 'SELECT * FROM ', $link );
>
> What SQL was sent to the database?
>
> Looking at bin logs I've found this.
>
> 1. use database => mysql_select_db
> 2. use database: SELECT * FROM => mysql_query
>
> The DB is usually a common bottle-neck for most applications.
> You can have several webservers, but can't do that with the DB... of
course,
> you can have multiples slaves but just 1 master.
>
> is this the best way to send queries?
> What's the better and faster way?
>
> --
> Martin Scotta
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
AW: Re: Is select_db necessary?
am 12.08.2009 16:41:22 von Ralph Deffke
--0-1901932502-1250088082=:59569
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
what are telling the logs on that code?
nect( /* settings */);=0Amysql_select_db( 'database', $link);=0A$result =3D=
mysql_query( 'SELECT * FROM
', $link );=0A$result =3D mysql_query( =
'SELECT * FROM ', $link );=0A$result =3D mysql_query( 'SELECT=
* FROM ', $link );=0A$result =3D mysql_query( 'SELECT * FROM=
', $link );=0A$result =3D mysql_query( 'SELECT * FROM
ertable>', $link );=0A$result =3D mysql_query( 'SELECT * FROM ', $li=
nk );
would be interesting to see.
I personaly woudn't spend the =
time on logs, a computer is logical, I try to be logical, and I would=0Atry=
to create code which is logical speedy. I expect the database kernel progr=
ammer the same.
I think then we are on the secure side.
ralph_def=
fke@yahoo.de
______________________________ __=0AVon: Marti=
n Scotta =0AAn: Ralph Deffke
>=0ACC: php-general@lists.php.net=0AGesendet: Mittwoch, den 12. August 2009=
, 16:18:01 Uhr=0ABetreff: Re: [PHP] Re: Is select_db necessary?
=0AWed=
, Aug 12, 2009 at 10:37 AM, Ralph Deffke wrote:=0A=
=0A>I agree totally, are we not dicussing speed issues all the time? and th=
en we=0A>>recommend a code doing an unessesary job on every call?=0A>=0A>>a=
n ANSI selct db in the sql forces any database to run the internal select=
=0A>>db because there would be no check if the databse is the current one.=
=0A>>because, databasedevelopers can espext some smartness of us, the=0A>>p=
rogrammers. its a lot off stuff to do for the database to select a=0A>>data=
base. for shure, the database leafs that IN OUR hand to avoid to force=0A>>=
time consuming server resources.=0A>=0A>>ralph=0A>ralph_deffke@yahoo.de=0A>=
=0A>>"Colin Guthrie" wrote in message=0A>>news:h5ug1=
h$tjb$1@ger.gmane.org...=0A>=0A>> 'Twas brillig, and Jay Blanchard at 12/08=
/09 13:53 did gyre and gimble:=0A>>> > Jay Blanchard wrote:=0A>>> >> SELECT=
a.foo, a.bar=0A>>> >> FROM myDatabase.myTable a=0A>>> >> WHERE you set oth=
er conditions here=0A>>> >>=0A>>> >> All that is required is that you estab=
lish a connection to a server.=0A>>> >=0A>>> > If I recall correctly, this =
will cause issues with replication in=0A>>> > MySQL... insofar as you perfo=
rm amodifying query.=0A>>> > [/snip]=0A>>> >=0A>>> > You're correct with re=
gards to queries that modify on replicated=0A>>> > systems. If all you're d=
oing is gathering data then this will work just=0A>>> > fine, is somewhat s=
elf-documenting (especially in lengthier code=0A>>> > containers), and very=
flexible. It also leaves the selection in the=0A>>> > database's hands, an=
d as we almost always say, "let the database do the=0A>>> > work when it ca=
n".=0A>>>=0A>>> I'm interested to know why you consider this to be very fle=
xible and how=0A>>> this leaves the selection in the database's hands?=0A>>=
>=0A>>> If I were to implement this and they try some destructive testing/d=
emo=0A>>> on a sacrificial database, I'd have to use a whole other server i=
nstance=0A>>> (as all the queries would hardcode in the db name).=0A>>>=0A>=
>> Is it not more flexible if you omit the table name in every single query=
=0A>>> and specify it once in your bootstrap/connection code? Thus doing te=
sts=0A>>> on other dbs etc. is a pretty simple switch of the connection cod=
e.=0A>>>=0A>>> Also telling the db engine what database you want to use in =
every query=0A>>> is not, IMO, leaving the selection in the the database's =
hands.=0A>>>=0A>>> Just curious as to the rationale here :)=0A>>>=0A>>> Col=
=0A>>>=0A>>>=0A>>>=0A>>>=0A>>> --=0A>>>=0A>>> Colin Guthrie=0A>>> gmane(at)=
colin.guthr.ie=0A>>> http://colin.guthr.ie/=0A>>>=0A>>> Day Job:=0A>>> T=
ribalogic Limited [http://www.tribalogic.net/]=0A>>> Open Source:=0A>>> =
Mandriva Linux Contributor [http://www.mandriva.com/]=0A>>> PulseAudio H=
acker [http://www.pulseaudio.org/]=0A>>> Trac Hacker [http://trac.edgewa=
ll.org/]=0A>>>=0A>=0A>=0A>=0A>>--=0A>>PHP General Mailing List (http://www.=
php.net/)=0A>>To unsubscribe, visit: http://www.php.net/unsub.php=0A>=0A>=
b( 'database', $link);=0A$result =3D mysql_query( 'SELECT * FROM ', =
$link );
What SQL was sent to the database?
Looking at bin logs I=
've found this.
1. use database =3D> mysql_select_db=0A2. use database=
: SELECT * FROM =3D> mysql_query
The DB is usually a common b=
ottle-neck for most applications. =0AYou can have several webservers, but c=
an't do that with the DB... of course, you can have multiples slaves but ju=
st 1 master.
is this the best way to send queries?=0AWhat's the better=
and faster way?
-- =0AMartin Scotta
--0-1901932502-1250088082=:59569--
Re: Re: Is select_db necessary?
am 12.08.2009 18:08:26 von Martin Scotta
--00163628513e7a43ee0470f407c5
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
On Wed, Aug 12, 2009 at 11:41 AM, Ralph Deffke wrote:
> what are telling the logs on that code?
>
>
>
> $link = mysql_connect( /* settings */);
> mysql_select_db( 'database', $link);
> $result = mysql_query( 'SELECT * FROM ', $link );
> $result = mysql_query( 'SELECT * FROM ', $link );
> $result = mysql_query( 'SELECT * FROM ', $link );
> $result = mysql_query( 'SELECT * FROM ', $link );
> $result = mysql_query( 'SELECT * FROM ', $link );
> $result = mysql_query( 'SELECT * FROM ', $link );
>
> would be interesting to see.
>
> I personaly woudn't spend the time on logs, a computer is logical, I try to
> be logical, and I would
> try to create code which is logical speedy. I expect the database kernel
> programmer the same.
>
> I think then we are on the secure side.
>
> ralph_deffke@yahoo.de
>
>
> ------------------------------
> *Von:* Martin Scotta
> *An:* Ralph Deffke
> *CC:* php-general@lists.php.net
> *Gesendet:* Mittwoch, den 12. August 2009, 16:18:01 Uhr
> *Betreff:* Re: [PHP] Re: Is select_db necessary?
>
> Wed, Aug 12, 2009 at 10:37 AM, Ralph Deffke wrote:
>
>> I agree totally, are we not dicussing speed issues all the time? and then
>> we
>> recommend a code doing an unessesary job on every call?
>>
>> an ANSI selct db in the sql forces any database to run the internal select
>> db because there would be no check if the databse is the current one.
>> because, databasedevelopers can espext some smartness of us, the
>> programmers. its a lot off stuff to do for the database to select a
>> database. for shure, the database leafs that IN OUR hand to avoid to force
>> time consuming server resources.
>>
>> ralph
>> ralph_deffke@yahoo.de
>>
>> "Colin Guthrie" wrote in message
>> news:h5ug1h$tjb$1@ger.gmane.org...
>> > 'Twas brillig, and Jay Blanchard at 12/08/09 13:53 did gyre and gimble:
>> > > Jay Blanchard wrote:
>> > >> SELECT a.foo, a.bar
>> > >> FROM myDatabase.myTable a
>> > >> WHERE you set other conditions here
>> > >>
>> > >> All that is required is that you establish a connection to a server.
>> > >
>> > > If I recall correctly, this will cause issues with replication in
>> > > MySQL... insofar as you perform amodifying query.
>> > > [/snip]
>> > >
>> > > You're correct with regards to queries that modify on replicated
>> > > systems. If all you're doing is gathering data then this will work
>> just
>> > > fine, is somewhat self-documenting (especially in lengthier code
>> > > containers), and very flexible. It also leaves the selection in the
>> > > database's hands, and as we almost always say, "let the database do
>> the
>> > > work when it can".
>> >
>> > I'm interested to know why you consider this to be very flexible and how
>> > this leaves the selection in the database's hands?
>> >
>> > If I were to implement this and they try some destructive testing/demo
>> > on a sacrificial database, I'd have to use a whole other server instance
>> > (as all the queries would hardcode in the db name).
>> >
>> > Is it not more flexible if you omit the table name in every single query
>> > and specify it once in your bootstrap/connection code? Thus doing tests
>> > on other dbs etc. is a pretty simple switch of the connection code.
>> >
>> > Also telling the db engine what database you want to use in every query
>> > is not, IMO, leaving the selection in the the database's hands.
>> >
>> > Just curious as to the rationale here :)
>> >
>> > Col
>> >
>> >
>> >
>> >
>> > --
>> >
>> > Colin Guthrie
>> > gmane(at)colin.guthr.ie
>> > http://colin.guthr.ie/
>> >
>> > Day Job:
>> > Tribalogic Limited [http://www.tribalogic.net/]
>> > Open Source:
>> > Mandriva Linux Contributor [http://www.mandriva.com/]
>> > PulseAudio Hacker [http://www.pulseaudio.org/]
>> > Trac Hacker [http://trac.edgewall.org/]
>> >
>>
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
>
>
> $link = mysql_connect( /* settings */);
> mysql_select_db( 'database', $link);
> $result = mysql_query( 'SELECT * FROM ', $link );
>
> What SQL was sent to the database?
>
> Looking at bin logs I've found this.
>
> 1. use database => mysql_select_db
> 2. use database: SELECT * FROM => mysql_query
>
> The DB is usually a common bottle-neck for most applications.
> You can have several webservers, but can't do that with the DB... of
> course, you can have multiples slaves but just 1 master.
>
> is this the best way to send queries?
> What's the better and faster way?
>
> --
> Martin Scotta
>
>
Mysql only stores the SQL in the log when the query affect something in the
DB.
So, SELECTS do not appear in the binlog.
That said, this is the script I have runned
$link = mysql_connect( /*settings*/ );
mysql_select_db('devtool', $link);
mysql_query( 'TRUNCATE TABLE dev_activities', $link );
mysql_query( 'TRUNCATE TABLE dev_files', $link );
mysql_query( 'INSERT INTO dev_activities VALUES (1, 1)', $link );
mysql_query( 'INSERT INTO dev_files VALUES (1, 1)', $link );
mysql_query( 'INSERT INTO dev_activities VALUES (2, 2)', $link );
mysql_query( 'INSERT INTO dev_files VALUES (2, 2)', $link );
and this is the binglog's entries
use `devtool`; TRUNCATE TABLE dev_activities
use `devtool`; TRUNCATE TABLE dev_files
use `devtool`; INSERT INTO dev_activities VALUES (1, 1)
use `devtool`; INSERT INTO dev_files VALUES (1, 1)
use `devtool`; INSERT INTO dev_activities VALUES (2, 2)
use `devtool`; INSERT INTO dev_files VALUES (2, 2)
--
Martin Scotta
--00163628513e7a43ee0470f407c5--
RE: Re: Is select_db necessary?
am 12.08.2009 18:32:14 von Jay Blanchard
[snip]
I'm interested to know why you consider this to be very flexible and how
this leaves the selection in the database's hands?
[/snip]
Flexible because I can connect to more than one database on a server
using one connection without having to re-issue a select_db command,
especially in a code container requiring connection to multiple
databases.=20
It leaves the connection on the database side because the selection of
the database is performed in the query rather than in a separate query.
A select_db essentially issues a "use database" query which can be
avoided by including database selection in the query itself.
[snip]
If I were to implement this and they try some destructive testing/demo=20
on a sacrificial database, I'd have to use a whole other server instance
(as all the queries would hardcode in the db name).
[/snip]
I am unsure of what you're after here. We are only using a hard-coded
example but we can certainly improve this by using a class or function.
[snip]
Is it not more flexible if you omit the table name in every single query
and specify it once in your bootstrap/connection code? Thus doing tests=20
on other dbs etc. is a pretty simple switch of the connection code.
[/snip]
Sure it is, unless you have to connect to more than one database in any
given code container. Consider this, I include a database server
connection (one file) and I do not have to do a select_db in other
subsequent files if I include the database name in the SQL query itself;
include("inc/dataConnect.inc"); // containing server connection only
Now in foo.php would you rather;
$theDatabaseSelected =3D select_db('database', $dbc);
$theQuery =3D "SELECT foo FROM bar WHERE glorp";
Or;
$theQuery =3D "SELECT a.foo FROM database.bar a WHERE glorp";
Now consider that I have to get information from more than one database
(on the same server) in a single container for display. Do you want to
issue the select_db each time?
$theDatabaseSelected =3D select_db('database', $dbc);
$theQuery =3D "SELECT foo FROM bar WHERE glorp";
$theNextDatabaseSelected =3D select_db('nextDatabase', $dbc);
$theQuery =3D "SELECT glorp FROM foo WHERE bar";
Or would it be easier to do this?
$theQuery =3D "SELECT a.foo FROM database.bar a WHERE glorp";
$theNextQuery =3D "SELECT a.glorp FROM database.foo a WHERE bar";
[snip]
Also telling the db engine what database you want to use in every query=20
is not, IMO, leaving the selection in the the database's hands.
[/snip]
Sure it is, if not you have to use PHP (select_db) to perform the
database selection which sends an additional query ('use database') to
the database system.
In other words, would you query all of the raw data out of the database
and use PHP to process that data when the database can do a much more
effective job of filtering out what you do not need?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Is select_db necessary?
am 13.08.2009 11:10:45 von Colin Guthrie
'Twas brillig, and Jay Blanchard at 12/08/09 17:32 did gyre and gimble:
> [snip]
> I'm interested to know why you consider this to be very flexible and how
>
> this leaves the selection in the database's hands?
> [/snip]
>
> Flexible because I can connect to more than one database on a server
> using one connection without having to re-issue a select_db command,
> especially in a code container requiring connection to multiple
> databases.
Fair point, but I would say that in the majority of cases an app pretty
much connects to one database. I personally have exceptions to that
rule, so I fully appreciate that this is not always the case and some
people may see more of this type of setup than others, but I think it
probably holds for the majority.
In this case it doesn't provide any extra flexibility - that's why I
asked :) I guess it's only flexible if you are are dealing with a
multi-db system. Even then it's arguably more flexible to keep a primary
db selected and use it sans db prefix and use only the "other" databases
in a fully "namespaced" way. (as this keeps flexibility of changing db
easily - without the need for a wrapper.
> [snip]
> If I were to implement this and they try some destructive testing/demo
> on a sacrificial database, I'd have to use a whole other server instance
>
> (as all the queries would hardcode in the db name).
> [/snip]
>
> I am unsure of what you're after here. We are only using a hard-coded
> example but we can certainly improve this by using a class or function.
True, but arguably unnecessary overhead - especially in the "one db app"
common case. Not necessarily significant, but it all adds up.
> [snip]
> Is it not more flexible if you omit the table name in every single query
>
> and specify it once in your bootstrap/connection code? Thus doing tests
> on other dbs etc. is a pretty simple switch of the connection code.
> [/snip]
>
> Sure it is, unless you have to connect to more than one database in any
> given code container. Consider this, I include a database server
> connection (one file) and I do not have to do a select_db in other
> subsequent files if I include the database name in the SQL query itself;
>
>
> include("inc/dataConnect.inc"); // containing server connection only
>
> Now in foo.php would you rather;
>
> $theDatabaseSelected = select_db('database', $dbc);
> $theQuery = "SELECT foo FROM bar WHERE glorp";
>
> Or;
>
> $theQuery = "SELECT a.foo FROM database.bar a WHERE glorp";
>
> Now consider that I have to get information from more than one database
> (on the same server) in a single container for display. Do you want to
> issue the select_db each time?
>
> $theDatabaseSelected = select_db('database', $dbc);
> $theQuery = "SELECT foo FROM bar WHERE glorp";
>
> $theNextDatabaseSelected = select_db('nextDatabase', $dbc);
> $theQuery = "SELECT glorp FROM foo WHERE bar";
>
> Or would it be easier to do this?
>
> $theQuery = "SELECT a.foo FROM database.bar a WHERE glorp";
> $theNextQuery = "SELECT a.glorp FROM database.foo a WHERE bar";
Aside from the incorrect db name :p, it is arguable easier :)
I'm not ultimately suggesting that this isn't a useful technique at
times (I do do this myself in some apps), but I still reckon that for
the majority of applications, it's makes more sense to work with a known
database at all times for your connection and avoid the whole db name
whenever possible.
> [snip]
> Also telling the db engine what database you want to use in every query
> is not, IMO, leaving the selection in the the database's hands.
> [/snip]
>
> Sure it is, if not you have to use PHP (select_db) to perform the
> database selection which sends an additional query ('use database') to
> the database system.
>
> In other words, would you query all of the raw data out of the database
> and use PHP to process that data when the database can do a much more
> effective job of filtering out what you do not need?
Well that analogy is lost on me... I really don't see what the
comparison of a select db statement vs a db delimited table in queries
has to do with reading raw data out and processing it in PHP....
But regardless (and this is more of a nitpicking semantic thing than
anything PHP/db related now!), if I let the "db do the work" then I set
it up with certain information and then give it limited information
repeatedly and let it work things out.
By setting it up with a select db and letting it figure out which schema
I want my data from by not telling it in multiple individual queries,
I'm very much letting the "db do the work".
If I tell it explicitly at all times what database to use then I'm doing
the work that could have been offloaded to the database. And if your
query system goes via a wrapper to put in the right db schema names
(e.g. from a config file) as you suggested, then the work you are doing
on each query is very much real work done in PHP (str_replace, regexp
matching/replacing, concatenation or whatever).
So perhaps it depends on your view point and preconceptions and we're
both coming at the "flexible" and "offloading" arguments with different
starting views.
Anyway, I only asked out of curiosity which I think has been satisfied
(i.e. ultimately I don't fully agree with you! :p).
Cheers
Col
--
Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/
Day Job:
Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
Mandriva Linux Contributor [http://www.mandriva.com/]
PulseAudio Hacker [http://www.pulseaudio.org/]
Trac Hacker [http://trac.edgewall.org/]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Re: Is select_db necessary?
am 13.08.2009 14:58:31 von Jay Blanchard
[snip]
So perhaps it depends on your view point and preconceptions and we're=20
both coming at the "flexible" and "offloading" arguments with different=20
starting views.
Anyway, I only asked out of curiosity which I think has been satisfied=20
(i.e. ultimately I don't fully agree with you! :p).
[/snip]
No worries! We are discussing semantics and situations which are always
unique. Our primary application is one that uses data from many
disparate and discrete data sources, integrating these things to make
life simpler for the end user. The methodology that I describe is quite
useful in that situation.
We do have other applications that use a single database and where we
explicitly state the database using select_db; the database name never
has to appear in the query at all. I occasionally hear a little whining
and moaning about this as the queries become less self-documenting,
forcing everyone to add more commentary to the code to make up for the
deficiency. Actually I have found that during code review in these
'single database' applications many of the queries do have the database
name stated. The devs have seen the value in this method across the
board.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php