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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:39:08+00:00 2026-05-26T01:39:08+00:00

Background My program is storing a series of objects, a set of tags, and

  • 0

Background

My program is storing a series of objects, a set of tags, and the many-to-many associations between tags and objects, in a MySQL database. To give you an idea of the structure:

CREATE TABLE objects (
    object_id INT PRIMARY KEY,
    ...
);
CREATE TABLE tags (
    tag_name VARCHAR(32) NOT NULL
);
CREATE TABLE object_tags (
    object_id INT NOT NULL,
    tag_name VARCHAR(32) NOT NULL,
    PRIMARY KEY (object_id, tag_name)
);

Problem

I want to be able to query for all objects that are tagged with all of the tags in a given set. As an example, let’s say I have a live tree, a dead flower, an orangutan, and a ship as my objects, and I want to query all of those tagged living and plant. I expect to receive a list containing only the tree, assuming the tags match the characteristics of the objects.

Current Solution

Presently, given a list of tags T1, T2, …, Tn, I am solving the problem as follows:

  1. Select all object_id columns from the object_tags table where tag_name is T1.
  2. Join the result of (1) with the object_tags table, and select all object_id columns where tag_name is T2.
  3. Join the result of (2) with the object_tags table again, and select all object_id columns where tag_name is T3.
  4. Repeat as necessary for T4, …, Tn.
  5. Join the result of (4) with the objects table and select the additional columns of the objects that are needed.

In practice (using Java), I start with the query string for the first tag, then prepend/append the string parts for the second tag, and so on in a loop, before finally prepending/appending the string parts that make up the overall query. Only then does the string actually get passed into a PreparedStatement and get executed on the server.

Edit: Expanding on my example from above, using this solution I would issue the following query:

SELECT object_id FROM object_tags JOIN (
    SELECT object_id FROM object_tags WHERE tag_name='living'
) AS _temp USING (object_id) WHERE tag_name='plant';

Question

Is there a better solution to this problem? Although the number of tags is not likely to be large, I am concerned about the performance of this solution, especially as the database grows in size. Furthermore, it is very difficult to read and maintain the code, especially when the additional concerns/constraints of the application are thrown in.

I am open to suggestions at any level, although the languages (MySQL and Java) are not variables at this point.

  • 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-26T01:39:08+00:00Added an answer on May 26, 2026 at 1:39 am

    I don’t know about the performance of this solution, but you can simplify by using pattern matching in MySql to match a set of pipe-delimited tags (or any delimiter). This is a solution I’ve used before for similar applications with tag tables (@match would be a variable passed in by your Java code, I’ve harded coded a value for demonstration):

    set @match = 'living|plant';
    set @numtags =
        length(@match) - length(replace(@match, '|', '')) + 1;
    
    select * from objects o
    where @numtags =
    (
        select count(*) from object_tags ot
        where concat('|',@match,'|')
            like concat('%|',ot.tag_name,'|%')
        and ot.object_id = o.object_id
    )
    

    Here is a working demo: http://sqlize.com/0vP6DgQh0j

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

Sidebar

Related Questions

Here's some background on my program: [self.channels ] is an array of Channel objects.
Background I am writing a program that targets several different phones, including the iPhone.
Background: I am writing a C++ program working with large amounts of geodata, and
My program does some network activity in a background thread. Before starting, it pops
Some background info: I'm trying to run a server program in python 2.5.1 (the
I have coded a background logging thread for my program, if a class needs
I have a simple program which is able to change the background color after
I have a java program that is running in the background(Windows). I would like
I creating a program which send newsletter with a background image. It works fine
I am writing a program to sit in the background on osx 10.6, listen

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.