I have a rather large query that I recently added a new condition to where I check if a field is not equal to some value. I would have used the not equals condition <> but I wanted to set it up to handle multiple values in case more are added in the future. So to keep the things simple, you can think of the larger query looking like the following smaller query:
SELECT * FROM FOO WHERE NVL(BAR, 1) NOT IN (2)
When I ran the query, it added an additional one minute to my execution time. The original query without this condition returns in a few milliseconds. So I tweaked the condition to look like this:
SELECT * FROM FOO WHERE NVL(BAR, 1) = 1
I also tried it out with this
SELECT * FROM FOO WHERE NVL(BAR, 1) <> 2
And both of these queries return in the desired few milliseconds. But why is using NOT IN (2) so much slower than the other approach? It makes no sense to me.
Note: The field bar has a lot of possible null values and the bar column is not indexed.
UPDATE:
Okay I just realized I left out a very important detail. This issue was only present when it was run through Java. So I have a String that contains the query and I run it using
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
I do not see the issue when I run the query directly on SqlDeveloper.
In some case the query optimizer is not able to manage in efficent way a not in as left join null or not exist so the full scan and check for match generate a slow query
Assuming you have proper index on BAR column you could try avoiding the combination of nvl and not in
SELECT *
FROM FOO
WHERE BAR NOT IN ( 2 )
AND BAR IS NOT NULL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.