title: MongoDB query speed: pymongo
body: <p>In my python program, I have a mongo query to select items which uses an index. I'm using <strong>mongo Atlas</strong>. when I run the query in the DB, i get the results within the DB, results come almost instantaneously. Here are some stats and the query:</p>
  <pre><code>db.properties.find({Address: /^208 S/},{FullAddress:1, Key:1, PropertyLatitude:1, PropertyLongitude:1} ).limit(10)
  
  inputStage: {
            stage: 'FETCH',
            nReturned: 10,
            executionTimeMillisEstimate: 0,
            works: 10,
            advanced: 10,
            needTime: 0,
            needYield: 0,
            saveState: 0,
            restoreState: 0,
            isEOF: 0,
            docsExamined: 10,
            alreadyHasObj: 0,
            inputStage: {
              stage: 'IXSCAN',
              nReturned: 10,
              executionTimeMillisEstimate: 0,
  </code></pre>
  <p>I implemented the query in <strong>pymongo</strong> and deployed the system in Azure. DB connection setup is done at the program startup:</p>
  <pre><code>client = MongoClient(application.config['DB_URL'])
  db = client[application.config['MONGODB_DB']]
  </code></pre>
  <p>When did the first search it takes around 1500ms to return the result, but the subsequent queries takes around 300ms. I do understand when the query runs for the first time, the query planner has to do an analysis, select the best plan to do the execution, and the subsequent queries will be faster as the plan is already selected. My question is, this time difference is significant to the user, in terms of UX.</p>
  <p>Is there a way to fix this (not doing a secret first search of course) meaning either:</p>
  <ol>
  <li>reduce the execution time difference between the first and the second queries</li>
  <li>speedup the first query</li>
  </ol>