Linq-to-SQL性能优化提升实践
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;
}
}
}
/// 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);
}
{
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 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......
}
{
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. 其它参考资料
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
2008-11-23 读取SQLServer数据库存储过程列表及参数信息