Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One complex query vs Multiple simple queries

Tags:

What is actually better? Having classes with complex queries responsible to load for instance nested objects? Or classes with simple queries responsible to load simple objects?

With complex queries you have to go less to database but the class will have more responsibility.

Or simple queries where you will need to go more to database. In this case however each class will be responsible for loading one type of object.

The situation I'm in is that loaded objects will be sent to a Flex application (DTO's).

like image 510
Lieven Cardoen Avatar asked Mar 26 '09 09:03

Lieven Cardoen


People also ask

What is faster one big query or many small queries?

In Postgres (and probably any RDBMS to a similar extent, MySQL to a lesser extent), fewer queries are almost always much faster. The overhead of parsing and planning multiple queries is already more than any possible gain in most cases.

What is the advantage of using sub query or multi query in data manipulation?

Advantage of Using SubqueryIt would be easy to implement with a subquery, which computes the average salary. Subquery can act as a column with a single value: You can also use a subquery as a new column. The only constraint is that the subquery must return only one value.

What are the complex queries?

Complex queries help to narrow a detailed listing obtained as output from an API. To generate the desired output, you can pass queries using And or Or operators in the input XML of an API.

What are multiple queries?

Definition. Multiple queries in a single report makes the report retrieve information from the Data Warehouse multiple times. Normally, a report will fetch data from the database only once.


1 Answers

The general rule of thumb here is that server roundtrips are expensive (relative to how long a typical query takes) so the guiding principle is that you want to minimize them. Basically each one-to-many join will potentially multiply your result set so the way I approach this is to keep joining until the result set gets too large or the query execution time gets too long (roughly 1-5 seconds generally).

Depending on your platform you may or may not be able to execute queries in parallel. This is a key determinant in what you should do because if you can only execute one query at a time the barrier to breaking up a query is that much higher.

Sometimes it's worth keeping certain relatively constant data in memory (country information, for example) or doing them as a separately query but this is, in my experience, reasonably unusual.

Far more common is having to fix up systems with awful performance due in large part to doing separate queries (particularly correlated queries) instead of joins.

like image 120
cletus Avatar answered Oct 07 '22 12:10

cletus