mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 18:52:50 +08:00
- 优化 GroupBy Page 未排序的查询;#1126
This commit is contained in:
parent
5736658e08
commit
e50c8ee7cd
@ -45,11 +45,16 @@ namespace FreeSql.Odbc.SqlServer
|
||||
if (_limit > 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_orderby))
|
||||
{
|
||||
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__");
|
||||
}
|
||||
@ -235,11 +240,16 @@ namespace FreeSql.Odbc.SqlServer
|
||||
if (_skip > 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_orderby))
|
||||
{
|
||||
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");
|
||||
}
|
||||
|
@ -45,11 +45,16 @@ namespace FreeSql.SqlServer.Curd
|
||||
if (_limit > 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_orderby))
|
||||
{
|
||||
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__");
|
||||
}
|
||||
@ -235,11 +240,16 @@ namespace FreeSql.SqlServer.Curd
|
||||
if (_skip > 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_orderby))
|
||||
{
|
||||
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");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user