Cost of processing between code and database

1

Among the many forms of development, there was a doubt about what will consume more of my processing between the same operation in different ways ...

If I have a function that can be done directly in my database (a insert using a select as a basic example)

Insert into Tabela1(Coluna1,2Coluna,3_coluna) select (select cod from tabela3 where tabela3.nome = tabela2.nome)as 'Coluna1',valorx,valory from Tabela2  Tabela2.cod=z

and the same function can be done through my code

 MySqlCommand mySqlCommand_Select = new MySqlCommand("Busca valores tabela2", mySqlConnection);

MySqlCommand mySqlCommand_Select_2 = new MySqlCommand("Busca valores tabela3", mySqlConnection);

//...
//Unir Selects, trabalha os valores etc
//...

 MySqlCommand mySqlCommand_insert = new MySqlCommand("Insere os valores tabela1", mySqlConnection);

If the process is the same, is the cost higher for one or the other, or is it the same?

The question includes other operations that can be done directly in the bank and / or code (such as calculations, other queries used as sub or as multiple searches) but it comes down to the cost of 'Outsource' or not these operations for my application

    
asked by anonymous 27.04.2017 / 20:55

1 answer

2

You can only answer this on a case-by-case basis, that is only by measuring with queries, available optimizations, data load, and actual access load. What counts in one circumstance is not worth another.

In any case, the two queires will be executed in the database, so the premise of the question is false. I am considering that query will be the same as that used in pure SQL.

Without further details, without knowing what optimizations are possible I would say the cost is the same. I do not know if MySQL has some specific optimization that can make one of them be faster.

It should have a negligible marginal difference, spatially if you have a lot of data.

If queries are different you are comparing oranges with bananas and the result is not useful.

In the case they seem different since one is doing everything at once and the other is doing it in three distinct parts. It's not so much a matter of doing it in the database or not, it's the fact that they are separate queries.

Getting started with the code only potentializes the problem because various data will make travel between the application and the database unnecessary. But again this only occurs because query is another, not because it was started in the application.

    
27.04.2017 / 21:13