Is it normal that sqlite.fetchall() is so slow?

前端 未结 1 579
忘掉有多难
忘掉有多难 2020-12-18 14:32

I have an sql query that selects from two inner joined tables. The execution of the select statement takes about 50 seconds. However, the fetchall() takes 788 seconds and it

相关标签:
1条回答
  • 2020-12-18 15:00

    By default fetchall() is as slow as looping over fetchone() due to the arraysize of the Cursor object being set to 1.

    To speed things up you can loop over fetchmany(), but to see a performance gain, you need to provide it with a size parameter bigger than 1, otherwise it'll fetch "many" by batches of arraysize, i.e. 1.

    It is quite possible that you can get the performance gain simply by raising the value of arraysize, but I have no experience doing this, so you may want to experiment with that first by doing something like:

    >>> import sqlite3
    >>> conn = sqlite3.connect(":memory:")
    >>> cu = conn.cursor()
    >>> cu.arraysize
    1
    >>> cu.arraysize = 10
    >>> cu.arraysize
    10
    

    More on the above here: http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.fetchmany

    0 讨论(0)
提交回复
热议问题