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 6574275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:14:26+00:00 2026-05-25T15:14:26+00:00

I have two tables. Forms has ~77000 rows. Logs has ~2.7 million rows. The

  • 0

I have two tables. Forms has ~77000 rows. Logs has ~2.7 million rows.

The following query returns “30198” in less than a second:

SELECT COUNT(DISTINCT logs.DOCID) FROM logs, forms WHERE logs.DOCID = forms.DOCID;

And this query has been running for ~15 minutes so far, and still hasn’t finished:

SELECT COUNT(DISTINCT logs.DOCID) FROM logs, forms WHERE logs.DOCID <> forms.DOCID;

Why is the “not equal” query so much slower?

  • 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-25T15:14:27+00:00Added an answer on May 25, 2026 at 3:14 pm

    Because = reduces the join operation to one single matching row from each table (presuming those docids are unique).

    Think of it this way- you’ve got a dance with 5 boys and 5 girls:

    Adam      Alice
    Bob       Betty
    Charly    Cathy
    Dick      Deb
    Evan      Elly
    

    You pair them up by first letter. So

    Adam->Alice
    Bob->Betty
    etc...
    

    One single pairing

    But if you pair them up by “First letters do NOT match”, you end up with:

    Adam->Betty
    Adam->Cathy
    Adam->Deb
    Adam->Elly
    Bob->Alice
    etc...
    

    you’ve MASSIVELY increased the number of pairings. This is why your <> query is taking so long. You’re essentially trying to fetch m x n rows, rather than just min(m,n). With this data, you end up with 25 rows, rather than 5. For your specified table sizes, you’re working with 77,000 * 2,700,000 = 207.9 billion rows, minus 77,000 where the two ids match up, for a total of 207,899,923,000 rows in the joined data set.


    given your query requirements, try a left join and look for null right-side records:

    SELECT DISTINCT logs.DOCID
    FROM logs
    LEFT JOIN forms ON logs.DOCID = forms.DOCID
    WHERE forms.DOCID IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that are joined together. A has many B Normally you
My application has pages of two basic types: forms and tables. As such, I
I have two tables: Person and Shift Person has as lookup field which is
I have a database that has two tables, one of which contains a foreign
In SQL server 2008: Suppose I have two tables. Table1 has 3 fields: Name,
I have two tables: OutputPackages (master) |PackageID| OutputItems (detail) |ItemID|PackageID| OutputItems has an index
I have two tables, Job and Client. Job has a column, ClientId, that is
Background I have an Android project that has a database with two tables: tbl_question
I have two tables, HELP_PROBLEMS and HELP_SOLUTIONS. HELP_PROBLEMS has a number of different fields,
Let me be clearer. my app has two tables/models: Bikes and Cars I have

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.