For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
The optimization process and recommendations:
- Avoid Calling Functions With Indexed Columns (query line: 29): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `stamp` is indexed, the index won’t be used as it’s wrapped with the function `date_part`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 30): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `stamp` is indexed, the index won’t be used as it’s wrapped with the function `date_part`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 13): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `calltime` is indexed, the index won’t be used as it’s wrapped with the function `date_part`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 14): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `calltime` is indexed, the index won’t be used as it’s wrapped with the function `date_part`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 16): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `calltime` is indexed, the index won’t be used as it’s wrapped with the function `date_trunc`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
CREATE INDEX mytable_idx_calltime ON "mytable" ("calltime");
The optimized query:
WITH cal AS (SELECT
generate_series('2011-02-02 00:00:00'::timestamp,
'2012-04-01 05:00:00'::timestamp,
'1 hour'::interval) AS stamp), qqq AS (SELECT
date_trunc('hour',
mytable.calltime) AS stamp,
count(*) AS zcount
FROM
mytable
WHERE
mytable.calltime >= '07-13-2011 00:21:09'
AND mytable.calltime <= '07-31-2011 21:11:21'
AND date_part('hour', mytable.calltime) >= 0
AND date_part('hour', mytable.calltime) <= 21
GROUP BY
date_trunc('hour',
mytable.calltime)) SELECT
cal.stamp,
COALESCE(qqq.zcount,
0) AS zcount
FROM
cal
LEFT JOIN
qqq
ON cal.stamp = qqq.stamp
WHERE
cal.stamp >= '07-13-2011 00:00:00'
AND cal.stamp <= '07-31-2011 21:11:21'
AND date_part('hour', cal.stamp) >= 0
AND date_part('hour', cal.stamp) <= 21
ORDER BY
cal.stamp ASC