Issue
I would like to like to store a percentage with two decimal places (0 to 100). Thus a decimal field (5 digits total) seems like a good choice, but smallint
would also get the job done with only two bytes.
How many bytes would be utilized in the database for this configuration of decimal field? And as a follow-up question, how would I query Postgres to investigate the details of the data structure used behind the scenes?
Solution
The documentation says:
The actual storage requirement is two bytes for each group of four decimal digits, plus three to eight bytes overhead.
That would be between 5 and 10 bytes in your case.
But storage size should be the last of your concerns here:
- Depending on the types of the fields before and after that number, you might not save as much space as you think, because some data types are aligned at 4 or 8 byte boundaries, and you might lose some of the saved space to padding anyway.
- If you use a lot of arithmetic operations on these data (number crunching),
smallint
orinteger
will perform much better thannumeric
. - On the other hand, you'll have to shift the comma around during arithmetical processing. That is fast, but might make your code less readable, resulting in a maintenance burden.
To learn about the storage and processing of numeric
, you'd have to browse the source code.
Answered By - Laurenz Albe Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.