[Solved] MySQL: Can I make a query to output a grid of performance by month?

EverSQL Database Performance Knowledge Base

MySQL: Can I make a query to output a grid of performance by month?

Database type:

I'm sorry if this has been asked before, but I just can't find any answer that does the exact thing I need.

My cycling club logs mileage for every ride. To collect data per ride/per rider I have this table (together with a membership table I join to it):

CREATE TABLE `mileage` (
  `id` int(11) NOT NULL,
  `rideID` mediumint(9) NOT NULL,
  `rideDate` date NOT NULL,
  `isWeekly` tinyint(1) NOT NULL,
  `riderUid` smallint(6) NOT NULL,
  `leaderUid` smallint(6) NOT NULL,
  `mileage` smallint(6) NOT NULL,
  `days` smallint(6) NOT NULL DEFAULT '1',
  `eBike` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

To output yearly data I use this MySQL query:

SELECT m.riderUid, w.firstName, w.lastName,
SUM(m.mileage) as miles,
COUNT(*) as cnt,
SUM(m.days) as days
FROM `mileage` m
    LEFT JOIN `members` w ON (w.uid = m.riderUid)
WHERE (m.rideDate >= '2018-01-01' AND m.rideDate < '2018-01-01')
GROUP BY m.riderUid
ORDER BY w.lastName;

This outputs totals by rider for the time period.

But I want to get the monthly totals as well, something like this:

Name         JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
Rider 1      301 | 213 | 313 | 432 | 556 | 866 | 901 | 877 | 806 | 545 | 512 | 503 |
Rider 2      444 | 525 | 445 | 532 | 636 | 966 | 989 | 900 | 866 | 665 | 633 | 585 |
... etc.

Each row returned from the database will have either an array of monthly data or 12 named (or indexed) months for each row.

I feel like this ought to be simple, but so far it's not. I can easily crunch this in post-processing, but the processing time for that is too great.

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.
Optimal indexes for this query:
ALTER TABLE `members` ADD INDEX `members_idx_uid` (`uid`);
ALTER TABLE `mileage` ADD INDEX `mileage_idx_ridedate` (`rideDate`);
ALTER TABLE `mileage` ADD INDEX `mileage_idx_rideruid` (`riderUid`);
The optimized query:
SELECT
        m.riderUid,
        w.firstName,
        w.lastName,
        SUM(m.mileage) AS miles,
        COUNT(*) AS cnt,
        SUM(m.days) AS days 
    FROM
        `mileage` m 
    LEFT JOIN
        `members` w 
            ON (
                w.uid = m.riderUid
            ) 
    WHERE
        (
            m.rideDate >= '2018-01-01' 
            AND m.rideDate < '2018-01-01'
        ) 
    GROUP BY
        m.riderUid 
    ORDER BY
        w.lastName

Related Articles



* original question posted on StackOverflow here.