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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:25:22+00:00 2026-05-18T22:25:22+00:00

I have three tables, bookmarks , tag and tagging . tagging is the association

  • 0

I have three tables, bookmarks, tag and tagging. tagging is the association table for linking tags to bookmarks. I would like to select related tags. The current query takes ages to run, but I could not wrap my head around the JOINs that could replace the subquery that selects all the related bookmarks that are tagged with specific tag IDs.

SELECT ta.name, count(*) 
FROM tagging t2 
JOIN tag ta ON t2.tag_id=ta.id  
WHERE t2.bookmark_id IN (
   SELECT bookmark_id 
   FROM tagging t1 
   WHERE t1.tag_id IN (1, 7) 
   GROUP BY t1.bookmark_id 
   HAVING COUNT(t1.id) = 2
) 
GROUP BY ta.id

Here is the result of the EXPLAIN of that query:

1   PRIMARY            ta  index PRIMARY    PRIMARY         8   NULL         3   
1   PRIMARY            t2  ref   tag_id_idx tag_id_idx      8   blinkz.ta.id 89   Using where
2   DEPENDENT SUBQUERY t1  index tag_id_idx bookmark_id_idx 8   NULL         71   Using where 

I have an index for the bookmark_id and tag_id fields in the tagging table. The tag table has a primary key named id

How could this query be optimised, preferably with a JOIN instead of a subquery?

  • 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-18T22:25:22+00:00Added an answer on May 18, 2026 at 10:25 pm

    The poor performance is due to a bug in MySQL that will hopefully be fixed soon. Until then try this instead:

    SELECT tag.name, COUNT(*) AS cnt
    FROM tagging T1
    JOIN tag
    ON T1.tag_id = tag.id  
    JOIN (
       SELECT bookmark_id 
       FROM tagging
       WHERE tag_id IN (1, 7) 
       GROUP BY bookmark_id 
       HAVING COUNT(id) = 2
    ) T2
    ON T1.bookmark_id = T2.bookmark_id
    GROUP BY tag.id
    

    I’m assuming that there is a unique contraint on (bookmark_id, tag_id).

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

Sidebar

Related Questions

I have three tables like these: movie: id, name tag: id, name, value tagged:
I have three tables I would like to join to pull off a MySQL
I have three tables: Campus Building Floor and table structure is looks like this:
I have three tables like this CREATE TABLE `money`.`categories` ( `ID` bigint(20) unsigned NOT
I have three tables of data: table: cars [10,000 rows] table: planes [2,000 rows]
I have three tables: Employee(EmployeeID,Fname,Lname...) ProjectHeader(ProjectID,LeadID,Status....) ProjectDetails(ProjectDetailsID,ProjectID....) Here's my current code: $get_projects = SELECT
I have three tables Author, Book and AuthorBook. AuthorBook table only contains two foreign
I have three tables in oracle db as newitems, itemdetails, ticketitems table. Some dummy
I have three tables: upload, tag and upload_tag. Upload holds details of a file
I have three tables like this: Person id | name | location ----------------------- 1

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.