Linq and sqlclient performance - asp.net

I want to create a web application using ASP.NET. Which method should I use, LINQ or SQL query? Which gives the best performance? Please help me.

This is an excellent series by a CLR Performance Architect describing LINQ to SQL performance: http://blogs.msdn.com/b/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx

Performance-wise I think you'll always be better off with SQL. If you take a look at the generated SQL code which LINQ produces you'll notice that it's not always that top notch.
On the other hand, LINQ isn't slow. It's not as fast as SQL, but it's definitely not slow. If you think about the advantages LINQ provides I would always prefer LINQ over SQL if I have the choice.
If performance is the BIG issue tho, sticking with SQL might be your best shot. If you were just wondering if the difference was that major, and if you don't expect database-performance to be a bottleneck I'd go for LINQ.

Related

Is there a performance difference SQL vs LINQ

We're building an application with DocumentDb backend that will get lots of hits and its reponsiveness is absolutely paramount.
I wanted to see if there was a "preferred" approach from a performance stand point in querying DocumentDb. Should we use SQL for our queries or LINQ?
Theoretically, there shouldn't be a noticable difference in regards to responsiveness.
LINQ is a simply a fluent wrapper API, that which given a LINQ expression generates a SQL expression. You can view the generated SQL expression by applying toString() to the end of the LINQ expression. The performance hit on converting a LINQ expression to SQL is negligible compared the time it takes to perform I/O.
In practice, the translation from a LINQ expression may result in a sub-optimal SQL expression when dealing with corner cases. For those corner cases, working directly with SQL would be preferred.

Which of these is better practice for CRUD? Simple.Data or Dapper?

I'm using both Dapper and simple.Data in my application, Dapper for retrieving data only and other operations by Simple.Data. I just need to know if using Simple.Data is better approach in all operations except retrieval and is it the same in performance like using ExecuteNonQuery in Dapper?
In my experience, I would suggest Simple.Data. This is mostly because Simple.Data has support for CRUD operations out of the box. (https://github.com/markrendle/Simple.Data/wiki/Inserting-and-updating-data). They are both extremely fast, but I think for an application doing mostly CRUDs, I would pick Simple.Data out of those two.
You may also want to consider ORMLite. Anything from the ServiceStack library is fantastic.
Thanks. Happy Coding!
Both have CRUD support now, so if you made an investment in Dapper, just add one of these nugets and you're good to go!
Dapper.Extensions
Dapper with Rainbow
Dapper.Contrib over 5K users starred this!!!

Linq To SQL: a newbies Journey

I am new to asp.net and I am trying to learn Linq to SQL. So I have found two different ways to pull from the database. The normal linq to SQL way and the direct SQL statement way.
I have both working, but I want to know which way is the accepted standard? I want to use straight SQL statements, because that is what I am use to, but I am trying to go with whatever is best practice.
Thanks
Take a look at the selected answer to this popular question.
Entity Framework became the preferred way to interact with a SQL database in ~2008.
Much of the heavy lifting in database calls and transactions is greatly simplified in EF.
If you continue to use Linq to SQL and want to learn from the master, have a look at Scott Guthrie's Post.
Entity Framework is to be Microsoft's best practice solution. Once you learn LINQ you will find it is much easier to code.
Personally, I much prefer using LINQ and have since the moment I learned it. You can turn on the log feature while you are learning and can see the SQL that gets created. If you have performance concerns and need to hand tweak SQL you can always revert to that. Just not having to hard code strings with table and field names makes code more maintainable and with intellisense also much more enjoyable to write.
There isn't really an accepted standard between straight SQL and LINQ (or any other ORM). It depends on what environment you're using it in, and what you want to do. However, Microsoft has announced that the LINQ to SQL project isn't going to be continued, so I suggest you consider the ADO.NET Entity Framework (which supports LINQ via LINQ to Entities) instead.
Refer to the following:
ADO.NET and LINQ to SQL
Advantages & Disadvantages of LINQ
Performance of LINQ to SQL over Normal Stored procedure
LINQ-to-SQL and Stored Procedures

linq vs ado.net performance

I am going to work with Linq data access model. Which Model performance is better? and why to use and why not to use Linq.
Thanks
There is always a trade off between abstraction level and performance, so of course, Linq To Entities or Linq To Sql, which both work on top of ADO.NET, will be slower than ADO.NET.
It is all about what level of performance you need for your particular project. If performance is more crucial for you than ease of development then go with straight DataReaders. If not then go for Linq To Entities.
Linq to SQL and Entity Framework etc. all use ADO.NET internally, thus they are slower since they do more work than ADO.NET alone.
The idea is not to get the best performance but the best abstraction, model your domain with objects so you can reason about your data and the commands that operate on that data.
So it's a good idea to trade some performance for other aspects.

Architectural Design DAL Layer

I am working on architecture of mid sized web application & for my DAL layer i am having 3 options
1) Traditional Stored proc Based Architecture (Using NTiers Template of Codesmith)
2) LINQ To SQL (or PLINQO Template of codesmith)
3) LINQ To Entity
From above LINQ to Entity is out of reach as we need to start application very quickly and we don't have the sufficient skillset for the same and as team has never worked on any OR/M tools it will be steep learning curve for them (This is what i read some where)
I prefer to go ahead with LINQ to SQL (But only fear is microsoft is not going to support or enhance LINQ to SQL further), from my point of view if microsoft is not going to enhance it further i am not having any issue as whatever feature i require in my project it is sufficient.
Now my issue is should i use linq to sql or should i stick to traditional architecture ?
OR else any other option is there ...
EDIT : I am going to use SQL Server as database and it does not require to interact with any other database
One of the most important objective in designing DAL Layer is faster development and maintainability for future database table changes, as there are chances that field may increase or decrease in future.
Also if you feel that any ORM tool is really good and does not have steep learning curve then also we can use
Please provide suggestions
As you are working in medium size project, I would suggest you to use LINQ-TO-SQL because of these advantages
Advantages using LINQ to SQL:
•No magic strings, like you have in SQL queries
•Intellisense
•Compile check when database changes
•Faster development
•Unit of work pattern (context)
•Auto-generated domain objects that are usable small projects
•Lazy loading.
•Learning to write linq queries/lambdas is a must learn for .NET developers.
Regarding performance:
•Most likely the performance is not going to be a problem in most solutions. To pre-optimize is an anti-pattern. If you later see that some areas of the application are to slow, you can analyze these parts, and in some cases even swap some linq queries with stored procedures or ADO.NET.
•In many cases the lazy loading feature can speed up performance, or at least simplify the code a lot.
Regarding debuging:
•In my opinion debuging Linq2Sql is much easier than both stored procedures and ADO.NET. I recommend that you take a look at Linq2Sql Debug Visualizer, which enables you to see the query, and even trigger an execute to see the result when debugging.
•You can also configure the context to write all sql queries to the console window, more information here
Regarding another layer:
•Linq2Sql can be seen as another layer, but it is a purely data access layer. Stored procedures is also another layer of code, and I have seen many cases where part of the business logic has been implemented into stored procedures. This is much worse in my opinion because you are then splitting the business layer into two places, and it will be harder for developers to get a clear view of the business domain.
There is no absolutely preffered way of writing DAL. These are all options. Which one to choose depends on your project, your skills and your inclinations.
Normally, with LINQ you can expect to be more productive. On the other hand, the DAL built with stored procedures can be expected to perform faster.
The issue only comes when you need some specific queries that the default LINQ to SQL provider won't be able to generate to be blazingly fast. In that case you will have to tap into your LINQ code to plug in your custom stored procedures where needed.
Regarding LINQ to SQL support and further development, it was grounded a long time ago already. So no official further development. Note: that is true for LINQ to SQL (it will be taken over by EF) relational solution, not for the main LINQ functionality.
Entity Framework in its v.1 only received massive critics. You're advised to wait until v2 comes out.
The most important limitation with LINQ (over Entity Framework or any other popular ORM) is that it doesn't support 1 to n mappings. That is, each your LINQ class can only map to a single table, not represent some sort of view over several others. Maybe it's not important to you, but maybe it is. Depends on your project.
The argument of stored procedures vs ORM's is long-standing and unlikely to be resolved any time soon. My recommendation would be to go with an ORM (Linq-to-Sql in your case).
Yes, stored procedures will always be faster since the queries are precompiled. The real question you have to ask yourself is whether you have such a performance-intensive system that your users will actually notice the difference. Keep in mind that using stored procedures means that you will need to manually write all your own queries where using an ORM does this for you. This usually means that an ORM will speed up your development.
Since you mention that speeding up development time is one of your goals I would recommend Linq-to-Sql - otherwise you will basically write the entire DAL yourself.
All of the options you've provided have significant drawbacks. None of them meet the requirements you've set out.
You need to prioritize what is most important for you.
If learning curve is your biggest issue, stay away from all ORMs if you are already comfortable with ADO.NET, DataTables, etc.
If development speed is your biggest issue, you should learn an ORM and go that route. The easiest ORM to recommend is NHibernate. Every other ORM has significant weaknesses. NHibernate works in the vast majority of projects, whereas other ORMs are much more situationally appropriate (depending on your DB design, model design, agility requirements, legacy schema support, etc.). All ORMs have learning curves, they just come into play at different times and in different ways.
Just to expand on #Developer Art, using the traditional stored proc approach enables you to put business logic in the database. Usually you will want to avoid this, but sometimes it is necessary to do. Not to mention you could also enforce constraints and permissions at the database level using this approach. It all depends on your requirements.
With the limitations mention I would say just stick to adhoc/custom queries and ADO.NET and not go for any jazzy stuff. Also stored procedure based DAL are faster is a notion based lame arguments like stored procedures are precompiled but they are not. All that they have is query plan cache. So lesser the investment in stored procedures the better you are. My advice ADO.Net and custom dynamic queries constructed from entity objects.

Resources