views:

34

answers:

4

Taken from one of my previous questions.

Table is telling on which item (post , photo or reply) the comment is taking place on.


commentid ---- postid ----- photoid-----replyid
-----------------------------------------------
1                22          NULL        NULL
2                NULL         56         NULL
3                23          NULL        NULL
4                NULL        NULL        55
5                26          NULL        NULL
...
...
...

Is this bad?

A: 

It does. I don't know how much and if it's really that relevant but it's never good to have too many NULL-entries.

alopix
A: 

A better structure would be...

commentid  itemid  itemtype
---------------------------
1          22      post
2          56      photo
3          23      post
4          55      reply
5          26      post

You can even add an index that combines the itemid and itemtype columns so that it's fast to look up all of the comments for a particular item.

Amber
I know :( .. but that still does not answer my question “In MySQL do colums that have ‘NULL’ waste memory”?. Does it take disk space to represent null, also the structure you have advised, can it be used with foreign keys? as in linked to the reply and post tables to if i delete a reply the comment on it also cascades and deletes accordingly? would be great if you could shed come light on that :)
Akay
The space usage depends on which storage engine you're using. MyISAM, there's no difference, whereas InnoDB NULLs don't take space to store.
Amber
And no, such a setup wouldn't allow foreign key. The other option would be to unify all of your replies, posts, and photos into a central "content" table that has a unique id for each of them, and foreign-key that.
Amber
[+1 for easing my pain ;) ]Ah ic, thanks for the clarification :).
Akay
Ahhh im a bit confused at this part "You can even add an index that combines the itemid and itemtype columns so that it's fast to look up all of the comments for a particular item." What do you mean exactly and how would I go about doing it ?
Akay
http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html
Amber
+2  A: 

It depends on the storage engine, and it's entirely up to said storage engine.

In MyISAM for example, whether a value is NULL or not neither takes up more space nor saves space.

In InnoDB, if a column is NULL its value does not need to be stored at all, thus it saves space. The amount of space saved is the size the value would have occupied had it been there.

More info is in this answer.

The bottom line, however, is that neither will have an appreciable effect on performance. It's a micro-optimization, and it's better to make a decision based on what makes sense in your database design rather than what takes up less memory or runs faster.

thomasrutter
A: 

Depends what yoiu mean by waste. Making a calumn NULLable means that a Null Inidicator of some sort needs to be stored, I am not sure about MySql but in other DBMSes I know its a smallint with where '1' inidicates null and '0' inidcates the presence of a value.

This extra two bytes gets carried around with the column value wherever it goes. So you will have a two bytes per nullable column stored, in the buffer, and in the structure presented to you program, plus in any index which references the column. So in your example above 20 bytes more disk storage and perhaps 60 bytes of memory has been "wasted". If this worries you then you should probably be coding everything in assembler and using raw disk IO.

James Anderson