efficient deletes with sub queries

efficient deletes with sub queries

Post by news.ox.ac.u » Thu, 01 Nov 2001 00:51:21



I have a very large table with > 1 million entries and I wish to delete rows
which match any entries in a second table.
What is the most efficient way of doing this?

CREATE TABLE AA (keyA integer NOT NULL, info TEXT);
CREATE TABLE obsolete_AA_entries (keyA integer NOT NULL);

I want to remove all entries from AA where keyA matches that from
obsolete_AA_entries,
i.e. SELECT * FROM AA NATURAL JOIN obsolete_AA_entries;

Both of the tables are UNIQUE indiced on keyA.

DELETE FROM AA WHERE EXISTS(
          SELECT * from obsolete_AA_entries o where AA.keyA = o.keyA);
seems to be faster than
DELETE FROM AA where (keyA) in (SELECT * from obsolete_AA_entries);

However, both are sequentially going through AA which is huge rather than
looking up values one by one from obsolete_AA_entries which is small.
How do I persuade the database to change its query strategy?
Thanks a lot
Llew

 
 
 

efficient deletes with sub queries

Post by Robert Lummer » Thu, 01 Nov 2001 03:01:02


Quote:> What is the most efficient way of doing this?

> CREATE TABLE AA (keyA integer NOT NULL, info TEXT);
> CREATE TABLE obsolete_AA_entries (keyA integer NOT NULL);

> I want to remove all entries from AA where keyA matches that from
> obsolete_AA_entries,
> i.e. SELECT * FROM AA NATURAL JOIN obsolete_AA_entries;

> Both of the tables are UNIQUE indiced on keyA.

> DELETE FROM AA WHERE EXISTS(
>           SELECT * from obsolete_AA_entries o where AA.keyA = o.keyA);
> seems to be faster than
> DELETE FROM AA where (keyA) in (SELECT * from obsolete_AA_entries);

in mssql i would try delete via

  delete aa from obsolete_AA_entries o inner join aa a on o.keyA =
a.keyA

but your code seems to be pl-sql(oracle) or something else but not for
mssql because t-sql
(thats also used by the sybase derivative mssql) not uses ';'.
Sure having asked the appropriate group?
Greetings

     Robert

 
 
 

efficient deletes with sub queries

Post by BP Margoli » Thu, 01 Nov 2001 08:06:07


Robert,

Beginning with SQL Server 7.0, T-SQL accepts semi-colons ... probably to
meet ANSI SQL compliance requirements.

-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.


Quote:> > What is the most efficient way of doing this?

> > CREATE TABLE AA (keyA integer NOT NULL, info TEXT);
> > CREATE TABLE obsolete_AA_entries (keyA integer NOT NULL);

> > I want to remove all entries from AA where keyA matches that from
> > obsolete_AA_entries,
> > i.e. SELECT * FROM AA NATURAL JOIN obsolete_AA_entries;

> > Both of the tables are UNIQUE indiced on keyA.

> > DELETE FROM AA WHERE EXISTS(
> >           SELECT * from obsolete_AA_entries o where AA.keyA = o.keyA);
> > seems to be faster than
> > DELETE FROM AA where (keyA) in (SELECT * from obsolete_AA_entries);

> in mssql i would try delete via

>   delete aa from obsolete_AA_entries o inner join aa a on o.keyA =
> a.keyA

> but your code seems to be pl-sql(oracle) or something else but not for
> mssql because t-sql
> (thats also used by the sybase derivative mssql) not uses ';'.
> Sure having asked the appropriate group?
> Greetings

>      Robert

 
 
 

efficient deletes with sub queries

Post by Erland Sommarsko » Thu, 01 Nov 2001 08:18:58



> but your code seems to be pl-sql(oracle) or something else but not for
> mssql because t-sql
> (thats also used by the sybase derivative mssql) not uses ';'.

Actually, from SQL7 you can use ; to terminate your statements.

Then again, he used NATURAL JOIN which is not present in T-SQL. But
that might have been for brevity.

--
Erland Sommarskog, Abaris AB

SQL Server MVP

 
 
 

1. efficient sub queries

Hello

I am having problems with this query being very slow.
(after checking for obvious indexes etc)

The query plan is after...

Is there a more efficient way of achieving this selection
rule.

Regards
Lee (still learning!)

SELECT ClientId FROM dbo.ClientQUICK
A WHERE  0 = (SELECT COUNT(*) FROM  dbo.InsuranceRiskQuick B WHERE
B.ClientId = A.ClientId AND NOT ( B.UnderwriterCd = 'ADV'
AND B.UnderwriterCd IS NOT NULL)) AND 1 <= (SELECT COUNT(*)
FROM  dbo.InsuranceRiskQuick B WHERE  B.ClientId = A.ClientId)

STEP 1
The type of query is SELECT (into a worktable)
GROUP BY
Vector Aggregate
FROM TABLE
dbo.InsuranceRiskQuick B
Nested iteration
Table Scan
FROM TABLE
dbo.ClientQUICK A
Nested iteration
Index : IXSM_PK1_ClientQuick
TO TABLE
Worktable 1
STEP 2
The type of query is SELECT (into a worktable)
GROUP BY
Vector Aggregate
FROM TABLE
dbo.InsuranceRiskQuick B
Nested iteration
Table Scan
FROM TABLE
dbo.ClientQUICK A
Nested iteration
Index : IXSM_PK1_ClientQuick
TO TABLE
Worktable 2
STEP 3
The type of query is SELECT
FROM TABLE
dbo.ClientQUICK A
Nested iteration
Index : IXSM_PK1_ClientQuick
FROM TABLE
Worktable 1
SUBQUERY : nested iteration
FROM TABLE
Worktable 2
SUBQUERY : nested iteration
STEP 1
The type of query is SETOFF
STEP 1

2. DBGrid and Row Position

3. Query using UDF joined to a sub query vs Temp Table to sub Query

4. SQL Server licenses for replication

5. delete with sub-query using temp table

6. Scheduling a custom stored procedure

7. Sub queries <> sub tablesl

8. Loading Database Table into Array

9. Type, sub-type, and sub-sub-type database design

10. Master Parts/Sub Parts/Sub-Sub Parts

11. SQL delete query deletes but does not delete

12. Subject: SQL Query - MAX, Grouping and Sub query

13. Query & Sub-Query