Optimizing pysqlite/sqlite3 databases and queries : Part I (notes/outline for 2009 Oct 12 presentation to TuPLE — edited post-presentation version)


Introduction

sqlite, pysqlite are included with a standard Python distribution >=2.5
in standard libraries. Presumably this makes them very easy to install. :-)

When importing pysqlite from within the python standard library,
it’s called sqlite3.

 import sqlite3 

Caveats

We might even get to something about convenience or savings in development time,
because that permits you more time/effort for optimizing should you need it. :-)

Important References

Performance Testing Wants To Trick You

Warnings about performance testing!

Stuff most (or all) of you know about in SQL in general, a quick overview/review

Some Miscellany of SQLite Optimization

Order of criteria in a query. YES RLY.

Joins can be executed (planned?) as equivalent of ‘nested loops’

(Notes of mine from a project using SQLite, 2009-08-10)
I leaned towards conclusion that the
DISGUSTING TIME-LENGTHENING [EXPURGATED] SCALING seems in line with
join implementation by ‘nested loops’ discussed by
http://www.sqlite.org/cvstrac/wiki?p=QueryPlans  .
Logically it seemed I could do MUCH better
even by doing subqueries manually in separate queries
and feeding results thereof into later query/ies
with a long in (?, ?, ?, ?, ….) clause.

Native types of db

Some weird query behavior I’ve seen:

AUDIENCE PARTICIPATION?

Transactions and Commits

Explicitly combining many queries into a single transaction can speed awesomely. Transactions have (a) weird difference(s) in different versions of pysqlite.
            
                    # ^ Isolation_level is here non-default,
                    #    to support explicit multi-query transactions.
                    #      Actually pretty important!
                    #   See e.g.:
                    #      http://bugs.python.org/issue4995
                    #        (people say or suggest
                    #           that this is a Python 2.5.2 issue, *not* 2.6.1)
                    #      Google [python sqlite3 begin transaction insert error]
                    #
                    #   On transactions in general:
                    #     http://stackoverflow.com/questions/564298/sqlite-transaction-doesnt-work-as-exprected
                    #     [sic]
                    #
                    #   On sqlite3 isolation_level and IMPLICIT TRANSACTIONS:
                    #     http://xania.org/200711/python-sqlite-gotcha explains:
                    #     ["Before every INSERT/UPDATE/DELETE/REPLACE statement,
                    #      Python's sqlite3 implicitly opens a transaction."
                    #      But you can prevent this by using
                    #      isolation_level=None .]
                    #
                    # BUT MUCH OF THIS COMMENTARY CRAP ABOVE ALL TOGETHER
                    #   IS STILL TOO MUCH LIKE 'CARGO CODING' IMHO.      
  

Database Locking

Conclusions

The End