Changes between Version 25 and Version 26 of DeveloperGuidelines/Optimisation


Ignore:
Timestamp:
06/18/10 07:59:54 (14 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/Optimisation

    v25 v26  
    1 == Optimisation ==
     1[[TOC]]
     2= Optimisation =
    23
    34Writing code which runs fast.
     
    89There are more things to consider besides performance, e.g. usability and maintainability of code. New contributors should be able to easily understand the code, and bugs should be easy to find.
    910
    10 Python Tips:
     11== Python Tips ==
    1112 * http://wiki.python.org/moin/PythonSpeed/PerformanceTips
    1213 * http://www.python.org/doc/essays/list2str/
     
    1516 * If a specific inner-loop routine cannot be optimised in Python, then consider writing a C routine for this use case.
    1617
    17 Web2Py Tips:
     18== Web2Py Tips ==
    1819 1. Optimize the models, throw away what we don't need. Every field counts.[[BR]]Especially problematic in view of performance are references (joins), as they execute implicit DB requests. The more complex references are, the slower the model loads.
    1920
     
    2930
    3031 7. Consider having configurations which are read from DB frequently but written-to rarely, be set in configuration files which are written-out from the DB (like the CSS from themes)
     32
     33== Golden Rules for DB Queries ==
     34
     35  These "rules" might seem a matter of course, however, sometimes you need to take a second look at your code:
     36
     37 1. '''Use joins''' - one complex query is usually more efficient than multiple simple queries (and gives the DB server a chance to optimize):
     38
     39{{{
     40codes = db(db.mytable.name == name).select()
     41for code in codes:
     42    records = db(db.othertable.code == code).select()
     43}}}
     44
     45better:
     46
     47{{{
     48rows = db((db.mytable.name == name) & (db.othertable.code == db.mytable.code)).select()
     49for row in rows:
     50    mytable_record = row.mytable
     51    othertable_record = row.othertable
     52}}}
     53
     54 2. '''Ask exactly for what you expect''':
     55    - if you expect only one result, then limit the search by limitby:
     56
     57{{{
     58db(db.mytable.id == id).select().first()
     59}}}
     60
     61should be:
     62
     63{{{
     64db(db.mytable.id == id).select(limitby=(0,1)).first()
     65}}}
     66
     67    - if you need only certain fields of a record, then don't ask for all:
     68
     69{{{
     70my_value = db(db.mytable.id == id).select(limitby=(0,1)).first().value
     71}}}
     72
     73should be:
     74
     75{{{
     76my_value = db(db.mytable.id == id).select(db.mytable.value, limitby=(0,1)).first().value
     77}}}
     78
     79 3. '''Don't ask twice for the same record''':
     80
     81{{{
     82my_value = db(db.mytable.id == id).select(db.mytable.value, limitby=(0,1)).first().value
     83...
     84other_value = db(db.mytable.id == id).select(db.mytable.other_value, limitby=(0,1)).first().other_value
     85}}}
     86
     87better:
     88
     89{{{
     90row = db(db.mytable.id == id).select(db.mytable.value, limitby=(0,1)).first()
     91if row:
     92    my_value = row.value
     93    other_value = row.other_value
     94}}}
     95
     96  4. '''Don't loop over queries''', if you can avoid id:
     97
     98{{{
     99for id in ids:
     100   my_record = db(mytable.id == id).select().first()
     101   ...
     102}}}
     103
     104(much) better:
     105{{{
     106records = db(mytable.id.belongs(ids)).select()
     107for record in records:
     108   ...
     109}}}
    31110
    32111=== Specific Examples ===