2013-08-23 63 views
1

我正在執行一個使用PetaPoco的SQL查詢,通常會返回大約4000行。使用Paged Fetch時性能下降

這裏是構建SQL代碼:

var sql = PetaPoco.Sql.Builder 
.Append("Select ") 
.Append("Participants.ParticipantID") 
.Append("From Participants") 
.Append("Inner Join Organizations") 
.Append("On Participants.OrgID = Organizations.OrgID") 
.Append("Left Join Departments") 
.Append("On Participants.DepartmentID = Departments.DepartmentID") 
.Append("Where") 
.Append("Participants.OrgID = @0", 6328); 
.Append("and Participants.Last_Name like @0", "P%"); 
.Append("and ") 
.Append("Participants.OrgID in ") 
.Append("    (") 
.Append("     Select") 
.Append("      OrgID ") 
.Append("     from ") 
.Append("      Organizations") 
.Append("     Where") 
.Append("      AssociationID = @0", 318) 
.Append("    )"); 

如果我拉整個記錄背部和使用LINQ頁面結果,頁面呈現在約250毫秒。這裏是代碼:

List<ParticipantVMItem> PagedResult = null; 
    var FullResult = db.Fetch<ParticipantVMItem>(sql); 
    PagedResult = FullResult.Skip((PageNo - 1) * PageSize).Take(PageSize).ToList(); 

如果我嘗試使用內置到PetaPoco中的分頁功能,頁面需要超過4200ms才能呈現。下面是代碼:

  List<ParticipantVMItem> PagedResult = null; 
      PagedResult = db.Fetch<ParticipantVMItem>(4, 250, sql); 

有什麼奇怪的是一瞥和SQL事件探查器告訴我,在這兩種情況下運行的實際SQL命令需要的時間大致相同的長度。但是Glimpse表明,在第二種情況下,延遲發生在連接打開之前。任何人都可以解釋這種行爲嗎?

更多信息:我在運行SQL Server 2008R2

回答

2

有一個與PetaPoco分頁正則表達式的問題。 這通常會成爲長SQL中的一個問題,但其他問題可能會受到影響。

這可以通過更換rxOrderBy正則表達式與

public static Regex rxOrderBy = new Regex(@"\bORDER\s+BY\s+(?:\((?>\((?<depth>)|\)(?<-depth>)|.?)*(?(depth)(?!))\)|‌​[\w\(\)\.\[\]""])+(?:\s+(?:ASC|DESC))?(?:\s*,\s*(?:((?>((?<depth>)|)(?<-depth‌​>)|.?)*(?(depth)(?!)))|[\w()\.[]""])+(?:\s+(?:ASC|DESC))?)*", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled);

或通過使用NPoco,與API的相容性的PetaPoco得多增強叉被固定。

+0

我會用我的sqlcode更新我的問題。 – Aheho

+0

你有與NPoco相同的問題嗎?你可以試試嗎?它使用diff正則表達式。 – Schotime

+0

我已經將問題隔離到以下REGEX: \t \t \t \t public static Regex rxOrderBy = new Regex(@「\ bORDER \ s + BY \ s +(?!。*?(?:\)| \ s +)AS \)(??((?(?))((??) 。])+(?:\ s +(?: ASC | DESC))?(?:\ s *,\ s *(?:\((?> \((?)| \)(?<-depth>) 。*)*(?(depth)(?!))\)| [\ w \(\)\。])+(?:\ s +(?: ASC | DESC))?)*「,RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled); – Aheho

0

解決方案中定義的rxOrderBy提供了一個例外,因爲組名深度未知。這一個的任何線索?我並不十分熟悉這件事。