Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Our DB design presently has a interval column which will only be storing days (no other interval type) so it is making sense to use INT2 (smallint) instead of interval. Reference to documentation.

Advantage: 2 bytes instead of 12 bytes (we have many such columns).

Is this line of thinking ok or am I overlooking something?

share|improve this question
1  
Sounds OK to me. –  a_horse_with_no_name Mar 8 at 9:20

1 Answer 1

up vote 3 down vote accepted

Perfectly ok - especially since you can add and subtract integer to / from date directly in Postgres.

However, int2 might not be better than a plain integer. True, integer occupies 4 bytes instead of 2, but many operations are optimized for integer. Among other things, integer is the default numeric type for numbers without decimal point. For int2 you often need to add an explicit cast to avoid errors.

Also, while we are talking about a single column, you will most probably gain nothing in regard to disk storage or RAM. Some closer understanding of storage mechanisms is needed here, in particular padding and data alignment.

If you have several int2 columns in a table (or even "many" as you mention) and / or know what you are doing, the odds for int2 get better.

Details:
Configuring PostgreSQL for read performance
Calculating and saving space in PostgreSQL

If in doubt, run a test and measure:
Measure the size of a PostgreSQL table row

share|improve this answer
    
Plus I always think that modern 64bit processors can't handle a two-byte integer anyway. I might be mistaken, but I don't think they can physically handle less than 64bit, everything else is just setting all the other bits to zero (but I could be mistaken there) –  a_horse_with_no_name Mar 10 at 22:17
    
@a_horse_with_no_name from this link stackoverflow.com/questions/163254/… what I understood is performance penalty, if any, is negligible as compared to memory storing and cache hits. But, a good point which should be considered. in fact int8 will be better for 64bit but I am more focused on diskIO. –  vedic Mar 11 at 9:26
    
@Erwin Brandsetter I do have several columns in a table and many such tables :). Some of the table do have 8 columns (or more) of INT2. Casting of INT2 is definitely I will keep in mind. –  vedic Mar 11 at 9:32
    
@vedic: I didn't really think that an int2 will be slower than an int8 on processor level. My point was that it won't be a difference once the data is in memory. –  a_horse_with_no_name Mar 11 at 9:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.