Optimizing SQL, part 2
Regarding my previous question, I noticed a slight mistake on my behalf: In each case, only one record should be retrieved by the select statement to be deleted by the outer query. The statement should look like this:

delete from table_name
 where exists(select 1 from table a, table b
              where a.column_name = b.column_name and
			   a.column_name = bind_variable)

    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

Even with the addition of the bind variable my explanation of how the optimizer will run and cost the two queries is still the same. When using exists, it executes the interior select for every row in . The interior query really only returns true or false (true if 1 or more rows is returned and false if 0 rows are returned) BUT it still must be executed for every row in the table being deleted from. That's where your performance hit comes from... EXISTS vs IN. In this case, IN is the clear winner.

For More Information


This was first published in March 2003

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.