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.
I’m guessing that you have NULL values in the phone column which means NOT IN doesn’t work (so it’s “fix” not “optimise”). So I’ve written it with NOT EXISTS:
If it is slow rather than “wrong” then you need to use indexes. What do you have now?
For this query, you need an index on
(ALERT_ORIGIN, PHONE, RECORD_ID).Note: use single quotes for string delimiters