You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ ./python -m timeit -s "import sqlite3; con = sqlite3.connect(':memory:'); con.row_factory = sqlite3.NamedTupleRow; con.execute('create table t (a, b)')" -s "for i in range(100): con.execute('insert into t values (1, 2)')" -- "con.execute('select * from t').fetchall()"
100 loops, best of 3: 2.74 msec per loop
But it is still 3 times slower than sqlite3.Row.
YoSTEALTH commented on Jan 21, 2015
YoSTEALTHmannequin
Mannequin
More actions
note: sqlite_namedtuplerow.patch _cache method conflicts with attached database with say common table.column name like "id"
Using namedtuple method over sqlite3.Row was a terrible idea for me. I thought namedtuple is like tuple so should be faster then dict! wrong. I wasted 2 days change my work to namedtuple and back to sqlite3.Row, the speed difference on my working project was:
$ python ../cpython-stuff/bench-row.py
.....................
default: Mean +- std dev: 2.48 us +- 0.02 us
.....................
row: Mean +- std dev: 2.60 us +- 0.02 us
.....................
named: Mean +- std dev: 3.71 us +- 0.03 us
Following up Serhiy's suggestion to add a cursor.fieldnames() method shows a small improvement:
$ python ../cpython-stuff/bench-row.py
.....................
default: Mean +- std dev: 2.48 us +- 0.02 us
.....................
row: Mean +- std dev: 2.60 us +- 0.02 us
.....................
named: Mean +- std dev: 3.26 us +- 0.05 us
These years, I would prefer a dataclass conversion rather than a named tuple.
I'm curious why?
IMO, named tuples are a better fit. A dataclass factory would be considerably slower than using named tuples1, and it does not offer subscripting nor slicing out of the box.
benchmark
$ python ../cpython-stuff/bench-row.py
.....................
default: Mean +- std dev: 1.35 us +- 0.02 us
.....................
row: Mean +- std dev: 1.44 us +- 0.03 us
.....................
namedtuple: Mean +- std dev: 2.00 us +- 0.04 us
.....................
dataclass: Mean +- std dev: 1.92 us +- 0.08 us
Footnotes
I re-ran the benchmark with an optimised build, and now it performed exactly like the namedtuple factory. ↩
I don’t like using named tuples after all! I prefer having attribute access only, it’s more explicit than tuple positions.
Nowadays I see named tuples as a great thing to add attributes to some data structure that used to be a tuple only, but as a tool to define new lightweight data classes I prefer the dataclasses module.
FYI, I've added a namedtuple row factory as an example in the proposed "row factory how-to" in gh-99507. Perhaps it is suffictient to keep this as an example/recipe in the docs instead of adding it as a new feature.
The sqlite3 docs now include a "row factory how-to" that includes a recipe for a simple namedtuple row factory (though without the class creation cache optimisation). I'm not sure it is worth it to add the namedtuple row factory as a feature, thought; some users want a named tuple, some want a dataclass, some want something else.
In the light of the docs update, I now suggest to close this feature request.
Activity
serhiy-storchaka commentedon Jan 11, 2015
Here is faster implementation.
But it is still 3 times slower than sqlite3.Row.
YoSTEALTH commentedon Jan 21, 2015
note: sqlite_namedtuplerow.patch _cache method conflicts with attached database with say common table.column name like "id"
Using namedtuple method over sqlite3.Row was a terrible idea for me. I thought namedtuple is like tuple so should be faster then dict! wrong. I wasted 2 days change my work to namedtuple and back to sqlite3.Row, the speed difference on my working project was:
namedtuple 0.035s/result
sqlite3.Rows 0.0019s/result
for(speed test) range: 10000
namedtuple 17.3s
sqlite3.Rows 0.4s
My solution was to use sqlite3.Row (for speed) but to get named like usage by convert dict keys() with setattr names:
Usage:
Now i can use:
and handy dict methods for dash column names:
Now working project speed:
sqlite3.Rows 0.0020s/result
for(speed test) range: 10000
sqlite3.Rows 0.8s with dict2named converting
This i can work with, tiny compromise in speed with better usage.
erlend-aasland commentedon Jun 2, 2021
See also bpo-39170
rhettinger commentedon Jun 4, 2021
FWIW, namedtuple speed improved considerably since these posts were made. When I last checked, their lookup speed was about the same as a dict lookup.
See: https://docs.python.org/3/whatsnew/3.9.html#optimizations
intellimath commentedon Aug 5, 2021
Instead of using cache, maybe better to use mutable default argument?
For example:
Seem it should add less overhead.
erlend-aasland commentedon Jul 17, 2022
FTR, here's a benchmark using @serhiy-storchaka's approach w/LRU cache:
Lib/sqlite3/dbapi2.py patch
bench-row.py
Following up Serhiy's suggestion to add a
cursor.fieldnames()method shows a small improvement:merwok commentedon Jul 17, 2022
These years, I would prefer a dataclass conversion rather than a named tuple.
erlend-aasland commentedon Jul 18, 2022
I'm curious why?
IMO, named tuples are a better fit. A dataclass factory
would be considerably slower than using named tuples1, and it does not offer subscripting nor slicing out of the box.benchmark
Footnotes
I re-ran the benchmark with an optimised build, and now it performed exactly like the namedtuple factory. ↩
merwok commentedon Jul 18, 2022
I don’t like using named tuples after all! I prefer having attribute access only, it’s more explicit than tuple positions.
Nowadays I see named tuples as a great thing to add attributes to some data structure that used to be a tuple only, but as a tool to define new lightweight data classes I prefer the dataclasses module.
erlend-aasland commentedon Jul 19, 2022
More importantly; is it worth it to pursue this issue further? Is the performance acceptable?
merwok commentedon Jul 19, 2022
I think that named tuples still make sense for a row factory. But I am the wrong person to evaluate performance numbers.
erlend-aasland commentedon Jul 20, 2022
An alternative approach is simply to add attribute access to sqlite3.Row.
erlend-aasland commentedon Nov 15, 2022
FYI, I've added a namedtuple row factory as an example in the proposed "row factory how-to" in gh-99507. Perhaps it is suffictient to keep this as an example/recipe in the docs instead of adding it as a new feature.
erlend-aasland commentedon Nov 25, 2022
The sqlite3 docs now include a "row factory how-to" that includes a recipe for a simple
namedtuplerow factory (though without the class creation cache optimisation). I'm not sure it is worth it to add thenamedtuplerow factory as a feature, thought; some users want a named tuple, some want a dataclass, some want something else.In the light of the docs update, I now suggest to close this feature request.