views:

330

answers:

2

I'm wondering how slow it's going to be switching between 2 databases on every call of every page of a site. The site has many different databases for different clients, along with a "global" database that is used for some general settings. I'm wondering if there would be much time added for the execution of each script if it has to connect to the database, select a DB, do a query or 2, switch to another DB and then complete the page generation. I could also have the data repeated in each DB, I just need to mantain it (will only change when upgrading).

So, in the end, how fast is mysql_select_db()?

Edit: Yes, I could connect to each DB separately, but as this is often the slowest part of any PHP script, I'd like to avoid this, especially since it's on every page. (It's slow because PHP has to do some kind of address resolution (be it an IP or host name) and then MySQL has to check the login parameters both times.)

+6  A: 

Assuming that both databases are on the same machine, you don't need to do the mysql_select_db. You can just specify the database in the queries. For example;

SELECT * FROM db1.table1;

You could also open two connections and use the DB object that is returned from the connect call and use those two objects to select the databases and pass into all of the calls. The database connection is an optional parameter on all of the mysql db calls, just check the docs.

Rob Prouse
+1  A: 

You're asking two quite different questions.

  1. Connecting to multiple database instances

  2. Switching default database schemas.

MySQL is known to have quite fast connection setup time; making two mysql_connect() calls to different servers is barely more expensive than one.

The call mysql_select_db() is exactly the same as the USE statement and simply changes the default database schema for unqualified table references.

Be careful with your use of the term 'database' around MySQL: it has two different meanings.

staticsan
I am partially wanting to compare what you are saying are 2 different things and also wanting to know if switching dbs takes any time. Thxs, but no thxs
Darryl Hein
If you qualify your table access, then mysql_select_db() is not needed. I don't have any experience at how fast it is because if I am using multiple schemas, then I fully qualify the table names.
staticsan