Coming from a C background, I may be getting too anal about this and worrying unnecessarily about bits and bytes here.
Still, I cant help thinking how the data is actually stored and that if I choose an N which is easily factorizable into a power of 2, the database will be more efficient in how it packs data etc.
Using this “logic”, I have a string field in a table which is a variable length up to 21 chars. I am tempted to use 32 instead of 21, for the reason given above – however now I am thinking that I am wasting disk space because there will be space allocated for 11 extra chars that are guaranteed to be never used. Since I envisage storing several tens of thousands of rows a day, it all adds up.
Question:
Mindful of all of the above, Should I declare varchar(21) or varchar(32) and why?
[Edit]
The data being stored conforms to an external specification, and can never be more than 21 chars long. I am using both mySQL and PostgreSQL, but ideally, I want the answer to be database agnostic, since I try to not get tied down by any particular vendor.
Let the database implementation do the optimization. Use the smallest size that makes sense for the application.
Performance is generally affected most by how many disk operations are necessary, and the smaller the data, the fewer the disk operations. Some databases will do compression or common prefix optimizations to keep the number of disk bytes used to a minimum.