I’m using MySQL, all my tables are using InnoDB engine. I have some columns declared as DECIMAL(38, 0) and they are used extensively. According to the MySQL documentation (http://dev.mysql.com/doc/refman/5.5/en/storage-requirements.html), 38-digit value requires 17 bytes (38 = 4 * 9 + 2; 4 * 4 + 1 = 17). Okay.
But, does that mean that any value stored in this column will take 17 bytes? For example, for value 432 – will it take 4 bytes only (I really hope so…) or will it take 17 bytes anyway?
Finally, I know that in Oracle the size occupied depends on the actual values stored. But is it optimized that way in MySQL as well?
I think the answer is that it will take 17 bytes anyway. If you notice, detailed in the linked manual page there is no means for the DBMS to record how “long” the value is. By comparison, for a
VARCHAR(255) CHARACTER SET asciicolumn there is a single byte at the start of the value that indicates how long the value is (for a maximum size of 256 bytes). For aVARCHAR(1000) CHARACTER SET asciicolumn there are two bytes to indicate the length. Here no means is detailed to record the length of the value, leading me to conclude that the column always takes the maximum amount of space.