mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-22 20:13:41 +08:00
Generated SQL optimization
This commit is contained in:
@@ -52,21 +52,25 @@ namespace SqlSugar
|
|||||||
var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
|
var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
|
||||||
AppendFilter();
|
AppendFilter();
|
||||||
sql = new StringBuilder();
|
sql = new StringBuilder();
|
||||||
|
var oldOrderByValue = this.OrderByValue;
|
||||||
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.HasValue())
|
if (this.PartitionByValue.HasValue())
|
||||||
{
|
{
|
||||||
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
||||||
}
|
}
|
||||||
var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false;
|
var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false;
|
||||||
var isRowNumber = (Skip != null || Take != null) && !isFirst;
|
var isTop = (Skip == null && Take != null && DisableTop == false);
|
||||||
|
var isRowNumber = (Skip != null || Take != null) && !isFirst && !isTop;
|
||||||
|
if (!isRowNumber && oldOrderByValue == null) { this.OrderByValue = null; }
|
||||||
|
if (isFirst && oldOrderByValue == "ORDER BY GETDATE() ") { this.OrderByValue = null; }
|
||||||
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
||||||
string groupByValue = GetGroupByString + HavingInfos;
|
string groupByValue = GetGroupByString + HavingInfos;
|
||||||
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
|
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
|
||||||
if (isIgnoreOrderBy) { orderByValue = null; }
|
if (isIgnoreOrderBy) { orderByValue = null; }
|
||||||
sql.AppendFormat(SqlTemplate, isFirst ? (" TOP 1 " + GetSelectValue) : GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue);
|
sql.AppendFormat(SqlTemplate, GetSelect(isFirst,isTop), base.GetTableNameString, base.GetWhereValueString, groupByValue, orderByValue);
|
||||||
sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
|
sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
|
||||||
if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
|
if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
|
||||||
var result = isFirst ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip);
|
var result = (isFirst || isTop) ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip);
|
||||||
if (ExternalPageIndex > 0)
|
if (ExternalPageIndex > 0)
|
||||||
{
|
{
|
||||||
if (externalOrderBy.IsNullOrEmpty())
|
if (externalOrderBy.IsNullOrEmpty())
|
||||||
@@ -85,10 +89,10 @@ namespace SqlSugar
|
|||||||
if (this.OldSql.HasValue())
|
if (this.OldSql.HasValue())
|
||||||
this.OldSql += " ORDER BY GETDATE() ";
|
this.OldSql += " ORDER BY GETDATE() ";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (this.OldSql.HasValue())
|
if (this.OldSql.HasValue())
|
||||||
this.OldSql += (" "+this.GetOrderByString);
|
this.OldSql += (" " + this.GetOrderByString);
|
||||||
}
|
}
|
||||||
result += this.Offset;
|
result += this.Offset;
|
||||||
|
|
||||||
@@ -98,5 +102,21 @@ namespace SqlSugar
|
|||||||
result = GetSqlQuerySql(result);
|
result = GetSqlQuerySql(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GetSelect(bool isFirst,bool isTop)
|
||||||
|
{
|
||||||
|
if (isFirst)
|
||||||
|
{
|
||||||
|
return (" TOP 1 " + GetSelectValue);
|
||||||
|
}
|
||||||
|
else if(isTop)
|
||||||
|
{
|
||||||
|
return ($" TOP {this.Take} " + GetSelectValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return GetSelectValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user