Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:30:25+00:00 2026-05-31T08:30:25+00:00

This query takes very much time in my application. Database contains about 2 million

  • 0

This query takes very much time in my application. Database contains about 2 million records.

    $results = $queryBuilder
        ->field('extra.targetPlayerId')->exists(FALSE)
        ->field('permission')->equals('public')
        ->field('time')->gt($weekEndTime) // variable is timestamp
        ->field('time')->lte($startTime) // variable is timestamp
        ->map(
            'function() {
                var total = 0;
                if (this.comments) {
                    total += this.comments.length;
                }
                if (this.likes) {
                    total += this.likes.length;
                }                    
                if (total > 0) {
                    emit(this.playerId, total);
                }
            }'
        )
        ->reduce(
            'function(key, values) {
                var total = 0;
                for (value in values) {
                    total += values[value];
                };
                return total;
            }'
        )->getQuery()->execute();

How can i optimize this query ? Can you give me suggestion for indexes ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:30:26+00:00Added an answer on May 31, 2026 at 8:30 am
    ->field('extra.targetPlayerId')->exists(FALSE)
    

    That right there is probably your biggest obstacle to making the query faster.

    from the docs:

    Before v2.0, $exists is not able to use an index. Indexes on other fields are still used.
    $exists is not very efficient even with an index, and esp. with {$exists:true} since it will effectively have to scan all indexed values.

    Perhaps consider adding a boolean field such that you can test for true/false instead of exists – and include that field in the index.

    Or even move that particular condition into your map function:

    function() {
        var total = 0;
    
        if (!this.extra || !this.extra.targetPlayerId) {
            return; //bail - count nothing
        }
    
        if (this.comments) {
            total += this.comments.length;
        }
        if (this.likes) {
            total += this.likes.length;
        }                    
        if (total > 0) {
            emit(this.playerId, total);
        }
    }
    

    Indexing isn’t a black and white answer, for example, if you have a lot of data:

    {
        time: 1,
        permission: 1,
        extra.hasTargetPlayer: 1
    }
    

    would probably work quite well – since it would permit mongo to focus on the right date range as a first criteria.

    Aside from that it may be more appropriate to simply change your data schema. Are you storing the result of that query?

    i.e. query once and store the result, so that for any subsequent requests you can just do:

    db.appstats.findOne({_id: "201152"})
    

    where _id is a year and weeknumber. If you are generating stats by week, every week – you can do the heavy query on a schedule (cron) and therefore users aren’t affected by the speed of the query to actually calculate the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This query takes about 52 seconds to complete. It appears 82% of the time
I have a MySql query that take a very long time to run (about
I have a query in my MVC application which takes about 20 seconds to
This query takes ~4 seconds to complete: SELECT DISTINCT resources_resource.id, resources_resource.heading, resources_resource.name, resources_resource.old_name, resources_resource.clean_name,
I'm having a problem with this query that takes several seconds to complete. I
I have a query that looks like this: it takes a list of IDs
i'm doing a query like this one and it takes 6 seconds to complete
I have a very simple query that is not much more complicated than: select
I am programming a very simple application to store results of soccer matches, and
Why does this MySQL query take forever (and never finishes) on a table that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.