[SQLObject] [PATCH] Add warning for negative slices on large result sets
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Andrew B. <an...@ca...> - 2005-07-12 07:19:31
|
This patch emits a warning when using negative slices or indices on large result sets. Because doing Foo.select(...)[-1] on a 1 million row table has to fetch 1 million rows rather than just 1, it's a hidden performance trap. This patch emits a warning when a SelectResults is sliced with a negative index, and the size of the result set being slices is more than 100 elements. SelectResults usually does a good job of efficiently getting the results you ask it for, but in this case it doesn't, which can be surprising. This warning helps make the problem less surprising. Index: SQLObject/sqlobject/sresults.py =================================================================== --- SQLObject.orig/sqlobject/sresults.py 2005-06-30 11:33:32.000000000 -0300 +++ SQLObject/sqlobject/sresults.py 2005-06-30 11:33:48.000000000 -0300 @@ -105,11 +105,16 @@ # handle) if (value.start and value.start < 0) \ or (value.stop and value.stop < 0): + L = list(self) + if len(L) > 100: + warnings.warn('Negative indices when slicing are slow: ' + 'fetched %d rows.' % (len(L),), + stacklevel=2) if value.start: if value.stop: - return list(self)[value.start:value.stop] - return list(self)[value.start:] - return list(self)[:value.stop] + return L[value.start:value.stop] + return L[value.start:] + return L[:value.stop] if value.start: @@ -137,7 +142,12 @@ return self.clone(start=start, end=end) else: if value < 0: - return list(iter(self))[value] + L = list(self) + if len(L) > 100: + warnings.warn('Negative indices when slicing are slow: ' + 'fetched %d rows.' % (len(L),), + stacklevel=2) + return L[value] else: start = self.ops.get('start', 0) + value return list(self.clone(start=start, end=start+1))[0] -Andrew. |