Is there the possibility to do something like this inside the SQL-Query? Maybe provide a list as input-argument?
The dates I want are consecutive, but not all dates exist in the database. If a date doesn’t exist, the result should be “None”.
dates = [dt.datetime(2008,1,1), dt.datetime(2008,1,2), dt.datetime(2008,1,3), dt.datetime(2008,1,4), dt.datetime(2008,1,5)]
id = "361-442"
result = []
for date in dates:
curs.execute('''SELECT price, date FROM prices where date = ? AND id = ?''', (date, id))
query = curs.fetchall()
if query == []:
result.append([None, arg])
else:
result.append(query)
To perform all the work in sqlite, you could use a LEFT JOIN to fill in missing prices with
None:However, this solution requires forming a (potentially) huge string in Python in order to create a temporary table of all desired dates. It is not only slow (including the time it takes sqlite to create the temporary table) it is also brittle: If
len(date)is greater than about 500, then sqlite raisesYou might be able to get around this if you already have all the desired dates in some other table. Then you could replace the ugly “UNION ALL” SQL above with
something like
While this is an improvement, my timeit tests (see below) show that doing part of the work in Python is still faster:
Doing part of the work in Python:
If you know that the dates are consecutive and can therefore be expressed as a range, then:
Otherwise, if the dates are arbitrary then:
To form the
resultlist withNoneinserted for missing prices, you could form adictout of(date,price)pairs, and use thedict.get()method tosupply the default value
Nonewhen thedatekey is missing:Note to form the
dictas a mapping from dates to prices, I swapped the order ofdateandpricein the SQL queries.Timeit tests:
I compared these three functions:
rowsdefined the data which was INSERTed into thepricetable.Timeit tests results:
Running timeit like this:
produced these benchmarks:
using_python_dictis about 2.3 times faster thanusing_sqlite_dates. Even if we increase the total number of dates to 10000, the speed ratio remains the same:Conclusion: shifting all the work into sqlite is not necessarily faster.