[pyMongo]insert_many的Bulkwrite實現機制

在SQL中,insert many的操做可能會出現插入數據量過大的問題。html

假設我構造了一個128M的insert語句,SQL或者driver是如何處理的?MySQL Driver對insert語句的數據量大小限制是多少?python

翻閱了相關的文檔,並無發現明確的答案。mongodb

僅查到配置項中有一個insert_buffer的配置項,默認項是8M。數據庫

猜想當insert語句中的數據過多,driver會循環處理數據,每當數據塊達到8M時,會自動執行commit操做,進程suspend,等待數據庫操做結束後,再繼續讀入數據。api

 

pyMongo對這個問題的操做解釋的十分明確。app

pyMongo在實現BulkWrite操做時,會自動將數據劃分紅小塊進行插入,避免插入數據過大的問題。htm

具體可看pyMongo對BulkWrite作的說明:http://api.mongodb.com/python/current/examples/bulk.htmlblog

"PyMongo will automatically split the batch into smaller sub-batches based on the maximum message size accepted by MongoDB, supporting very large bulk insert operations."進程

在網上看到有人將數據分塊後進行循環插入,即:文檔

buffer = []
for item in data:
    buffer.append(item)
    if len(buffer) == 20000:
        <mgoclient>.insert_many(buffer)
        buffer =[]  

我不肯定MongoDB自己在提供Driver API的時候有沒有考慮buffer過大的處理問題。

不過若是使用pyMongo來鏈接Mongo的話,本身作數據拆分的步驟是多餘的。

相關文章
相關標籤/搜索