Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:24:51+00:00 2026-05-25T00:24:51+00:00

I have an SQL query that I am looking to optimize. SELECT * FROM

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:24:52+00:00Added an answer on May 25, 2026 at 12:24 am

    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:

    SELECT *
    FROM QUEUE_SMS_ALERT Q1 
    WHERE
        Q1.ALERT_ORIGIN = 'FOO'
        AND
        Q1.RECORD_ID is null
        AND 
        NOT EXISTS (SELECT *
            FROM QUEUE_SMS_ALERT Q2
            WHERE 
                Q2.ALERT_ORIGIN = 'BAR'
                AND
                Q1.PHONE = Q2.PHONE)
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to optimize one SQL query that I have. I'm
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a sql query that runs super fast, around one second, when not
We have a SQL query that pulls a large number of fields from many
I have an sql query that has a parameter that can be null in
I have a rather complex SQL query that I am looking for a little
I have a LINQ from SQL query that returns sorted rows. For instance, it
I have a SQL query that takes a very long time to run on
I have a SQL query that is supposed to pull out a record and
I have a SQL query that I'm trying to debug. It works fine for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.