mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Paging optimization
This commit is contained in:
parent
3bc24087c4
commit
f8c5aef4c5
@ -18,19 +18,19 @@ namespace OrmTest
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// /***Unit Test***/
|
||||
//new Select(1).Init();
|
||||
//new Field(1).Init();
|
||||
//new Where(1).Init();
|
||||
//new Method(1).Init();
|
||||
//new JoinQuery(1).Init();
|
||||
//new SingleQuery(1).Init();
|
||||
//new SelectQuery(1).Init();
|
||||
//new AutoClose(1).Init();
|
||||
//new Insert(1).Init();
|
||||
//new Delete(1).Init();
|
||||
//new Update(1).Init();
|
||||
//new Mapping(1).Init();
|
||||
//new DataTest(1).Init();
|
||||
new Select(1).Init();
|
||||
new Field(1).Init();
|
||||
new Where(1).Init();
|
||||
new Method(1).Init();
|
||||
new JoinQuery(1).Init();
|
||||
new SingleQuery(1).Init();
|
||||
new SelectQuery(1).Init();
|
||||
new AutoClose(1).Init();
|
||||
new Insert(1).Init();
|
||||
new Delete(1).Init();
|
||||
new Update(1).Init();
|
||||
new Mapping(1).Init();
|
||||
new DataTest(1).Init();
|
||||
|
||||
/***Performance Test***/
|
||||
new SqlSugarPerformance(100).Select();
|
||||
|
@ -39,24 +39,15 @@ namespace OrmTest.UnitTest
|
||||
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] ORDER BY [ID] ASC", null, t3.Key, null, "single t3 Error");
|
||||
|
||||
var t4 = db.Queryable<Student>().OrderBy(it => it.Id).Take(3).ToSql();
|
||||
base.Check(@"WITH PageTable AS(
|
||||
SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent]
|
||||
)
|
||||
SELECT * FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY [ID] ASC) AS RowIndex FROM PageTable ) T WHERE RowIndex BETWEEN 1 AND 3", null, t4.Key, null, "single t4 Error");
|
||||
base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] ASC) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 1 AND 3", null, t4.Key, null, "single t4 Error");
|
||||
|
||||
var t5 = db.Queryable<Student>().OrderBy(it => it.Id).Skip(3).ToSql();
|
||||
base.Check(@"WITH PageTable AS(
|
||||
SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent]
|
||||
)
|
||||
SELECT * FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY [ID] ASC) AS RowIndex FROM PageTable ) T WHERE RowIndex BETWEEN 4 AND 9223372036854775807", null, t5.Key,null, "single t5 Error");
|
||||
base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] ASC) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 4 AND 9223372036854775807", null, t5.Key,null, "single t5 Error");
|
||||
|
||||
int pageIndex = 2;
|
||||
int pageSize = 10;
|
||||
var t6 = db.Queryable<Student>().OrderBy(it => it.Id,OrderByType.Desc).Skip((pageIndex-1)*pageSize).Take(pageSize).ToSql();
|
||||
base.Check(@"WITH PageTable AS(
|
||||
SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent]
|
||||
)
|
||||
SELECT * FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY [ID] DESC) AS RowIndex FROM PageTable ) T WHERE RowIndex BETWEEN 11 AND 20", null, t6.Key, null, "single t6 Error");
|
||||
base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] DESC) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 11 AND 20", null, t6.Key, null, "single t6 Error");
|
||||
|
||||
|
||||
int studentCount=db.Ado.GetInt("select count(1) from Student");
|
||||
@ -104,10 +95,7 @@ namespace OrmTest.UnitTest
|
||||
.Where(it=>it.Id==1)
|
||||
.WhereIF(true,it=> SqlFunc.Contains(it.Name,"a"))
|
||||
.OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize ).With(SqlWith.NoLock).ToSql();
|
||||
base.Check(@"WITH PageTable AS(
|
||||
SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WITH(NOLOCK) WHERE ( [ID] = @Id0 ) AND ([Name] like '%'+@MethodConst1+'%')
|
||||
)
|
||||
SELECT * FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY [ID] DESC) AS RowIndex FROM PageTable ) T WHERE RowIndex BETWEEN 11 AND 20", new List<SugarParameter>() {
|
||||
base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] DESC) AS RowIndex FROM [STudent] WITH(NOLOCK) WHERE ( [ID] = @Id0 ) AND ([Name] like '%'+@MethodConst1+'%') ) T WHERE RowIndex BETWEEN 11 AND 20", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),new SugarParameter("@MethodConst1","a")
|
||||
}, t8.Key, t8.Value,"single t8 Error");
|
||||
|
||||
|
@ -123,7 +123,7 @@ namespace SqlSugar
|
||||
public virtual ISugarQueryable<T> Where<T2>(string whereString, object whereObj = null)
|
||||
{
|
||||
var whereValue = QueryBuilder.WhereInfos;
|
||||
whereValue.Add(SqlBuilder.AppendWhereOrAnd(whereValue.Count == 0, whereString+PubConst.Space));
|
||||
whereValue.Add(SqlBuilder.AppendWhereOrAnd(whereValue.Count == 0, whereString + PubConst.Space));
|
||||
if (whereObj != null)
|
||||
QueryBuilder.Parameters.AddRange(Context.Ado.GetParameters(whereObj));
|
||||
return this;
|
||||
@ -495,7 +495,10 @@ namespace SqlSugar
|
||||
public virtual List<T> ToPageList(int pageIndex, int pageSize, ref int totalNumber)
|
||||
{
|
||||
totalNumber = this.Count();
|
||||
return ToPageList(pageIndex, pageSize);
|
||||
if (totalNumber == 0)
|
||||
return new List<T>();
|
||||
else
|
||||
return ToPageList(pageIndex, pageSize);
|
||||
}
|
||||
|
||||
public virtual KeyValuePair<string, List<SugarParameter>> ToSql()
|
||||
@ -677,7 +680,7 @@ namespace SqlSugar
|
||||
_Where(expression);
|
||||
return this;
|
||||
}
|
||||
public new ISugarQueryable<T, T2> Where(string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2> Where(string whereString, object whereObj)
|
||||
{
|
||||
Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
@ -807,27 +810,27 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISugarQueryable<T, T2, T3> WhereIF(bool isWhere,Expression<Func<T, T2, bool>> expression)
|
||||
public ISugarQueryable<T, T2, T3> WhereIF(bool isWhere, Expression<Func<T, T2, bool>> expression)
|
||||
{
|
||||
if (isWhere)
|
||||
_Where(expression);
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3> WhereIF(bool isWhere, Expression<Func<T, bool>> expression)
|
||||
public new ISugarQueryable<T, T2, T3> WhereIF(bool isWhere, Expression<Func<T, bool>> expression)
|
||||
{
|
||||
if (isWhere)
|
||||
_Where(expression);
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2,T3> Where(string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3> Where(string whereString, object whereObj)
|
||||
{
|
||||
Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2,T3> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where<T>(whereString, whereObj);
|
||||
@ -890,13 +893,13 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3,T4> Where(string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4> Where(string whereString, object whereObj)
|
||||
{
|
||||
Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3,T4> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where<T>(whereString, whereObj);
|
||||
@ -1031,13 +1034,13 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4,T5> Where(string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5> Where(string whereString, object whereObj)
|
||||
{
|
||||
Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4,T5> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where<T>(whereString, whereObj);
|
||||
@ -1198,13 +1201,13 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5,T6> Where(string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6> Where(string whereString, object whereObj)
|
||||
{
|
||||
Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5,T6> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where<T>(whereString, whereObj);
|
||||
@ -1342,7 +1345,7 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> WhereIF(bool isWhere, Expression<Func<T, bool>> expression)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> WhereIF(bool isWhere, Expression<Func<T, bool>> expression)
|
||||
{
|
||||
if (isWhere)
|
||||
_Where(expression);
|
||||
@ -1391,13 +1394,13 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6,T7> Where(string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> Where(string whereString, object whereObj)
|
||||
{
|
||||
Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6,T7> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where<T>(whereString, whereObj);
|
||||
@ -1611,13 +1614,13 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7,T8> Where(string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> Where(string whereString, object whereObj)
|
||||
{
|
||||
Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7,T8> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> WhereIF(bool isWhere, string whereString, object whereObj)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where<T>(whereString, whereObj);
|
||||
|
@ -229,13 +229,15 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
sql = new StringBuilder();
|
||||
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " Order By GetDate() ";
|
||||
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY GetDate() ";
|
||||
if (this.PartitionByValue.IsValuable())
|
||||
{
|
||||
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
||||
}
|
||||
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos);
|
||||
sql.Replace("{$:OrderByString:$}", (Skip != null || Take != null) ? string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString) : null);
|
||||
var isRowNumber = Skip != null || Take != null;
|
||||
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
||||
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos,(!isRowNumber&&this.OrderByValue.IsValuable())?GetOrderByString:null);
|
||||
sql.Replace("{$:OrderByString:$}", isRowNumber? (this.IsCount?null: rowNumberString): null);
|
||||
if (IsCount) { return sql.ToString(); }
|
||||
if (Skip != null && Take == null)
|
||||
{
|
||||
|
@ -12,7 +12,7 @@ namespace SqlSugar
|
||||
{
|
||||
get
|
||||
{
|
||||
return "SELECT {0}{{$:OrderByString:$}} FROM {1}{2}{3}";
|
||||
return "SELECT {0}{{$:OrderByString:$}} FROM {1}{2}{3}{4}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user