﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum / StrataFrame Application Framework / Business Objects and Data Access (How do I?)  / Speed of Stored Procedure / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>forum@strataframe.net</webMaster><lastBuildDate>Fri, 04 Jul 2008 23:27:56 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>Hi Trent,&lt;/P&gt;&lt;P&gt;Thanks for the extra info. Hopefully we never have to get down to that level but it is very interesting that changing the data type made such a humongous improvement.&lt;/P&gt;&lt;P&gt; Cheers, Peter</description><pubDate>Mon, 12 May 2008 03:47:42 GMT</pubDate><dc:creator>Peter Jones</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>Thanks for the info...it always good to hear how other developers solve their isssues.  In our case, we have some extremely complex queries that take place between 8+ tables and get extremely nested while calculating something called "pending."  This basically determine how much a patient owes and an insurance owes....but it has to take into account all of their tran history, insurance plans (primary, secondary, tertiary, etc.), deductibles, write-offs, bad debt, and about 50 other things (not kidding on the 50 :D).  We tried using dates, indexes, and even tried the a nmber of conversion routines...and once we turned this into ticks with and index versus dates with an index (that was the only change) the query went from 4 1/2 minutes for a single patient with 6000 trans (don't ask me why they have 6000 trans for one patient...we just crunch the numbers :D) to 30 ms...so we started doing a little digging and learned that between and ORs are bad words with SQL Server and dates when dealing with any type of complex query.  This proved true again just the other day...I had a query running in 4 seconds (way too slow) once we started testing on a large database...changed the dates to ticks...1 ms...crazy.&lt;/P&gt;&lt;P&gt;One other thing on this too, we have to be able to have extremely fast queries run on MS Express with siingle core processors as we have a lot of users with 2 GB plus databases that will till use MS SQL Express on existing equipment iin the field...we call this zero impact.  It may not be but we have to get as close to that as possible for existing users.  Then are the much larger sites they will have a more complex server setup and a full version of SQL Server...so we have to work in a lot of different environments...so absolute optimization is the only way we can do this.</description><pubDate>Sun, 11 May 2008 21:43:06 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>Hi Guys,&lt;/P&gt;&lt;P&gt;While I've never tried converting a date to a bigint I thought I would just let you have my specific experience in that area. Most of our reports are date range based and use transactional data as their source. For this reason we have clustered index on the 'date created' column in the transaction files. I've just connected to one of our sites where the main transaction file is 11+ million rows. I opened Query in SQL Server entered the following (date randomly selected):&lt;/P&gt;&lt;P&gt;Select HIDDateTime&lt;/P&gt;&lt;P&gt;From dbo.tblHIDHides&lt;/P&gt;&lt;P&gt;Where HIDDateTime Between Convert(DateTime, '2006-05-04', 102) And Convert(DateTime, '2006-05-05', 102)&lt;/P&gt;&lt;P&gt;So, no stored procedure, no caching from previous queries. The result: 6501 rows returned in &amp;lt; 1 second.&lt;/P&gt;&lt;P&gt;The database is low end Xeon database server with just 2Gb of memory, Windows 2003 Standard that wasn't busy when I did the above test.&lt;/P&gt;&lt;P&gt;Cheers, Peter</description><pubDate>Sat, 10 May 2008 03:34:39 GMT</pubDate><dc:creator>Peter Jones</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>All of Peter's comments were excellent...I thought I would toss in a few more things as well:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;You can tell a query which index to use by using the WITH and INDEX commands.  Sometimes SQL needs a little help...we ran into this the other day.  It would look like this:&lt;BR&gt;&lt;BR&gt;[codesnippet]SELECT * FROM Customers WITH(INDEX(IX_MyIndex))[/codesnippet]&lt;BR&gt;&lt;/LI&gt;&lt;LI&gt;The framework is not going to change anything in regards to the execution speed and performance....so if you get it down to 1 second in SQL Server management Studio executing the sproc...this will not change on the framework side unless you have some type of connection issue or something else in the mix.&lt;BR&gt;&lt;/LI&gt;&lt;LI&gt;DateTime columns are aweful about slowing down queries when in the WHERE...one way to get around this is to store dates as a BigInt data type and then store then DateTimes using Ticks.  We then create a Custom property on the BO that wraps this as a DateTime so that while using the BOs inside of your app you interact with a DateTime...but it is stored as Ticks on the SQL Server side...and this will drastically improve performance....by like a ton when you are testing with &amp;lt;, &amp;gt;, or betweens.&lt;/LI&gt;&lt;/OL&gt;</description><pubDate>Fri, 09 May 2008 09:39:47 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>Hi Bill,&lt;/P&gt;&lt;P&gt;A few random comments that may help:&lt;/P&gt;&lt;P&gt;1) Be careful with data types in a Where clause. I see you have what looks like date parameters defined as varChar. If the database column is a real date and you are comparing with a varChar then SQL Server will not use any index you may have on that column.&lt;/P&gt;&lt;P&gt;2) Big time differences like this will invariable mean that one way is using indexes and the other is using full table scans. The Profiler will show this up.&lt;/P&gt;&lt;P&gt;3) I notice you have:&lt;/P&gt;&lt;PRE&gt;@itemcode varchar(30),IF @itemcode = '' OR @itemcode IS Null	BEGIN			SET @itemcode = '%%'	ENDWHERE 		Items.Code LIKE @itemcode &lt;/PRE&gt;&lt;PRE&gt;I think a more efficient approach would be to sort out your parameter and have a defualt and only pass in data if you have a specfic selection criteria. Then you could have:&lt;/PRE&gt;&lt;PRE&gt;@itemcode varchar(30) = Null,WHERE 		((@itemcode Is Null) Or  (Items.Code = @itemcode)) &lt;/PRE&gt;&lt;PRE&gt;Cheers, Peter&lt;/PRE&gt;&lt;PRE&gt;&lt;PRE&gt; &lt;/PRE&gt;&lt;/PRE&gt;</description><pubDate>Thu, 08 May 2008 18:05:22 GMT</pubDate><dc:creator>Peter Jones</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>I'm not quite sure why LIKE isn't...er...liked by SQL Server, but I do know enough to include that in my list of things to check out when a query is slow. :D&lt;br&gt;&lt;br&gt;Glad you got it working (faster).</description><pubDate>Thu, 08 May 2008 17:46:26 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>The stored procedure does not like the LIKE.&lt;/P&gt;&lt;P&gt;Who woulda thunk it?!?!?!&lt;/P&gt;&lt;P&gt;I have removed the LIKE and have followed another approach:&lt;/P&gt;&lt;P&gt;IF @itemcode = ''&lt;BR&gt;    BEGIN&lt;BR&gt;        'run script without the Items.Code filter&lt;BR&gt;    END&lt;BR&gt;ELSE&lt;BR&gt;    BEGIN&lt;BR&gt;        'run the script with the Items.Code = 'MyCode'&lt;BR&gt;    END&lt;BR&gt;&lt;BR&gt;Thanks, again, Greg!!&lt;BR&gt;Bill</description><pubDate>Thu, 08 May 2008 14:30:07 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>[quote]I'd see what replacing the LIKE in your where clause with an equals does&lt;BR&gt;. . .&lt;BR&gt;I'm assuming that Items.Code is indexed. &lt;BR&gt;. . .&lt;BR&gt;Also, is there just one code in the field?[/quote]&lt;/P&gt;&lt;P&gt;I'll try the sproc without the LIKE...I suppose an IF block may work better.&lt;/P&gt;&lt;P&gt;The code column is indexed.  Only one code would be passed if the user wanted the list limited.&lt;/P&gt;&lt;P&gt;Thanks for you attention on this, Greg.  Much appreciated.&lt;/P&gt;&lt;P&gt;Bill</description><pubDate>Thu, 08 May 2008 14:24:11 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>OK, that is different.  I understand why you are baffled.:blink:&lt;br&gt;&lt;br&gt;I'd see what replacing the LIKE in your where clause with an equals does, leaving @itemcode as a varchar(30).  I.e.&lt;br&gt;&lt;br&gt;[codesnippet]WHERE &lt;br&gt;-- Items.Code LIKE @itemcode  (original code)&lt;br&gt;Items.Code = @itemcode -- Try this new code&lt;br&gt;AND Items.Class = 1&lt;br&gt;AND Items.DefaultDiv = @div&lt;br&gt;AND Items.inactive = 0[/codesnippet]&lt;br&gt;&lt;br&gt;Now, you might need the LIKE, but at least this might help see where the problem lies.  &lt;br&gt;&lt;br&gt;I'm assuming that Items.Code is indexed.  Also, is there just one code in the field?  I.e. it isn't a list of codes or anything weird like that is it?&lt;br&gt;&lt;br&gt;</description><pubDate>Thu, 08 May 2008 14:09:15 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>If I remove the first parameter (varchar(30)), then the stored procedure works under 4 sec every time.  If I reintroduce the parameter, then it goes right back to the 10 minute mark.  The third parameter is a varchar(10).  I do not think the type is the problem, here.&lt;/P&gt;&lt;P&gt;Still investigating.</description><pubDate>Thu, 08 May 2008 13:17:45 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>I took the parameters out of the stored procedure and hardcoded them into the sproc just like the raw script.&lt;/P&gt;&lt;P&gt;BAM!!  2 sec. to run the sproc!!!!&lt;/P&gt;&lt;P&gt;Why would processing parameters cause the sproc to execute 300x slower?&lt;/P&gt;&lt;P&gt;I am going to add them back one at a time and see which one is causing the slowdown.</description><pubDate>Thu, 08 May 2008 12:42:15 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>Interesting note: the execution plan for the raw sql script is &lt;EM&gt;&lt;STRONG&gt;&lt;U&gt;way different&lt;/U&gt;&lt;/STRONG&gt;&lt;/EM&gt; than the execution plan for the stored procedure!&lt;/P&gt;&lt;P&gt;Investigating now.</description><pubDate>Thu, 08 May 2008 12:35:08 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>The raw script executes in 3 sec.&lt;/P&gt;&lt;P&gt;The stored procedure, using the exact same script, takes 10 minutes.&lt;/P&gt;&lt;P&gt;Here is my code to fill the BO:&lt;/P&gt;&lt;FONT color=#2b91af size=2&gt;&lt;P&gt;SqlParameter&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; mItemCode = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SqlParameter&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"@itemcode"&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt;, MatCodeTE.Text);&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;ADUserBO&lt;/FONT&gt;&lt;FONT size=2&gt; mADUser = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;ADUserBO&lt;/FONT&gt;&lt;FONT size=2&gt;();&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SqlParameter&lt;/FONT&gt;&lt;FONT size=2&gt; mDiv = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SqlParameter&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"@div"&lt;/FONT&gt;&lt;FONT size=2&gt;, mADUser.LocationIndex);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SqlParameter&lt;/FONT&gt;&lt;FONT size=2&gt; mInvDate = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SqlParameter&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"@invdate"&lt;/FONT&gt;&lt;FONT size=2&gt;, InvDateDE.DateTime.ToShortDateString());&lt;BR&gt;rawMaterialValuationBO1.FillByStoredProcedure(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"spx_GetRunningInventory_Material"&lt;/FONT&gt;&lt;FONT size=2&gt;, mItemCode, mDiv, mInvDate);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;This times out in the application because the stored procedure is taking way too long to run.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;I have gone through the execution plan of the raw script in the Query Analyzer and updated all statistics where it wanted it.  I even edited some tables to add indexes where I thought it may help.  Nothing is helping that sproc to run quicker.  To me, it should run in 1 or 2 seconds since there is caching and compiling and such going on.  It is a server-based process...it should be lightning fast.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;I have never had this happen before.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;*scratches head*&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;:blink:&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Thu, 08 May 2008 12:03:26 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>Bill,&lt;br&gt;&lt;br&gt;I'm not quite sure what you are doing to get the time differences.  Is the sproc taking three seconds, but when you just run the script that is within the sproc it takes longer?  I seem to recall that Query Analyzer is just using one of the older db access technologies (ODBE or OLEDB, something like that) to run the sproc/script and SQL Server Management Studio is using ADO.NET, so there should be no differences time between them running there and within your app (unless you app is doing something else to eat up time, of course).  &lt;br&gt;&lt;br&gt;If so, it [i]could[/i] be explained because the sproc is "compiled" within SQL server (the query plan is already determined and saved), were as a script needs to determine the query plan, then execute the query.  You could improve the speed of the script by providing hints about how the query should be executed, but it would be easier to just call the sproc directly from the BO.&lt;br&gt;&lt;br&gt;If you are using a script in the app, try just calling the sproc and see if that makes a difference.&lt;br&gt;&lt;br&gt;</description><pubDate>Thu, 08 May 2008 11:26:03 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>Speed of Stored Procedure</title><link>http://forum.strataframe.net/Topic16247-6-1.aspx</link><description>I am not getting it.  Earlier this spring I discussed in these forums the utilization of UDFs in filling a BO.  The conclusion was to not use them since they have a great deal of overhead involved.  So, instead, I went with some raw SQL to achieve the same results.  I was able to get one of my more complex queries down to about 3 seconds.  That was wonderful.  Now, I am simply taking the same script that runs in the SQL Query Analyzer at 3 seconds and placing it into a stored procedure with three parameters.  These parameters are declared at the beginning of the script in the query analyzer, too.  I run the stored procedure and it takes many minutes to run (last run: 10 min 04 sec).  I really don't get this.  It is the exact same script.  Is there really that much of a difference in quality between raw SQL script and the same SQL script placed into a stored procedure?  What am I doing wrong?  I have attached the script that I am running in the Query Analyzer and the stored procedure.  I know that these are not pretty...I am still working through the details of this query.  Any help, tips, criticism is welcome! :)&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR&gt;Bill</description><pubDate>Thu, 08 May 2008 11:08:08 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item></channel></rss>