Linq-to-SQL性能优化提升实践
2012-11-23 16:54
796 查看
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查看这个页面
10 Tips to Improve your LINQ to SQL Application Performance
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-sql10 Tips to Improve your LINQ to SQL Application Performance
相关文章推荐
- linq to sql 系列之 linq to sql性能优化技巧
- linq to sql 系列之 linq to sql性能优化技巧
- linq to sql 系列之 linq to sql性能优化技巧
- LINQ to Sql系列四 性能优化总结
- linq to sql 系列之 linq to sql性能优化技巧
- 也记一次性能优化:LINQ to SQL中Contains方法的优化
- linq to sql 系列之 linq to sql性能优化技巧
- 智能SQL优化工具--SQL Optimizer for SQL Server(帮助提升数据库应用程序性能,最大程度地自动优化你的SQL语句 )
- SQL优化(SQL TUNING)可大幅提升性能的实战技巧之一——让计划沿着索引跑
- 优化临时表使用,SQL语句性能提升100倍
- Linq to sql 简单性能差异指引 2 (转)
- SQL优化:采用新语法提升分页性能(本质上就是新的算法)
- LinqToSql开发实践之认识LinqToSql
- WEB站点性能优化实践(加载速度提升2s)
- 使用LINQ to SQL更新数据库(下):性能测试
- 转--优化临时表使用,SQL语句性能提升100倍
- 提升性能 Java程序优化的一些最佳实践
- 一次性能提升300%的优化实践
- DataRabbit 3.0 与 Linq to sql 性能比较
- WEB站点性能优化实践(加载速度提升2s)