Showing posts with label theyre. Show all posts
Showing posts with label theyre. Show all posts

Monday, March 26, 2012

Question about optimization

Hi All,
I know that SQL server cursors are general a bad idea and the first thing to
go when optimizing. They're heavy and slow and generally avoidable. On
occasion it can happen that you get stuck with an SP that you need to
optimize and moving it to dynamic SQL is not an option (even though speed
wise it would be better). So here's my question for all those DB experts.
I have logic that requires me to get a list of rows from one table and take
each row and execute another SP using that rows data. Which is faster
1. FAST_FORWARD CURSOR
OR
2. WHILE LOOP THAT CHECKS TILL COUNT = 0 AND INSIDE THE LOOP DOING A SELECT
TOP 1 TO GET MY ROW DATA AND REMOVING THE ROW FROM THE TEMP TABLE
The cursor is easier to understand code and seems like less queries but it's
a dreaded evil cursor. My test db is small so it's hard for me to see major
performance differences so I'm hoping somebody knows the answer already. Even
with my small data, once I got the endless recursion solved, it seems like
the while loop might actually be faster (hard to tell though).
Is there a 3 way to achieve this functionality?
Oh yeah and I can't use the query analyzer to analyze the performance since
some of my inner sp's use temp tables (to avoid using CURSORS).
No one will be able to say for sure... This is one of those things you'll
have to test... If cursors work better, then use them...
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"lee" <lee@.discussions.microsoft.com> wrote in message
news:64E22F44-072E-44E5-AA43-EE7EE5398F5C@.microsoft.com...
> Hi All,
> I know that SQL server cursors are general a bad idea and the first thing
> to
> go when optimizing. They're heavy and slow and generally avoidable. On
> occasion it can happen that you get stuck with an SP that you need to
> optimize and moving it to dynamic SQL is not an option (even though speed
> wise it would be better). So here's my question for all those DB experts.
> I have logic that requires me to get a list of rows from one table and
> take
> each row and execute another SP using that rows data. Which is faster
> 1. FAST_FORWARD CURSOR
> OR
> 2. WHILE LOOP THAT CHECKS TILL COUNT = 0 AND INSIDE THE LOOP DOING A
> SELECT
> TOP 1 TO GET MY ROW DATA AND REMOVING THE ROW FROM THE TEMP TABLE
> The cursor is easier to understand code and seems like less queries but
> it's
> a dreaded evil cursor. My test db is small so it's hard for me to see
> major
> performance differences so I'm hoping somebody knows the answer already.
> Even
> with my small data, once I got the endless recursion solved, it seems like
> the while loop might actually be faster (hard to tell though).
> Is there a 3 way to achieve this functionality?
> Oh yeah and I can't use the query analyzer to analyze the performance
> since
> some of my inner sp's use temp tables (to avoid using CURSORS).
>
|||"lee" <lee@.discussions.microsoft.com> wrote in message
news:64E22F44-072E-44E5-AA43-EE7EE5398F5C@.microsoft.com...
> Hi All,
> I know that SQL server cursors are general a bad idea and the first thing
to
> go when optimizing. They're heavy and slow and generally avoidable. On
> occasion it can happen that you get stuck with an SP that you need to
> optimize and moving it to dynamic SQL is not an option (even though speed
> wise it would be better). So here's my question for all those DB experts.
> I have logic that requires me to get a list of rows from one table and
take
> each row and execute another SP using that rows data. Which is faster
> 1. FAST_FORWARD CURSOR
> OR
> 2. WHILE LOOP THAT CHECKS TILL COUNT = 0 AND INSIDE THE LOOP DOING A
SELECT
> TOP 1 TO GET MY ROW DATA AND REMOVING THE ROW FROM THE TEMP TABLE
> The cursor is easier to understand code and seems like less queries but
it's
> a dreaded evil cursor. My test db is small so it's hard for me to see
major
> performance differences so I'm hoping somebody knows the answer already.
Even
> with my small data, once I got the endless recursion solved, it seems like
> the while loop might actually be faster (hard to tell though).
> Is there a 3 way to achieve this functionality?
> Oh yeah and I can't use the query analyzer to analyze the performance
since
> some of my inner sp's use temp tables (to avoid using CURSORS).
>
Instead of doing a TOP 1 and then deleting, you could create an IDENTITY
column and skip the delete. It will probably be faster than doing the
deletes on a large lookup table.
DECLARE @.tmp TABLE (IDCol IDENTITY(1,1), SomeValue nvarchar(100))
DECLARE @.i int
DECLARE @.Lookup nvarchar(100)
INSERT @.tmp (SomeValue)
SELECT Value FROM LookupTable
SELECT @.i = COUNT(*) FROM @.tmp
WHILE @.i > 0
BEGIN
-- Get lookup value from temp table
SELECT @.Lookup = Value FROM @.tmp WHERE @.tmp.IDCol = @.i
-- Process
EXEC SomeProc @.Lookup
SET @.i = @.i - 1
END
Rick Sawtell
MCT, MCSD, MCDBA
|||As I was reviewing my SPs, I realized that and implemented something similar.
Speed increase when I did that was minimal but any speed increase is welcomed
at this stage.
I've just done what I didn't want to do. I've started grouping a lot of the
SP's into single large SPs that can handle some of the load in batch queries.
Speed increase is dramatic when doing this.
I've gone from 23 seconds (on my dev machine with no indexes) to traverse
20000 records in 16 tables to about 11 seconds. Speed is better but the
maintenance team is going to hate me for this.
FYI: From my tests the FAST_FOWARD cursor is faster then the While loop.
I've left the While loop in places with Large amount of rows and Cursor in
places with small amount of rows. Seems to be the best of both worlds so far.
"Rick Sawtell" wrote:

> "lee" <lee@.discussions.microsoft.com> wrote in message
> news:64E22F44-072E-44E5-AA43-EE7EE5398F5C@.microsoft.com...
> to
> take
> SELECT
> it's
> major
> Even
> since
> Instead of doing a TOP 1 and then deleting, you could create an IDENTITY
> column and skip the delete. It will probably be faster than doing the
> deletes on a large lookup table.
> DECLARE @.tmp TABLE (IDCol IDENTITY(1,1), SomeValue nvarchar(100))
> DECLARE @.i int
> DECLARE @.Lookup nvarchar(100)
>
> INSERT @.tmp (SomeValue)
> SELECT Value FROM LookupTable
> SELECT @.i = COUNT(*) FROM @.tmp
> WHILE @.i > 0
> BEGIN
> -- Get lookup value from temp table
> SELECT @.Lookup = Value FROM @.tmp WHERE @.tmp.IDCol = @.i
> -- Process
> EXEC SomeProc @.Lookup
> SET @.i = @.i - 1
> END
>
> Rick Sawtell
> MCT, MCSD, MCDBA
>
>
>
>
>

Friday, March 23, 2012

question about memory usage

Using SS2000. I'm having some timeouts on some queries from a web site and
I'm trying to figure out why they're occurring. We have some dts packages
that run every 30 minutes so there may be a conflict.
We have memory configured dynamically for min - 0 and max 3583MB. However,
when I look at System Monitor for SQL Server:Memory Manager:Total Server
Manager, the memory is always around 1,678,880(KB). It only seems to
fluctuate by a few MB. In looking at the Memory Object:% Committed Bytes in
Use for the system, it is always around 40%. Memory Object:Available MBytes
is about 1086. Between what SS is using and the system memory available,
about 2.5G is accounted for. I don't know where the other 1G has gone to.
I'm also wondering why SS never seems to get any more than 1.6G. Is it
actually using all of the 1.6G or only a part of it? We have a 4 processor
system and even when all 4 processors are around 80% processor time, the
memory doesn't change.
Any ideas or suggestions?
Thanks,
Dan
Did you turn on AWE flag?
Eric Li
SQL DBA
MCDBA
Dan wrote:

> Using SS2000. I'm having some timeouts on some queries from a web site and
> I'm trying to figure out why they're occurring. We have some dts packages
> that run every 30 minutes so there may be a conflict.
> We have memory configured dynamically for min - 0 and max 3583MB. However,
> when I look at System Monitor for SQL Server:Memory Manager:Total Server
> Manager, the memory is always around 1,678,880(KB). It only seems to
> fluctuate by a few MB. In looking at the Memory Object:% Committed Bytes in
> Use for the system, it is always around 40%. Memory Object:Available MBytes
> is about 1086. Between what SS is using and the system memory available,
> about 2.5G is accounted for. I don't know where the other 1G has gone to.
> I'm also wondering why SS never seems to get any more than 1.6G. Is it
> actually using all of the 1.6G or only a part of it? We have a 4 processor
> system and even when all 4 processors are around 80% processor time, the
> memory doesn't change.
> Any ideas or suggestions?
> Thanks,
> Dan
>
>
|||Do you have the Enterprise version of 2000?
-John Oakes
"Dan" <ddonahue@.archermalmo.com> wrote in message
news:OX1gKzKTEHA.3016@.tk2msftngp13.phx.gbl...
> Using SS2000. I'm having some timeouts on some queries from a web site and
> I'm trying to figure out why they're occurring. We have some dts packages
> that run every 30 minutes so there may be a conflict.
> We have memory configured dynamically for min - 0 and max 3583MB. However,
> when I look at System Monitor for SQL Server:Memory Manager:Total Server
> Manager, the memory is always around 1,678,880(KB). It only seems to
> fluctuate by a few MB. In looking at the Memory Object:% Committed Bytes
in
> Use for the system, it is always around 40%. Memory Object:Available
MBytes
> is about 1086. Between what SS is using and the system memory available,
> about 2.5G is accounted for. I don't know where the other 1G has gone to.
> I'm also wondering why SS never seems to get any more than 1.6G. Is it
> actually using all of the 1.6G or only a part of it? We have a 4 processor
> system and even when all 4 processors are around 80% processor time, the
> memory doesn't change.
> Any ideas or suggestions?
> Thanks,
> Dan
>
>
|||You need to set the /3GB switch in the Boot.ini file to get use of up to 3GB
of ram for SQL Server. What you are seeing is the 2Gb minus the memtoleave
area that is reserved up front. Your timeouts are most likely due to
blocking fromt eh DTS packages though. Running sp_who2 will give you a
start as to who is blocking.
Andrew J. Kelly
SQL Server MVP
"Dan" <ddonahue@.archermalmo.com> wrote in message
news:OX1gKzKTEHA.3016@.tk2msftngp13.phx.gbl...
> Using SS2000. I'm having some timeouts on some queries from a web site and
> I'm trying to figure out why they're occurring. We have some dts packages
> that run every 30 minutes so there may be a conflict.
> We have memory configured dynamically for min - 0 and max 3583MB. However,
> when I look at System Monitor for SQL Server:Memory Manager:Total Server
> Manager, the memory is always around 1,678,880(KB). It only seems to
> fluctuate by a few MB. In looking at the Memory Object:% Committed Bytes
in
> Use for the system, it is always around 40%. Memory Object:Available
MBytes
> is about 1086. Between what SS is using and the system memory available,
> about 2.5G is accounted for. I don't know where the other 1G has gone to.
> I'm also wondering why SS never seems to get any more than 1.6G. Is it
> actually using all of the 1.6G or only a part of it? We have a 4 processor
> system and even when all 4 processors are around 80% processor time, the
> memory doesn't change.
> Any ideas or suggestions?
> Thanks,
> Dan
>
>
|||We're running developer.
"John Oakes" <john@.nospam.networkproductions.net> wrote in message
news:Ov2dPRLTEHA.760@.TK2MSFTNGP12.phx.gbl...[vbcol=seagreen]
> Do you have the Enterprise version of 2000?
> -John Oakes
> "Dan" <ddonahue@.archermalmo.com> wrote in message
> news:OX1gKzKTEHA.3016@.tk2msftngp13.phx.gbl...
and[vbcol=seagreen]
packages[vbcol=seagreen]
However,[vbcol=seagreen]
> in
> MBytes
to.[vbcol=seagreen]
processor
>
|||It's not enabled. Do I need Enterprise version for that? We're running
Developer.
"Eric.Li" <anonymous@.microsoftnews.org> wrote in message
news:%23xaNDJLTEHA.1232@.TK2MSFTNGP09.phx.gbl...[vbcol=seagreen]
> Did you turn on AWE flag?
> --
> Eric Li
> SQL DBA
> MCDBA
> Dan wrote:
and[vbcol=seagreen]
packages[vbcol=seagreen]
However,[vbcol=seagreen]
in[vbcol=seagreen]
MBytes[vbcol=seagreen]
to.[vbcol=seagreen]
processor[vbcol=seagreen]
|||Will I be able to tell in profiler that blocking is going on? If so, can you
suggest what to look for? Otherwise, I'll have to get lucky and catch it in
current activity.
Thanks,
Dan
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:OQZBBSLTEHA.3752@.TK2MSFTNGP12.phx.gbl...
> You need to set the /3GB switch in the Boot.ini file to get use of up to
3GB
> of ram for SQL Server. What you are seeing is the 2Gb minus the
memtoleave[vbcol=seagreen]
> area that is reserved up front. Your timeouts are most likely due to
> blocking fromt eh DTS packages though. Running sp_who2 will give you a
> start as to who is blocking.
> --
> Andrew J. Kelly
> SQL Server MVP
>
> "Dan" <ddonahue@.archermalmo.com> wrote in message
> news:OX1gKzKTEHA.3016@.tk2msftngp13.phx.gbl...
and[vbcol=seagreen]
packages[vbcol=seagreen]
However,[vbcol=seagreen]
> in
> MBytes
to.[vbcol=seagreen]
processor
>
|||I can't remember if developer lets you use more than 2G, regardless of the
3G switch. Maybe someone here will remember?
-John Oakes
"Dan" <ddonahue@.archermalmo.com> wrote in message
news:%23Aar7nLTEHA.3552@.TK2MSFTNGP09.phx.gbl...[vbcol=seagreen]
> We're running developer.
> "John Oakes" <john@.nospam.networkproductions.net> wrote in message
> news:Ov2dPRLTEHA.760@.TK2MSFTNGP12.phx.gbl...
> and
> packages
> However,
Server[vbcol=seagreen]
Bytes[vbcol=seagreen]
available,[vbcol=seagreen]
> to.
> processor
the
>
|||You need Enterprise Edition to access full 4GB RAM
Eric Li
SQL DBA
MCDBA
Dan wrote:

> It's not enabled. Do I need Enterprise version for that? We're running
> Developer.
> "Eric.Li" <anonymous@.microsoftnews.org> wrote in message
> news:%23xaNDJLTEHA.1232@.TK2MSFTNGP09.phx.gbl...
>
> and
>
> packages
>
> However,
>
> in
>
> MBytes
>
> to.
>
> processor
>
>
|||I see that we need to be running Advanced Server or Data Center. We don't have either.
Thanks,
Dan