<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx</link><description>If you are using Linq to SQL, instead of writing regular Linq Queries, you should be using Compiled Queries . if you are building an ASP.NET web application that&amp;rsquo;s going to get thousands of hits per hour, the execution overhead of Linq queries is</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1731738</link><pubDate>Mon, 12 Oct 2009 01:05:07 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731738</guid><dc:creator>PimpThisBlog.com</dc:creator><description>&lt;p&gt;Thank you for submitting this cool story - Trackback from PimpThisBlog.com&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1731738" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1716655</link><pubDate>Wed, 19 Aug 2009 14:29:42 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1716655</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t get at all how you are &amp;quot;passing&amp;quot; the delegate to the compiled query. The delegate is called GetUserFromUserName, right? There is no reference to this function in the Compiled_GetUserFromUserName. &amp;nbsp;When I mimic this code I get an error stating &amp;quot;cannot convert method group SingleOrDefault to non-delegate type System.Linq.IQueryable&amp;quot;. &amp;nbsp;What am I missing?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1716655" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1694268</link><pubDate>Fri, 05 Jun 2009 23:41:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1694268</guid><dc:creator>Günther Foidl</dc:creator><description>&lt;p&gt;Hi, very good explaination.&lt;/p&gt;
&lt;p&gt;But I missed one thing. The comparison to stored procedures in SQL Server. They can be executed by LINQ.&lt;/p&gt;
&lt;p&gt;Finally I would say this is just an opinion when it&amp;#39;s not possible to add a stored procedure.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1694268" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1692250</link><pubDate>Tue, 12 May 2009 17:18:28 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1692250</guid><dc:creator>Magnus</dc:creator><description>&lt;p&gt;Hi, I tried to use this but it failed.&lt;/p&gt;
&lt;p&gt;Im using an object of type Expression&amp;lt;Func&amp;lt;table, bool&amp;gt;&amp;gt; in may where clause. like this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;internal static readonly &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func&amp;lt;ApciPlusSSCE, Expression&amp;lt;Func&amp;lt;MswToParameter,bool&amp;gt;&amp;gt;,IQueryable&amp;lt;Parameter&amp;gt;&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Compiled_GetParameters = CompiledQuery.Compile&amp;lt;ApciPlusSSCE, Expression&amp;lt;Func&amp;lt;MswToParameter, bool&amp;gt;&amp;gt;, IQueryable&amp;lt;Parameter&amp;gt;&amp;gt;(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(db,predicate) =&amp;gt; db.MswToParameter.Where(predicate).Select(m =&amp;gt; m.Parameter).Distinct());&lt;/p&gt;
&lt;p&gt;When called I get an NotSupportedException: Unsupported overload used for query operator &amp;#39;Where&amp;#39;.&lt;/p&gt;
&lt;p&gt;The query works as a dynamic linq query.&lt;/p&gt;
&lt;p&gt;What have I missed? Where shall I begin to search for the error?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1692250" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1687332</link><pubDate>Tue, 14 Apr 2009 02:24:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1687332</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;Hi Omar, thanks for that.&lt;/p&gt;
&lt;p&gt;I guess because the Compiled Queries return an IQueryable I was expecting to be able to reuse it like you do a dynamic query:&lt;/p&gt;
&lt;p&gt;IQueryable&amp;lt;MyEntity&amp;gt; query = ...(ExecuteCompiledQuery)&lt;/p&gt;
&lt;p&gt;and then be able to go:&lt;/p&gt;
&lt;p&gt;int totalCount = query.Count();&lt;/p&gt;
&lt;p&gt;List&amp;lt;MyEntity&amp;gt; results = query.Skip(10).Take(10).ToList();&lt;/p&gt;
&lt;p&gt;But having looked into it, I can see that the query is actually executed immediately on the SQL box when you call the Compiled Query.&lt;/p&gt;
&lt;p&gt;So I guess I would need a seperate compiled query for the count and then another compiled query which has &amp;quot;skip&amp;quot; and &amp;quot;take&amp;quot; paramters to actually get the search result range I&amp;#39;m after... &amp;nbsp;The problem with this is that I have to write and maintain duplicate code.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1687332" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1685538</link><pubDate>Tue, 07 Apr 2009 08:48:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1685538</guid><dc:creator>omar</dc:creator><description>&lt;p&gt;Hi daniel,&lt;/p&gt;
&lt;p&gt;&amp;quot;in fact if they are they become less efficient that dynamic queries. &amp;quot;&lt;/p&gt;
&lt;p&gt;Compiled Queries are definitely more efficient than dynamic queries in terms of execution. They are compiled, faster and does not consume memory like dynamic queries does on every execution.&lt;/p&gt;
&lt;p&gt;I believe you wanted to say compiled queries become less reusable than dynamic queries.&lt;/p&gt;
&lt;p&gt;Compiled queries are like fixed queries. You have to build the whole query and just call it. I do not know a way to join a compiled query with another compiled query or dynamic query. In fact I am curious how you join a dynamic query with another one.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1685538" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1685297</link><pubDate>Mon, 06 Apr 2009 20:59:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1685297</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;Omar, great article. I had hoped this would solve some of the speed issues that we have been facing, however was disapointed to find that the compiled queries couldn&amp;#39;t be reused further, in fact if they are they become less efficient that dynamic queries. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Eg: I tried to join another table onto a compiled query, but this blew out the query. &amp;nbsp;However it looks like I can refine the query without losing performance so long as I&amp;#39;m not changing the structure by adding more tables etc.&lt;/p&gt;
&lt;p&gt;Are there any way to join onto compiled queries without losing the performance gain?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1685297" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1672398</link><pubDate>Sat, 14 Feb 2009 22:24:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1672398</guid><dc:creator>Guy Noir</dc:creator><description>&lt;p&gt;I too have been searching for a way to use the DataLoadOptions w/ compiled queries, so thanks for the post!&lt;/p&gt;
&lt;p&gt;It&amp;#39;s only natural that you&amp;#39;d want to use DataLoadOptions w/ a compiled query, but it took me way to long to find this answer.&lt;/p&gt;
&lt;p&gt;Is there a way that doesn&amp;#39;t require an additional line of code to use it each time you want to use the compiled query?&lt;/p&gt;
&lt;p&gt;Couldn&amp;#39;t the static DataLoadOptions somehow be part of the initialization of the compiled query? &amp;nbsp;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1672398" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1652953</link><pubDate>Mon, 03 Nov 2008 20:16:17 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652953</guid><dc:creator>skain</dc:creator><description>&lt;p&gt;Thanks for the info on how to use CompiledQueries with LoadWith! &amp;nbsp;I&amp;#39;ve been trying to figure that out for a while now and had just about concluded it couldn&amp;#39;t be done. &lt;/p&gt;
&lt;p&gt;Now I&amp;#39;ve just got to update my app to use static instances . . .&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652953" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1652768</link><pubDate>Sat, 01 Nov 2008 21:26:16 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652768</guid><dc:creator>bigjim</dc:creator><description>&lt;p&gt;Well, this is all academic. &amp;nbsp;Apparently this week MS announced the death of Linq To SQL in favor of Entity Framework. &amp;nbsp;Unfortunately, in my opinion.&lt;/p&gt;
&lt;p&gt;Look to David Hayden (among others) for more on this news &lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://codebetter.com/blogs/david.hayden/archive/2008/10/31/linq-to-sql-is-dead-read-between-the-lines.aspx"&gt;codebetter.com/.../linq-to-sql-is-dead-read-between-the-lines.aspx&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652768" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1652757</link><pubDate>Sat, 01 Nov 2008 15:05:23 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652757</guid><dc:creator>niran</dc:creator><description>&lt;p&gt;Nice post.&lt;/p&gt;
&lt;p&gt;I would welcome any performance improvement in linq to sql. However, there is some stories floating around saying linq to sql is dead.&lt;/p&gt;
&lt;p&gt;BTW, is it possible to use pure code instead of posting the screen shots?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652757" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1652715</link><pubDate>Fri, 31 Oct 2008 22:01:26 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652715</guid><dc:creator>Lee</dc:creator><description>&lt;p&gt;I was planning on changing my dal to use linq to sql to make my life creating queries in the bll abit easier and less painful but the performance hit has put me off completely. &amp;nbsp;I appreciate the workaround but it&amp;#39;s more work than going back to basics. &amp;nbsp;I hope this will be resolved in .net 4.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652715" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1652469</link><pubDate>Thu, 30 Oct 2008 07:59:39 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652469</guid><dc:creator>Omari</dc:creator><description>&lt;p&gt;You must make all DataLoadOptions &amp;nbsp;static even if don&amp;#39;t use compiled queries. It is a deep dive secret of L2S I&amp;#39;ve found hard way. &lt;/p&gt;
&lt;p&gt;L2S cache last 10 entity materializers and will reuse them but only if DataContext has has the same (or empty) instance of DataLoadOptions &amp;nbsp;assigned. otherwise it will compile it EACH time spending cpu and memory.&lt;/p&gt;
&lt;p&gt;Look at System.Data.Linq.SqlClient.ObjectReaderCompiler.Compile(SqlExpression, Type) : IObjectReaderFactory&lt;/p&gt;
&lt;p&gt;and System.Data.Linq.SqlClient.ObjectReaderCompiler.maxReaderCacheSize : Int32&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652469" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1652468</link><pubDate>Thu, 30 Oct 2008 07:47:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652468</guid><dc:creator>Guy Harwood</dc:creator><description>&lt;p&gt;So much code for such little reward.&lt;/p&gt;
&lt;p&gt;It seems a lot easier in many respects to go back to basics, as ive had my fingers burnt a few times with things like this in live situations.&lt;/p&gt;
&lt;p&gt;LINQ to SQL requires a huge learning investment just to get it performing at an acceptable performance level.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652468" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1652370</link><pubDate>Wed, 29 Oct 2008 14:52:18 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652370</guid><dc:creator>roncansan</dc:creator><description>&lt;p&gt;Isn&amp;#39;t easier and faster to create a store procedure and call the SP through LINQ?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652370" width="1" height="1"&gt;</description></item><item><title>re: Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites</title><link>http://msmvps.com/blogs/omar/archive/2008/10/27/solving-common-problems-with-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites.aspx#1652261</link><pubDate>Tue, 28 Oct 2008 15:18:17 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652261</guid><dc:creator>Muhammad Mosa</dc:creator><description>&lt;p&gt;I&amp;#39;ve applied this on LINQ to Entites, but Omar I have a question! Shall I use this will frequently used queries?! what if I decided to be smart and complie each Query? what would be the concequences?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652261" width="1" height="1"&gt;</description></item></channel></rss>