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 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

Ask A Question

Stats

  • Questions 93k
  • Answers 93k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer According to the documentation, you do it by creating an… May 11, 2026 at 6:36 pm
  • Editorial Team
    Editorial Team added an answer You could use the callback mechanism for when the cache… May 11, 2026 at 6:36 pm
  • Editorial Team
    Editorial Team added an answer WCF is a programming model and API. "WCF Service" implies… May 11, 2026 at 6:36 pm

Related Questions

I have a database populated with 1 million objects. Each object has a 'tags'
I have a database table on a development server that is now fully populated
I've read a few questions on SO (such as this one ) in regards
What is the fastest way to load data from flatfiles into a MySQL database,
I have a VB6 application which works with datetime values in SQL Server (which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.