Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a query or code [closed]

I am asking for a concrete case for Java + JPA / Hibernate + Mysql, but I think you can apply this question to a great number of languages.

Sometimes I have to perform a query on a database to get some entities, such as employees. Let's say you need some specific employees (the ones with 'John' as their firstname), would you rather do a query returning this exact set of employees, or would you prefer to search for all the employees and then use a programming language to retrieve the ones that you are interested with? why (ease, efficiency)? Which is (in general) more efficient?

Is one approach better than the other depending on the table size?

Considering:

  • Same complexity, reusability in both cases.
like image 422
dgmora Avatar asked Dec 12 '12 15:12

dgmora


People also ask

Why would you use a query?

A query can give you an answer to a simple question, perform calculations, combine data from different tables, add, change, or delete data from a database. Since queries are so versatile, there are many types of queries and you would create a type of query based on the task.

What is the difference between power query and SQL?

When connecting to SQL DB, Power Query tries to do Query Folding and tries to push maximum logics to data source, means the time take in Power Query and SQL will be the same in such cases. In your example, ideally Power Query should just trigger a SQL with a where clause for the time filter.

What is the most efficient way to get SQL data in Power Query?

By first running the SQL query in a platform like Visual Studio, then saving the file, then uploading to Power Query is the most efficient approach.


2 Answers

Always do the query on the database. If you do not you have to copy over more data to the client and also databases are written to efficiently filter data almost certainly being more efficient than your code.

The only exception I can think of is if the filter condition is computationally complex and you can spread the calculation over more CPU power than the database has.

In the cases I have had a database the server has had more CPU power than the clients so unless overloaded will just run the query more quickly for the same amount of code.

Also you have to write less code to do the query on the database using Hibernates query language rather than you having to write code to manipulate the data on the client. Hibernate queries will also make use of any client caching in the configiration without you having to write more code.

like image 80
mmmmmm Avatar answered Oct 26 '22 22:10

mmmmmm


There is a general trick often used in programming - paying with memory for operation speedup. If you have lots of employees, and you are going to query a significant portion of them, one by one (say, 75% will be queried at one time or the other), then query everything, cache it (very important!), and complete the lookup in memory. The next time you query, skip the trip to RDBMS, go straight to the cache, and do a fast look-up: a roundtrip to a database is very expensive, compared to an in-memory hash lookup.

On the other hand, if you are accessing a small portion of employees, you should query just one employee: data transfer from the RDBMS to your program takes a lot of time, a lot of network bandwidth, a lot of memory on your side, and a lot of memory on the RDBMS side. Querying lots of rows to throw away all but one never makes sense.

like image 35
Sergey Kalinichenko Avatar answered Oct 26 '22 22:10

Sergey Kalinichenko