Changes between Version 47 and Version 48 of DeveloperGuidelines/Optimisation
- Timestamp:
- 06/18/10 09:25:15 (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DeveloperGuidelines/Optimisation
v47 v48 71 71 == Golden Rules for DB Queries == 72 72 73 These "rules" might seem a matter of course, however, sometimes you need to take a second look at your code: 74 * Insert a temporary {{{print >> sys.stderr, self.query}}} into web2py's {{{select()}}} function and take a look at what it says. 73 These "rules" might seem a matter of course, however, sometimes you need to take a second look at your code: 75 74 76 75 === Use Joins === … … 174 173 }}} 175 174 175 Or more complex: 176 177 {{{ 178 for x in y: 179 if x.a == some_value or other_variable == other_value: 180 record = db(db.mytable.id == x.key).select() 181 ...<branch 1> 182 else: 183 record = db(db.othertable.id == x.other_key).select() 184 ...<branch 2> 185 }}} 186 187 could be: 188 189 {{{ 190 if other_variable == other_value: 191 ids1 = [x.key for x in y] 192 ids2 = [] 193 else: 194 ids1 = filter(lambda x: (x.a == some_value) and x.key or None, y) 195 ids2 = filter(lambda x: (x.a != some_value) and x.other_key or None, y) 196 if ids1: 197 records = db(db.mytable.id.belongs(ids1)).select() 198 for record in records: 199 ...<branch 1> 200 if ids2: 201 records = db(db.othertable.id.belongs(ids2)).select() 202 for record in records: 203 ...<branch 2> 204 }}} 205 176 206 ---- 177 207 DeveloperGuidelines 208 DeveloperGuidelines