原創|使用教程|編輯:龔雪|2014-01-24 09:14:26.000|閱讀 338 次
概述:Linq to SQL Profiler是由以色列著名公司Hibernating Rhinos開發出來的數據庫工具。并且該軟件是由 OR/M 社區的高層領導人親自開發設計,能夠精準的幫助優化App的冗余的代碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Select N+1是一種反模式的數據訪問,對于數據庫來說這是一種次優的訪問方式。以下列代碼為例,我們來討論個中緣由。代碼背景:我們需要向用戶展示來自所有帖子的所有評論,以至于用戶可以自行選擇刪除那些不合適的評論:
// SELECT * FROM Posts
var postsQuery = from post in blogDataContext.Posts
                 select post;
foreach (Post post in postsQuery)
{  
    //lazy loading of comments list causes:    
    // SELECT * FROM Comments where PostId = @p0   
    foreach (Comment comment in post.Comments)
    {      
        //print comment...   
    }
}
在這個例子中,我們正在加載第一批選中的帖子列表。但是我們以非常緩慢的方式在訪問采集,它導致了Linq to Sql每一次去訪問數據庫然后只帶回一行的結果。這種效率是極其低下的,Linq to Sql Profiler在任何時候只要檢測到有這種情況發生時,都會發出警告。
要解決這種問題比較輕松,強迫collection使用DataLoadOptions class來指定需要被加載的具體的對象模型:
var loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Post>(p => p.Comments);
blogDataContext.LoadOptions = loadOptions;
// SELECT * FROM Posts JOIN Comments ...
var postsQuery = (from post in blogDataContext.Posts
                 select post);
foreach (Post post in postsQuery)
{  
    // no lazy loading of comments list causes    
    foreach (Comment comment in post.Comments)
    {      
        //print comment...   
    }
}
這樣一來對于數據庫來說就只有一個join和query了。
>>歡迎點此免費下載和試用Linq to SQL Profiler
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn