Avatar of bmgoa
bmgoa asked on

Index design and synchronization

Hello,
we have a table like:
       Table X
       (
         id bigint,
         nam varchar(10),
         stock varchar(5),
         Updated_Date datetime
        )

we insert records into the above Table X at a high rate in a multi-threaded fashion.

now, the id should be picked up from another table RefId.

Table RefID
 (
   NextUseID int
 )
 

This RefID table stores the Id to be used next when inserting into X and then we have to increment NextUseId by 1.

I query Table X extensively on nam and stock fields.

the updated_datetime is to be populated using getdate().

Now my question i)is what would be the ideal candidates for indexes on X
2)how should i ensure the NextUseId is
not corrupted via multithreaded references.i.e. several clients wanting to insert into X will read nextuseid and then increment it by 1.If this is simulaneous, then there will be data integrity issues.

we have not yet decided on whether to use Sql2k or 2005. so a genric soln would be appreaciated


Regards
bm
Microsoft SQL Server 2005Microsoft SQL Server

Avatar of undefined
Last Comment
kevindockerty

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

question:
why no just make the field ID a identity field?
that way, the "incremental" values will be provided directly from SQL server, safe for multiple user inserts.
after the insert, a SELECT SCOPE_IDENTITY() can tell you the value generated.
kevindockerty

I totally agree with Angelll ( identity )

Table X
(
         id bigint IDENTITY (1, 1) NOT NULL,
         nam varchar(10),
         stock varchar(5),
         Updated_Date datetime
)

As for indexing
" query Table X extensively on nam and stock fields."
You've answered your own question here then - "name" & "stock"
 
ASKER
bmgoa

Hello ,
       Yeah , i thought of that but i need to recycle the ID i.e. when the ID value crosses a certain threshold, i need to reset it to an old value. e.g. ID is greater than 99999 then reset it to 33333.
can this be safely done in an multithreaded insert enviroement using identity?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
kevindockerty

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
kevindockerty

if your resetting id's like that perhaps a re think of your database design might be useful. In particular a good look at exactly what you are trying to achieve here and why ?

re using id's, I've always found this to be a bit dangerous !
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
bmgoa

Kevin,
        would you please elaborate when u say it is "dangerous"?
         thn
Regards,
kevindockerty

perhaps "dangerous" was a bit strong - I should have said be careful

What I meant was, you need to make sure you don't re use an ID value that you have previously used and need to preserve.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.