Subject | Question about a subquery |
---|---|
Author | Paul R. Gardner |
Post date | 2007-12-18T15:17:23Z |
Can anyone explain this behavior to me? I have an INVOICE table with
100 records. I wanted to update a maximum of 25 records.
UPDATE INVOICE I SET I.FIELD = FOO
WHERE I.INVOICE_ID IN (SELECT FIRST 25 I2.INVOICE_ID FROM INVOICE I2);
To me, the subquery should have executed first returning 25 rows. It
should have had 25 non indexed or natural reads. (Running the subquery
by itself does this exactly). So this is all that should be updated.
In this case all 100 records are updated at the same time. Why is this?
It's not limited to UPDATE statements.
I realize this select statement is VERY poor, but for another example,
here's a similar statement as above rewritten as a selection:
SELECT I.INVOICE_ID FROM INVOICE I
WHERE I.INVOICE_ID IN (SELECT FIRST 25 I2.INVOICE_ID FROM INVOICE I2);
The above statement should only return 25 rows (to me). There should be
25 natural reads from the subselect, and another 25 indexed reads for
the main query since there is an index on that field. Instead I get 100
rows returned, 100 natural reads, and 100 indexed reads.
Is this a bug, or am I not understanding something?
Thanks in advance,
Paul
[Non-text portions of this message have been removed]
100 records. I wanted to update a maximum of 25 records.
UPDATE INVOICE I SET I.FIELD = FOO
WHERE I.INVOICE_ID IN (SELECT FIRST 25 I2.INVOICE_ID FROM INVOICE I2);
To me, the subquery should have executed first returning 25 rows. It
should have had 25 non indexed or natural reads. (Running the subquery
by itself does this exactly). So this is all that should be updated.
In this case all 100 records are updated at the same time. Why is this?
It's not limited to UPDATE statements.
I realize this select statement is VERY poor, but for another example,
here's a similar statement as above rewritten as a selection:
SELECT I.INVOICE_ID FROM INVOICE I
WHERE I.INVOICE_ID IN (SELECT FIRST 25 I2.INVOICE_ID FROM INVOICE I2);
The above statement should only return 25 rows (to me). There should be
25 natural reads from the subselect, and another 25 indexed reads for
the main query since there is an index on that field. Instead I get 100
rows returned, 100 natural reads, and 100 indexed reads.
Is this a bug, or am I not understanding something?
Thanks in advance,
Paul
[Non-text portions of this message have been removed]