Storing hexadecimal values as binary in MySQL

后端 未结 6 952
情话喂你
情话喂你 2020-12-15 08:42

I was thinking about how I\'m storing passwords in my database : appropriately salted SHA1 strings in a CHAR(40) field. However, since the character data in there is actuall

6条回答
  •  情书的邮戳
    2020-12-15 09:10

    Here is my breakdown:

    1. If you use strings instead of binary, use a fixed length field. Since the hashing algos all output a fixed length you can save yourself some space there.
    2. Since you are only doing an equality comparison, there is no need for indexes. Binary fields have no collation type or character set.
    3. BINARY column types have no odd storage caveats like BLOBs do.
    4. Each hexadecimal character represents 4 bits in the 8 (or 7) bits it consumes. This means that binary storage is twice as efficient.
    5. MOST IMPORTANT: Unless you are working in an embedded system where each byte counts, don't do it. Having a character representation will allow you better debugging. Plus, every time a developer is working a problem like this I have to wonder why. Every architectural decision like this has trade-offs and this one does not seem like it adds value to your project.
    6. You can always convert to BINARY later with a simple SQL script.

    In short, use a fixed length text field. There is no gain to counting bytes in the current world, especially when change is easy to achieve.

    Hope this helps.

提交回复
热议问题