sql server – SQL and search query calculated value – Education Career Blog

Suppose we have a table with collumns (A,B,C)
We want to search this table based on a criteria that is calculated from collumn C… and other parameters

Because the calculation is complex and cant be represented in sql query…
what should i do? Make the filtering at application level ?


No silver bullet here.


You can have pretty complex calculations at the database level, the pros of doing it at database level are that the RDBMS is closer to the data and transporting data back and forth from disks, through RDBMS, through network, to application is usually more expensive then doing calculations in RDBMS (where SQL is executed) and shipping only results to the application.

Another pro is that centralizing the logic makes the system easier to maintain, but…


…depending on your database of choice, you might not have a wide choice of programming languages available in the RDBMS layer, so for more complex things it might get really awkward coding your task in some procedural SQL variant.

Other reasons for not doing it at the RDBMS level is if the calculations are resource intensive (cpu, memory; think O(n!) problems and such) and the size of the output is comparable to input. In this case shipping data to the application side might be interesting to relieve the database server resources (you are in a sense distributing the computing tasks; however if your application server lives on the same hardware as database server you might not gain anything, unless you can push it all the way to the clients; look at three tier and multi tier architecture).


An alternative could be to create a function/stored procedure on the database that do the math for you and then call it in the where clause of your query

Leave a Comment