alanm wrote:
Thanks for the reply.
I was concerned with the speed of execution by having to pull data back to process it on the client side and was looking for a solution where the ORM would give me the choice to handle the processing within the database if this was possible.
The main benefit of stored procedures that I can see is execution speed on the server side
That doesn't make a difference. In modern databases, parameterized queries and procs are both compiled to an execution plan which is cached: if the same parameterized query or proc is executed, the same execution plan is executed. This has advantages for optimization as the execution plan can be tweaked at runtime by the db engine based on the live statistics of the data involved.
So if you do:
INSERT INTO Foo (Field1, Field2) VALUES (@V1, @V2)
or
CREATE PROCEDURE pr_InsertFoo(int @v1, int @v2)
AS
INSERT INTO Foo (Field1, Field2) VALUES (@V1, @V2)
GO
that doesn't make a difference: calling pr_InsertFoo or executing the parameterized query doesn't make a difference, performance wise.
For an O/R mapper there are advantages which stored procs don't have. For example, if I have a 10 field table Customers, and I want to update a customer, and want to change the contactname field, the query executed will be:
UPDATE Customers SET ContactName = @p1
WHERE ID = @p2
If you want to do stored procedure updates for this, you have to either
- write for each update scenario in your application a stored procedure, this can be cumbersome and tedious
or
- write a single stored procedure and use nullable parameters and coalesce. This is much slower than the update above.
Another example is graph fetching. Say I want all customers from germany and their orders, I can fetch that in 2 queries and get the graph accordingly. With stored procedures I can't, as I don't know the filter on the customers to fetch the orders.
So the queries you get with an O/R mapper are much more tailored towards the situation at runtime and therefore can be more optimal. Due to the parameterized nature, they're not slower than procs, and you don't have to write the procs As a bonus, they're automatically db agnostic: they're generated at runtime, so you write the code once, and you can utilize it on the db's supported by your o/r mapper framework.
It is important to keep in mind that the queries generated from an O/R mapper could need tweaking. This is because you can query a database in numerous ways and from the abstraction level of .net code, it might be a simple query which might result in a slow db query. Luckily in many cases you can tweak an o/r mapper query to make it faster, e.g. by rewriting it a bit, so the sql generated is more optimal. With a proc, you can change it in the proc directly, which is an advantage of procs. However, with a larger system, with a lot of tables, the # of procs you have to deal with is quickly getting out of hand. 3000 or more procs is not an exception.
Most O/R mappers support executing sql on the DB directly and/or a call to a proc as well, for situations where you need to consume a lot of data for example: in that situation it's not feasible to pull the data to the client, consume it there in a method and send it back. A good example is when you want to update a lot of rows with an expression based on a filter. It's easier to simply run 1 Update query than to first fetch all entities, update all properties and then send them back. Our own framework for example supports that kind of queries, besides stored procedure calls, for the situations when you want to write a stored procedure.
Hopefully this explains things a bit.