How to save unique insertions of datetime2 columns

I have a table where stock ticks are stored in the 2008 sql server. It is used in the character column and in the date column, which is datetime2. In C # applications, inserts are sent at a high speed to the table. Currently, a string is created using the character and DateTime.Now, and then inserted. Sometimes a situation may arise when it includes 2 ticks for one character and the datetime.now time will return to the same time, up to a millisecond. My question is how can I assure that my inserts do not use the same time for a given character. I fiqure I could have another unique key, for example, rowver or somenind autonumber in db as one solution, but even if the insert was successful, I do not want there to be duplicate time for this character.I also thought about saving the last insert time and just adding a millisecond to it if it is equal. I am not sure if this is the best way. In addition, datetime2 loops have more precession than .net date, so I don’t know if I will try to solve this problem on the server or client.

thanks

I want to keep every insertion no matter what, and for performance reasons, I don’t want to check the old values, so the solution that I will go for is an identity column. This will allow me to keep the insertion order by allowing duplicates in datetime2.

+3
source share
4 answers

Do not use datetime in the key.

Use the identifier column as the primary key and just put the index (can be clustered if you want) at the + date-time mark. I'm not sure what you want with duplications, but in this way you can record that they happened ...

+2
source

, , , ?

datetime, . , .

- timestamp, , , .

: ? , , , ? , ?

+1

# DateTime.Now, DateTime.Now , . Datetime2.

edit:..., SYSDATETIME()

+1
source

Is your thread plugin database safe? That is, is it possible that two inserts can occur simultaneously and not know about each other? In this case, maintaining the highest and adding a millisecond will not work.

You may need to have a regular procedure that goes through and increases time in the event of a collision.

0
source

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


All Articles