PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 12, 2022

[FIXED] How many bytes will Django/Postgres use for a decimal field?

 August 12, 2022     decimal, django, postgresql, python     No comments   

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 or integer will perform much better than numeric.
  • 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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing