Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to add estimated_document_count implementation #678

Open
iYasha opened this issue Aug 22, 2023 · 0 comments
Open

Proposal to add estimated_document_count implementation #678

iYasha opened this issue Aug 22, 2023 · 0 comments

Comments

@iYasha
Copy link

iYasha commented Aug 22, 2023

I work with a lot of data in MongoDB. And I got a problem with performance while I worked with count. I even wrote some tests to demonstrate a difference between count that used aggregation and estimated_document_count that used metadata of collection to return the estimated count. My proposal is to add estimated_document_count implementation to the library. If you think this is a good idea, let me know and I send you PR as soon as possible.

My tests

def djongo_count_test():
    start_time = time.time()
    res = Model.objects.count()
    print("--- djongo_count_test %s seconds | count %s ---" % (time.time() - start_time, res))


def estimated_count_test():
    start_time = time.time()
    if connections["mongo_db"].connection is None:
        connections["mongo_db"].connect()
    res = connections["mongo_db"].connection.collection_name.estimated_document_count()
    print("--- estimated_count_test %s seconds | count %s ---" % (time.time() - start_time, res))

Results of testing

--- estimated_count_test 0.01596400260925293 seconds | count 1074517 ---
--- djongo_count_test 0.32147812843322754 seconds | count 1074517 ---

P.S. I ran these tests only on my working machine, and I know that depending on hardware these tests result will be different, but the general point of this issue will be the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant