tags:

views:

94

answers:

8

I have a table in my database that has about 200 rows of data that I need to retrieve. How significant, if at all, is the difference in efficiency when retrieving all of them at once in one query, versus each row individually in separate queries?

A: 

In general, you want to minimize the number of calls to the database. You can already assume that MySQL is optimized to retrieve rows, however you cannot be certain that your calls are optimized, if at all.

AlbertoPL
A: 

Extremely significant, Usually getting all the rows at once will take as much time as getting one row. So let's say that time is 1 second (very high but good for illustration) then getting all the rows will take 1 second, getting each row individually will take 200 seconds (1 second for each row) A very dramatic difference. And this isn't counting where are you getting the list of 200 to begin with.

Solmead
+6  A: 

The queries are usually made via a socket, so executing 200 queries instead of 1 represents a lot of overhead, plus the RDBMS is optimized to fetch a lot of rows for one query.

200 queries instead of 1 will make the RDBMS initialize datasets, parse the query, fetch one row, populate the datasets, and send the results 200 times instead of 1 time.

It's a lot better to execute only one query.

FWH
Thanks for the advice, I appreciate it!
James Skidmore
Turning multiple queries into a single query is often the easiest way to improve database performance.
gradbot
+1  A: 

I think the difference will be significant, because there will (I guess) be a lot of overhead in parsing and executing the query, packaging the data up to send back etc., which you are then doing for every row rather than once.

It is often useful to write a quick test which times various approaches, then you have meaningful statistics you can compare.

Tom Haigh
+1 -- very true with the parsing/executing/packaging, etc. Thanks!
James Skidmore
A: 

All that said, you've only got 200 rows, so in practice it won't matter much.

But still, get them all at once.

+1  A: 

If you were talking about some constant number of queries k versus a greater number of constant queries k+k1 you may find that more queries is better. I don't know for sure but SQL has all sorts of unusual quirks so it wouldn't surprise me if someone could come up with a scenario like this.

However if you're talking about some constant number of queries k versus some non-constant number of queries n you should always pick the constant number of queries option.

Spencer Ruport
A: 

Exactly as the others have said. Your RDBMS will not break a sweat throwing 200+++++ rows at you all at once. Getting all the rows in one associative array will also not make much difference to your script, since you no doubt already have a loop for grabbing each individual row.

All you need do is modify this loop to iterate through the array you are given [very minor tweak!]

Peter Spain
A: 

The only time I have found it better to get fewer results from multiple queries instead of one big set is if there is lots of processing to be done on the results. I was able to cut out about 40,000 records from the result set (plus associated processing) by breaking the result set up. Anything you can build into the query that will allow the DB to do the processing and reduce result set size is a benefit, but if you truly need all the rows, just go get them.

acrosman