我正在使用PetaPoco执行一个sql查询,通常会返回大约4000行。
以下是构建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来分页结果,则页面呈现大约250ms。这是代码:
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);
奇怪的是Glimpse和Sql Profiler向我展示了在任何一种情况下运行的实际SQL命令所花费的时间大致相同。然而,Glimpse建议在第二种情况下延迟发生在连接打开之前。任何人都可以解释这种行为吗?
更多信息:我正在运行Sql Server 2008R2
答案 0 :(得分:2)
PetaPoco Paging正则表达式存在问题。 这通常会成为长SQL的问题,但其他SQL可能会受到影响。
这可以通过用
替换rxOrderBy Regex来解决
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增强版。
答案 1 :(得分:0)
解决方案中定义的rxOrderBy会产生异常,因为组名称深度未知。有关这一个的线索吗?我对此事并不熟悉。