I have a table XX_TEMP. Let us say the columns are inventory_item, organization_id and description
The values are:
Inventory_item | Organization | Description |
---|
200 | m1 | Sample |
200 | m2 | Not Sample |
400 | m4 | check |
700 | m5 | Test |
I just want to check the existence of an item in the table, I have written two queries and would like to know which one is better in terms of performance:
Q1:
select count(1) from xx_temp where inventory_item=200 and rownum=1;
Q2:
select count(1) from dual where exists (select 1 from xx_temp where inventory_item=200);
Both Q1 and Q2 return the same result. In fact, I was surprised with the result from Q1 as I expected that the rownum would be evaluated after the where condition. I expected Q1 to return 2
I thought that the below query:
select count(1) from xx_temp where inventory_item=200;
and Q1 would return the same result as rownum would be evaluated at end. In effect, I've 2 questions:
1. Isn't rownum calculated at the end?
2. What is the best way in terms of performance to check for an existence of record?