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

The Archive Base Latest Questions

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

I have three tables: categories, articles, and article_events, with the following structure categories: id,

  • 0

I have three tables: categories, articles, and article_events, with the following structure

categories: id, name                        (100,000 rows)
articles: id, category_id                   (6000 rows)
article_events: id, article_id, status_id   (20,000 rows)

The highest article_events.id for each article row describes the current status of each article.

I’m returning a table of categories and how many articles are in them with a most-recent-event status_id of ‘1’.

What I have so far works, but is fairly slow (10 seconds) with the size of my tables. Wondering if there’s a way to make this faster. All the tables have proper indexes as far as I know.

SELECT c.id, 
       c.name, 
       SUM(CASE WHEN e.status_id = 1 THEN 1 ELSE 0 END) article_count
FROM categories c
LEFT JOIN articles a ON a.category_id = c.id
LEFT JOIN (
    SELECT article_id, MAX(id) event_id
    FROM article_events
    GROUP BY article_id
) most_recent ON most_recent.article_id = a.id
LEFT JOIN article_events e ON most_recent.event_id = e.id
GROUP BY c.id

Basically I have to join to the events table twice, since asking for the status_id along with the MAX(id) just returns the first status_id it finds, and not the one associated with the MAX(id) row.

Any way to make this better? or do I just have to live with 10 seconds? Thanks!

Edit:

Here’s my EXPLAIN for the query:

ID | select_type | table          | type   | possible_keys | key         | key_len | ref                  | rows   | Extra 
---------------------------------------------------------------------------------------------------------------------------
1  | PRIMARY     | c              | index  | NULL          | PRIMARY     | 4       | NULL                 | 124044 | Using index; Using temporary; Using filesort
1  | PRIMARY     | a              | ref    | category_id   | category_id | 4       | c.id                 | 3      |
1  | PRIMARY     | <derived2>     | ALL    | NULL          | NULL        | NULL    | NULL                 | 6351   |
1  | PRIMARY     | e              | eq_ref | PRIMARY       | PRIMARY     | 4       | most_recent.event_id | 1      |
2  | DERIVED     | article_events | ALL    | NULL          | NULL        | NULL    | NULL                 | 19743  | Using temporary; Using filesort
  • 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-03T16:16:30+00:00Added an answer on June 3, 2026 at 4:16 pm

    If you can eliminate subqueries with JOINs, it often performs better because derived tables can’t use indexes. Here’s your query without subqueries:

    SELECT c.id, 
           c.name, 
           COUNT(a1.article_id) AS article_count
    FROM categories c
    LEFT JOIN articles a ON a.category_id = c.id
    LEFT JOIN article_events ae1
      ON ae1.article_id = a.id
    LEFT JOIN article_events ae2
      ON ae2.article_id = a.id
      AND ae2.id > a1.id
    WHERE ae2.id IS NULL
    GROUP BY c.id
    

    You’ll want to experiment with the indexes and use EXPLAIN to test, but here’s my guess (I’m assuming id fields are primary keys and you are using InnoDB):

    categories: `name`
    articles: `category_id`
    article_events: (`article_id`, `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: Stories (id, category_id, sub_category_id, name, story), Categories (id, parent_id, lft.
I have say three tables. Projects Id Name Categories Id Name ProjectCategories Id ProjectId
I have three tables: videos, videos_categories, and categories. The tables look like this: videos:
I have three mysql tables items =========== id title items_in_categories ============================ id item_id category_id
I have three tables A: A.pID primary key, A.Name nvarchar(250) B: B.pID primary key,
I have three tables like this: Person table: person_id | name | dob --------------------------------
I have three mysql tables, first holds information about articles (Article ID, author ID,
Let's assume I have a database with two tables: categories and articles . Every
I have three tables. Product, product categories and product category links. The links table
I have three database tables - one containing units, one containing categories of those

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.