Database Optimization of SqlServer add a primary key and self-growth

Today there is need to add 5 million data table primary key column and self-growth, the biggest difficulty is how to UPDATE that more than five million pieces of data and get started!

 

 

Table 1. give add a field called ID, and allowed to air

 

2. Query the table, I think the time to use them to create a table column sort of number we want to generate auto-increment ID.

- where the query out of the fields as the matching criteria when I UPDATE 
the SELECT ROW_NUMBER () over ( the Order  by InsertTime asc ) AS NUM, UserID cUserID, Score cScore, InsertTime cInsertTime, InoutIndex cInoutIndex, ChairID cChairID the FROM  [ RecordDrawScore ] ) c

Serial number has been automatically generated, the next update to our table

- which my query to optimize the results into a temporary table #Temp

 

These data 3.update

UPDATE [RecordDrawScore] SET ID = c.num
FROM #Temp c
WHERE UserID=c.cUserID AND InsertTime = c.cInsertTime AND Score = c.cScore AND InoutIndex =c.cInoutIndex AND ChairID=cChairID 

 

How do we update 4. Check results

SELECT id FROM [dbo].[RecordDrawScore] GROUP BY  id HAVING COUNT(id)>1

 

We add the id did not repeat, it represents a success!

 

5. The table id as the primary key and add growth from

 

6. Try new record

If everything runs smoothly, then there will be a new id

Note that if the table in the UPDATE, INSERT operation to be stopped! Otherwise there will be no new columns ID, you can not continue to grow from the column!

Guess you like

Origin www.cnblogs.com/nnnnnn/p/10948864.html