Problem
String concatenation is slowing down a query:
date(extract(YEAR FROM m.taken)||'-1-1') d1,
date(extract(YEAR FROM m.taken)||'-1-31') d2
This is realized in code as part of a string, which follows (where the p_ variables are integers, provided as input by end users):
date(extract(YEAR FROM m.taken)||''-'||p_month1||'-'||p_day1||''') d1,
date(extract(YEAR FROM m.taken)||''-'||p_month2||'-'||p_day2||''') d2
This part of the query runs in 3.2 seconds with the dates, and 1.5 seconds without, leading me to believe there is ample room for improvement.
The query’s total run time is under 10 seconds; am looking to bring the entire query down to about 2 or 3 seconds. A hardware upgrade has already happened. 😉
Version
PostgreSQL 8.4.4.
Question
What is a better way to create the date (presumably without concatenation)?
Update
This looks promising: PGTYPESdate_mdyjul
Many thanks!
Wow. I’m surprised, but using the functions from this page – specifically the one to build a date value from three integers – which really do nothing more expose the internal C date functions, really is a lot faster. Benchmarking for me show that creating the dates that way was much faster.
First one is the implementation of the “dateserial” function:
Full Solution
Edit
dateserial.c:Edit
Makefile:Edit
inst.sh(optional):Run
bash inst.sh.Create a SQL function
dateserial:Test the function: