I’m creating a view in postgresql
. The logic for the view is as follow;
CREATE OR REPLACE VIEW my_view as
SELECT integer_id_column, datetime_column
FROM my_table r
WHERE state NOT IN ('A', 'B', 'C', 'D', 'E')
AND (
(NOT EXISTS (SELECT 1 FROM another_table q WHERE q.integer_id_column = r.integer_id_column and q.float_column is not null)
AND r.datetime_column > NOW() - INTERVAL '15 minutes'
) OR
(
(SELECT 1 FROM another_table1 res
JOIN another_table ON q.another_integer_id_column = res.another_integer_id_column AND
q.text_id_column = r.text_id_column
)
AND r.datetime_column BETWEEN NOW() - INTERVAL '15 minutes' AND NOW()
)
);
When I do select count(*) from my_view
it times out after 20-30minutes. If I create my_view
as follows:
CREATE OR REPLACE VIEW my_view as
SELECT integer_id_column, datetime_column
FROM my_table r
WHERE state NOT IN ('A', 'B', 'C', 'D', 'E')
the select count(*) from my_view
takes a couple seconds, max, so I’m a bit confused because the extra conditional in the where
clause only decrease the number of rows in my_view
, so how is it that the former view takes much longer to query count(*)
for than the second query?