mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-09 02:44:58 +08:00
Paging optimization
This commit is contained in:
@@ -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");
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user