UUID performance in MySQL?

前端 未结 9 1429
猫巷女王i
猫巷女王i 2020-11-27 10:14

We\'re considering using UUID values as primary keys for our MySQL database. The data being inserted is generated from dozens, hundreds, or even thousands of remote computer

相关标签:
9条回答
  • 2020-11-27 10:43

    I tend to avoid UUID simply because it is a pain to store and a pain to use as a primary key but there are advantages. The main one is they are UNIQUE.

    I usually solve the problem and avoid UUID by using dual key fields.

    COLLECTOR = UNIQUE ASSIGNED TO A MACHINE

    ID = RECORD COLLECTED BY THE COLLECTOR (auto_inc field)

    This offers me two things. Speed of auto-inc fields and uniqueness of data being stored in a central location after it is collected and grouped together. I also know while browsing the data where it was collected which is often quite important for my needs.

    I have seen many cases while dealing with other data sets for clients where they have decided to use UUID but then still have a field for where the data was collected which really is a waste of effort. Simply using two (or more if needed) fields as your key really helps.

    I have just seen too many performance hits using UUID. They feel like a cheat...

    0 讨论(0)
  • 2020-11-27 10:44

    Since the primary key is generated decentralised, you don't have the option of using an auto_increment anyway.

    If you don't have to hide the identity of the remote machines, use Type 1 UUIDs instead of UUIDs. They are easier to generate and can at least not hurt the performance of the database.

    The same goes for varchar (char, really) vs. binary: it can only help matters. Is it really important, how much performance is improved?

    0 讨论(0)
  • 2020-11-27 10:46

    Something to take into consideration is that Autoincrements are generated one at a time and cannot be solved using a parallel solution. The fight for using UUIDs eventually comes down to what you want to achieve versus what you potentially sacrifice.

    On performance, briefly:

    A UUID like the one above is 36 characters long, including dashes. If you store this VARCHAR(36), you're going to decrease compare performance dramatically. This is your primary key, you don't want it to be slow.

    At its bit level, a UUID is 128 bits, which means it will fit into 16 bytes, note this is not very human readable, but it will keep storage low, and is only 4 times larger than a 32-bit int, or 2 times larger than a 64-bit int. I will use a VARBINARY(16) Theoretically, this can work without a lot of overhead.

    I recommend reading the following two posts:

    • Brian "Krow" Aker's Idle Thoughts - Myths, GUID vs Autoincrement
    • To UUID or not to UUID ?

    I reckon between the two, they answer your question.

    0 讨论(0)
  • 2020-11-27 10:50

    Instead of centrally generating unique keys for each insertion, how about allocating blocks of keys to individual servers? When they run out of keys, they can request a new block. Then you solve the problem of overhead by connecting for each insert.

    Keyserver maintains next available id

    • Server 1 requests id block.
    • Keyserver returns (1,1000)
      Server 1 can insert a 1000 records until it needs to request a new block
    • Server 2 requests index block.
    • Keyserver returns (1001,2000)
    • etc...

    You could come up with a more sophisticated version where a server could request the number of needed keys, or return unused blocks to the keyserver, which would then of course need to maintain a map of used/unused blocks.

    0 讨论(0)
  • 2020-11-27 10:51

    I would assign each server a numeric ID in a transactional manner. Then, each record inserted will just autoincrement its own counter. Combination of ServerID and RecordID will be unique. ServerID field can be indexed and future select performance based on ServerID (if needed) may be much better.

    0 讨论(0)
  • 2020-11-27 10:51

    What about some hand crafted UID? Give each of the thousands of servers an ID and make primary key a combo key of autoincrement,MachineID ???

    0 讨论(0)
提交回复
热议问题