<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://completit.com/communityserver/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Ivan Dragoev's Blog</title><subtitle type="html" /><id>http://completit.com/communityserver/blogs/idragoev/atom.aspx</id><link rel="alternate" type="text/html" href="http://completit.com/communityserver/blogs/idragoev/default.aspx" /><link rel="self" type="application/atom+xml" href="http://completit.com/communityserver/blogs/idragoev/atom.aspx" /><generator uri="http://communityserver.org" version="2.1.61129.2">Community Server</generator><updated>2007-06-18T05:52:00Z</updated><entry><title>Animating ListBox items - the VisualStateManager</title><link rel="alternate" type="text/html" href="http://completit.com/communityserver/blogs/idragoev/archive/2008/06/13/animating-listbox-items-the-visualstatemanager.aspx" /><id>http://completit.com/communityserver/blogs/idragoev/archive/2008/06/13/animating-listbox-items-the-visualstatemanager.aspx</id><published>2008-06-13T13:13:00Z</published><updated>2008-06-13T13:13:00Z</updated><content type="html">&lt;P&gt;Check out my &lt;A class="" href="http://www.silverlightshow.net/items/Animating-ListBox-items-the-VisualStateManager.aspx"&gt;second article on ListBox items animations&lt;/A&gt;. Here you can see how to use VisualStateManager and also some problems I faced during the migration to Silverlight 2 beta 2&lt;/P&gt;
&lt;P&gt;Enjoy!&lt;/P&gt;&lt;img src="http://completit.com/communityserver/aggbug.aspx?PostID=4337" width="1" height="1"&gt;</content><author><name>idragoev</name><uri>http://completit.com/communityserver/members/idragoev.aspx</uri></author><category term="Silverlight" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/Silverlight/default.aspx" /></entry><entry><title>Interactive Silverlight Voting Control</title><link rel="alternate" type="text/html" href="http://completit.com/communityserver/blogs/idragoev/archive/2007/12/05/interactive-silverlight-voting-control.aspx" /><id>http://completit.com/communityserver/blogs/idragoev/archive/2007/12/05/interactive-silverlight-voting-control.aspx</id><published>2007-12-05T14:43:00Z</published><updated>2007-12-05T14:43:00Z</updated><content type="html">&lt;P&gt;As you might know, we at CompletIT are behind the&amp;nbsp;&lt;A href="http://www.silverlightshow.net/Default.aspx"&gt;SilverlightShow.net&lt;/A&gt; - the independent Silverlight community site. Also, the last few weeks we've published several articles (with source code included) just to show how easy is to create nice&amp;nbsp;stuff with&amp;nbsp;Silverlight 1.1.&lt;/P&gt;
&lt;P&gt;I also decided to write an article based on simple &lt;A class="" href="http://www.silverlightshow.net/items/2477.aspx"&gt;interactive voting control&lt;/A&gt;. The purpose of this control is to show the current results for certain vote. The site visitors can give their vote simply by clicking on the option result they like and the results are automatically updated.&lt;/P&gt;
&lt;P&gt;We will continue publishing articles with source code. We hope such examples are helpful for&amp;nbsp;the developers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;You can submit your own&amp;nbsp;article Silverlight on &lt;A href="http://www.silverlightshow.net/Default.aspx"&gt;SilverlightShow.net&lt;/A&gt;&amp;nbsp;- just click &lt;A class="" href="http://www.silverlightshow.net/SubmitArticle.aspx"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://completit.com/communityserver/aggbug.aspx?PostID=1157" width="1" height="1"&gt;</content><author><name>idragoev</name><uri>http://completit.com/communityserver/members/idragoev.aspx</uri></author><category term="Silverlight" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/Silverlight/default.aspx" /><category term="SilverlightShow" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/SilverlightShow/default.aspx" /><category term="Silverlight Control" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/Silverlight+Control/default.aspx" /></entry><entry><title>CompletIT at DevReach, Sofia, Bulgaria 1-2 October, 2007</title><link rel="alternate" type="text/html" href="http://completit.com/communityserver/blogs/idragoev/archive/2007/10/08/ms-sql-triggers-inserted-deleted-tables.aspx" /><id>http://completit.com/communityserver/blogs/idragoev/archive/2007/10/08/ms-sql-triggers-inserted-deleted-tables.aspx</id><published>2007-10-08T15:36:00Z</published><updated>2007-10-08T15:36:00Z</updated><content type="html">&lt;P&gt;At the beginning of October I have visited DevReach - the premier conference for Microsoft technologies in South Eastern Europe. I was on several lectures about MS SQL:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;What's New in SQL Server 2008 for Developers - by Vladi Tchalkov;&lt;/LI&gt;
&lt;LI&gt;T–SQL Querying: Tips and Techniques (With SQL 2008 features too) by Stephen Forte;&lt;/LI&gt;
&lt;LI&gt;Lino Does LINQ by Lino Tadros;&lt;/LI&gt;
&lt;LI&gt;Being Smart About Database Design by Vladi Tchalkov,&lt;/LI&gt;
&lt;LI&gt;100 Years of Transaction Solitude...Transactions and isolation levels by Ami Levin;&lt;/LI&gt;
&lt;LI&gt;SQL Server Service Broker - Your Gateway to the World of Transactional Asynchronicity by Ami Levin;&lt;/LI&gt;
&lt;LI&gt;Security and .NET by Hadi Hariri;&lt;/LI&gt;
&lt;LI&gt;Silverlight, Flash on Steroids by Lino Tadros.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;What I appreciated most is the fact, that I had the chance to discuss different topics with the speakers. Most of them are experienced, well known in the community, with knowledge about the cutting-edge MS technologies.&lt;/P&gt;
&lt;P&gt;I’m looking forward to meeting the guys again.&lt;BR&gt;&lt;/P&gt;&lt;img src="http://completit.com/communityserver/aggbug.aspx?PostID=208" width="1" height="1"&gt;</content><author><name>idragoev</name><uri>http://completit.com/communityserver/members/idragoev.aspx</uri></author><category term="DevReach" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/DevReach/default.aspx" /></entry><entry><title>How to select the primary key data type?</title><link rel="alternate" type="text/html" href="http://completit.com/communityserver/blogs/idragoev/archive/2007/07/04/how-to-select-the-primary-key-data-type.aspx" /><id>http://completit.com/communityserver/blogs/idragoev/archive/2007/07/04/how-to-select-the-primary-key-data-type.aspx</id><published>2007-07-05T08:00:00Z</published><updated>2007-07-05T08:00:00Z</updated><content type="html">&lt;P&gt;When a new database is about to be designed, have to think about&amp;nbsp;what data type to use for the primary keys. There are several things that should be taken into consideration: the average database size expected, the possibility of growing in the future, replication or other data synchronization needs, etc. Here I will try to give you a simple way for primary key type selection depending on the database you have to design.&lt;/P&gt;
&lt;P&gt;When I design database I'm trying to follow several simple rules:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Add column named ID in each table and set it as primary key; 
&lt;LI&gt;Do not use logical primary keys as physical primary keys: Client table&amp;nbsp;have column ID as PK and Code as logical PK, but ID column will be used in relations; 
&lt;LI&gt;Use stored procedure for data fetching and manipulation - makes database changes easier, no recompilation of existing applications needed; 
&lt;LI&gt;Avoid auto increment columns - you never know what the value for the PK will be until the row is stored in the database - means no control;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Generally I always have to choose between 16-byte uniqueidentifier and 4-byte int. The INTs are smaller, easy to remember, meaningfully to the user and easy to make them sequent. The uniqueidentifiers are simply unique and have unlimited values.&lt;/P&gt;
&lt;P&gt;The primary keys of type int are smaller, which means smaller indexes and better performance. It is also easy to keep the values in a sequence - either by using auto increment (not recommended) or any other way to get the next value. This allows to make clustered index on the column too without performance impact.&lt;/P&gt;
&lt;P&gt;The disadvantage of INTs are that you need some logic for obtaining new value. If auto increment is used, then inserting master-detail data might be more complicated, because the primary key value is unknown until the record is stored. This problem can be solved by using stored procedures and logical keys to link master-details.&lt;/P&gt;
&lt;P&gt;To add new record when the primary key is unique identifier is easy. But here the size is 4 times bigger! Also you should never make clustered index on it because inserting new records are not sequential and causes the index to be reorganized each time.&lt;/P&gt;
&lt;P&gt;The unique identifiers are only useful when you need to synchronize data between databases or you have application that might work off line. If INTs are used, you can have problems with doubled IDs because another user might use the same values for&amp;nbsp;the new records - in this case you need to implement a logic to renumber the PK and all related records and to update the clients with the new values.&lt;/P&gt;
&lt;P&gt;What I think is best is to combine the advantages of the INTS with the uniqueness of the guids. Make the primary key of type int, and add additional column of type uniqueidentifier only if you need data synchronization. This is the way the replication works - using rowguid column.&lt;/P&gt;&lt;img src="http://completit.com/communityserver/aggbug.aspx?PostID=34" width="1" height="1"&gt;</content><author><name>idragoev</name><uri>http://completit.com/communityserver/members/idragoev.aspx</uri></author><category term="PK" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/PK/default.aspx" /><category term="database design" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/database+design/default.aspx" /><category term="Primary Key" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/Primary+Key/default.aspx" /><category term="uniqueidentifier" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/uniqueidentifier/default.aspx" /><category term="GUID" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/GUID/default.aspx" /></entry><entry><title>Increase Stored Procedure execution performance dramatically</title><link rel="alternate" type="text/html" href="http://completit.com/communityserver/blogs/idragoev/archive/2007/06/18/increase-stored-procedure-execution-performance.aspx" /><id>http://completit.com/communityserver/blogs/idragoev/archive/2007/06/18/increase-stored-procedure-execution-performance.aspx</id><published>2007-06-18T14:52:00Z</published><updated>2007-06-18T14:52:00Z</updated><content type="html">&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Last month we spent a lot of time in data loading optimization due to performance problems. I started with optimization of the heaviest views and put them in parameterized &lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;stored procedures . I also used a simple method to check the performance by measuring the time needed for execution. I was using MS SQL Management studio. During the test I had the following source with some local variables declared:&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;declare&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Param1 int,&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @param2 int,&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @TraceStart datetime&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Set @TraceStart = GetDate()&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Select Column1, Column2, ….&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;From Table1&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Join …&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Where Column1 = @Param1 and …&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;…&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Select DateDiff(ms, @TraceStart, GetDate())&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;The idea was simple – to test, and then to place the same code in a stored procedure, and the declared local variables to be introduced as parameters. I managed to optimize the view drastically – from 500ms down to 1. And I was pretty happy.&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Then I made the stored procedure:&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Create procedure LoadData(@Param1 int @param2 int)&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Select Column1, Column2, ….&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;From Table1&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Join …&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Where Column1 = @Param1 and …&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Then I executed it using the following approach:&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;declare&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Param1 int,&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @param2 int,&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @TraceStart datetime&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Set @TraceStart = GetDate()&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Exec LoadData @Param1 int @param2 int&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Select DateDiff(ms, @TraceStart, GetDate())&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;The results bring me a hard attack: more than 10 minutes!!!&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri" size="3"&gt;Thanks to a friend of mine – &lt;/font&gt;&lt;a href="http://dreams.a-wake.net/"&gt;&lt;font face="Calibri" color="#800080" size="3"&gt;Petar Atanasov&lt;/font&gt;&lt;/a&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt; – we solved the problem. He&amp;nbsp;advised me to make a local copy of the parameters and to use them in the procedure’s body and that's&amp;nbsp;exactly what I did:&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Create procedure LoadData(@Param1_ int @param2_ int)&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;declare&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Param1 int,&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @param2 int,&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Set @Param1 = @Param1_&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Set @param2 = @Param2_&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Select Column1, Column2, ….&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;From Table1&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Join …&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;span style="font-size:10pt;color:#3366ff;"&gt;Where Column1 = @Param1 and …&lt;/span&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri" size="3"&gt;And that was it! The stored procedure was executed for less than 1ms!!!&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;font size="3"&gt;&lt;/font&gt;&lt;/o:p&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font face="Calibri" size="3"&gt;The problem is that the query optimizer cannot make a good execution plan if the parameters are directly used, but it can if local variables are used – especially for date time parameters.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://completit.com/communityserver/aggbug.aspx?PostID=24" width="1" height="1"&gt;</content><author><name>idragoev</name><uri>http://completit.com/communityserver/members/idragoev.aspx</uri></author><category term="execution plan" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/execution+plan/default.aspx" /><category term="optimization" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/optimization/default.aspx" /><category term="Stored Procedure" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/Stored+Procedure/default.aspx" /><category term="Performance" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/Performance/default.aspx" /><category term="Stored Procedure execution" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/Stored+Procedure+execution/default.aspx" /><category term="sql optimization" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/sql+optimization/default.aspx" /><category term="sql stored procedure optimization" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/sql+stored+procedure+optimization/default.aspx" /><category term="database design" scheme="http://completit.com/communityserver/blogs/idragoev/archive/tags/database+design/default.aspx" /></entry></feed>