PostgreSQL Date & Time Function
해당 날짜의 데이터
select count(watt_max) from tbl_test_watt3_sm2ch_min
where to_char(regdate, 'YYYY-MM-DD') = '2016-10-17'
where to_char(regdate, 'YYYY-MM-DD') = '2016-10-17'
소요시간 : 124초 (150만건) (하루: 20*60*60*24 = 1,728,000) 이렇게 하면 망함!
해당 날짜의 데이터
select count(watt) from tbl_test_watt_lsh where
regdate >= date '2016-10-17'
and regdate < date '2016-10-17' + integer '1' // 여기선 하루
소요시간 : 634ms (150만건) (하루: 20*60*60*24 = 1,728,000)
해당 날짜의 데이터
select count(watt) from tbl_test_watt_lsh where
regdate >= current_date
and regdate < current_date + 1
소요시간 : 634ms (150만건) (하루: 20*60*60*24 = 1,728,000)
해당 시간의 데이터
select count(watt) from tbl_test_watt_lsh where regdate
between to_timestamp('2016-10-17 07:40:00' , 'YYYY-MM-DD HH24:MI:SS') and to_timestamp('2016-10-17 07:43:00', 'YYYY-MM-DD HH24:MI:SS')
소요시간 : 14ms (3600건) (하루: 20*60*60*24 = 1,728,000)
select count(watt) from tbl_test_watt_lsh where regdate
between to_timestamp('2016-10-17 07:40:00' , 'YYYY-MM-DD HH24:MI:SS') and to_timestamp('2016-10-17 07:43:00', 'YYYY-MM-DD HH24:MI:SS') + interval '1' // 여기선 1초
소요시간 : 14ms (3620건)
select count(watt) from tbl_test_watt_lsh where regdate
between to_timestamp('2016-10-17 07:40:00' , 'YYYY-MM-DD HH24:MI:SS') and to_timestamp('2016-10-17 07:43:00', 'YYYY-MM-DD HH24:MI:SS') + interval '1' HOUR // 여기선 1 시간
소요시간 : 23ms (63340건)
select count(watt) from tbl_test_watt_lsh where regdate
between to_timestamp('2016-10-17 00:00:00' , 'YYYY-MM-DD HH24:MI:SS') and to_timestamp('2016-10-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS') + '1-1 00:00:00' // 여기선 하루 , 인터벌 값을 이렇게 나타낼 수 있다
'10-10' 는 10년 10개월
소요시간 : 433ms (1496720건)
select * from tbl_test_watt_lsh where to_char(regdate , 'YYYY-MM-DD HH24;MI:SS') > '2016-10-17 07:40:00' AND to_char(regdate , 'YYYY-MM-DD HH24;MI:SS') < '2016-10-17 07:43:00'
역시 to_char 사용하면 망함!
Subscribe to read the full article.