Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single database call vs multiple database calls Performance

Tags:

database

mysql

What is the difference in performance of making a single database (MySql) call that retrieves 10 results versus making 10 single db calls to retrieve those results individually?

If one is better than the other, is there a way I can go about to test the performance between the two? (I'm just curious because in the future if I have another question on performance, I could use the suggested way to test it out myself)

like image 415
Glide Avatar asked Dec 16 '22 21:12

Glide


2 Answers

A single call will always be faster than several calls for the same data. Simply the network turn around and latency alone is a component, but also the start up and tear down of the SQL processing will have some impact.

like image 146
Will Hartung Avatar answered Jan 14 '23 06:01

Will Hartung


What is heavier - a pound of apples or a bike?
Queries aren't equal. You cannot just compare them by numbers.
A query must be sensible in the first place. Doing its job the best way possible, not being imaginably "faster". That's the only reason to prefer one query to another.

I hope you meant different queries, not just a single query called 10 times.
Of course, running the same query multiple times should be avoided, as any other repetitive action - that's one of the basic rules in the art of programming.

And once your query/queries are right, you got to make them fast. And to answer the question how to do that - learn about profiling.

BENCHMARK <query> and EXPLAIN <query> are the tools for mysql profiling and performance tuning

like image 36
Your Common Sense Avatar answered Jan 14 '23 07:01

Your Common Sense