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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:31:16+00:00 2026-06-03T14:31:16+00:00

I currently have the current MySQL query taking up to 10 seconds to run

  • 0

I currently have the current MySQL query taking up to 10 seconds to run in my application:

SELECT tagid, tag FROM tags WHERE tagid IN 
(SELECT DISTINCT tagid FROM news_tags WHERE newsid IN 
(SELECT newsid FROM news_tags WHERE tagid IN (16,32)
GROUP BY newsid HAVING COUNT(newsid)>=2)) 
AND tagid NOT IN (16,32) ORDER BY level, tagid

The tables used are:

  • table news_tags, with columns newsid, tagid
  • table tags, with columns tagid, tag, level

The purpose of the query is to find “news” items which have been tagged with tags with tagid 16 and 32, then find other tags these news items have also been tagged with, for the purposes of allowing a user to further narrow down the “news” items with more specific tag combinations. The ultimate goal is to grab the remaining relevant tag and tagid columns from the tags table.

I have tried different attempts at an equivalent JOIN but have failed to select all remaining tagids on the news items which have the provided tags attached to them.

Here is my EXPLAIN SQL results, in case they point to another cause of slowness which I’m missing:

id|select_type       |table    |type          |possible_keys|key    |key_len|ref |rows|Extra
 1|PRIMARY           |tags     |range         |PRIMARY      |PRIMARY|      4|NULL|  55|Using where; Using filesort
 2|DEPENDENT SUBQUERY|news_tags|index_subquery|tagid        |tagid  |      4|func|  26|Using index; Using where
 3|DEPENDENT SUBQUERY|news_tags|index         |tagid        |PRIMARY|      8|NULL|  11|Using where; Using index

Just to clarify the problem: I wanted remaining tags for news items tagged with BOTH tags 16 and 32, not either 16 or 32. Sorry for any confusion.

  • 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-03T14:31:19+00:00Added an answer on June 3, 2026 at 2:31 pm
    SELECT DISTINCT tags.tagid, tags.tag
    FROM
           tags                             -- tags from the ...
      JOIN news_tags AS n0 USING (tagid)    -- ... news items tagged with ...
      JOIN news_tags AS n1 USING (newsid)   -- ... tagid = 16 and ...
      JOIN news_tags AS n2 USING (newsid)   -- ... tagid = 32
    WHERE
      n1.tagid = 16 AND n2.tagid = 32
      AND tags.tagid NOT IN (16,32)         -- not the tags we already know about
    ORDER BY tags.level, tags.tagid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that currently stores timestamps in MySQL DATETIME and TIMESTAMP values.
I currently have this code: $rawsql2 = SELECT * FROM _erc_foffices n INNER JOIN
I have a page that that lists products returned from a mysql query. The
This is how I have my current query setup: SELECT * , ( SELECT
I have a troublesome MySQL query as follows: SELECT camera_id, ((avg(low_price) + avg(high_price)) /
I have read the topic Mysql query: retrieve current date query ., but my
At current i have an index page which shows values from an XML page
I have recently decided to switch all my current plain mysql queries performed with
I think I have reached my current limits with Left Joins. Currently I have
I currently am using mysql and php to display 9 random results from my

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.