views:

155

answers:

9

A simple question. There are cases when I fetch the data and then process it in my BLL. But I realized that the same processing/filtering can be done in my stored procedure and the filtered results returned to BLL.

Which is better, processing at DB or processing in BLL? And Why?

consider the scenario, I want to check whether a product exists in my db and if it exists add that to the order (Example taken from answer by Nour Sabony below)now i can do this checking at my BLL or I an do this at the stored procedure as well. If I combine things to one procedure, i reduce the whole operation to one db call. Is that better?

+1  A: 

at the database.

Arthur Rizzo
+5  A: 

At the database level.

Databases should be optimized for querying, so why go through all the effort to return a large dataset, and then do filtering in your application afterwards?

LittleBobbyTables
+3  A: 

As a general rule: Anything that reasonably belongs to the db's responsibilities and can be done at your db, do it at your db.

Dennis Haarbrink
really?????????
Haoest
-1 this is a bad advice, because well anything can be done in DB. And yet, is everything done in DB normally?
Kugel
-1 database driven projects are DOA even before release!
Chris Marisic
Maybe poor choice of words. Edited now. I thought we were all sensible people with at least *some* common sense :)
Dennis Haarbrink
Changed to +1 after edit. I think too many of us have seen PL-SQL stored procs that are 2000 lines long to not see that when someone says do everything in the db.
Chris Marisic
@Chris Marisic: Thanks. I think it has to do with perspective. I too often see scripts of incompetent developers offloading everything to php, while it would be 100% more efficient if it were done at the db level.
Dennis Haarbrink
ok.. consider the scenario, I want to check whether a product exists in my db and if it exists add that to the order (Example taken from answer by Nour Sabony below)now i can do this checking at my BLL or I an do this at the stored procedure as well. If I combine things to one procedure, i reduce the whole operation to one db call. Is that better?
sassyboy
@sassyboy: Shouldn't that be enforced by a foreign key constraint?
Dennis Haarbrink
+1  A: 

I have consistently been taught that data should be handled in the data layer whenever possible. Filtering data is what a DB is specifically there to do and is optimized to do. Therefore that is where it should occur.

This also prevents the duplication of effort. If the data filtering occurs in a data layer then other code/systems could benefit from the same stored procedure instead of duplicating the work in your BLL.

I have found that people like to put the primary logic in the section that they are most comfortable working in. Regardless of a developer's strengths, data processing should go in the data layer whenever possible.

dredful
+1  A: 

I asked a similar question in a SSRS class earlier this year when I wanted to know "best practice" for having a stored procedure retrieve queried data instead of executing TSQL in the report's data set. The response I received was: Allow the database engine to do the "heavy lifting." That is, do all the selecting, grouping, sorting in the stored procedure and allow SSRS to concentrate on delivering the results.

The same is true for your scenario. By all means, my vote would be to do all the filtering in your stored procedure. Call the sproc from your application and let the database do the work.

SidC
A: 

If you filter at the database, then only the data that is required by the application is actually sent on the wire between the database and the application server. For this reason, I suggest that it is better to filter at the database.

dwb
+1  A: 

To throw a differing view point out there it also really depends upon your data abstraction model if you have a large 2nd level cache that sits on top of your database and the majority (or entirety) of your data you will be filtering on is in the cache then yes stay inside the application and use the cached data.

Chris Marisic
+2  A: 

well, the fatest answer is at database, but you may consider something like Linq2Sql, I mean to write an expression at the presentation layer, and it will be parsed as Sql Statement at Data Access Layer.

of course there are some situation BLL should get some data from DAL ,process it ,and then return it to DAL. take an example : PutOrder(Order value) procedure , which should check for the availability of the ordered product.

public void PutOrder(Order _order)
{
foreach (OrderDetail _orderDetail in _order.Details)
   {
    int count = dalOrder.GetProductCount(_orderDetail.Product.ProductID);
    if (count == 0)
    throw new Exception (string.Format("Product {0} is not available",_orderDetail.Product.Name));
    }
    dalOrder.PutOrder(_order);
}

but if you are making a Browse view, it is not a good idea (from a performance viewpoint) to bring all the data from Dal and then choose what to display in the Browse view.

may be the following could help:

public List<Product> SearchProduts(Criteria _criteria)
{
string sql = Parser.Parse(_criteria);
///code to pass the sql statement to Database procedure and get the corresponding data.
}
Nour Sabouny
taking the example you have mentioned, I can check whether product is available within the same stored procedure as put order. Here you are making two calls to the db, but if I combine that business logic in my stored procedure, I can achieve the functionality with a single db call. So which approach is better in that case? I think my doubt is a bit clearer now!
sassyboy
i know it is possiple to move the logic to the database,but from the Application Architecture viewpoint, you should not know any thing about database or (Persistence service), what if you wanted to change the database provider?
Nour Sabouny
the n-tier principle implies that you should not know any thing about the storage provider,except the querying and storing procedures. (excuse my weak langauge) you may think that make an overhead of the performance, i agree with you about that.
Nour Sabouny
ok.. so I am not asking to know anything about the provider.. am I..? Say I check for that particular condition in a procedure p_chk in Sql server. This is currently called from DAL. Now I switch to MySQl. Since I am using a Repository pattern like implementation, there is absolutely no way I am having to change my BLL implementation. There is a very minor change which I do in my DAL to incorporate MySql provider as well. But the DAL method which calls this stored procedure does not change. So now the real issue is whether the sproc can be implemented to yield identical results on MySQL.
sassyboy
And if I can implement the procedure to yield identical results, then changing of persistence does not remain an issue anymore does it? Anyway if I decide to change the persistence, say from MySql to SqlServer, all the tables, sprocs etc will have to be ported to the new db. So if the performance of my app is better by moving the processing of certain logic to db, should I not follow that? Probably a dumb question what I am asking, but as somebody once said"The dumbest questions are the ones which are never asked"..
sassyboy
well, first of all it is not a dumb question at all (i think dumb means stupid,isn't it ?). now, the whole idea is that BLL is a business logic layer, and it should take care of any logic that is necessary to process the data, and i think deciding how many products should be available in order to proceed the selling process, is related to BLL, not to DAL Procedures, isn't it?and what i mean by changing the provider, is when you authorize some one to make a database, he shouldn't be aware of the logic needed to process the data,and all that he needs to care about is how to save data.
Nour Sabouny
Ya.. that makes a lot of sense..
sassyboy
+1  A: 

The difference is not very important when you consider tables that will never contain more than a few dozen rows.

Filtering at the db becomes the obvious choice when you consider tables that will grow to thousands, hundreds of thousands or millions of rows. It would make zero sense to transfer that much data to filter for a subset of records.

Paul Sasik