tinyint or varchar(1)?
I have some fields in some tables that can contain only value from 0 to 9
, and other that can contain 0 or 1
.
For your experience, is it better to store it with TINYINT
or VARCHAR(1)
?
As memory usage, speed to re开发者_如何学编程cognize them, and so on...
It depends on how you are going to use the values: as strings or as numbers. If you're going to use them as numbers, TINYINT(1)
is definitely better as mysql won't need to do unnecessary conversions. For 1-character strings you could use CHAR(1)
.
It shouldn't matter a whol lot, although I would choose CHAR(1)
over varchar.
If you use the value as a number, ie you're going to Add or Subtract them, use TinyInt.
Otherwise, try to look ahead. Is there a chance there will ever be new values? What would be the range then?
If you can use a TINYINT(1)
, you're better off using that, as a VARCHAR(1)
takes up twice the amount of space, per row; TINYINT(1)
uses only 1 byte per row, whereas VARCHAR(1)
uses 1 byte for the length, and then another byte for the actual data. This doubles the amount of data used in querying, and also any associated indexes, etc.
精彩评论