Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ queries vs Stored procedures [closed]

What are the pros and cons of using linq queries(along with an ORM like EF or linq2sql) VS. Stored Procedures(SQL server 2008) to query and update a data model? Performance? Speed? Etc...

like image 961
stephen776 Avatar asked Jan 14 '11 02:01

stephen776


People also ask

Which is faster LINQ or SQL?

More importantly: when it comes to querying databases, LINQ is in most cases a significantly more productive querying language than SQL. Compared to SQL, LINQ is simpler, tidier, and higher-level. It's rather like comparing C# to C++.

Are stored procedure better than queries?

Stored procedures (SPs) are a better choice than views, but views are much better than SQL queries embedded in reports.

What is the advantages of using LINQ?

Advantages of Using LINQLINQ offers a common syntax for querying any type of data sources. Secondly, it binds the gap between relational and object-oriented approachs. LINQ expedites development time by catching errors at compile time and includes IntelliSense & Debugging support. LINQ expressions are Strongly Typed.

Is LINQ to SQL deprecated?

No it is not.


2 Answers

Linq is definitely more readable when you're in the code. Seeing a call to execute a sproc called "sp_GetSomething" doesn't tell you anything as a developer, unless you go and physically look at what the sproc does. seeing code like

var query = from c in db.TableName
            where c.Name == "foo"
            select c;

That tells you exactly what data is being pulled.

Stored procedures on the other hand do not require you to recompile the application if you decide to change the code. If you decide to suddenly change a "where" clause or change the Order By - changing a sproc is easy. Changing the Linq code could be more time consuming.

I'm sure there are plenty more, but these are two I've noticed.

like image 139
Jack Marchetti Avatar answered Oct 06 '22 03:10

Jack Marchetti


There are 2 camps: for stored procs and against stored procs.

I've found that it is lack of experience that make people go one way or another. There are different kinds of shops where we develop.

In practice

  • if you're a corporate programmer, then you'll never change your RDBMS platform. You refactor your client every now and and you'll reimplement your DAL/repository. Why? Use stored procs.
  • if you work for a vendor, then you will probably have to support several RDBMS. An ORM abstracts this away mostly.

I'm in a corporate shop so...

  • Pros: with Linq you don't have to know SQL
  • Cons: you're screwed when things go wrong

We (as a developer DBA team) frequently have to bail out ORM users in sister teams.

There are also more subtle issues such that:

  • stored procedures can be used by any client
  • will outlast your refactor into EF or whatever .net 5 brings
  • encapsulation offered by stored procedures to abstract schema away
  • reduced round trips because shouldn't stored procs be treated like methods, or atomic calls?
like image 26
gbn Avatar answered Oct 06 '22 03:10

gbn