http://www.cnblogs.com/Leo_wl/p/4921799.html
SELECT TOP 30 * FROM users WHERE nID>2000 And nID<50000 ORDER BY nID DESC; SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY nID asc) AS rownum,* FROM users Where nID>2000 And nID<50000 ) AS D WHERE rownum>0 AND rownum<31; SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY nID asc) AS rownum,* FROM users Where nID>2000 And nID<50000 ) AS D WHERE rownum BETWEEN 1 AND 30;
他们的不同之处在于“提取N条记录”的这个操作,Top N和 WHERE rownum>0 AND rownum<31。
它们具体是如何运行我讲不出理论,但是我可以做个比方:
体育老师让我们跑30步的距离,Top N的做法就是跑30步,自己边跑边数;WHERE rownum>0 AND rownum<31 相当于老师在30步的位置花了个标记,你只管死跑,到了那个标记就相当于跑了30步。我想做了标记死跑这个要快点吧,:)
在查询列表时,不要将查询总条数与查询列表数据一起执行,应分开查询,这样效果会提升
public string GetCommentCountSql(MQueryCommentsReqParam model) { var sql = string.Empty; string where = GetWhereSql(model); sql = string.Format(@"SELECT COUNT(1) FROM dbo.VipCommentProduct {0}", where); return sql; } private static string GetWhereSql(MQueryCommentsReqParam model) { string where = string.Empty; var queryItems = new List<string>(); if (!string.IsNullOrEmpty(model.CommentIds)) { queryItems.Add(model.CommentIds.IndexOf(',') > 0 ? string.Format(" Id in ({0})", model.CommentIds) : string.Format(" Id={0}", model.CommentIds)); } else { //会员号 if (model.UserId != null && model.UserId > 0) { queryItems.Add(string.Format(" UserId={0}", model.UserId)); } //会员昵称 if (!string.IsNullOrEmpty(model.ShowNickName)) { queryItems.Add(string.Format(" charindex('{0}',ShowNickName) > 0", model.ShowNickName)); } //订单号 if (model.OrderId != null && model.OrderId > 0) { queryItems.Add(string.Format(" OrderId={0}", model.OrderId)); } //商品查询条件(商品名或商品 if (!string.IsNullOrEmpty(model.ProductCondition)) { var regex = new Regex(@"^d+$"); //正则 判断为数字 if (regex.IsMatch(model.ProductCondition)) { queryItems.Add(string.Format(" ProductId='{0}'", model.ProductCondition)); } else //为字符串 { queryItems.Add(string.Format(" ProductName like '%{0}%'", model.ProductCondition)); } } //评论内容 if (!string.IsNullOrEmpty(model.ContentInfo)) { queryItems.Add(string.Format(" charindex('{0}',ContentInfo) > 0", model.ContentInfo)); } //评分 if (model.Score != null && model.Score > 0) { queryItems.Add(string.Format(" Score={0}", model.Score)); } //精华,1:精华 if (model.IsHighLight != null && model.IsHighLight > -1) { queryItems.Add(string.Format(" IsHighLight={0}", model.IsHighLight)); } //是否有图 if (model.HasPic != null && model.HasPic > -1) { if (model.HasPic == 0) //无图 { queryItems.Add(" UserImgObj is null"); } if (model.HasPic == 1) //有图 { queryItems.Add(" UserImgObj is not null"); } } //是否追评 if (model.HasReComment != null && model.HasReComment > -1) { if (model.HasReComment == 0) //无追评 { queryItems.Add(" ParentId =0"); } if (model.HasReComment == 1) //有追评 { queryItems.Add(" ParentId >0"); } } //审核状态:1:未审核,2:审核通过,3:审核未通过 if (model.CheckState != null && model.CheckState > 0) { queryItems.Add(string.Format(" CheckState={0}", model.CheckState)); } //审核人 if (!string.IsNullOrEmpty(model.CheckUser)) { queryItems.Add(string.Format(" CheckUser='{0}'", model.CheckUser)); } //是否回复 if (model.HasReply != null && model.HasReply > -1) { if (model.HasReply == 0) //未回复 { queryItems.Add(" ReplyContent is null"); } if (model.HasReply == 1) { queryItems.Add(" ReplyContent is not null"); } } //回复人 if (!string.IsNullOrEmpty(model.ReplyUser)) { queryItems.Add(string.Format(" ReplyUser='{0}'", model.ReplyUser)); } if (model.PeriodTimeType != null && model.PeriodTimeType > 0 && (model.StartTime != null || model.EndTime != null)) { var periodDic = new Dictionary<int?, string>() { {1,"CheckTime"}, //审核时间 {2,"AddTime"}, //评论时间 {3,"ReplyTime"} //回复时间 }; if (model.StartTime != null && model.EndTime == null) { queryItems.Add(string.Format(" " + periodDic[model.PeriodTimeType] + ">'{0}'", model.StartTime)); } if (model.EndTime != null && model.StartTime == null) { queryItems.Add(string.Format(" " + periodDic[model.PeriodTimeType] + "<'{0}'", model.EndTime)); } if (model.StartTime != null && model.EndTime != null) { queryItems.Add(string.Format(" (" + periodDic[model.PeriodTimeType] + ">'{0}' and " + periodDic[model.PeriodTimeType] + "<'{1}')", model.StartTime, model.EndTime)); } } } if (queryItems.Any()) { where = "where " + string.Join(" and ", queryItems.ToArray()); } return where; }