If submit_date
and Last_Changed_date
each have indexes, then the following is likely to perform better than the original first query:
WHERE submit_date >= extract(epoch FROM now() - interval '3 days')
OR Last_Changed_date >= now() - interval '3 days')
Avoid operations on columns in the WHERE
clause so that the condition will be sargable (Search ARGument ABLE); i.e., the database engine is able to scan an index when one is available instead if sequentially reading the whole table.
I chose to use - interval '3 days'
instead of + interval '-3 days'
for readability. Subtracting an interval rather than adding a negative interval imposes less cognitive load on the reader, especially when the -
is buried in a string somewhat distance from the +
.
The second query can be rewritten as follows:
WHERE Test."Log" ILIKE '%%Status: Closed%%'
GROUP BY dbo.PYR.ID_Number
While dbo.Test."Log"
also works as the column reference, it is better to only include the schema in the table reference in the FROM
clause since this minimizes the number of changes that have to be made to the query if the schema changes.
If this is a frequent query condition and it's a large table, then explore options to index Test."Log"
.