MiniProfiler工具介紹(監控EF生成的SQL語句)--EF,迷你監控器,哈哈哈


十年河東,十年河西,莫欺少年窮...

今天是抄襲的別人的博客,不過我感覺蠻好,挺有用,特別是老板讓你優化EF項目SQL耗時的情況下,你可以采用這種方式來優化你的LINQ。

時間很寶貴,廢話還是不多說,直接入主題

  MiniProfiler是一款針對.NET, Ruby, Go and Node.js的性能分析的輕量級程序。可以對一個頁面本身,及該頁面通過直接引用、Ajax、Iframe形式訪問的其它頁面進行監控,監控內容包括數據庫內容,並可以顯示數據庫訪問的SQL(支持EF、EF CodeFirst等 )。並且以很友好的方式展現在頁面上。

    MiniProfiler官網:http://miniprofiler.com/

    MiniProfiler的一個特別有用的功能是它與數據庫框架的集成。除了.NET原生的 DbConnection類,MiniProfiler還內置了對實體框架(Entity Framework)以及LINQ to SQL、RavenDb和MongoDB的支持。任何執行的Step都會包括當時查詢的次數和所花費的時間。為了檢測常見的錯誤,如N+1反模式,profiler將檢測僅有參數值存在差異的多個查詢。

    MiniProfiler是以Apache License V2.0協議發布的,你可以在NuGet找到。

  過去一直使用Sqlserver Profiler,但是發現實在是太痛苦了,你不得不進行新建、過濾、清除、關閉等操作,而且過濾篩選往往比較難以控制。后來發現MiniProfiler工具非常好用。

  同類監控工具有NanoProfiler,下載地址:https://github.com/ef-labs/nanoprofiler/issues/1

Demo演示

Demo開發環境

  • Win10
  • VS2013

准備工作

新建MVC項目WebAppEF,使用Northwind數據庫。

1、先安裝MiniProfiler

2、安裝MiniProfiler.MVC4

3、安裝MiniProfiler.EF

4、修改Global.asax文件

我這里只需要在開發環境使用SQL性能監控,所以使用了#if DEBUG,因為生產環境,我們一般是采用release模式。

using StackExchange.Profiling;
using StackExchange.Profiling.EntityFramework6;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

namespace BingFa.UI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
             #if DEBUG
            MiniProfilerEF6.Initialize();
             #endif
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
        protected void Application_BeginRequest(Object source, EventArgs e)
        {
             #if DEBUG
            MiniProfiler.Start();
             #endif
        }
        protected void Application_EndRequest()
        {
            #if DEBUG
            MiniProfiler.Stop();
            #endif
        }
    }
}

5、在你的布局頁(_Layout)中/或者普通頁面,加入以下這種結構,修改_Layout.cshtml為:

@using StackExchange.Profiling;
<head>
 ..
</head>
<body>
  ...
  @MiniProfiler.RenderIncludes()
</body>
6、修改配置文件Web.config,在節點<handlers>之間加如下配置
<handlers>
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode"/>
</handlers>
7、頁面中注冊:MiniProfiler
        public ActionResult Index()
        {  
            var profiler = MiniProfiler.Current;
           
            using (profiler.Step("查詢Student的數據"))
            {
                using (BingFaTestEntities context = new BingFaTestEntities())
                {
                    var b = context.Student.Where(A => A.StuName.Contains("")).ToList();

                }
            }
            using (profiler.Step("查詢Student的數據"))
            {
                using (BingFaTestEntities context = new BingFaTestEntities())
                {
                    var a = context.Student.AsNoTracking().Where(A => A.StuName.Contains("")).ToList();

                }
            }
            return View();
        }
View Code
8、按F5調試運行

說明:標記為duplicate的部分,代表在一次請求當中,重復執行了查詢,可以進行優化。通過Step可以對獨立的sql塊進行標記。

@陳卧龍的博客


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM