Pregunta

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

¿Fue útil?

Solución

There is an issue with the PetaPoco Paging regex. This usually becomes an issue on long SQLs but others can be affected.

This can be fixed by replacing the rxOrderBy Regex with

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);

or by using NPoco, a much enhanced fork of PetaPoco with API compatibility.

Otros consejos

The rxOrderBy defined in the solution gives an exception because the group name depth is not known. Any clues on this one? I'm not really familiar with this matter.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top