Skip to content

Commit 8a07c04

Browse files
author
Richard Jones
committed
more efficient find() in RDBMS [SF#1012781]
1 parent 8a05a03 commit 8a07c04

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Fixed:
4848
- date.Interval() now accepts an Interval as a spec (sf bug 1041266)
4949
- handle deleted properties in RDBMS history
5050
- apply timezone in correct direction in user input (sf bug 1013097)
51+
- more efficient find() in RDBMS (sf bug 1012781)
5152

5253

5354
2004-07-21 0.7.6

roundup/backends/rdbms_common.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: rdbms_common.py,v 1.135 2004-10-08 00:56:12 richard Exp $
1+
# $Id: rdbms_common.py,v 1.136 2004-10-08 01:28:32 richard Exp $
22
''' Relational database (SQL) backend common code.
33
44
Basics:
@@ -1904,8 +1904,8 @@ def find(self, **propspec):
19041904

19051905
# first, links
19061906
a = self.db.arg
1907-
allvalues = (1,)
1908-
o = []
1907+
allvalues = ()
1908+
sql = []
19091909
where = []
19101910
for prop, values in propspec:
19111911
if not isinstance(props[prop], hyperdb.Link):
@@ -1926,35 +1926,32 @@ def find(self, **propspec):
19261926
allvalues += tuple(values)
19271927
s += '_%s in (%s)'%(prop, ','.join([a]*len(values)))
19281928
where.append('(' + s +')')
1929-
tables = ['_%s'%self.classname]
19301929
if where:
1931-
o.append('(' + ' and '.join(where) + ')')
1930+
allvalues = (1, ) + allvalues
1931+
sql.append('''select id from _%s where __retired__ <> %s
1932+
and %s'''%(self.classname, a, ' and '.join(where)))
19321933

19331934
# now multilinks
19341935
for prop, values in propspec:
19351936
if not isinstance(props[prop], hyperdb.Multilink):
19361937
continue
19371938
if not values:
19381939
continue
1940+
allvalues += (1, )
19391941
if type(values) is type(''):
19401942
allvalues += (values,)
19411943
s = a
19421944
else:
19431945
allvalues += tuple(values.keys())
19441946
s = ','.join([a]*len(values))
19451947
tn = '%s_%s'%(self.classname, prop)
1946-
tables.append(tn)
1947-
o.append('(id=%s.nodeid and %s.linkid in (%s))'%(tn, tn, s))
1948+
sql.append('''select id from _%s, %s where __retired__ <> %s
1949+
and id = %s.nodeid and %s.linkid in (%s)'''%(self.classname,
1950+
tn, a, tn, tn, s))
19481951

1949-
if not o:
1952+
if not sql:
19501953
return []
1951-
elif len(o) > 1:
1952-
o = '(' + ' or '.join(['(%s)'%i for i in o]) + ')'
1953-
else:
1954-
o = o[0]
1955-
t = ', '.join(tables)
1956-
sql = 'select distinct(id) from %s where __retired__ <> %s and %s'%(
1957-
t, a, o)
1954+
sql = ' union '.join(sql)
19581955
self.db.sql(sql, allvalues)
19591956
# XXX numeric ids
19601957
l = [str(x[0]) for x in self.db.sql_fetchall()]

0 commit comments

Comments
 (0)