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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:53:51+00:00 2026-05-12T16:53:51+00:00

I have 2 large mysql tables: Articles and ArticleTopics. I want to query the

  • 0

I have 2 large mysql tables: Articles and ArticleTopics. I want to query the DB and retrieve the last 30 articles published for a given topicID. My current query is rather slow. Any ideas on how to improve it?

More details:

The tables:

Articles (~1 million rows)
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| articleId | int(11)      | NO   | PRI | NULL    | auto_increment | 
| title     | varchar(255) | NO   |     | NULL    |                | 
| content   | longtext     | NO   |     | NULL    |                | 
| pubDate   | datetime     | NO   | MUL | NULL    |                | 
+-----------+--------------+------+-----+---------+----------------+


ArticleTopics (~10 million rows)
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| articleId | int(11)      | NO   | MUL | NULL    |       | 
| topicId   | int(11)      | NO   | MUL | NULL    |       | 
+-----------+--------------+------+-----+---------+-------+

And my query:

SELECT a.articleId, a.pubDate 
FROM Articles a, ArticleTopics t 
WHERE t.articleId=a.articleId AND t.topicId=3364 
ORDER BY a.pubDate DESC LIMIT 30;

And the EXPLAIN of the query:

+----+-------------+-------+--------+-------------------------------------+-------------------+---------+-------------------+------+----------------------------------------------+
| id | select_type | table | type   | possible_keys                       | key               | key_len | ref               | rows | Extra                                        |
+----+-------------+-------+--------+-------------------------------------+-------------------+---------+-------------------+------+----------------------------------------------+
|  1 | SIMPLE      | t     | ref    | articleId,topicId,topicId_articleId | topicId_articleId | 4       | const             | 4281 | Using index; Using temporary; Using filesort | 
|  1 | SIMPLE      | a     | eq_ref | PRIMARY,articleId_pubDate           | PRIMARY           | 4       | t.articleId       |    1 |                                              | 
+----+-------------+-------+--------+-------------------------------------+-------------------+---------+-------------------+------+----------------------------------------------+

The slowness, I believe, is coming from the ORDER BY a.pubDate DESC. I can greatly improve performance by faking it a bit by instead doing an ORDER BY t.articleId DESC and having an index in ArticleTopics on both articleId & topicId, since in general, the articleIds are in the same order as pubDates. They are not always, however, so it’s not ideal. I’d like to be able to sort it on the pubDate.

Update: Added EXPLAIN.

  • 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-12T16:53:51+00:00Added an answer on May 12, 2026 at 4:53 pm

    You can rewrite the query in various ways to see if it speeds things up:

    SELECT a.articleId, a.pubDate 
    FROM Articles a
    WHERE a.articleId in (
        select articleId 
        from ArticleTopics
        where topicId = 3364 
    )
    ORDER BY a.pubDate DESC LIMIT 30;
    

    Or:

    SELECT a.articleId, a.pubDate 
    FROM Articles a
    INNER JOIN ArticleTopics t ON t.articleId = a.articleId
    WHERE t.topicId = 3364 
    ORDER BY a.pubDate DESC LIMIT 30;
    

    The important index for both queries is on Articles, and contains articleId as first field.

    If article is a large table, with say the entire PDF in binary, you can create an index that fully covers the query. Full coverage means all selected fields are part of the index. For this query, a fully covering index would be (articleId, pubDate).

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

Sidebar

Related Questions

I have a large amount of text in Mysql tables. I want to do
I have two large tables in MySQL, one containing about 6,00,000 records and another
I have some large MySQL production tables that I need to dump so that
I am looking for some help with a MYSQL query. I have two tables,
I have three large MySQL tables. They are approaching 2 million records. Two of
I have a 30 GB large mysql database where all tables use innodb engine.
We have a very large InnoDB MySQL 5.1 database with all tables using the
I have large MySQL tables with hundreds of thousands of rows. I need to
I have 2 tables in mySQL db... they are very large.. its about 1
i have a large mysql database table in which one column contains values ranging

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.