json cast optimization

* We found that having multiple `::json` casts in a query slows down
queries more and more by =~> 33%.
* This change coerces postgres into only casting once. Micro
benchmarking shows =~ 2-3x performance boost
This commit is contained in:
Chris Meyers 2021-04-23 07:59:09 -04:00 committed by Jim Ladd
parent 4d7edbbad0
commit ecdf6cccf8

View File

@ -366,7 +366,7 @@ def _events_table(since, full_path, until, tbl, **kwargs):
{tbl}.uuid,
{tbl}.parent_uuid,
{tbl}.event,
{event_data}->'task_action' AS task_action,
task_action,
(CASE WHEN event = 'playbook_on_stats' THEN event_data END) as playbook_on_stats,
{tbl}.failed,
{tbl}.changed,
@ -377,12 +377,12 @@ def _events_table(since, full_path, until, tbl, **kwargs):
{tbl}.job_id,
{tbl}.host_id,
{tbl}.host_name,
CAST({event_data}->>'start' AS TIMESTAMP WITH TIME ZONE) AS start,
CAST({event_data}->>'end' AS TIMESTAMP WITH TIME ZONE) AS end,
{event_data}->'duration' AS duration,
{event_data}->'res'->'warnings' AS warnings,
{event_data}->'res'->'deprecations' AS deprecations
FROM {tbl}
CAST(x.start AS TIMESTAMP WITH TIME ZONE) AS start,
CAST(x.end AS TIMESTAMP WITH TIME ZONE) AS end,
x.duration AS duration,
x.res->'warnings' AS warnings,
x.res->'deprecations' AS deprecations
FROM {tbl}, json_to_record({event_data}) AS x("res" json, "duration" text, "task_action" text, "start" text, "end" text)
WHERE ({tbl}.id > {since} AND {tbl}.id <= {until})
ORDER BY {tbl}.id ASC) TO STDOUT WITH CSV HEADER'''