Changes between Version 47 and Version 48 of DeveloperGuidelines/Optimisation


Ignore:
Timestamp:
06/18/10 09:25:15 (14 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/Optimisation

    v47 v48  
    7171== Golden Rules for DB Queries ==
    7272
    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:
    7574
    7675=== Use Joins ===
     
    174173}}}
    175174
     175Or more complex:
     176
     177{{{
     178for 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
     187could be:
     188
     189{{{
     190if other_variable == other_value:
     191    ids1 = [x.key for x in y]
     192    ids2 = []
     193else:
     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)
     196if ids1:
     197    records = db(db.mytable.id.belongs(ids1)).select()
     198    for record in records:
     199        ...<branch 1>
     200if ids2:
     201    records = db(db.othertable.id.belongs(ids2)).select()
     202    for record in records:
     203        ...<branch 2>
     204}}}
     205
    176206----
    177207DeveloperGuidelines
     208DeveloperGuidelines