Query #1 from the code below at the very first run takes 1-2 seconds to execute, second and further runs under 0.1 second. Meanwhile queries #2 and #3 always execute within 0.1 - 0.2 seconds. Is there any explanation for this behaviour? I would understand if it was vice versa because of false condition in #1.
I am not providing execute plan yet because that view is a bit complicated and not sure that plan itself would help. But I will if it's indeed required
1
SELECT
doc_id, line_num, load_id, ord_qty
FROM someview v
WHERE doc_id = 2987541 and 'A'='X' and v.iter_num =
(select max(iter_num) from someview where doc_id=v.doc_id);
2
SELECT
doc_id, line_num, load_id, ord_qty
FROM someview v
WHERE doc_id = 2987541 and v.iter_num =
(select max(iter_num) from someview where doc_id=v.doc_id);
3
SELECT
doc_id, line_num, load_id, ord_qty
FROM someview v
WHERE doc_id = 2987541 and 'X'='X' and v.iter_num =
(select max(iter_num) from someview where doc_id=v.doc_id);