Question

In Postgres table with ca million rows I have a column bookingtype (character varying(20)), which default value is null and besides that it may have 7 distinct values (1-2 chars). Current situation:

 count  | bookingtype 
--------+-------------
 389408 | w
 143780 | [null]
  18240 | i
  29496 | a
 356323 | m
    959 | e
   7936 | sl
     55 | kr
(8 rows)

I have btree index for the column, but I am not sure, is it optimal for such situation, where is so few distinct values.

I need to filter rows based on the value of bookingtype and it is always equality filtering I use.

Does special enum-type with 7 possible values have advantage before varchar? Does it need indexing?

What is the best way to implement such column with few values in many rows?

Postgres 9.6

Was it helpful?

Solution

It doesn't matter much: text (= varchar) would be a little smaller, and an enum type would be a little faster (to compare).

Make the decision based on usability: if you might ever need to delete one of the values, don't use an enum. The best is probably to stick with what you have now.

As for indexing, that might just about make sense for 8 different values, but you would have to check if any of your queries might actually benefit. Without knowing the queries, it is impossible to say for sure.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top