🌐 이 글의 한국어 원문이 있습니다.한국어 원문 보기 →

· 10 min read

Database Lab - CHAR vs NUMERIC vs TIME

Which type is more efficient for storing and querying time data in PostgreSQL?

I ran an experiment storing a time in HH:MM format in each of three types: CHAR, NUMERIC, and TIME. I used PostgreSQL version 13.

Condition 1.

,ALARM_TIME char(4)

The first condition stores the HH:MM time format as four CHARs. 04:30 is stored as ‘0430’.

Condition 2.

,ALARM_HOUR numeric(2, 0) NOT NULL
    CHECK (0 <= ALARM_HOUR and ALARM_HOUR <= 23)
,ALARM_MIN numeric(2, 0) NOT NULL
    CHECK (0 <= ALARM_MIN and ALARM_MIN <= 59)

The second condition splits the time across two NUMERIC columns. For 04:30, ‘4’ goes into the first column and ‘30’ into the second.

Condition 3.

,ALARM_TIME time NOT NULL

This one uses the TIME data type supported by PostgreSQL as is. 04:30 is stored as ‘04:30:00’. Assuming the trailing part is always ‘00’, queries fetch results that exactly match ‘04:30:00’.

All three use the same setup, but to make the environment more realistic I added a USER_ID column and a MODIFIED_TIME column.

After inserting 5,000 randomly generated rows into each, I measured how long it takes to query every time from 00:00 through 23:59. One cycle issues 1,440 queries. I'll run each condition 10 times and take the overall average.

Results

Full results
-- alarm table 1 --
count: 5000
try 1: 26.156119346618652
count: 5000
try 2: 26.675641775131226
count: 5000
try 3: 25.536415815353394
count: 5000
try 4: 26.134907960891724
count: 5000
try 5: 26.375614166259766
count: 5000
try 6: 26.147720336914062
count: 5000
try 7: 25.97940969467163
count: 5000
try 8: 26.59768557548523
count: 5000
try 9: 26.10726761817932
count: 5000
try 10: 25.555443286895752
average: 26.126652979850768
-- alarm table 2 --
count: 5000
try 1: 27.78176712989807
count: 5000
try 2: 28.27575707435608
count: 5000
try 3: 28.567619562149048
count: 5000
try 4: 27.774380207061768
count: 5000
try 5: 28.072192430496216
count: 5000
try 6: 27.556775093078613
count: 5000
try 7: 28.35179305076599
count: 5000
try 8: 27.923873901367188
count: 5000
try 9: 28.38708519935608
count: 5000
try 10: 28.01127815246582
average: 28.070282173156738
-- alarm table 3 --
count: 5000
try 1: 26.133213996887207
count: 5000
try 2: 26.16308617591858
count: 5000
try 3: 26.199642419815063
count: 5000
try 4: 26.07395052909851
count: 5000
try 5: 25.948384284973145
count: 5000
try 6: 26.366111516952515
count: 5000
try 7: 25.966031551361084
count: 5000
try 8: 25.623215198516846
count: 5000
try 9: 25.9598867893219
count: 5000
try 10: 26.27301859855652
average: 26.070684099197386

Condition 1 average: 26.126652979850768

Condition 2 average: 28.070282173156738

Condition 3 average: 26.070684099197386

What stands out is that ‘Condition 2’ is about 2 seconds slower than the other conditions. I suspect the delay comes from querying two columns. Interestingly, Condition 3 with the TIME data type comes out ahead by about 0.5 seconds even over Condition 1, which uses just char(4).

Next, I increased the number of rows to 100,000. I also increased the number of repetitions to 20 each.

Full results
-- alarm table 1 --
try 1: 49.7254912853241
try 2: 46.3009729385376
try 3: 46.4652533531189
try 4: 45.91221594810486
try 5: 49.378690242767334
try 6: 49.91247344017029
try 7: 49.37316608428955
try 8: 49.696730852127075
try 9: 49.3984112739563
try 10: 48.69150996208191
try 11: 49.45136594772339
try 12: 50.06901669502258
try 13: 49.74210596084595
try 14: 49.26009392738342
try 15: 50.01587390899658
try 16: 50.19118285179138
try 17: 49.86866998672485
try 18: 50.08266472816467
try 19: 49.106056451797485
try 20: 49.522252559661865
average: 49.10826389789581
-- alarm table 2 --
try 1: 66.98592329025269
try 2: 66.49290442466736
try 3: 63.25885629653931
try 4: 64.06613063812256
try 5: 63.3563506603241
try 6: 64.6199848651886
try 7: 63.98414373397827
try 8: 64.18078875541687
try 9: 64.31526279449463
try 10: 64.02151012420654
try 11: 64.07046294212341
try 12: 63.84868574142456
try 13: 64.07298874855042
try 14: 64.08221626281738
try 15: 63.90463852882385
try 16: 63.61009621620178
try 17: 64.52658748626709
try 18: 62.94241213798523
try 19: 64.85735058784485
try 20: 63.55243444442749
average: 64.23751240968704
-- alarm table 3 --
try 1: 41.83094239234924
try 2: 44.932230949401855
try 3: 44.792407274246216
try 4: 44.53584957122803
try 5: 44.88702201843262
try 6: 44.57245588302612
try 7: 44.692646503448486
try 8: 43.89505863189697
try 9: 44.2195839881897
try 10: 44.22889471054077
try 11: 45.12163472175598
try 12: 44.2561240196228
try 13: 44.0642147064209
try 14: 45.43576169013977
try 15: 45.286035776138306
try 16: 43.428226947784424
try 17: 43.472302198410034
try 18: 44.39334988594055
try 19: 44.21790099143982
try 20: 43.478670597076416
average: 44.287118303775785
[Finished in 3152.7s]

Condition 1 average: 49.10826389789581

Condition 2 average: 64.23751240968704

Condition 3 average: 44.287118303775785

Increasing the data about ‘twentyfold’, from 5,000 to 100,000 rows, roughly doubled the query time. As the volume grows, the differences in query time become pronounced: Condition 2 took a full 15 seconds longer than Condition 1. Interestingly, Condition 3 is about 5 seconds faster than Condition 1.

Conclusion 1. PostgreSQL optimizes ‘time columns’ well on its own, so for anything time-related, use a time-related data type.

Conclusion 2. Unless you're in an environment with more than 100,000 rows piling up, the actual difference in query time is small. The differences between data types are really in a minor range.

Related posts

Home