I use the sqlite3 module in Python, but find it incredibly slow for a specific SELECT query regarding running a query in sqlite3 in a command shell. I will start by saying that both versions are the same 3.7.17.
My request
SELECT r.ID, r.Date FROM my_table r WHERE r.Date IN (SELECT Date FROM my_table WHERE ID = r.ID GROUP BY Date LIMIT 2);
Python code
con = lite.connect(path_to_database) cur = con.cursor() with con: cur.execute(sql_query)
where sql_query is a string variable containing the original query.
I guess the problem is optimizing the IN subquery.
Performance details: my_table contains 167,000 entries, a query in the shell takes ~ 10 seconds, a query in Python takes> 5 minutes (I stopped it when it went this far).
Currently, since this is creating a table, I am just copying and pasting the code into the shell as a workaround, how can I fix this so that I can run the query from Python?
Addition
When I run EXPLAIN QUERY PLAN , I get the following
Shell:
0 0 0 SCAN TABLE PIT_10_Days AS r (~500000 rows) 0 0 0 EXECUTE CORRELATED LIST SUBQUERY 1 1 0 0 SEARCH TABLE PIT_10_Days USING AUTOMATIC C 1 0 0 USE TEMP B-TREE FOR GROUP BY
Python:
0 0 TABLE PIT_10_Days AS r 0 0 TABLE PIT_10_Days
I'm not sure if the difference is a problem when getting an EXPLAIN QUERY PLAN in Python or if this is actually the problem itself.
source share