mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Paging optimization
This commit is contained in:
parent
3bc24087c4
commit
f8c5aef4c5
@ -18,19 +18,19 @@ namespace OrmTest
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
// /***Unit Test***/
|
// /***Unit Test***/
|
||||||
//new Select(1).Init();
|
new Select(1).Init();
|
||||||
//new Field(1).Init();
|
new Field(1).Init();
|
||||||
//new Where(1).Init();
|
new Where(1).Init();
|
||||||
//new Method(1).Init();
|
new Method(1).Init();
|
||||||
//new JoinQuery(1).Init();
|
new JoinQuery(1).Init();
|
||||||
//new SingleQuery(1).Init();
|
new SingleQuery(1).Init();
|
||||||
//new SelectQuery(1).Init();
|
new SelectQuery(1).Init();
|
||||||
//new AutoClose(1).Init();
|
new AutoClose(1).Init();
|
||||||
//new Insert(1).Init();
|
new Insert(1).Init();
|
||||||
//new Delete(1).Init();
|
new Delete(1).Init();
|
||||||
//new Update(1).Init();
|
new Update(1).Init();
|
||||||
//new Mapping(1).Init();
|
new Mapping(1).Init();
|
||||||
//new DataTest(1).Init();
|
new DataTest(1).Init();
|
||||||
|
|
||||||
/***Performance Test***/
|
/***Performance Test***/
|
||||||
new SqlSugarPerformance(100).Select();
|
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");
|
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();
|
var t4 = db.Queryable<Student>().OrderBy(it => it.Id).Take(3).ToSql();
|
||||||
base.Check(@"WITH PageTable AS(
|
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");
|
||||||
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");
|
|
||||||
|
|
||||||
var t5 = db.Queryable<Student>().OrderBy(it => it.Id).Skip(3).ToSql();
|
var t5 = db.Queryable<Student>().OrderBy(it => it.Id).Skip(3).ToSql();
|
||||||
base.Check(@"WITH PageTable AS(
|
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");
|
||||||
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");
|
|
||||||
|
|
||||||
int pageIndex = 2;
|
int pageIndex = 2;
|
||||||
int pageSize = 10;
|
int pageSize = 10;
|
||||||
var t6 = db.Queryable<Student>().OrderBy(it => it.Id,OrderByType.Desc).Skip((pageIndex-1)*pageSize).Take(pageSize).ToSql();
|
var t6 = db.Queryable<Student>().OrderBy(it => it.Id,OrderByType.Desc).Skip((pageIndex-1)*pageSize).Take(pageSize).ToSql();
|
||||||
base.Check(@"WITH PageTable AS(
|
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");
|
||||||
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");
|
|
||||||
|
|
||||||
|
|
||||||
int studentCount=db.Ado.GetInt("select count(1) from Student");
|
int studentCount=db.Ado.GetInt("select count(1) from Student");
|
||||||
@ -104,10 +95,7 @@ namespace OrmTest.UnitTest
|
|||||||
.Where(it=>it.Id==1)
|
.Where(it=>it.Id==1)
|
||||||
.WhereIF(true,it=> SqlFunc.Contains(it.Name,"a"))
|
.WhereIF(true,it=> SqlFunc.Contains(it.Name,"a"))
|
||||||
.OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize ).With(SqlWith.NoLock).ToSql();
|
.OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize ).With(SqlWith.NoLock).ToSql();
|
||||||
base.Check(@"WITH PageTable AS(
|
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>() {
|
||||||
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>() {
|
|
||||||
new SugarParameter("@Id0",1),new SugarParameter("@MethodConst1","a")
|
new SugarParameter("@Id0",1),new SugarParameter("@MethodConst1","a")
|
||||||
}, t8.Key, t8.Value,"single t8 Error");
|
}, t8.Key, t8.Value,"single t8 Error");
|
||||||
|
|
||||||
|
@ -123,7 +123,7 @@ namespace SqlSugar
|
|||||||
public virtual ISugarQueryable<T> Where<T2>(string whereString, object whereObj = null)
|
public virtual ISugarQueryable<T> Where<T2>(string whereString, object whereObj = null)
|
||||||
{
|
{
|
||||||
var whereValue = QueryBuilder.WhereInfos;
|
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)
|
if (whereObj != null)
|
||||||
QueryBuilder.Parameters.AddRange(Context.Ado.GetParameters(whereObj));
|
QueryBuilder.Parameters.AddRange(Context.Ado.GetParameters(whereObj));
|
||||||
return this;
|
return this;
|
||||||
@ -495,6 +495,9 @@ namespace SqlSugar
|
|||||||
public virtual List<T> ToPageList(int pageIndex, int pageSize, ref int totalNumber)
|
public virtual List<T> ToPageList(int pageIndex, int pageSize, ref int totalNumber)
|
||||||
{
|
{
|
||||||
totalNumber = this.Count();
|
totalNumber = this.Count();
|
||||||
|
if (totalNumber == 0)
|
||||||
|
return new List<T>();
|
||||||
|
else
|
||||||
return ToPageList(pageIndex, pageSize);
|
return ToPageList(pageIndex, pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -807,7 +810,7 @@ namespace SqlSugar
|
|||||||
return this;
|
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)
|
if (isWhere)
|
||||||
_Where(expression);
|
_Where(expression);
|
||||||
@ -821,13 +824,13 @@ namespace SqlSugar
|
|||||||
return this;
|
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);
|
Where<T>(whereString, whereObj);
|
||||||
return this;
|
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;
|
if (!isWhere) return this;
|
||||||
this.Where<T>(whereString, whereObj);
|
this.Where<T>(whereString, whereObj);
|
||||||
@ -890,13 +893,13 @@ namespace SqlSugar
|
|||||||
return this;
|
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);
|
Where<T>(whereString, whereObj);
|
||||||
return this;
|
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;
|
if (!isWhere) return this;
|
||||||
this.Where<T>(whereString, whereObj);
|
this.Where<T>(whereString, whereObj);
|
||||||
@ -1031,13 +1034,13 @@ namespace SqlSugar
|
|||||||
return this;
|
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);
|
Where<T>(whereString, whereObj);
|
||||||
return this;
|
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;
|
if (!isWhere) return this;
|
||||||
this.Where<T>(whereString, whereObj);
|
this.Where<T>(whereString, whereObj);
|
||||||
@ -1198,13 +1201,13 @@ namespace SqlSugar
|
|||||||
return this;
|
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);
|
Where<T>(whereString, whereObj);
|
||||||
return this;
|
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;
|
if (!isWhere) return this;
|
||||||
this.Where<T>(whereString, whereObj);
|
this.Where<T>(whereString, whereObj);
|
||||||
@ -1391,13 +1394,13 @@ namespace SqlSugar
|
|||||||
return this;
|
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);
|
Where<T>(whereString, whereObj);
|
||||||
return this;
|
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;
|
if (!isWhere) return this;
|
||||||
this.Where<T>(whereString, whereObj);
|
this.Where<T>(whereString, whereObj);
|
||||||
@ -1611,13 +1614,13 @@ namespace SqlSugar
|
|||||||
return this;
|
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);
|
Where<T>(whereString, whereObj);
|
||||||
return this;
|
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;
|
if (!isWhere) return this;
|
||||||
this.Where<T>(whereString, whereObj);
|
this.Where<T>(whereString, whereObj);
|
||||||
|
@ -229,13 +229,15 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sql = new StringBuilder();
|
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())
|
if (this.PartitionByValue.IsValuable())
|
||||||
{
|
{
|
||||||
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
||||||
}
|
}
|
||||||
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos);
|
var isRowNumber = Skip != null || Take != null;
|
||||||
sql.Replace("{$:OrderByString:$}", (Skip != null || Take != null) ? string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString) : 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 (IsCount) { return sql.ToString(); }
|
||||||
if (Skip != null && Take == null)
|
if (Skip != null && Take == null)
|
||||||
{
|
{
|
||||||
|
@ -12,7 +12,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
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