Is there a standard way to store year and month in a database ? I need to make some reports based on months and years.
I can't use dates and functions to extract months in real time because the tables are huge, so I need preprocessing.
Is there a standard way to store year and month in a database ? I need to make some reports based on months and years.
I can't use dates and functions to extract months in real time because the tables are huge, so I need preprocessing.
I would go with what @Michael proposes.
Extracting month and year from a date
is super fast with EXTRACT
or to_char()
, there is probably no need for pre-processing.
A date
only occupies 4 bytes on disk, hardly gets better than this.
A possible alternative could be 2 integer
columns with column constraints to safeguard against illegal dates. Occupies 2 x 4 bytes.
Or even 2 smallint
to save RAM and disk storage. Read about and understand data alignment in storage. In many cases you save nothing with smallint
columns. See:
Best to go with a date
column.
Probably the most sensible is to use date_trunc()
to month. You may also add a check constraint for date_trunc()
being equal to the value if you want to.