2010-12-02 42 views
1

我需要在Django中迭代大集合(3 * 10^6個元素)來做一些不能用單個SQL語句完成的分析。迭代在Django中的大集合 - 緩存問題

  • 是否有可能在django中關閉集合緩存? (緩存所有的數據是不可接受的數據有大約0.5GB)
  • 是否有可能使django fetch集合成塊?它似乎試圖預取整個集合到內存中,然後遍歷它。我認爲,觀察執行速度:
    • iter(Coll.objects.all()).next() - 這需要永遠
    • iter(Coll.objects.all()[:10000]).next() - 這需要不到一秒鐘

回答

1

使用QuerySet.iterator()對結果走加載它們全部代替第一。

+0

但它沒有接縫的工作。但它可能是django中的一個bug。 – 2010-12-02 09:58:26

1

它認爲問題是由不支持在塊中讀取的數據庫後端(sqlite)引起的。 我已經使用sqlite作爲數據庫將被刪除後,我做了所有的計算,但似乎sqlite是不好,即使這樣。

以下是我在sqlite的後端的Django的源代碼已經發現:

class DatabaseFeatures(BaseDatabaseFeatures): 
    # SQLite cannot handle us only partially reading from a cursor's result set 
    # and then writing the same rows to the database in another cursor. This 
    # setting ensures we always read result sets fully into memory all in one 
    # go. 
    can_use_chunked_reads = False