[Solved] SQL: Delete by primary key vs by foreign key performance?

EverSQL Database Performance Knowledge Base

SQL: Delete by primary key vs by foreign key performance?

Wondering if there is any performance difference in following two DELETE statements?:

Delete by primary key:

DELETE FROM dbo.Users WHERE Id IN (1,2,3,4,5)

Delete by some foreign key:

DELETE FROM dbo.Users WHERE Email_Id IN (454,851,909)

Suppose we're talking about ~30 million rows dbo.Users table, and IN() clause contains about 5-200K items.

Is there any inbuilt features for deleting by primary-key vs by other field?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `Users` ADD INDEX `users_idx_id` (`Id`);
The optimized query:
        dbo.Users.Id IN (
            1, 2, 3, 4, 5

Related Articles

* original question posted on StackOverflow here.