Subject Re: [ib-support] Selects and Updates based on sub-queries
Author Svein Erling Tysvær
Congratulations Sean, you managed to confuse Helen to give you the wrong
answer (that happens very rarely with simple questions)!

The correct SQL to execute this query lightening fast (unless there is
no index for Temp_Table.RecNo or I am confused too) is:

UPDATE Table1
SET Flag = -1
WHERE EXISTS(SELECT 1 FROM Temp_Table WHERE Temp_Table.RecNo =
Table1.RecNumber)

The problem with the original query is that the subselect is executed
once for each record. Using EXISTS is generally much better (if you care
about speed or CPU usage) than using IN <subselect>.

The bad news is that using temporary tables is not generally recommended
with Interbase. Normal tables holding values temporarily is OK though.

Set

sean_brad wrote:
>
> Hi,
>
> I'm using values in a temporary table as a means to select rows from
> a main table, as in the example below:
>
> update Table1 set Flag = -1 where RecNumber in
> (select RecNo from Temp_Table)
>
> However when I run this query it does not seem to terminate, and the
> IBServer process processor usage goes up to 99% even when there are
> very few
> rows in the temporary table.
>
> Is there a better way to write this type of query or could there be
> something wrong with my Server setup?
>
> Thanks in advance,
>
> Sean