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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:46:57+00:00 2026-05-10T23:46:57+00:00

I have a database populated with 1 million objects. Each object has a ‘tags’

  • 0

I have a database populated with 1 million objects. Each object has a ‘tags’ field – set of integers.

For example:

object1: tags(1,3,4) object2: tags(2) object3: tags(3,4) object4: tags(5) 

and so on.

Query parameter is a set on integers, lets try q(3,4,5)

object1 does not match ('1' not in '3,4,5') object2 does not match ('2' not in '3,4,5') object3 matches ('3 and 4' in '3,4,5' ) object4 matches ('5' in '3,4,5' ) 

How to select matched objects efficiently?

  • 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. 2026-05-10T23:46:57+00:00Added an answer on May 10, 2026 at 11:46 pm

    You’re making a common mistake in database design, by storing a comma-separated list of tag id’s. It’s not a surprise that performing efficient queries against this is a blocker for you.

    What you need is to model the mapping between objects and tags in a separate table.

    CREATE TABLE Tagged (   object_id  INT NOT NULL,   tag_id     INT NOT NULL,   PRIMARY KEY (object_id, tag_id),   FOREIGN KEY (object_id) REFERENCES Objects(object_id),   FOREIGN KEY (tag_id) REFERENCES Tags(tag_id) ); 

    Insert one row for each object/tag pairing. Of course, this means you have several rows for each object_id, but that’s okay.

    You can query for all objects that have tags 3,4,5:

    SELECT DISTINCT object_id FROM Tagged WHERE tag_id IN (3, 4, 5); 

    But this matches object1, which you don’t want. You want to exclude objects that have other tags not in 3,4,5.

    SELECT DISTINCT t1.object_id FROM Tagged t1   LEFT OUTER JOIN Tagged t2  ON (t1.object_id = t2.object_id AND t2.tag_id NOT IN (3, 4, 5)) WHERE t1.tag_id IN (3, 4, 5)  AND t2.object_id IS NULL; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an oracle database populated with million records. I am trying to write
I have a database table on a development server that is now fully populated
I have database with many tables. In the first table, I have a field
I have a database that contains a date and we are using the MaskedEditExtender
I have a database table and one of the fields (not the primary key)
I have designed database tables (normalised, on an MS SQL server) and created a
I have a database with names in it such as John Doe etc. Unfortunately
I have a database with two tables ( Table1 and Table2 ). They both
I have a database in single user mode and I am trying to drop
I have a database file that is generated on a PC using Sqlite. This

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.