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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:25:34+00:00 2026-05-30T15:25:34+00:00

I have a database structure serving up news articles with the following associations: HABTM

  • 0

I have a database structure serving up news articles with the following associations:

  • HABTM news_categories
  • HABTM tags
  • HABTM uploads

I have written an SQL query to pull all this together:

 SELECT `news_articles`.*, 
 GROUP_CONCAT(DISTINCT tags.title) AS `tags`, 
 GROUP_CONCAT(DISTINCT tags.id) AS `tag_ids`,
 GROUP_CONCAT(DISTINCT news_categories.title) AS `news_categories`,
 GROUP_CONCAT(DISTINCT news_categories.id) AS `news_category_ids`,
 GROUP_CONCAT(DISTINCT news_categories.slug) AS `news_category_slugs`, 
 `news_articles_uploads`.`caption` AS `upload_caption`,
 `uploads`.`title` AS `upload_title`, 
 `uploads`.`basename` AS `upload_basename`,
 `uploads`.`extension` AS `upload_extension`,
 `uploads`.`path` AS `upload_path`
 FROM `news_articles`
 LEFT JOIN `news_articles_tags` ON news_articles_tags.news_article_id = news_articles.id
 LEFT JOIN `tags` ON news_articles_tags.tag_id = tags.id
 LEFT JOIN `news_articles_news_categories` ON news_articles_news_categories.news_article_id = news_articles.id
 LEFT JOIN `news_categories` ON news_articles_news_categories.news_category_id = news_categories.id
 LEFT JOIN `news_articles_uploads` ON (news_articles_uploads.news_article_id = news_articles.id AND news_articles_uploads.order = 0)
 LEFT JOIN `uploads` ON news_articles_uploads.upload_id = uploads.id 
 WHERE (news_categories.slug IN ("category-one","category-two","category-three","category-four","category-five")) AND (news_articles.published = 1)
 GROUP BY `news_articles`.`id`
 ORDER BY `news_articles`.`lead_article` DESC, `news_articles`.`created` DESC LIMIT 20;

The problem is that whilst the query runs, it is slow, and during busy periods the CPU usage is getting pretty out of hand!

Here is an EXPLAIN for the above query (right-click open in new tab to see full size):

Explain result for the above query

You can find the schema here: http://pastie.org/private/qoe2qo16rbqr5mptb4bug

The server is running MySQL 5.1.55 and the website uses Zend Framework to execute the query and PHP 5.2.8.

I’ve been through the MySQL slow query log and added missing indexes to the best of my knowledge but the query still shows up as taking 1-3 seconds to execute. If anyone has any ideas I’ve be very grateful. Thanks in advance.

  • 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-30T15:25:36+00:00Added an answer on May 30, 2026 at 3:25 pm

    Since your “WHERE” clause originally included “AND” for your news categories within
    a specified list, that would FORCE the joins to get there as INNER joins, not LEFT JOINs.
    Also, I would try adding the “STRAIGHT_JOIN” clause. This typically forces the engine
    to do the joining in the order specifically stated, instead of trying to think of its own
    alternative for you… especially when the other tables are more “lookup” references.

    I would also apply the index as suggested by Jordan.

    SELECT STRAIGHT_JOIN
          NA.*, 
          GROUP_CONCAT(DISTINCT tags.title) AS `tags`, 
          GROUP_CONCAT(DISTINCT tags.id) AS tag_ids,
          GROUP_CONCAT(DISTINCT NC.title) AS news_categories,
          GROUP_CONCAT(DISTINCT NC.id) AS news_category_ids,
          GROUP_CONCAT(DISTINCT NC.slug) AS news_category_slugs, 
          NAUp.`caption` AS upload_caption,
          Up1.`title` AS upload_title, 
          Up1.`basename` AS upload_basename,
          Up1.`extension` AS upload_extension,
          Up1.`path` AS upload_path
       FROM 
          news_articles NA
             INNER JOIN news_articles_news_categories NACats
                ON NA.id = NACats.news_article_id
    
                INNER JOIN news_categories NC
                   ON NACats.news_category_id = NC.id
                   AND NC.slug IN ( "category-one",
                                    "category-two",
                                    "category-three",
                                    "category-four",
                                    "category-five" )
    
    
             LEFT JOIN news_articles_tags NATags
                ON NA.ID = NATags.news_article_id
    
                LEFT JOIN tags
                   ON NATags.tag_id = tags.id
    
             LEFT JOIN news_articles_uploads NAUp
                ON    NA.ID = NAUp.news_article_id 
                  AND NAUp.order = 0
    
                LEFT JOIN uploads Up1
                   ON NAUp.upload_id = Up1.id 
    
       WHERE 
          NA.Published = 1
       GROUP BY 
          NA.ID
       ORDER BY 
          NA.lead_article DESC, 
          NA.created DESC 
       LIMIT 20;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a mysql query for following scenario I have following database structure 1]
I have the following database structure. ID | Name | Marks I am trying
I have two database tables with the following structure: actions: action_id int(11) primary key
I have the following database structure: Event table Id - Guid (PK) Name -
I have the following database structure: Sites table id | name | other_fields Backups
I have the following database structure: Notice that the field fk_parent_group_id can either be
I have the following database structure: Children --> Gifts <-- Possible_gifts, where the arrow
I have the following database structure: CREATE TABLE LookupTable ( PK UNIQUEIDENTIFIER PRIMARY KEY,
I have the following database structure where products have a default price that can
I have the following database structure (simplified) Store StoreId RateId Products ProductId Name Rates

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.