Created
May 8, 2022 22:06
-
-
Save vhxs/be506d1900da3daa517c9ce270368eae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
import random | |
import time | |
mongo_client = MongoClient() | |
query1 = 0 | |
query2 = 0 | |
j = 0 | |
while True: | |
j += 1 | |
# generate random ints | |
num_ints = random.randint(10, 2000) | |
ints = [random.randint(0, 1000000) for _ in range(num_ints)] | |
time1_before = time.time() | |
res1 = mongo_client['test']['index'].find({"_id": {"$in": ints}}) | |
time1_after = time.time() | |
query1 += time1_after - time1_before | |
doc_ids = set() | |
for d in res1: | |
doc_ids.update(d["docs"]) | |
print(len(doc_ids)) | |
time2_before = time.time() | |
res2 = mongo_client['test']['with_index'].find({"ints": {"$in": ints}}) | |
time2_after = time.time() | |
query2 += time2_after - time2_before | |
print(len(list(res2))) | |
print(f"With an explicit index: {query1 / j}") | |
print(f"With an implicit index: {query2 / j}") | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment