Using INT or GUID as primary key

I tried to create a column IDon the VB.net SQL server that would provide a sequence of numbers for each new row created in the database. Therefore, to create an ID column, I used the following method.

select * from T_Users
ALTER TABLE T_Users     
ADD User_ID INT NOT NULL IDENTITY(1,1) Primary Key

Then I registered several usernames in the database and it worked fine. For example, the first six lines will be 1,2,3,4,5,6. Then I registered 4 more users the next day, but this time the ID numbers moved from 6 to A, a very large number, for example: 1,2,3,4,5,6,1002,1003,1004,1005. Then two days later I registered two more users, and new lines - 3002,3004. So my question is why it misses such a large amount every other day when I register users. Is the technique I used to create the sequence wrong? If this is wrong, can someone tell me how to do it right? Now that I have become disillusioned with the technique described above, I tried using sequentially generated GUID values.A sequence of GUID values ​​was generated by a penalty. However, the only drawback is that it generates very long numbers (4 times the size of INT). My question here is does the GUID take any significant advantage over INT?

Hello,

+4
source share
2 answers

GUID Surface:

GUIDs are good if you ever want offline clients to be able to create new records, since you will never encounter a primary key when new records are synchronized with the main database.

Bottom side of the GUID:

GUIDS as primary keys can affect database performance, because for a clustered primary key, the database wants to keep the rows in the order of the key values. But that means a lot of insertions between existing entries, because the GUIDs will be random.

IDENTITY , , , . .

, -GUID, , 70 , .

, : ) , ) SQL, .. UPDATE TABLE SET FIELD = 'value' where KEY = 50003, UPDATE TABLE SET FIELD = 'value' where KEY = '{F820094C-A2A2-49cb-BDA7-549543BB4B2C}'

IDENTITY . , , . IDENTITY , . , .

EDIT:

, -GUID. GUID

SQL Server 2005+ NEWSEQUENTIALID(), , , . . http://technet.microsoft.com/en-us/library/ms189786%28v=sql.90%29.aspx

+4

, ?

. -, Google . " Sql" TON , :

SQL Server 2012, 6 1000+ 7-

:

IDENTITY?

, sql . - , , .

: SQL Server 1000 - (, ), .

http://www.sqlserver-training.com/sequence-breaks-gap-in-numbers-after-restart-sql-server-gap-between-numbers-after-restarting-server/-

sqyuence ( nin sql 2012), (pregeneration) 1 - , .

, GUID - INT?

. GUID, int. , int32 2 . ( 10 ) 64 int . - zetabyte- , guid , .

, . ( = = = /io ). .

- - 1.

0

Source: https://habr.com/ru/post/1527808/


All Articles