原創(chuàng)|使用教程|編輯:龔雪|2014-01-24 09:14:26.000|閱讀 320 次
概述:Linq to SQL Profiler是由以色列著名公司Hibernating Rhinos開發(fā)出來的數(shù)據(jù)庫工具。并且該軟件是由 OR/M 社區(qū)的高層領(lǐng)導(dǎo)人親自開發(fā)設(shè)計,能夠精準(zhǔn)的幫助優(yōu)化App的冗余的代碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Select N+1是一種反模式的數(shù)據(jù)訪問,對于數(shù)據(jù)庫來說這是一種次優(yōu)的訪問方式。以下列代碼為例,我們來討論個中緣由。代碼背景:我們需要向用戶展示來自所有帖子的所有評論,以至于用戶可以自行選擇刪除那些不合適的評論:
// 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... } }
在這個例子中,我們正在加載第一批選中的帖子列表。但是我們以非常緩慢的方式在訪問采集,它導(dǎo)致了Linq to Sql每一次去訪問數(shù)據(jù)庫然后只帶回一行的結(jié)果。這種效率是極其低下的,Linq to Sql Profiler在任何時候只要檢測到有這種情況發(fā)生時,都會發(fā)出警告。
要解決這種問題比較輕松,強迫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... } }
這樣一來對于數(shù)據(jù)庫來說就只有一個join和query了。
>>歡迎點此免費下載和試用Linq to SQL Profiler
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn