Skip to content

Commit

Permalink
- 优化 GroupBy Page 未排序的查询;#1126
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed May 27, 2022
1 parent 5736658 commit e50c8ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ internal static string ToSqlStaticRowNumber(CommonUtils _commonUtils, CommonExpr
{
if (string.IsNullOrEmpty(_orderby))
{
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
if (string.IsNullOrEmpty(_groupby))
{
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
}
else
_orderby = _groupby.Replace("GROUP BY ", "ORDER BY ");
}
if (_skip > 0) // 注意这个判断,大于 0 才使用 ROW_NUMBER ,否则属于第一页直接使用 TOP
sb.Append(", ROW_NUMBER() OVER(").Append(_orderby).Append(") AS __rownum__");
Expand Down Expand Up @@ -236,9 +241,14 @@ internal static string ToSqlStaticOffsetFetchNext(CommonUtils _commonUtils, Comm
{
if (string.IsNullOrEmpty(_orderby))
{
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
if (string.IsNullOrEmpty(_groupby))
{
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
}
else
_orderby = _groupby.Replace("GROUP BY ", "ORDER BY ");
}
sb.Append(_orderby).Append($" \r\nOFFSET {_skip} ROW");
if (_limit > 0) sb.Append($" \r\nFETCH NEXT {_limit} ROW ONLY");
Expand Down
22 changes: 16 additions & 6 deletions Providers/FreeSql.Provider.SqlServer/Curd/SqlServerSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ internal static string ToSqlStaticRowNumber(CommonUtils _commonUtils, CommonExpr
{
if (string.IsNullOrEmpty(_orderby))
{
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
if (string.IsNullOrEmpty(_groupby))
{
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
}
else
_orderby = _groupby.Replace("GROUP BY ", "ORDER BY ");
}
if (_skip > 0) // 注意这个判断,大于 0 才使用 ROW_NUMBER ,否则属于第一页直接使用 TOP
sb.Append(", ROW_NUMBER() OVER(").Append(_orderby).Append(") AS __rownum__");
Expand Down Expand Up @@ -236,9 +241,14 @@ internal static string ToSqlStaticOffsetFetchNext(CommonUtils _commonUtils, Comm
{
if (string.IsNullOrEmpty(_orderby))
{
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
if (string.IsNullOrEmpty(_groupby))
{
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
}
else
_orderby = _groupby.Replace("GROUP BY ", "ORDER BY ");
}
sb.Append(_orderby).Append($" \r\nOFFSET {_skip} ROW");
if (_limit > 0) sb.Append($" \r\nFETCH NEXT {_limit} ROW ONLY");
Expand Down

0 comments on commit e50c8ee

Please sign in to comment.