Re: update 2 fields problem
From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 01/12/05
- Next message: Juraj Rojko: "Re: Permissions question..."
- Previous message: Klaus: "Re: Value of a identity-column"
- In reply to: Agnes: "Re: update 2 fields problem"
- Next in thread: Juraj Rojko: "Re: update 2 fields problem"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 12 Jan 2005 09:05:22 +0100
On Wed, 12 Jan 2005 09:55:59 +0800, Agnes wrote:
>Thanks Juraj, however, some said JOIN will make SQL statment process
>slowley, will it make my program getting slow ?
Hi Agnes,
Whoever told you that just shows a thorough lack of understanding of SQL
Server.
Sure, joining can be slow if you don't have appropriate indexes - but the
same can be said about subqueries or straight one-table queries: without
proper indexes, ALL queries will be slow.
The choice between using a join or using a subquery is often only a matter
of personal taste, as the SQL Server optimizer will usually generate the
same execution plan for both queries.
This changes if the subquery has to be duplicated. The optimizer doesn't
seem to understand that it's not necessary to repeat the execution of two
completely equal subqueries. As soon as you find yourself repeating the
same subquery expression, you should consider converting to a join.
There are, however, other problems with the proprietary UPDATE FROM syntax
suggested by Juraj. For starters, it is non-standard, so you won't be able
to port your code to any other RDBMS. But even more important is that the
result is not well defined in some cases. Here's a simple example, using
the UPDATE FROM syntax:
UPDATE OneTable
SET Column1 = OtherTable.Column1
Column2 = OtherTable.Column2
FROM OneTable
INNER JOIN OtherTable
ON OtherTable.KeyCol = OneTable.KeyCol
WHERE OneTable.Selection = 'Y'
Two important things to note are:
1. If a row in OneTable is not matched to any row in OtherTable, it won't
be affected by the UPDATE statement. In most cases, this is the desired
result - but not in all cases!
2. If a row in OneTable is matched to more than one row in OtherTable, the
new values for Column1 and Column2 will be based on the values in any of
the matching rows. Which of the matching rows will be used can't be
predicted. (SQL Server will use the last matching row if finds, but the
order in which matches are found depends on the execution plan, and the
execution plan may change when new indexes are created, a service pack is
applied, available memory changes or SQL Server feels like it).
The corresponding UPDATE statement in ANSI-standard syntax would look like
this:
UPDATE OneTable
SET Column1 = (SELECT Column1
FROM OtherTable
WHERE OtherTable.KeyCol = OneTable.KeyCol)
Column2 = (SELECT Column2
FROM OtherTable
WHERE OtherTable.KeyCol = OneTable.KeyCol)
WHERE OneTable.Selection = 'Y'
-- The following is optional - see comments below
AND EXISTS (SELECT *
FROM OtherTable
WHERE OtherTable.KeyCol = OneTable.KeyCol)
Two important things to note are:
1. If a row in OneTable is not matched to any row in OtherTable, it will
still be affected by the UPDATE statement and the new values for Column1
and Column2 will be NULL. If this is not desired, then the subquery has to
be repeated in the WHERE clause (as indicated above) to exclude this
column from the set to be updated.
2. If a row in OneTable is matched to more than one row in OtherTable, the
subquery will result in more than one value; SQL Server will raise an
error condition to signal that you need to change the subquery so as to
result in no more than one value. The error message might be a pain, but
it's much better than trying to find the cause of an error because a
change in execution plan causes SQL Server to no longer pick the row that
it always picked before!
Best, Hugo
-- (Remove _NO_ and _SPAM_ to get my e-mail address)
- Next message: Juraj Rojko: "Re: Permissions question..."
- Previous message: Klaus: "Re: Value of a identity-column"
- In reply to: Agnes: "Re: update 2 fields problem"
- Next in thread: Juraj Rojko: "Re: update 2 fields problem"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|