Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How expensive sqlite3.connect and close in SQLite?

I use connect() and cursor() for using SQLite

self.connector = sqlite3.connect(self.dbFile)
self.cursor = self.connector.cursor()

And close() for stop using it.

self.cursor.close()

How expensive (in terms of processing time) are they? Is it so expensive that it's necessary to use it only absolutely necessary? Or, is it just OK to use it multiple times in a function?

ADDED

I tested with the following simple code. proc1() uses the code that opens and closes all the time when it runs the query, and proc2() runs only once.

from sqlite import *
import timeit
import math

def proc1():
    db = SQLiteDB("./example.db", False)
    db.getOpenRunClose("SELECT * from Benchmark")
    db.getOpenRunClose("SELECT * from Benchmark")
    db.getOpenRunClose("SELECT * from Benchmark")
    db.getOpenRunClose("SELECT * from Benchmark")
    db.getOpenRunClose("SELECT * from Benchmark")
    db.getOpenRunClose("SELECT * from Benchmark")

def proc2():
    db = SQLiteDB("./example.db")
    res = db.runSQLToGetResult("SELECT * from Benchmark")
    res = db.runSQLToGetResult("SELECT * from Benchmark")
    res = db.runSQLToGetResult("SELECT * from Benchmark")
    res = db.runSQLToGetResult("SELECT * from Benchmark")
    res = db.runSQLToGetResult("SELECT * from Benchmark")
    res = db.runSQLToGetResult("SELECT * from Benchmark")
    db.close()

if __name__ == '__main__':
    t = timeit.Timer(proc1)
    count = 5000
    print t.timeit(count) / count

    t = timeit.Timer(proc2)
    count = 5000
    print t.timeit(count) / count

The result is as follows.

0.00157478599548
0.000539195966721
like image 333
prosseek Avatar asked May 13 '26 19:05

prosseek


2 Answers

Connections are fairly expensive – they correspond to opening the file – but cursors aren't very so use as many as you need[1]. What does cost is transaction starts and especially commits when there's an insert or update (or if you create a table or index, of course) even if you're in auto-commit mode. That's because the database engine has to sync the data to disk before it finishes the commit (required for a durability guarantee) and that's just plain expensive on modern hardware. (Transaction starts cost because they require doing some locking of the DB file, which can have an impact.)

Compilation of statements can also cost a bit; reuse compiled statements if possible. Of course, you should be doing that anyway. Why? It's because you should never put user data in generated SQL; not only does that lead to trouble with SQL injection vulnerabilities, but it also forces the DB engine to recompile the statement every time you run it. Compiled statements are both safer and (probably) faster too.


[1] Of course, it's silly to use more cursors than you need. That's just plain wasting time and effort.

like image 121
Donal Fellows Avatar answered May 16 '26 10:05

Donal Fellows


Connect is more than just opening a file. As soon as you run any queries against the database it has to parse out all the SQL in the sqlite_master table. Therefore time to connect is heavily dependent on the complexity of the database. Simple databases can be connected to in a few milliseconds but larger ones will take more. Ours clocks in at around 45ms (over a hundred tables, several hundred triggers).

like image 28
Samuel Neff Avatar answered May 16 '26 09:05

Samuel Neff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!