1

I am executing a sql query using PetaPoco that would normally return around 4000 rows.

Here is the code that builds the 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("              )");

If I pull the entire recordset back and use LINQ to page the results, the page renders in about 250ms. Here is the code:

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

If I try to use the paging feature built into PetaPoco, the page takes over 4200ms to render. Here is the code:

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

What's odd is Glimpse and Sql Profiler show me that the actual SQL commands running in either case take approximately the same length of time. However Glimpse suggests that in the second case the delay takes place all before the connection gets opened. Can anybody explain this behavior?

More Information: I'm running Sql Server 2008R2

4

2 回答 2

2

PetaPoco Paging 正则表达式存在问题。这通常会成为长 SQL 的问题,但其他可能会受到影响。

这可以通过将 rxOrderBy 正则表达式替换为

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

或者通过使用 NPoco,它是具有 API 兼容性的 PetaPoco 的一个大大增强的分支。

于 2013-08-26T01:43:07.853 回答
0

解决方案中定义的 rxOrderBy 给出了异常,因为组名深度未知。这个有什么线索吗?我对这个问题不是很熟悉。

于 2015-04-07T12:51:45.917 回答