开发者

mysql, multiple columns or a varchar column?

I have to store about 10 int, is it better to use 10 int columns or a only varchar column whit values splitted by a character?

I'll never need to index these columns, or to do mathematical operations.

An exemple of datas could be:

In a 开发者_StackOverflowrow of a thread ( of a forum ) Number of comments | Number of votes | Number of partecipants

These are datas that need only to be cached, not to be updated frequently.


I would use separated ints. Few reasons for this:

  • It's probably more clean solution
  • Gives you flexibility to add indexes later if needed
  • You can sort by them
  • no need to split them for the actual use

More insight in your problem would be necessary to provide some more sophisticated answer.

Edit: Don't believe you will save space. INT is 4 bytes always, number as varchar takes 1 byte for each digit + the delimiter. So if you store number greater than 999 you will actually loose space.


Depends on your needs. Performance is better with int columns, the varchar saves storage space.


I'd prefer to store values separately but it mostly depends on what kind of data it is. it can be better to store them together in particular cases.


10 int columns - with exclusion if they are needed ONLY for display


If you will ever need to access the individual integers you should store them in separate fields. Depending on the intended usage it might be best to have a separate table for the integers, but given your question it's tough to determine if this would be applicable.

Share and enjoy.


10 integer columns will take up less space. 1 integer = 4 bytes (unless it's long int). So 40 bytes constantly regardless of what's entered, whereas varchar can grow plus you need to waste a byte for the delimiter character.


Use separate Integer columns.

Arguments:
- a integer value takes less volume in the database than its string representation
- in the future you will be able to do operations search/filter/sum/average on any of those columns.
- it will ensure the saved data to be integer, any attempt to save something else will be make an error to raise.

They are some exceptional situation where it may be more accurate to save the data in string but we need to know more to judge that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜