Completion:
Prefer writing queries on the application side instead of writing in stored procedures.
Why:
The following are some factors that lean towards writing queries on the application side instead of writing on the database side.
Performance
For the database, there is no difference between executing queries in stored procedures or received from the client.
You will only be able to observe a minimal difference in performance between one and the other due to variations in the environment that are very difficult to control, and now stored procedures will be slightly faster and sometimes queries sent by the client will be slightly faster.
Where stored procedures can gain advantage is in the heavy processing of large data volumes, for two reasons: why you are closer to the data and do not need to traffic itthem over the network and because you can easily count on the use of the disk using physical and temporary tables so as not to have a huge amount of RAM.
So, if you do not intend to do in stored procedures heavy processing of large data volume, you can rule out the factor performance of its decision-making.
See, for example, the results of this measurement.
Complexity
Each language introduced in your project adds complexity. It is common for a project to use several languages (SQL, C#, ASP and Javascript, for example), but complexity is something to fight against and not to search for free. Each complexity needs to have a good reason to be added. What’s a good reason to add the stored language to the project?
In addition to the complexity of an additional language, there is the complexity of additional tools, such as code debugging. Besides the tools for C# are much more evolved than the tools for Transact-SQL, for example.
Project structure, dependencies, tracking, versioning, deploy...
All of these needs are more complex when it comes to database code than when it comes to standard application code. Luckily, table definitions vary much less than everything else (business rules, queries, UI...). So that using database code for queries or business rules increases the challenges of code management, distribution and application update.
Scalability
When there is a need to process large volumes of data and opt for stored procedures, another major problem usually arises: scaling the solution.
The demand for a lot of performance is usually endless - the volume of data only grows and yesterday’s solution no longer serves today, and database servers are recognized for not allowing horizontal scaling. You put more and more processors on the server but find it costs a lot more than adding a new server - it turns out that not all database servers support parallelism between physical machines (or "active cluster")and the ones that support are very expensive solutions, much more expensive than just buying another single server or allocating another machine to the cloud.
I have seen projects migrating processing to the database (stored procedures) and a few years later migrating back to the application side, and the two movements had the same demand: more performance.
Portability
The SQL language is quite standardized among the databases, while the language for writing stored Procedure is quite specific in each of them. So that it is easier to support more than one database by writing only SQL instead of also writing stored procedures. For "easier" understand less costly and less risky, since each query only needs to be written once while the stored Procedure needs to be rewritten for each database.
Of course this factor is only important if you want to support more than one database server, which is a fairly common need.
So the conclusion varies from user to user? In my opinion the code is also easier to maintain. Answering the questions the first I think would be in the code, and the second would have no reason to leave it in the bank.
– Marconi
It varies according to the database. What may be good today may not be tomorrow (of course that tomorrow is not real tomorrow). But it can remain the same as well. It depends on the data pattern contained in it. It is not random, depending on the pattern you will have different statistics and the optimizer can work differently. I usually don’t put it in the database until I prove I’ll have leverage meaningful. Putting the code on DB usually brings an advantage but in most cases it is so little that it is not worth the trouble of having to maintain it.
– Maniero
I’m finding it easier gives maintenance in Querys by code @bigown, more in relation to performance I’m feeling no difference.
– Marconi
In almost all cases the difference is almost zero even. It’s not zero in fact but zero is too absolute a number :) If you’re not noticing difference it’s what matters. Speed just to say you used the fastest is never an advantage. Speed because it makes the use experience better, can give a real noticeable advantage is advantage.
– Maniero