0

I have a Postgre table “tasks” with the fields “start”:timestamptz, “finish”:timestamptz, “type”:int (and a lot of others). It contains about 200m records. Start, finish and type fields have a separate b-tree indexes. I’d like to build a report “Tasks for a period” and need to get all tasks which lay (fully or partially) inside the reporting period. Report could be built for all task types or for the specific one. So I wrote the SQL:

SELECT * FROM tasks 
WHERE start<={report_to} 
AND finish>={report_from}
AND ({report_tasktype} IS NULL OR type={report_tasktype})

and it runs for ages even on short reporting periods. Please advice if there a way to improve performance by altering the query or by creating new indexes on the table? For some reasons I can’t change the structure of the “tasks” table

2
  • 3
    Understand your query using explain Commented May 30, 2021 at 10:07
  • @bigbounty depending on the report parameters. When report_tasktype provided, it uses Index scan on “type” with filter on “start” and “finish” clauses. When the report period is like previous month, it uses Index scan on “finish” with filter on “start”. Otherwise it uses seqscan Commented May 30, 2021 at 13:37

1 Answer 1

2

You would want a GiST index on the range. Since you already have it stored as two end points rather than as a range, you could do a functional index to convert them on the fly.

ON task USING GIST (tstzrange(start,finish))

And then compare the ranges for overlap with &&

It may also improve things to add "type" as a second column to the index, which would require the btree_gist extension.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! GIST keyword brings me here dba.stackexchange.com/questions/39589/…
(and yes, I promise to use search before asking questions)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.