一:在较小的结果集上上操作 1.仅返回需要的列 2.分页获取数据 EF实现分页: public object getcp(int skiprows,int currentpagerows) { HRUser dbcontext = new HRUser(); var cps = dbcontext.Product.Join(dbcontext.ProductCategory, a => a.ProductSubcategoryKey, ar => ar.ProductCategoryKey,
CREATE PROCEDURE [dbo].[GetTree] @Id int AS BEGIN with cte as ( as lvl from Entity where Id = @Id union all from cte c inner join Entity e on c.Id = e.Pid ) select * from cte END
qs.total_worker_time/qs.execution_count as [Avg CPU Time], , ( ) as query_text, qt.dbid, dbname=db_name(qt.dbid), qt.objectid FROM sys.dm_exec_query_stats qs cross apply sys.dm_exec_sql_text(qs.sql_handle) as qt ORDER BY [Avg CPU Time] DESC
有些人还不知道sql语句的基本性能优化方法,在此我简单提醒一下,最基本的优化方法: 1.检查是否缺少索引.调试的时候开启“包括实际的执行计划” 执行后会显示缺少的索引, 然后让dba帮助添加适当的索引,以提高sql执行性能. 2.监控io读写情况和cpu耗时情况,调试的时候通过 set STATISTICS io on ; set STATISTICS time on ; 来开启监控,然后调整sql逻辑来对比优化情况,以选择最优的sql逻辑,比如: set STATISTICS