diff --git a/gluon/dal.py b/gluon/dal.py index 2cd087e6..c8fafcdd 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -3458,15 +3458,21 @@ class MSSQL4Adapter(MSSQLAdapter): """ support for true pagination in MSSQL >= 2012""" def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if not sql_o: - #if there is no orderby, we can't use the brand new statements - #that being said, developer chose its own poison, so be it random - sql_o += ' ORDER BY %s' % self.RANDOM() if limitby: (lmin, lmax) = limitby - sql_o += ' OFFSET %i ROWS FETCH NEXT %i ROWS ONLY' % (lmin, lmax - lmin) + if lmin == 0: + #top is still slightly faster, especially because + #web2py's default to fetch references is to not specify + #an orderby clause + sql_s += ' TOP %i' % lmax + else: + if not sql_o: + #if there is no orderby, we can't use the brand new statements + #that being said, developer chose its own poison, so be it random + sql_o += ' ORDER BY %s' % self.RANDOM() + sql_o += ' OFFSET %i ROWS FETCH NEXT %i ROWS ONLY' % (lmin, lmax - lmin) return 'SELECT %s %s FROM %s%s%s;' % \ - (sql_s, sql_f, sql_t, sql_w, sql_o) + (sql_s, sql_f, sql_t, sql_w, sql_o) def rowslice(self,rows,minimum=0,maximum=None): return rows