views:

231

answers:

3

I am using LINQ to SQL and seeing my CPU Usage sky rocketting. See below screenshot. I have three questions

  • What can I do to reduce this CPU Usage. I have done profiling and basically removed everything. Will making every LINQ to SQL statement into a compiled query help?

  • I also find that even with compiled queries simple statements like ByID() can take 3 milliseconds on a server with 3.25GB RAM 3.17GHz - this will just become slower on a less powerful computer. Or will the compiled query get faster the more it is used?

  • The CPU Usage (on the local server goes to 12-15%) for a single user will this multiply with the number of users accessing the server - when the application is put on a live server. i.e. 2 users at a time will mean 15*2 = 30% CPU Usage. If this is the case is my application limited to maximum 4-5 users at a time then. Or doesnt LINQ to SQL .net share some CPU usage. alt text

+5  A: 

Profile. Profile. Profile.

Profile to find out exactly which query is taking the most resources and improve the performance of that query. You can use the Log property of the DataContext to view the SQL - see this article. You can get the query plans for a query in SQL Server - see this article.

Examples of ways to improve a query:

  • Add missing indexes.
  • Rewrite the query to take advantage of the indexes that are already there.
  • Don't fetch too much data per query - use paging and only fetch more rows when requested. Don't fetch fields you don't need.
  • Don't fetch too little data per query - don't make a loop fetching one row at a time. Fetch many rows at once.

Once you have done that, profile again to check if you have improved the performance of that query. If not, repeat until you have.

Then profile again to see what the next killer query is and repeat the process until your performance is acceptable.

You say you already have profiled, but you haven't posted any profiling information such as queries, query plans, execution times, query frequency, etc. Without more profiling information all we can do is guess.

Mark Byers
Index on what? The Table in SQL server. Does LINQ to SQL even care about indexes on tables - it sas no here http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/ad8cc0d5-e1a3-4333-b0a1-1d67c406a6c7
soldieraman
@soldieraman: LINQ to SQL converts query expressions to SQL. The SQL server creates query plans for these SQL statements, based on which indexes are available and based on statistics for the table. You can log the SQL statements sent to the database by assigning something to the Log property of your context.
Mark Byers
LINQ's involvement with your database's indexes is the same as any other external entity--end user, developer, ADO.NET, ODBC, etc. If you have created indexes as part of your database schema, LINQ will use them because your database is already set up to use them.
Neil T.
Sadly the queries that are taking this much time are on the primary key id which already is indexed. What about the answer to the other 2 questions. Anyone?
soldieraman
@soldieraman: Primary key lookups should be very fast, so I suspect that you are fetching too few or too many rows per query - see my 3rd and 4th suggestions. How many rows are you fecthing per query? How many queries are you running per second?
Mark Byers
+1. from me. ...
Mitch Wheat
A: 

Compiled queries will not "get faster" with more use. The primary benefit of compiled queries is to save the need for the LINQ engine to repeatedly perform the translation process every time it is called.

As far as the CPU usage goes, if this is your development machine, the odds are very good that something else is going on to cause such high activity. Even if this is a dedicated database server, I would highly suggest using SQL Profiler to investigate what statements are being generated by your LINQ queries. It may require tweaking your schema, your code or your database settings to get the usage back to a more acceptable level.

Neil T.
A: 
  1. Are there any common queries that can be cached?
  2. Can the linq queries be rewritten
  3. Is most of the query processing being done on the web server or in SQL?
  4. Are you running both SQL sever and web server on the same box? Do you have a test environment that mimics the production environment?
Raj Kaimal