[Solved] MySQL - how to show the latest topic per thread

EverSQL Database Performance Knowledge Base

MySQL - how to show the latest topic per thread

Database type:

I am trying to create SQL for retrieveing a list of latests posts for the forum thread. I have the following code:

SELECT
 item_discuss_thread_id
 , item_discuss_post_title
 , COUNT(item_discuss_thread_id) AS nb_posts
FROM
 item_discuss_posts
GROUP BY
 item_discuss_thread_id

Obviously this will group without the respect of if the post is latest or not. item_discuss_post_title will just get the first row in the group.

I wonder if there's some way around this? If not, what is the best way to solve the problem... only subqueries?

Thanks, Pavel

UPDATE: Please note that I need all threads, LIMIT 1 is not solving the problem. Also ORDER BY is not an option as GROUP BY will select the first record from group anyway. This is not such a simple question as it can seem to be.

UPDATE:

I really want to try to avoid using subqueries or if doing so - use it the optimal may. What I came with currently is something like this:

SELECT
  ordered_by_date.item_discuss_thread_id
  , item_discuss_post_title
  , COUNT(item_discuss_thread_id) AS nb_posts
FROM
  (
   SELECT
     item_discuss_thread_id
     , item_discuss_post_title
   FROM
      item_discuss_posts
   ORDER BY
      item_discuss_post_datetime DESC
  ) AS ordered_by_date
GROUP BY
   item_discuss_thread_id

EXPLAIN EXTENDED gives the following result:

id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, PRIMARY, <derived2>, ALL, \N, \N, \N, \N, 20, Using temporary; Using filesort
2, DERIVED, item_discuss_posts, index, \N, item_discuss_post_datetime, 8, \N, 20, 

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `item_discuss_posts` ADD INDEX `item_posts_idx_item_id` (`item_discuss_thread_id`);
The optimized query:
SELECT
        item_discuss_posts.item_discuss_thread_id,
        item_discuss_posts.item_discuss_post_title,
        COUNT(item_discuss_posts.item_discuss_thread_id) AS nb_posts 
    FROM
        item_discuss_posts 
    GROUP BY
        item_discuss_posts.item_discuss_thread_id 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.