<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 03:10:57 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-6176] Having compound indexes scans less objects during a query but takes more time than a single index</title>
                <link>https://jira.mongodb.org/browse/SERVER-6176</link>
                <project id="10000" key="SERVER">Core Server</project>
                    <description>&lt;p&gt;I am storing documents with 2 fields: &quot;date&quot; and &quot;b&quot;. For &quot;date&quot; I want to check that it is greater than or less than a certain date. For &quot;b&quot; I want to know whether or not it exists.&lt;/p&gt;

&lt;p&gt;I want to query on both fields simultaneously.&lt;/p&gt;

&lt;p&gt;I created some test data as follows:&lt;/p&gt;

&lt;p&gt;date = new ISODate();&lt;/p&gt;

&lt;p&gt;for (var i=0; i &amp;lt; 5000000; i++) {&lt;br/&gt;
  m = Math.floor(Math.random()*12);&lt;br/&gt;
  d = Math.floor(1+Math.random()*28);&lt;br/&gt;
  date.setMonth(m);&lt;br/&gt;
  date.setDate(d);&lt;br/&gt;
  db.bar.insert(&lt;/p&gt;
{&quot;date&quot;:date}
&lt;p&gt;);&lt;br/&gt;
}&lt;br/&gt;
for (var i=0; i &amp;lt; 5000000; i++) {&lt;br/&gt;
  m = Math.floor(Math.random()*12);&lt;br/&gt;
  d = Math.floor(1+Math.random()*28);&lt;br/&gt;
  date.setMonth(m);&lt;br/&gt;
  date.setDate(d);&lt;br/&gt;
  b = (Math.random() &amp;gt; 0.5);&lt;br/&gt;
  db.bar.insert(&lt;/p&gt;
{&quot;date&quot;:date,&quot;b&quot;:b}
&lt;p&gt;);&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;Then I run 2 tests.&lt;/p&gt;

&lt;p&gt;Test 1) I put an index on date only, and do a query explain.&lt;/p&gt;

&lt;p&gt;db.bar.ensureIndex(&lt;/p&gt;
{&quot;date&quot;:1}
&lt;p&gt;);&lt;br/&gt;
db.bar.find({&quot;date&quot;:{$lte:date},&quot;b&quot;:{$exists:false}}).explain();&lt;/p&gt;

&lt;p&gt;Test 2) I put a compound index on date and b, and do a query explain.&lt;/p&gt;

&lt;p&gt;db.bar.ensureIndex(&lt;/p&gt;
{&quot;date&quot;:1,&quot;b&quot;:1}
&lt;p&gt;);&lt;br/&gt;
db.bar.find({&quot;date&quot;:{$lte:date},&quot;b&quot;:{$exists:false}}).explain();&lt;/p&gt;

&lt;p&gt;Results of the tests: Having a compound index on &quot;date&quot; and &quot;b&quot; runs a bit slower, even though it scans less documents. Here is the output of the explain()s:&lt;/p&gt;

&lt;p&gt;Test 1)&lt;/p&gt;

&lt;p&gt;{&lt;br/&gt;
	&quot;cursor&quot; : &quot;BtreeCursor date_1&quot;,&lt;br/&gt;
	&quot;nscanned&quot; : 6128089,&lt;br/&gt;
	&quot;nscannedObjects&quot; : 6128089,&lt;br/&gt;
	&quot;n&quot; : 3775151,&lt;br/&gt;
	&quot;millis&quot; : 17420,&lt;br/&gt;
	&quot;nYields&quot; : 0,&lt;br/&gt;
	&quot;nChunkSkips&quot; : 0,&lt;br/&gt;
	&quot;isMultiKey&quot; : false,&lt;br/&gt;
	&quot;indexOnly&quot; : false,&lt;br/&gt;
	&quot;indexBounds&quot; : &lt;/p&gt;
{
		&quot;date&quot; : [
			[
				true,
				ISODate(&quot;2012-06-18T22:06:03.419Z&quot;)
			]
		]
	}
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Test 2)&lt;/p&gt;

&lt;p&gt;{&lt;br/&gt;
	&quot;cursor&quot; : &quot;BtreeCursor date_1_b_1&quot;,&lt;br/&gt;
	&quot;nscanned&quot; : 3775309,&lt;br/&gt;
	&quot;nscannedObjects&quot; : 3775151,&lt;br/&gt;
	&quot;n&quot; : 3775151,&lt;br/&gt;
	&quot;millis&quot; : 18100,&lt;br/&gt;
	&quot;nYields&quot; : 0,&lt;br/&gt;
	&quot;nChunkSkips&quot; : 0,&lt;br/&gt;
	&quot;isMultiKey&quot; : false,&lt;br/&gt;
	&quot;indexOnly&quot; : false,&lt;br/&gt;
	&quot;indexBounds&quot; : &lt;/p&gt;
{
		&quot;date&quot; : [
			[
				true,
				ISODate(&quot;2012-06-18T22:06:03.419Z&quot;)
			]
		],
		&quot;b&quot; : [
			[
				null,
				null
			]
		]
	}
&lt;p&gt;}&lt;/p&gt;</description>
                <environment>MacBook Pro 2010 running Mac OS X Snow Leopard</environment>
        <key id="42152">SERVER-6176</key>
            <summary>Having compound indexes scans less objects during a query but takes more time than a single index</summary>
                <type id="6" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14720&amp;avatarType=issuetype">Question</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</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="9">Done</resolution>
                                        <assignee username="aaron">Aaron Staple</assignee>
                                    <reporter username="jc">Justin</reporter>
                        <labels>
                            <label>performance</label>
                            <label>query</label>
                    </labels>
                <created>Fri, 22 Jun 2012 18:03:39 +0000</created>
                <updated>Mon, 11 Jul 2016 18:35:47 +0000</updated>
                            <resolved>Tue, 24 Jul 2012 19:12:16 +0000</resolved>
                                    <version>2.0.2</version>
                                                    <component>Querying</component>
                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="146811" author="aaron" created="Tue, 24 Jul 2012 19:12:16 +0000"  >&lt;p&gt;No prob.&lt;/p&gt;</comment>
                            <comment id="146808" author="jc" created="Tue, 24 Jul 2012 19:10:29 +0000"  >&lt;p&gt;Using the index &lt;/p&gt;
{b:1,date:1}
&lt;p&gt; worked much better. No further questions. Thanks!&lt;/p&gt;</comment>
                            <comment id="146163" author="aaron" created="Mon, 23 Jul 2012 17:15:01 +0000"  >&lt;p&gt;Hi Justin,&lt;/p&gt;

&lt;p&gt;Do you have any further questions?&lt;/p&gt;</comment>
                            <comment id="135940" author="aaron" created="Sat, 23 Jun 2012 01:10:38 +0000"  >&lt;p&gt;Hi Justin,&lt;/p&gt;

&lt;p&gt;A couple of points:&lt;/p&gt;

&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;With the compound index you tried, the indexing code will have to identify every date less than the date in your query, and for each date skip the non null values of &apos;b&apos;.  There may not be significant performance gains in doing this vs using a single key index for your data distribution.  You may get better performance with the index 
{b:1,date:1}
&lt;p&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;I just want to check - did you remove the single key index before you ran test #2?  If not that could affect the outcome.  Also, the tests would probably be a bit more representative if you used hint() to hint the desired index.&lt;/li&gt;
&lt;/ul&gt;
</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                <customfield id="customfield_10050" key="com.atlassian.jira.toolkit:comments">
                        <customfieldname># Replies</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>4.0</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_10055" key="com.atlassian.jira.ext.charting:firstresponsedate">
                        <customfieldname>Date of 1st Reply</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>Sat, 23 Jun 2012 01:10:38 +0000</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10052" key="com.atlassian.jira.toolkit:dayslastcommented">
                        <customfieldname>Days since reply</customfieldname>
                        <customfieldvalues>
                                        11 years, 30 weeks, 1 day 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>
                            11 years, 30 weeks, 1 day ago
                        </customfieldvalues>
                    </customfield>
                                                                                                                        <customfield id="customfield_10000" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Old_Backport</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10000"><![CDATA[No]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_10051" key="com.atlassian.jira.toolkit:participants">
                        <customfieldname>Participants</customfieldname>
                        <customfieldvalues>
                                        <customfieldvalue>aaron</customfieldvalue>
            <customfieldvalue>jc</customfieldvalue>
    
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                        <customfield id="customfield_14254" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Product Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|hrnyzz:</customfieldvalue>

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

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>23075</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|ht0mrb:</customfieldvalue>

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