sách gpt4 ai đã đi

python - 何时更新 mongo 数据库索引

In lại 作者:太空宇宙 更新时间:2023-11-03 17:58:39 26 4
mua khóa gpt4 Nike

所以我预计在不久的将来会有大约 2000 个包含 10,000-100,000 个文档的集合,并且我正在尝试找出如何构建索引。从基本角度来看,如何执行此操作似乎非常简单,但何时运行重新索引却让我感到困惑。因此,假设我有这个函数,并且这会创建我需要的所有索引:

def ensure_indexes(self):
collections = get_collections()
for coll in collections:
coll.ensure_index([('time_stamp', pymongo.DESCENDING])
coll.ensure_index([('raw_value', pymongo.DESCENDING])
coll.ensure_index([('time_stamp', pymongo.DESCENDING, ('raw_value', pymongo.DESCENDING])

白天会有很多数据库更新,并且会有一些人查询。我是否应该创建一个 cron 作业在夜间运行上述功能,而没有多少人会在集合中插入新文档?如果人们查询数据库并且集合已更新但索引未更新,则该查询响应是否不包含最近添加的文档?或者新添加的文档是否会包含在索引中?

1 Câu trả lời

一般情况下不需要重建索引,只需要创建一次索引,参见MongoDB FAQ :

Should you run ensureIndex() after every insert?¶

No. You only need to create an index once for a single collection. After initial creation, MongoDB automatically updates the index as data changes.

While running ensureIndex() is usually ok, if an index doesn’t exist because of ongoing administrative work, a call to ensureIndex() may disrupt database availability. Running ensureIndex() can render a replica set inaccessible as the index creation is happening. See Build Indexes on Replica Sets.

如果发生损坏并且您需要再次构建索引,请使用db.collection.reIndex(),您可以从HEREĐọc thêm

关于python - 何时更新 mongo 数据库索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28078771/

26 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com