PDA

View Full Version : question about DB queries


oldmanstan
February 25th, 2006, 03:44 AM
i have a mysql table that i want to pull rows out of and stuff them into objects in php. i can either query the DB and have it return a series of arrays with the values for each column (ex: $name[0] and $email[0] would be the values for the first row... etc.) or i could do several queries.

basically the question is which is more expensive in terms of server usage: one large query or several smaller queries?

my gut instinct tells me the large query would be better but i was wondering if anyone has any input?

sas
February 25th, 2006, 07:08 AM
Executing multiple queries would probably take longer. Definetly so if you aren't using persistant connections.

Try both with a crude benchmark.
<?php
$time_start = microtime(true);

//your script here

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Execution time: $time seconds\n";
?>

sapo
February 25th, 2006, 07:56 AM
I ve always used the less number of queries possible, and you should really consider using the mysql LIMIT function, or you can slow everything on your site with this query :p

LordHunter317
February 25th, 2006, 12:12 PM
Always minimize queries, and do as much work on the DB side as possible. It's faster than you.

oldmanstan
February 25th, 2006, 03:42 PM
thanks guys, looks like my instinct was right, cool