开发者

.Net to SQL, best way to compare unique values

开发者 https://www.devze.com 2023-02-05 23:16 出处:网络
i have an import process, where i take a CSV file in .Net, and it writes to two user tables, in the DataBase. It\'s a pretty complex process, it takes several minutes to process about five hundred use

i have an import process, where i take a CSV file in .Net, and it writes to two user tables, in the DataBase. It's a pretty complex process, it takes several minutes to process about five hundred users at at time. In that process, i need to generate a st开发者_如何学运维ring, random string, that will be unique to each user, as it gives him access to some promotions. I can't use GUIDs because it has to be a simple string for the user to input in a splash screen. What I need to know is, what is the best way to check if each newly generated key doesn't repeat any already created in thousands of pre-existing users. I don't want to add a new query in each inserted row, asking if the string is already there.

Thanks, i hope i was clear enough


How many users are there compared to the number of possible unique keys?

If there are many more possible keys than there are users then I'd just add a unique constraint on the key column, and generate a new key if you hit a constraint violation.

If you're likely to get a lot of collisions with the above technique then there are a few options open to you:

  • Pre-generate sets of unique keys, store them in a table somewhere and take one when needed.
  • Add some uniqueness to the keys: do the users have a unique id that could be incorporated?


You can store string in Dictionary in Key value. If string is repeated then it will generate an error, here you can handle error and generate new string for user.

Hope it will help for you.


One simple way could be to use part of an MD5 for the customer + the customerid encoded in hex.

The customerid part ensures uniqueness and the MD5 part ensures that you cannot guess another users key.

Depending on how short string you can handle you can use just the first 6-10 chars from the MD5 and if you need to shorten it further reencode using somthing more compact than Hex, like base-64 or if you cannot handle different case make your own selection, A-Z + 0-9 and maybe some special chars to get an exponent of 2 that is easy to map to hex.

0

精彩评论

暂无评论...
验证码 换一张
取 消