Optimisation

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Optimisation

Post by Archy »

A simple question really, would it be quicker, and more server friendly to either:

- Get 3 columns from a database via 1 query, or
- Get 3 rows from a database via 3 seperate queries.

I know which I think is quicker, but since I am no expert on databased, I wondered if someone could enlighten me.

Thanks.
Last edited by Archy on Wed Feb 16, 2005 9:26 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

3 rows should be quicker, as it should require less checking/matching for what to return.
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

Sorry, I made a mistake in my other post, I meant (And is corrected in my previous post):

- Get 3 columns from a database via 1 query, or
- Get 3 rows from a database via 3 seperate queries.

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

1 query scenarios are generally the faster.. but that depends on the query.
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

That's what I thought. The queries are only very basic.

Thanks.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

experiment and share the results with us..... :)

i can imagine mysql is better in performing 3 simple queries faster than 1 complex query....
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

In my experience Postgres is generally quicker when querying once, even with complex joins. Let the DB do the work rather than PHP as the numebr of things you return are generally smaller. This also makes maintenance of the PHP easier.

As always it depends on the php server and db server setup. Having a slow DBserver but a mega webserver may reverse things.

Also it should be worth pointing out that it is generally quicker to get only the columns required rather than using SELECT *, especially when joining tables.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Yea, using php w/ mysql I find that generally it is better to let the db handle as much as the data processing as I can give to it. It is generally much more efficient then pulling it over and then doing all sorts of traversing of arrays or whatnot.

Of course, this is not always true. Only testing can show the true path.
Post Reply