亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何提高插入的寫入速度,pymongo?

如何提高插入的寫入速度,pymongo?

慕的地8271018 2023-10-11 22:53:59
我有以下代碼將文檔插入到 MongoDB 中,問題是它非常慢,因為我無法對其進行多處理器處理,并且考慮到我必須檢查插入的每個文檔是否已經存在,我相信不可能使用批量-插入。我想知道是否有更快的方法來解決這個問題。在進行下面的分析后,我發現check record()和update_upstream()是兩個非常耗時的函數。因此優化它們會提高整體速度。任何有關如何優化以下內容的意見都將受到高度贊賞。謝謝你!import osimport pymongofrom directory import Directoryfrom pymongo import ASCENDINGfrom pymongo import DESCENDINGfrom pymongo import MongoClientfrom storage_config import StorageConfigfrom tqdm import tqdmdir = Directory()def DB_collections(collection_type):    types = {'p': 'player_stats',             't': 'team_standings',             'f': 'fixture_stats',             'l': 'league_standings',             'pf': 'fixture_players_stats'}    return types.get(collection_type)class DB():    def __init__(self, league, season, func=None):        self.db_user = os.environ.get('DB_user')        self.db_pass = os.environ.get('DB_pass')        self.MONGODB_URL = f'mongodb+srv://{self.db_user}:{self.db_pass}@cluster0-mbqxj.mongodb.net/<dbname>?retryWrites=true&w=majority'        self.league = league        self.season = str(season)        self.client = MongoClient(self.MONGODB_URL)        self.DATABASE = self.client[self.league + self.season]        self.pool = multiprocessing.cpu_count()        self.playerfile = f'{self.league}_{self.season}_playerstats.json'        self.teamfile = f'{self.league}_{self.season}_team_standings.json'        self.fixturefile = f'{self.league}_{self.season}_fixturestats.json'        self.leaguefile = f'{self.league}_{self.season}_league_standings.json'        self.player_fixture = f'{self.league}_{self.season}_player_fixture.json'        self.func = func    def execute(self):        if self.func is not None:            return self.func(self)def import_json(file):    """Imports a json file in read mode        Args:            file(str): Name of file    """    return dir.load_json(file , StorageConfig.DB_DIR)
查看完整描述

1 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

您可以使用 將檢查/插入/更新邏輯合并到單個update_one()命令中upsert=True,然后使用批量運算符,例如:


updates = []


for player in tqdm(playerstats):

    updates.append(UpdateOne({'p_id': player['p_id']}, player, upsert=True))


collection.bulk_write(updates)

最后,在 MongoDB shell 中使用以下命令檢查您的索引是否正在使用:


db.mycollection.aggregate([{ $indexStats: {} }])

并查看 accesses.ops 指標。


查看完整回答
反對 回復 2023-10-11
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號