Desktop productivity for business analysts and programmers

Delete with an exists statement really slow

Reply
Occasional Contributor mgm
Occasional Contributor
Posts: 10

Delete with an exists statement really slow

proc sql;
delete from DATA1 a
	where exists (select 1 from  DATA2 b where a.ID = b.ID and a.ID2 = b.ID2);
quit;

This statement is tremendously slow for a table with 8.3 million rows. 

PROC Star
Posts: 1,167

Re: Delete with an exists statement really slow

Two questions:

 

First, the obvious one. How "slow" is really slow? How long is it taking?

 

Second, what kind of tables are "DATA1" and "DATA2". SAS datasets? Database tables accessed via SAS/Access? Other?

 

And if they're database tables, which technology (Oracle, DB2...)

 

Tom

Super User
Posts: 7,758

Re: Delete with an exists statement really slow


mgm wrote:
proc sql;
delete from DATA1 a
	where exists (select 1 from  DATA2 b where a.ID = b.ID and a.ID2 = b.ID2);
quit;

This statement is tremendously slow for a table with 8.3 million rows. 


- define "slow". Use options fullstimer to get a clear picture.

- given that all datasets reside in WORK, and the SQL utility file will also be placed there, I'm not surprised that the performance of this step is dismal. Depending on the size of data2, I'd either use sort/data steps, or a format created from data2, or a hash object created from data2.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,424

Re: Delete with an exists statement really slow

Correlated subquery with exists can be slow.
Indexing on id could help.
But if your id is unique it could be faster recreating the table with a data step merge (like if a a not b).
Data never sleeps
Occasional Contributor mgm
Occasional Contributor
Posts: 10

Delete with an exists statement really slow

proc sql;
delete from DATA1 a
	where exists (select 1 from  DATA2 b where a.ID = b.ID and a.ID2 = b.ID2);
quit;

This statement is tremendously slow for a table with 8.3 million rows, any suggestions on improving the performance ?

Super Contributor
Posts: 438

Re: Delete with an exists statement really slow

With 8.3M rows I would have thought either indexing the ID and ID2 fields on both files or pre-sorting them by ID and ID2 might help.

Super User
Posts: 10,018

Re: Delete with an exists statement really slow

I would try Hash Table if the table is very big.

Or try this one , maybe save you some time.

 

proc sql;
delete from DATA1
	where catx('|',ID,ID2) in (select catx('|',ID,ID2) from  DATA2 );
quit;
Ask a Question
Discussion stats
  • 6 replies
  • 177 views
  • 2 likes
  • 6 in conversation