We Care Through What We Share
  • In the last few months, we have been facing many performances related issues on production servers and we have made many fixes/optimization of existing code. I have summarized all in the below listed points. I hope this will help you too.

    1. Detailed code review.

    The code written can itself tell you about how/what it can break on the server. It is very much important to do detailed code review of each and every related method /components when you are fixing any page performance.

    You should keep following points in mind while doing detailed code review.
    1. Review fewer than 400 lines of code at a time
    2. Use checklists
    3. Establish a process for fixing defects found
    4. Know What to Look for in Code Reviews
    5. Add comments before each lines you have understood

    2. Optimize queries.

    Most of the times, server performance degrades because of some long running queries are called. Many timeout cases happen because of non-index based/locking db queries are written.

    You should keep the following points in mind while checking such queries and optimize them.

    1. Avoid * in SELECT statement. Give the name of columns which you require. Selecting all the columns means more bandwidth/memory usage.

    2. Avoid like operations as much as possible.

    3. Use WITH (NOLOCK) while querying the data from any table.

    4. Create Clustered and Non-Clustered Indexes. Fix up query to use existing created indexes.

    5. Drop unused Indexes. It will speedup insertion and updation.

    6. Use joins instead of sub-queries.

    7. Use WHERE expressions to limit the size of result tables that are created with joins.

    8. Use SET NOCOUNT ON and use TRY- CATCH to avoid deadlock condition.

    9. Avoid Cursors since cursor are very slow in performance.

    10. Use Table variable in place of Temp table. Use of Temp tables required interaction with TempDb database which is a time taking task.

    11. Use UNION ALL instead of UNION if possible.

    12. Use Schema name before SQL objects name.

    13. Use Stored Procedure for frequently used data and more complex queries.

    14. Keep transaction as small as possible since transaction lock the processing tables data and may results into deadlocks.

    3. Use for loop instead of foreach.

    I will now explain a very interesting fact. I think all of you are familiar with both for and foreach loops. Now if I ask you which one is faster? Hmm... Don't know. Right? Guys, a for loop is much faster than a foreach loop. Let's look at the following example.

    List Count = new List();  
    List lst1 = new List();  
    List lst2 = new List();  
      
    for (int i = 0; i < 10000; i++)  
    {  
       Count.Add(i);  
    }  
      
     Stopwatch sw =new Stopwatch();  
     sw.Start();  
     for (int i = 0; i < Count.Count; i++)  
      {  
             lst1.Add(i);  
      }  
      sw.Stop();  
      
     Console.Write("For Loop :- "+ sw.ElapsedTicks+"\n");  
      sw.Restart();  
      
     foreach (int a in Count)  
    {  
         lst2.Add(a);  
     }  
    sw.Stop();  
    Console.Write("Foreach Loop: - " +  sw.ElapsedTicks);  
     Console.ReadLine();