SQL: What is better a Bit or a char(1)

后端 未结 5 523
谎友^
谎友^ 2020-12-17 18:06

Is there any performance difference on retrieving a bit or a char(1) ?

Just for curiosity =]

UPDATE: Suposing i\'m using SQL Server 2008!

5条回答
  •  一整个雨季
    2020-12-17 18:43

    For SQL Server: up to 8 columns of type BIT can be stored inside a single byte, while each column of type CHAR(1) will take up one byte.

    On the other hand: a BIT column can have two values (0 = false, 1 = true) or no value at all (NULL) - while a CHAR(1) can have any character value (much more possibilities)

    So really, it comes down to:

    • do you really need a true/false (yes/no) field? If so: use BIT
    • do you need something with more than just two possible values - use CHAR(1)

    I don't think it makes any significant difference, from a performance point of view - unless you have tens of thousands of columns. Then of course, using BIT which can store up to 8 columns in a single byte would be beneficial. But again: for your "normal" database case, where you have a few, a dozen of those columns, it really doesn't make a big difference. Pick the column type that suits your needs - don't over-worry about performance.....

提交回复
热议问题