What kind of fruit fits more units from inside a box of same size, grape or watermelon? Even if you know what it is, what’s the point if you like watermelon?
You should use numeric types to represent numbers, something that depends on calculations, that represents quantities of something (date, stock value, price, etc.). And you should use text to represent descriptions, even if these descriptions have only numeric characters (phone, CPF, etc.).
What you are talking about is something countable (do not need to count, just be a quantity) and should be integer
.
But if you really want to know which takes up the most space is the string
. A guy integer
will take 4 bytes and is per value, so the value is already in the object itself. A string
is a type by reference, so it will already take 4 or 8 bytes (if it is 32 or 64 bytes) only to represent the pointer to the real object. And then the real object will occupy at least 4 bytes (in fact it is almost certain that it is much more, I do not know the implementation details of Delphi for this type).
There will be 4 bytes because I know this type has its lifetime controlled by a reference counter that needs at least 4 bytes, maybe 8 bytes. I’m considering that a string empty has some optimization, but is unlikely. It probably has another 1 to 8 bytes (always depending on the architecture that is running and the type string specific) to indicate the size of this string (in fact it may be that limit to 4 per decision of the implementers). And there may be some other metadata, like a pointer to the type to indicate polymorphism, but maybe you don’t need it in string
because it is a class that cannot be inherited, but it inherits, so you may need to.
And then it will have at least 1 byte more for each character, in this case if it is the text 50000
, will be 5 bytes in standard implementations.
As I don’t know the actual implementation of Delphi it may be that it still has one more terminator character (\0
). And it may be more than 1 byte per character, depending on the encoding (I know Delphi has several , but I don’t know what the default is). And there may be optimizations to store the size of the string.
All this in Delphi, already in the database is different, but the basic idea is the same, the text has 1 byte (or 2 or 4) per character and the integer has 4 bytes (depends a little on the technology).
Anyway, integer
is much smaller and simpler, and gives less problem, is more performatic for several reasons.
Thank you very much, that’s exactly what you put that I wanted to understand.
– Giovani