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

  • SEARCH
  • Home
  • 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 8185699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:56:23+00:00 2026-06-07T01:56:23+00:00

I would like to search my database for sets that intersect with my search

  • 0

I would like to search my database for sets that intersect with my search set. I would like for the results to be returned to me in order of the size of the intersection.

The sets inside the database row will be on the order of about 10,000. The search sets are on the order of about 500. The number of rows in the database is about 1,000,000.

EXAMPLE QUERY:

search_set = [ This set has 500 id's ]

SELECT rows WHERE "find_set" INTERSECTS "search_set" 
    ORDER BY "size of the intersection"

EXAMPLE DATABASE:

index         find_set
1             [set with 10,000 ids]
2             [set with  5,000 ids]
...
1,000,000     [set with 15,000 ids]
  • How long cam I expect this query to take?
  • Is there a particular database or database library that I should be using?
  • Do I need to do some pre-processing?
  • How do databases implement this type of query? Do they do one search for each of the 500 ids in “search_set”?
  • What other things do I need to know about this type of problem and how it is solved?

Thanks so much!

  • 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-06-07T01:56:26+00:00Added an answer on June 7, 2026 at 1:56 am

    The performance of this query depends strongly on the database optimization engine and the way you perform the query.

    First of all databases don’t generally have tables with 15,000 ids in a column. Instead you’ll need something like this pair of tables:

    set
    ---
    id
    
    set_entry
    -----------
    id
    set_id
    entry
    

    The first table will have a million rows. The second more like 10 billion. Put an index on set_entry.entry.

    The best way generally to arrange your query is to have a temporary table of some sort whose rows are the values of your query set. Then execute a query like this:

    SELECT set_entry.id, COUNT(*)
    FROM set_entry
      JOIN query_entry
        ON set_entry.entry = query_entry.entry
    GROUP BY set_entry.id
    ORDER BY count(*) DESC
    

    The query plan that you want is that for each of your elements it should do a lookup on the index, pull back all matching rows, then proceed to do a grouping operation to figure out how many there are for each set you intersect. On the first step you’d do 500 lookups, then pull back somewhere between 0 and 500 million rows. Let’s say you’re pulling back 5 million. The grouping operation will be done either by building a hash or sorting the data (databases can do it either way), both of which should be plenty fast.

    There are a lot of unknowns, but this plan is likely to take a few seconds.

    What you want to be careful about is a query like this:

    SELECT set_entry.id, COUNT(*)
    FROM set_entry
    WHERE entry IN (id1, id2, ....)
    GROUP BY set_entry.id
    ORDER BY count(*) DESC
    

    In my experience most database engines look at this, then decide that they cannot use the index. Instead they will scan through all of set_entry (which had 10 billion rows), and for each one scan through that set of 500 elements, doing pairwise comparisons. This means an initial step of about 5 trillion pairwise comparisons. This plan will easily keep your CPU busy for hours.

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

Sidebar

Related Questions

We have a database that we would like to index with Sphinx search engine.
I have a database that I would like to leverage with Zend_Search_Lucene . However,
I would like to search tables in sql server for a table that has
I would like to search for an image ID that contains a value AND
We have a small data set and would like to search through it in
I'm currently implementing a search form in our users database. I would like to
I would like to search an entire MS SQL 2000 database for one value.
i have a textbox with button search. i would like to search my database
I would like to search into directory for all files end by .m for
I would like to search and replace all occurrences outside of <>. I can

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.