I have an SQL query that I am looking to optimize.
SELECT *
FROM QUEUE_SMS_ALERT Q1
where ALERT_ORIGIN = "FOO"
AND RECORD_ID is null
and PHONE NOT IN (
SELECT DISTINCT PHONE
FROM QUEUE_SMS_ALERT Q2
where Q2.ALERT_ORIGIN = "BAR"
);
Basically need to get all rows where ALERT_ORIGIN is "FOO" Which do not have a corresponding row in the same table with ALERT_ORIGIN "BAR". The table contains abt 17000 rows and there are only abt 1000 records with ALERT_ORIGIN "BAR". So my query is supposed to give me abt 16000 rows.
EDIT : The current query is very slow. I do not have any indexes currently.
ALERT_ORIGINandPHONEDISTINCTfrom the...PHONE NOT IN (SELECT DISTINCT PHONE ..., it's not needed.