Showing posts with label max. Show all posts
Showing posts with label max. Show all posts

Wednesday, 7 March 2012

Confused whether to generate a nextNumber or to use Autoincrement

In my office we generate an unique ID on a table by doing something like:

Declare @.ID int
SELECT @.ID= ISNULL(MAX(@.ID), 0) + 1 FROM MyTable

Are there any benefit in using this methods rather then the Autoincrement functionality provided by sql server?

Could somebody explain which method is better and why?
Hi,
I think there is a benefit is that you will not has gaps in the ID field if you rolled back your transaction.
if you have an identity field in a table and the next value for example will be 100 and you make an insert statement then rolled back the insert then the next insert statement will have 101. but your way you have the control
on the other side I think having the identity field will be more efficient in terms of performance because you dont have to aggregate to get the max|||On a heavily used system your method will likely get conflicts as several connections attempt to do the insert at the same time, retrieving the same max value, and then doing the insert. You'll have to have some incrementing retry. And you'll likely have reduced concurrency when compared to using IDENTITY.

Another alternative that removes the MAX aggregate is to have a table with a single row with the next value to use, but then you you have to increment that, locking out anyone else trying to do the same thing until your transaction completes .. and you'll likely end up rolling back your insert in some cases, thereby losing your incremented value and having gaps in your sequence.

IDENTITY was added to SQL Server as a solution to exactly these drawbacks to creating nextNumber values. You don't want to do a MAX aggregate every time, and you don't want to cause serialization through a next-value table.

So if you need unique values and you also need good concurrency, IDENTITY is a great option. Gaps are possible, but not necessarily a bad thing.

Don|||Thats exactly the kind of reply i was looking for.

What we do exactly we have a table called "SystemNumber"
where we put a ID and value and then we increment from there.

LIke " CustomerID 1212
next time somebody does an insert it will because CustomerID 1213 etc

If my understading is correct in a heavily used system this will method will lock out everyone else trying to do the same thing.
Doubt
If you want to retrieve the ID that "Autoincrement" generated you have to use @.@.Identity.

Does this cause performance issues?
Why is it that this method does not lock?

Do you have to enclose the @.@.Identity within a transaction in order not to retrieve somebody else identity?

What Im trying to say How do i retrieve my new identity and guarantee that it is my one and not somebody else?

Could you give an example or link that will clarify everything?
Thanks a lot|||

hi,
i want to generate a next number with out auto increment.I want to write a trigger so when ever the data into other columns the trigger should be fired and the row should be updated with incremented value
advance thanks

|||Re: "What Im trying to say How do i retrieve my new identity and guarantee that it is my one and not somebody else?"

I think the SCOPE_IDENTITY function is what you are after.

Regards|||Thanks for your reply.
I will read more about Scope_identity .I think this is what I am looking for.
|||I'll consider that you hav a table Mytable with the primary key ID and the field you want to increment named RowNum


declare @.ID int
declare c cursor local
for select ID from Inserted
open c
fetch next from c into @.ID
while @.@.fetch_status =0
begin
update @.tbl
set RowNum = (SELECT ISNULL(MAX(RowNum), 0) + 1 FROM MyTable )
where @.ID = ID
fetch next from c into @.ID
end
close c
deallocate c

|||> Gaps are possible, but not necessarily a bad thing.

And if gaps are a bad thing, could this be a nice candidate for a CLR function?
Maybe even a Microsoft sample?

CREATE FUNCTION GetCounterValue(@.counterName nvarchar(256)) RETURNS bigint

Say, a light-weight method that uses a named mutex and a counter (maybe in a memory-mapped file? or...) in tempdb, or somewhere smart.

Or you might have enough work to do already...

Gorm Braarvig

Confused whether to generate a nextNumber or to use Autoincrement

In my office we generate an unique ID on a table by doing something like:

Declare @.ID int
SELECT @.ID= ISNULL(MAX(@.ID), 0) + 1 FROM MyTable

Are there any benefit in using this methods rather then the Autoincrement functionality provided by sql server?

Could somebody explain which method is better and why?
Hi,
I think there is a benefit is that you will not has gaps in the ID field if you rolled back your transaction.
if you have an identity field in a table and the next value for example will be 100 and you make an insert statement then rolled back the insert then the next insert statement will have 101. but your way you have the control
on the other side I think having the identity field will be more efficient in terms of performance because you dont have to aggregate to get the max|||On a heavily used system your method will likely get conflicts as several connections attempt to do the insert at the same time, retrieving the same max value, and then doing the insert. You'll have to have some incrementing retry. And you'll likely have reduced concurrency when compared to using IDENTITY.

Another alternative that removes the MAX aggregate is to have a table with a single row with the next value to use, but then you you have to increment that, locking out anyone else trying to do the same thing until your transaction completes .. and you'll likely end up rolling back your insert in some cases, thereby losing your incremented value and having gaps in your sequence.

IDENTITY was added to SQL Server as a solution to exactly these drawbacks to creating nextNumber values. You don't want to do a MAX aggregate every time, and you don't want to cause serialization through a next-value table.

So if you need unique values and you also need good concurrency, IDENTITY is a great option. Gaps are possible, but not necessarily a bad thing.

Don|||Thats exactly the kind of reply i was looking for.

What we do exactly we have a table called "SystemNumber"
where we put a ID and value and then we increment from there.

LIke " CustomerID 1212
next time somebody does an insert it will because CustomerID 1213 etc

If my understading is correct in a heavily used system this will method will lock out everyone else trying to do the same thing.
Doubt
If you want to retrieve the ID that "Autoincrement" generated you have to use @.@.Identity.

Does this cause performance issues?
Why is it that this method does not lock?

Do you have to enclose the @.@.Identity within a transaction in order not to retrieve somebody else identity?

What Im trying to say How do i retrieve my new identity and guarantee that it is my one and not somebody else?

Could you give an example or link that will clarify everything?
Thanks a lot|||

hi,
i want to generate a next number with out auto increment.I want to write a trigger so when ever the data into other columns the trigger should be fired and the row should be updated with incremented value
advance thanks

|||Re: "What Im trying to say How do i retrieve my new identity and guarantee that it is my one and not somebody else?"

I think the SCOPE_IDENTITY function is what you are after.

Regards|||Thanks for your reply.
I will read more about Scope_identity .I think this is what I am looking for.
|||I'll consider that you hav a table Mytable with the primary key ID and the field you want to increment named RowNum


declare @.ID int
declare c cursor local
for select ID from Inserted
open c
fetch next from c into @.ID
while @.@.fetch_status =0
begin
update @.tbl
set RowNum = (SELECT ISNULL(MAX(RowNum), 0) + 1 FROM MyTable )
where @.ID = ID
fetch next from c into @.ID
end
close c
deallocate c

|||> Gaps are possible, but not necessarily a bad thing.

And if gaps are a bad thing, could this be a nice candidate for a CLR function?
Maybe even a Microsoft sample?

CREATE FUNCTION GetCounterValue(@.counterName nvarchar(256)) RETURNS bigint

Say, a light-weight method that uses a named mutex and a counter (maybe in a memory-mapped file? or...) in tempdb, or somewhere smart.

Or you might have enough work to do already...

Gorm Braarvig

Sunday, 19 February 2012

configuring the number of worker threads

We have a SQL 2000 server with max worker threads configured to be
3232767275275
The server sees some pretty high usage and we've been seeing some slow down
in the server. I've read that most systems run well with a value of 255.
I've also read something that suggests that lowering this value may help. Is
it better to lower the value or increase it to help performance?
Thanks
MG
Hi,
The default value set is 255 and is more than enough.
Most cases it is not required to increase the value; if the actual number
of connections exceeds the amount set in threads, SQL Server does a pooling
for worker threads so that the next available worker thread can handle the
request.
From Query anayzer run the command to get the correct value
SP_CONFIGURE 'max worker threads'
From the output see the running value. If the value is higher than 255 try
to change and monitor the threads using performance montor, Process, Thread
counter. This will give a clear picture of thread usage.
Thanks
Hari
SQL Server MVP
"MGeles" <michael.geles@.thomson.com> wrote in message
news:608FFF4F-87C3-4011-AB16-6C4412A23870@.microsoft.com...
> We have a SQL 2000 server with max worker threads configured to be
> 32 32767 275 275
> The server sees some pretty high usage and we've been seeing some slow
> down
> in the server. I've read that most systems run well with a value of 255.
> I've also read something that suggests that lowering this value may help.
> Is
> it better to lower the value or increase it to help performance?
> Thanks
> --
> MG
|||See http://support.microsoft.com/default...b;en-us;319942 for
additional information.
Adrian
"MGeles" <michael.geles@.thomson.com> wrote in message
news:608FFF4F-87C3-4011-AB16-6C4412A23870@.microsoft.com...
> We have a SQL 2000 server with max worker threads configured to be
> 32 32767 275 275
> The server sees some pretty high usage and we've been seeing some slow
> down
> in the server. I've read that most systems run well with a value of 255.
> I've also read something that suggests that lowering this value may help.
> Is
> it better to lower the value or increase it to help performance?
> Thanks
> --
> MG
|||MGeles,
This is a very small part of a much wider area of performance tuning. Have a
look through the articles at www.sql-server-performance.com for some
excellent methods on tuning your server.
You need to ascertain in a nutshell:
1) Is there any blocking happening > 1 sec
2) Is the disk IO bound
3) Is the server CPU bound
4) Which queries slow down the server
5) How much paging is happening in windows
6) How's the sql server buffer cache hit ratio - indicates RAM starvation
Also check your query plans and make sure your indexing is optimal. A
technique I use is to trace a typical workload into a SQL server table and
then query the trace table for the most cpu, io intensive and longest running
queries. I then check the query plans for these queries, and also the code.
You may even want to look at the database design itself - the biggest
performance bangs for your buck can often be obtained through tuning your
database design.
Regards,
Mark.
"MGeles" wrote:

> We have a SQL 2000 server with max worker threads configured to be
> 3232767275275
> The server sees some pretty high usage and we've been seeing some slow down
> in the server. I've read that most systems run well with a value of 255.
> I've also read something that suggests that lowering this value may help. Is
> it better to lower the value or increase it to help performance?
> Thanks
> --
> MG

configuring the number of worker threads

We have a SQL 2000 server with max worker threads configured to be
32 32767 275 275
The server sees some pretty high usage and we've been seeing some slow down
in the server. I've read that most systems run well with a value of 255.
I've also read something that suggests that lowering this value may help. I
s
it better to lower the value or increase it to help performance?
Thanks
--
MGHi,
The default value set is 255 and is more than enough.
Most cases it is not required to increase the value; if the actual number
of connections exceeds the amount set in threads, SQL Server does a pooling
for worker threads so that the next available worker thread can handle the
request.
From Query anayzer run the command to get the correct value
SP_CONFIGURE 'max worker threads'
From the output see the running value. If the value is higher than 255 try
to change and monitor the threads using performance montor, Process, Thread
counter. This will give a clear picture of thread usage.
Thanks
Hari
SQL Server MVP
"MGeles" <michael.geles@.thomson.com> wrote in message
news:608FFF4F-87C3-4011-AB16-6C4412A23870@.microsoft.com...
> We have a SQL 2000 server with max worker threads configured to be
> 32 32767 275 275
> The server sees some pretty high usage and we've been seeing some slow
> down
> in the server. I've read that most systems run well with a value of 255.
> I've also read something that suggests that lowering this value may help.
> Is
> it better to lower the value or increase it to help performance?
> Thanks
> --
> MG|||See http://support.microsoft.com/defaul...kb;en-us;319942 for
additional information.
Adrian
"MGeles" <michael.geles@.thomson.com> wrote in message
news:608FFF4F-87C3-4011-AB16-6C4412A23870@.microsoft.com...
> We have a SQL 2000 server with max worker threads configured to be
> 32 32767 275 275
> The server sees some pretty high usage and we've been seeing some slow
> down
> in the server. I've read that most systems run well with a value of 255.
> I've also read something that suggests that lowering this value may help.
> Is
> it better to lower the value or increase it to help performance?
> Thanks
> --
> MG|||MGeles,
This is a very small part of a much wider area of performance tuning. Have a
look through the articles at www.sql-server-performance.com for some
excellent methods on tuning your server.
You need to ascertain in a nutshell:
1) Is there any blocking happening > 1 sec
2) Is the disk IO bound
3) Is the server CPU bound
4) Which queries slow down the server
5) How much paging is happening in windows
6) How's the sql server buffer cache hit ratio - indicates RAM starvation
Also check your query plans and make sure your indexing is optimal. A
technique I use is to trace a typical workload into a SQL server table and
then query the trace table for the most cpu, io intensive and longest runnin
g
queries. I then check the query plans for these queries, and also the code.
You may even want to look at the database design itself - the biggest
performance bangs for your buck can often be obtained through tuning your
database design.
Regards,
Mark.
"MGeles" wrote:

> We have a SQL 2000 server with max worker threads configured to be
> 32 32767 275 275
> The server sees some pretty high usage and we've been seeing some slow dow
n
> in the server. I've read that most systems run well with a value of 255.
> I've also read something that suggests that lowering this value may help.
Is
> it better to lower the value or increase it to help performance?
> Thanks
> --
> MG

configuring the number of worker threads

We have a SQL 2000 server with max worker threads configured to be
32 32767 275 275
The server sees some pretty high usage and we've been seeing some slow down
in the server. I've read that most systems run well with a value of 255.
I've also read something that suggests that lowering this value may help. Is
it better to lower the value or increase it to help performance?
Thanks
--
MGHi,
The default value set is 255 and is more than enough.
Most cases it is not required to increase the value; if the actual number
of connections exceeds the amount set in threads, SQL Server does a pooling
for worker threads so that the next available worker thread can handle the
request.
From Query anayzer run the command to get the correct value
SP_CONFIGURE 'max worker threads'
From the output see the running value. If the value is higher than 255 try
to change and monitor the threads using performance montor, Process, Thread
counter. This will give a clear picture of thread usage.
Thanks
Hari
SQL Server MVP
"MGeles" <michael.geles@.thomson.com> wrote in message
news:608FFF4F-87C3-4011-AB16-6C4412A23870@.microsoft.com...
> We have a SQL 2000 server with max worker threads configured to be
> 32 32767 275 275
> The server sees some pretty high usage and we've been seeing some slow
> down
> in the server. I've read that most systems run well with a value of 255.
> I've also read something that suggests that lowering this value may help.
> Is
> it better to lower the value or increase it to help performance?
> Thanks
> --
> MG|||See http://support.microsoft.com/default.aspx?scid=kb;en-us;319942 for
additional information.
Adrian
"MGeles" <michael.geles@.thomson.com> wrote in message
news:608FFF4F-87C3-4011-AB16-6C4412A23870@.microsoft.com...
> We have a SQL 2000 server with max worker threads configured to be
> 32 32767 275 275
> The server sees some pretty high usage and we've been seeing some slow
> down
> in the server. I've read that most systems run well with a value of 255.
> I've also read something that suggests that lowering this value may help.
> Is
> it better to lower the value or increase it to help performance?
> Thanks
> --
> MG