<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 03:40:45 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[SERVER-16350] Wrong index choice when request contains _id in query + sort command</title>
                <link>https://jira.mongodb.org/browse/SERVER-16350</link>
                <project id="10000" key="SERVER">Core Server</project>
                    <description>&lt;p&gt;Hello, &lt;/p&gt;

&lt;p&gt;I encountered a strange behavior in Mongo 2.6.x. i am using Mongoid in ruby talk to MongoDB and Mongoid allows having default sort command. When I run the following command :&lt;/p&gt;

&lt;p&gt;Person.find(&quot;someindex&quot;) in Mongoid, it is translated into &lt;br/&gt;
db.persons.find(&lt;/p&gt;
{deleted_at: null, _id: ObjectId(&quot;someindex&quot;)}
&lt;p&gt;).sort(created_at: -1)&lt;/p&gt;

&lt;p&gt;Mongoid is not really smart by generating such query because we know we will get either 0 or 1 result, so the sort operator does not make sense. &lt;/p&gt;

&lt;p&gt;However, I&apos;ve found cases where MongoDB is even less smart and caches the wrong index, including the created_at field. &lt;/p&gt;

&lt;p&gt;I would expect MongoDB to automatically use the _id index whenever it is present in the query, no matter other fields that are present in the query, in partcular sort commands.&lt;/p&gt;

&lt;p&gt;I already had a discussion about this on google group:&lt;br/&gt;
&lt;a href=&quot;https://groups.google.com/forum/#!searchin/mongodb-user/rpechayr/mongodb-user/-btb_43ONfk/w0dMlECPInIJ&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://groups.google.com/forum/#!searchin/mongodb-user/rpechayr/mongodb-user/-btb_43ONfk/w0dMlECPInIJ&lt;/a&gt;&lt;/p&gt;</description>
                <environment></environment>
        <key id="171748">SERVER-16350</key>
            <summary>Wrong index choice when request contains _id in query + sort command</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="2" iconUrl="https://jira.mongodb.org/images/icons/priorities/critical.svg">Critical - P2</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="3">Duplicate</resolution>
                                        <assignee username="david.storch@mongodb.com">David Storch</assignee>
                                    <reporter username="rpechayr">Romain Pechayre</reporter>
                        <labels>
                    </labels>
                <created>Sat, 29 Nov 2014 12:32:47 +0000</created>
                <updated>Thu, 25 Jun 2015 18:42:10 +0000</updated>
                            <resolved>Wed, 3 Dec 2014 17:19:53 +0000</resolved>
                                    <version>2.6.5</version>
                                                    <component>Index Maintenance</component>
                    <component>Querying</component>
                                        <votes>1</votes>
                                    <watches>7</watches>
                                                                                                                <comments>
                            <comment id="777515" author="david.storch" created="Wed, 3 Dec 2014 17:18:45 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=rpechayr&quot; class=&quot;user-hover&quot; rel=&quot;rpechayr&quot;&gt;rpechayr&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Thanks for the bug report. This appears to be a duplicate of &lt;a href=&quot;https://jira.mongodb.org/browse/SERVER-15802&quot; title=&quot;Query optimizer should always use equality predicate over unique index when possible&quot; class=&quot;issue-link&quot; data-issue-key=&quot;SERVER-15802&quot;&gt;&lt;del&gt;SERVER-15802&lt;/del&gt;&lt;/a&gt;. I am going to resolve this ticket as a dup, but please watch &lt;a href=&quot;https://jira.mongodb.org/browse/SERVER-15802&quot; title=&quot;Query optimizer should always use equality predicate over unique index when possible&quot; class=&quot;issue-link&quot; data-issue-key=&quot;SERVER-15802&quot;&gt;&lt;del&gt;SERVER-15802&lt;/del&gt;&lt;/a&gt; for progress updates. As described in &lt;a href=&quot;https://jira.mongodb.org/browse/SERVER-15802?focusedCommentId=762455&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-762455&quot; class=&quot;external-link&quot; rel=&quot;nofollow&quot;&gt;this comment&lt;/a&gt; MongoDB&apos;s query system is likely caching the query plan using non-&lt;tt&gt;_id&lt;/tt&gt; index when that other index can more quickly return an answer of &quot;zero matching documents&quot; than {&lt;tt&gt;_id: 1&lt;/tt&gt;}.&lt;/p&gt;

&lt;p&gt;Our server development team has several open improvement tickets that could fix this and similar problems with caching bad plans:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;https://jira.mongodb.org/browse/SERVER-15225&quot; title=&quot;CachedPlanStage should execute for trial period and re-plan if query performs poorly&quot; class=&quot;issue-link&quot; data-issue-key=&quot;SERVER-15225&quot;&gt;&lt;del&gt;SERVER-15225&lt;/del&gt;&lt;/a&gt;: This change would cause the query planner to evict a plan cache entry and re-plan the query as soon as it detects that a plan taken from the cache is performing poorly.&lt;/li&gt;
	&lt;li&gt;An &quot;index pruning&quot; strategy along the lines of &lt;a href=&quot;https://jira.mongodb.org/browse/SERVER-13211&quot; title=&quot;Optimal index not chosen for query plan when many indexes match same prefix&quot; class=&quot;issue-link&quot; data-issue-key=&quot;SERVER-13211&quot;&gt;&lt;del&gt;SERVER-13211&lt;/del&gt;&lt;/a&gt;. That is, we could have logic in the index selection stage which detects that no index other than {&lt;tt&gt;&amp;#95;id: 1&lt;/tt&gt;} is necessary to answer this query. In general, when we have an equality predicate over a unique index, the query system should be able to throw out any plan which will &lt;em&gt;not&lt;/em&gt; execute as a single key-value lookup in a unique index.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I hope this response adequately addresses the concerns brought forth in this ticket. Please feel free to reach out with any further questions or comments.&lt;/p&gt;

&lt;p&gt;Best,&lt;br/&gt;
Dave&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10010">
                    <name>Duplicate</name>
                                            <outwardlinks description="duplicates">
                                        <issuelink>
            <issuekey id="165316">SERVER-15802</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                            <issuelinktype id="10012">
                    <name>Related</name>
                                            <outwardlinks description="related to">
                                        <issuelink>
            <issuekey id="120781">SERVER-13211</issuekey>
        </issuelink>
            <issuelink>
            <issuekey id="157798">SERVER-15225</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                    </issuelinks>
                <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                <customfield id="customfield_10050" key="com.atlassian.jira.toolkit:comments">
                        <customfieldname># Replies</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1.0</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_10055" key="com.atlassian.jira.ext.charting:firstresponsedate">
                        <customfieldname>Date of 1st Reply</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>Sat, 29 Nov 2014 17:14:58 +0000</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10052" key="com.atlassian.jira.toolkit:dayslastcommented">
                        <customfieldname>Days since reply</customfieldname>
                        <customfieldvalues>
                                        9 years, 11 weeks ago
    
                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_18254" key="com.onresolve.jira.groovy.groovyrunner:scripted-field">
                        <customfieldname>Dependencies</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue><![CDATA[]]></customfieldvalue>


                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_10057" key="com.atlassian.jira.toolkit:lastusercommented">
                        <customfieldname>Last comment by Customer</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>true</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                            <customfield id="customfield_10056" key="com.atlassian.jira.toolkit:lastupdaterorcommenter">
                        <customfieldname>Last commenter</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>ramon.fernandez@mongodb.com</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_11151" key="com.atlassian.jira.toolkit:LastCommentDate">
                        <customfieldname>Last public comment date</customfieldname>
                        <customfieldvalues>
                            9 years, 11 weeks ago
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                    <customfield id="customfield_10032" key="com.atlassian.jira.plugin.system.customfieldtypes:select">
                        <customfieldname>Operating System</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10026"><![CDATA[ALL]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_10051" key="com.atlassian.jira.toolkit:participants">
                        <customfieldname>Participants</customfieldname>
                        <customfieldvalues>
                                        <customfieldvalue>david.storch@mongodb.com</customfieldvalue>
            <customfieldvalue>rpechayr</customfieldvalue>
    
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                        <customfield id="customfield_14254" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Product Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|hrkwa7:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hs4d27:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>150402</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                            <customfield id="customfield_23361" key="com.onresolve.jira.groovy.groovyrunner:scripted-field">
                        <customfieldname>Requested By</customfieldname>
                        <customfieldvalues>
                                

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <customfield id="customfield_10053" key="com.atlassian.jira.ext.charting:timeinstatus">
                        <customfieldname>Time In Status</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <customfield id="customfield_22870" key="com.onresolve.jira.groovy.groovyrunner:scripted-field">
                        <customfieldname>Triagers</customfieldname>
                        <customfieldvalues>
                                

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_14350" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>serverRank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|hsgfl3:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                    </customfields>
    </item>
</channel>
</rss>