<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:40:22 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>[CSHARP-1679] Count using LINQ is slower than collection Count method</title>
                <link>https://jira.mongodb.org/browse/CSHARP-1679</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;h4&gt;&lt;a name=&quot;RevisedDescription&quot;&gt;&lt;/a&gt;Revised Description&lt;/h4&gt;

&lt;p&gt;This ticket is the result of an inquiry regarding the performance of counting the items in a collection using LINQ. There are two ways one could count the items in a collection:&lt;/p&gt;

&lt;h5&gt;&lt;a name=&quot;UsingthecollectionCountmethod&quot;&gt;&lt;/a&gt;Using the collection Count method&lt;/h5&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var count = collection.Count(new BsonDocument());&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;h5&gt;&lt;a name=&quot;UsingtheLINQCountmethod&quot;&gt;&lt;/a&gt;Using the LINQ Count method&lt;/h5&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var count = collection.AsQueryable().Count();&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;The first way is much faster. The reason is that the first method results in the count command being used, which is optimized at the server (for the case where no filter is supplied). In that case it can return the number of items in the collection in O(1) time from the collection metadata.&lt;/p&gt;

&lt;p&gt;The second way, like all LINQ queries, is translated to an aggregation framework pipeline. The aggregation framework does not have an equivalent to the count command, so we translate the query to the best possible pipeline we know of. As it turns out, the LINQ implementation ends up having to do a full collection scan to count the items.&lt;/p&gt;

&lt;p&gt;So for this particular case of Count (with no filter), the collection method is &lt;b&gt;much&lt;/b&gt; faster than the LINQ method.&lt;/p&gt;

&lt;p&gt;In general LINQ methods perform well; this is a special case. Although it is worth pointing out that LINQ queries will always be somewhat slower than hand crafted queries using the collection methods. One reason is that LINQ queries must be translated at run time to the equivalent aggregation framework pipeline. Another reason is that when hand crafting queries you can often use optimizations that are outside the realm of what a general purpose system like LINQ might be able to apply.&lt;/p&gt;

&lt;p&gt;It is possible that we might optimize this one special case LINQ query, I&apos;m not sure. But there is a very easy workaround in the meantime.&lt;/p&gt;

&lt;h4&gt;&lt;a name=&quot;Generalcommentaboutperformance&quot;&gt;&lt;/a&gt;General comment about performance&lt;/h4&gt;

&lt;p&gt;Most of our users find the performance of the .NET driver good. We have put a lot of effort into improving performance over time. If there are specific areas (like this one) where performance issues are encountered we appreciate reports of that.&lt;/p&gt;

&lt;h4&gt;&lt;a name=&quot;OriginalDescription&quot;&gt;&lt;/a&gt;Original Description&lt;/h4&gt;

&lt;p&gt;After few months with C# driver and Linq syntax i feel, that driver in undetermined state. Big performance issues with highloads,  many different ways to doing something with worst performance in one case and pretty performance in other case. &lt;br/&gt;
.NET Ecosystem now drives around Linq (not Builders) and Linq Count query &lt;br/&gt;
don&apos;t need to use pipeline to simple Count query! Try to catch ideas from EntityFramework. Now driver looks like strage for many developers.&lt;br/&gt;
You are cool developers, but if people many times write only libraries with stable salary - it begin tunnel vision and no improvements in code and no motivation to make driver better (if no improvement issues in Jira found)&lt;br/&gt;
I think driver developers need to write highload code for other projects (maybe already ?)with C# mongo driver and try to see problems, that causing rejecting MongoDB as main database for many companies and i know many companies that rejecting MongoDB becouse C# driver was bottleneck.&lt;br/&gt;
Yes, many problems with performance in incorrect using API, but you target - minimize ways for using API incorrect.&lt;br/&gt;
Thank you for your work.&lt;br/&gt;
Sorry for my English&lt;/p&gt;</description>
                <environment>Windows</environment>
        <key id="289797">CSHARP-1679</key>
            <summary>Count using LINQ is slower than collection Count method</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</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="-1">Unassigned</assignee>
                                    <reporter username="zoxexivo@gmail.com">Ivan Artemov</reporter>
                        <labels>
                    </labels>
                <created>Fri, 27 May 2016 09:42:19 +0000</created>
                <updated>Mon, 15 Nov 2021 22:30:33 +0000</updated>
                            <resolved>Thu, 3 Dec 2020 17:32:28 +0000</resolved>
                                    <version>2.2.4</version>
                                                    <component>Linq</component>
                    <component>Operations</component>
                    <component>Performance</component>
                                        <votes>2</votes>
                                    <watches>3</watches>
                                                                                                                        <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hr8utz:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>9223372036854775807</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>