Linq-to-SQL效能最佳化提升實踐

來源:互聯網
上載者:User

Linq-to-SQL的效能最佳化,根據我的個人實踐和效果降序排列,如下:

 

1. 先行編譯 CompiledQuery (如果執行次數不止一次的話)
/// <summary>
/// Utility class to store compiled queries
/// </summary>
public static class QueriesUtility
{
  /// <summary>
  /// Gets the query that returns categories with more than five products.
  /// </summary>
  /// <value>The query containing categories with more than five products.</value>
  public static Func<NorthwindDataContext, int, IEnumerable<Category>>
    GetCategoriesWithMoreThanFiveProducts
    {
      get
      {
        Func<NorthwindDataContext, IEnumerable<Category>> func =
          CompiledQuery.Compile<NorthwindDataContext,  int, IEnumerable<Category>>
          ((NorthwindDataContext context, int count) => context.Categories.
            Where<Category>(cat => cat.Products.Count > count));
        return func;
      }
    }
}

調用:

using (NorthwindDataContext context = new NorthwindDataContext())
{
  QueriesUtility.GetCategoriesWithMoreThanFiveProducts(context, 5);
}

 

2. 緩衝MappingSource

把預設的DataContext用自己的代替,代碼如下:

using System;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace My.Company.Depart
{
    public class MyDataContext : DataContext
    {
        /// <summary>
        /// Thread-safe
        /// Cached MappingSource
        /// </summary>
        private static MappingSource _cachedMappingSource = new AttributeMappingSource(); /* Thread-safe */
        
        public MyDataContext(IDbConnection cx)
            : base(cx, _cachedMappingSource)
        {
        }
                
        public MyDataContext(string cx, bool ojectTrackingEnabled = true, bool consoleLogging = false)
            : base(cx, _cachedMappingSource)
        {
            this.ObjectTrackingEnabled = ojectTrackingEnabled;

            if (consoleLogging)
                this.Log = Console.Out;
        }

        public Table<MyTable> MyTable
        {
            get { return this.GetTable<MyTable>(); }
        }

        //blah. blah, blah......
    }
}

然後這樣調用:

using (var dataContext = new MyDataContext(ConnectionStringProvider.GetDefault()))
{
  var list = dataContext.MyTable.Where(t => t.ID > 5).ToList();
  //blah. blah, blah......
}

 

3. 查詢的時候關閉ObjectTrackingEnabled
dataContext.ObjectTrackingEnabled = false;

關於ObjectTrackingEnabled更詳細的解釋查看這個頁面。

 

4. 關聯表查詢的時候用 LoadOptions

關於Load,LoadWith,AssociateWith查看這個頁面

 

5. 其它參考資料
  • Speeding up linq-to-sql
  • 10 Tips to Improve your LINQ to SQL Application Performance

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.