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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:44:27+00:00 2026-05-11T23:44:27+00:00

I built a custom forum for my site using MySQL. The listing page is

  • 0

I built a custom forum for my site using MySQL. The listing page is essentially a table with the following columns: Topic, Last Updated, and # Replies.

The DB table has the following columns:

id
name
body
date
topic_id
email

A topic has the topic_id of “0”, and replies have the topic_id of their parent topic.

SELECT SQL_CALC_FOUND_ROWS
    t.id, t.name, MAX(COALESCE(r.date, t.date)) AS date, COUNT(r.id) AS replies
FROM
    wp_pod_tbl_forum t
LEFT OUTER JOIN
    wp_pod_tbl_forum r ON (r.topic_id = t.id)
WHERE
    t.topic_id = 0
GROUP BY
    t.id
ORDER BY
    date DESC LIMIT 0,20;

There are about 2,100 total items in this table, and queries usually take a whopping 6 seconds. I added an INDEX to the “topic_id” column, but that didn’t help much. Are there any ways of speeding up this query w/out doing significant restructuring?

EDIT: not quite working yet. I can’t seem to get the examples below to work properly.

  • 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-11T23:44:27+00:00Added an answer on May 11, 2026 at 11:44 pm
    SELECT  id, name, last_reply, replies
    FROM    (
            SELECT  topic_id, MAX(date) AS last_reply, COUNT(*) AS replies
            FROM    wp_pod_tbl_forum
            GROUP BY
                    topic_id
            ) r
    JOIN    wp_pod_tbl_forum t
    ON      t.topic_id = 0
            AND t.id = r.topic_id
    UNION ALL
    SELECT  id, name, date, 0
    FROM    wp_pod_tbl_forum t
    WHERE   NOT EXISTS
            (
            SELECT  NULL
            FROM    wp_pod_tbl_forum r
            WHERE   r.topic_id = t.id
            )
            AND t.topic_id = 0
    ORDER BY
           date DESC
    LIMIT 0, 20
    

    If your table is MyISAM or id is not a PRIMARY KEY, you need to create a composite ondex on (topic_id, id).

    If your table is InnoDB and id is a PRIMARY KEY, an index just on (topic_id) will do (id will be implicitly added to the index).

    Update

    This query will most probably be even more efficient, provided that you have indexes on (topic_id, id) and (date, id):

    See this article in my blog for performance details:

    • Selecting last forum posts

    This query completes in 30 ms on a 100,000 rows sample data:

    SELECT  id, name, last_reply,
            (
            SELECT  COUNT(*)
            FROM    wp_pod_tbl_forum fc
            WHERE   fc.topic_id = fl.topic_id
            ) AS replies
    FROM    (
            SELECT  topic_id, date AS last_reply
            FROM    wp_pod_tbl_forum fo
            WHERE   id = (
                    SELECT  id
                    FROM    wp_pod_tbl_forum fp
                    WHERE   fp.topic_id = fo.topic_id
                    ORDER BY
                            fp.date DESC, fp.id DESC
                    LIMIT 1
                    )
                    AND fo.topic_id <> 0
            ORDER BY
                    fo.date DESC, fo.id DESC
            LIMIT 20
            ) fl
    JOIN    wp_pod_tbl_forum ft
    ON      ft.id = fl.topic_id
    UNION ALL
    SELECT  id, name, date, 0
    FROM    wp_pod_tbl_forum t
    WHERE   NOT EXISTS
            (
            SELECT  NULL
            FROM    wp_pod_tbl_forum r
            WHERE   r.topic_id = t.id
            )
            AND t.topic_id = 0
    ORDER BY
           last_reply DESC, id DESC
    LIMIT  20
    

    Both indexes are required for this query to be efficient.

    If your table is InnoDB and id is a PRIMARY KEY, then you can omit id from the indexes above.

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

Sidebar

Related Questions

I have built a custom hashmap using two arrays. One contains keys another values.
I've built a custom UI for my table that has a darker UI and
I have built a custom component using some containers and a TileList. Now when
I've built a custom HTML5 audio player using jQUery according to the guide found
I have just built a custom autocomplete component using jquery. My problem is that
Ok so I have built a custom control that handles wysiwyg editing. Essentially on
For a personal project, I need to build a forum using PHP and MySQL.
Ok, I have built a custom CRM web app for a client using PHP
I have built a Custom MaskedTextBox , changing the values of BeepOnError and AsciiOnly
I've built a custom login system for my asp.net mvc 1.0 web application as

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.