From 7c8c9bc3afdc2083fe3d5542d14158e69746b3cd Mon Sep 17 00:00:00 2001 From: niphlod Date: Wed, 13 Nov 2013 23:18:07 +0100 Subject: [PATCH] improved performance on mssql4 --- gluon/dal.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/gluon/dal.py b/gluon/dal.py index 6005634b..fbc85b05 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