Mysql – Limit WHERE to MAX() & COUNT()

join;MySQLperl

This is my current mySQL query:

SELECT e.*, MAX(m.datetime) AS unread_last, COUNT(m.id) AS unread
  FROM TAB_EVENT e
  LEFT JOIN TAB_MESSAGE m ON e.id=m.event_id
  WHERE ( m.`read` IS NULL OR m.`read` = 0)
  GROUP BY e.id
  ORDER BY m.datetime DESC, e.id ASC
  LIMIT 10;

I get: All TAB_EVENT rows which have no TAB_MESSAGE messages at all or have at least one unread one (read = BOOLEAN DEFAULT 0).

I need: All TAB_EVENT rows no matter of their messages AND the datetime of the newest unread and the count of unread messages for each TAB_EVENT.

There may be thousands of TAB_EVENT rows and many more TAB_MESSAGE rows and I'd like to avoid running one subquery for each single event.

Alternate 1: Using UNION using the query above plus another one with m.read = 1.
Drawback: Two JOINed heavy queries plus another temporary table.

Alternate 2: SELECT e.* unconditional plus another SELECT event_id, MAX(), COUNT() FROM TAB_MESSAGE GROUP BY event_id ORDER BY MAX() and merge everything within the calling Perl script.
Drawback: Loosing the ability of a server-side LIMIT.

UPDATE/RESOLUTION

Here is my final query doing the job:

SELECT e.*, m.unread_last, m.unread 
FROM
TAB_EVENT e LEFT JOIN
(
    SELECT event_id,MAX(`datetime`) AS unread_last, COUNT(*) AS unread FROM TAB_MESSAGE
    WHERE `read` = 0
    GROUP BY event_id
    ORDER BY `datetime` DESC
) m
ON e.id=m.event_id
ORDER BY m.datetime DESC, e.id ASC
LIMIT 10;

The read IS NULL condition caught up events not having any messages in the original query, the read column is boolean NOT NULL.

This query now returns all events and adds the newest unread message timestamp plus the number of unread messages. Both message columns are NULL for events not having any messages which is perfectly ok (will be translated to "" for datetime and 0 for the count on the Perl side).

Thanks to RolandoMySQLDBA, my query is based on his answer.

Best Answer

Here is your original query from the question

SELECT e.*, MAX(m.datetime) AS unread_last, COUNT(m.id) AS unread 
FROM TAB_EVENT e 
LEFT JOIN TAB_MESSAGE m ON e.id=m.event_id 
WHERE ( m.`read` IS NULL OR m.`read` = 0) 
GROUP BY e.id 
ORDER BY m.datetime DESC, e.id ASC 
LIMIT 10; 

Maybe try refactoring the query in such a way that in executes in this sequence

  • only collect necessary columns from TAB_MESSAGE
  • apply LIMIT 10 against the collected rows from TAB_MESSAGE
  • run the JOIN
  • apply the MAX() and COUNT() last

Here is what I am proposing

SELECT e.*, MAX(m.datetime) AS unread_last, COUNT(m.id) AS unread 
FROM
(
    SELECT * FROM
    (SELECT id,event_id,datetime FROM TAB_MESSAGE
    WHERE read IS NULL OR read = 0
    ORDER BY datetime DESC) mm
    LIMIT 10
) m
LEFT JOIN TAB_EVENT e 
ON e.id=m.event_id
ORDER BY m.datetime DESC, e.id ASC;

Give it a Try !!!

UPDATE 2012-02-21 17:06 EDT

SELECT e.*, MAX(m.datetime) AS unread_last, COUNT(m.id) AS unread 
FROM
TAB_EVENT e LEFT JOIN
(
    SELECT * FROM
    (SELECT id,event_id,datetime FROM TAB_MESSAGE
    WHERE read IS NULL OR read = 0
    ORDER BY datetime DESC) mm
    LIMIT 10
) m
ON e.id=m.event_id
ORDER BY m.datetime DESC, e.id ASC;

@Sebastian, I put the query back in the original join order. Please try this as well !!!

UPDATE 2012-02-21 17:11 EDT

Make sure the datetime field is indexed

ALTER TABLE TAB_MESSAGE ADD INDEX read_datetime_ndx (read,datetime);