toonvanstrijp
toonvanstrijp

Reputation: 417

How to optimise a SQL query with many of the same selections?

I've created this query, but I think it can be optimised a lot... but I don't know how?

It's a query that selected the notCompleted tasks that a student has, also it select the planned task count, and the count of how much tasks there are that he requested help for.

Later on I don't want a count but the full row of the answer that he/she requested help for, but for now I started by doing it with a count since that seemed easier.

Is there someone that can explain me how to optimise this, or where I can learn this?

SELECT
    gu.user_id,
    notComplete.count AS notComplete,
    planned.count AS planned,
    helpNeeded.need AS helpNeeded
FROM
    `groups_users` gu
LEFT JOIN(
    SELECT
        a.created_by AS user_id,
        COUNT(1) AS 'count'
    FROM
        `answers` a
    WHERE
        a.deleted_at IS NULL AND a.completed_at IS NULL AND a.checked_at IS NULL
    GROUP BY
        a.created_by
) AS notComplete
ON
    notComplete.user_id = gu.user_id
LEFT JOIN(
    SELECT
        a.created_by AS user_id,
        COUNT(1) AS 'count'
    FROM
        `answers` a
    WHERE
        a.deleted_at IS NULL
    GROUP BY
        a.created_by
) AS planned
ON
    planned.user_id = gu.user_id
LEFT JOIN(
    SELECT
        a.created_by AS user_id,
        COUNT(1) AS need
    FROM
        `answers` a
    WHERE
        a.deleted_at IS NULL AND a.requested_help_at IS NOT NULL
    GROUP BY
        a.created_by
) AS helpNeeded
ON
    helpNeeded.user_id = gu.user_id
INNER JOIN `users` u ON
    u.id = gu.user_id AND u.type = 'student'
WHERE
    gu.group_id = 213

Upvotes: 0

Views: 43

Answers (1)

Sergey Menshov
Sergey Menshov

Reputation: 3906

Try the following

SELECT
  gu.user_id,
  SUM(IF(a.deleted_at IS NULL AND a.completed_at IS NULL AND a.checked_at IS NULL,1,0)) AS notComplete,
  SUM(IF(a.deleted_at IS NULL,1,0)) AS planned,
  SUM(IF(a.deleted_at IS NULL AND a.requested_help_at IS NOT NULL,1,0)) AS helpNeeded
FROM `groups_users` gu
JOIN `users` u ON u.id = gu.user_id
LEFT JOIN `answers` a ON a.created_by = gu.user_id
WHERE gu.group_id = 213
  AND u.type = 'student'
GROUP BY gu.user_id

Try the following for your second question

SELECT
  gu.user_id,
  IF(a.deleted_at IS NULL AND a.completed_at IS NULL AND a.checked_at IS NULL,1,0) AS notComplete,
  IF(a.deleted_at IS NULL,1,0) AS planned,
  IF(a.deleted_at IS NULL AND a.requested_help_at IS NOT NULL,1,0) AS helpNeeded,
  a.* -- full answer row
FROM `groups_users` gu
JOIN `users` u ON u.id = gu.user_id
LEFT JOIN `answers` a ON a.created_by = gu.user_id
LEFT JOIN `answer_plannedday` ap ON ap.answer_id = a.id
LEFT JOIN `plannedday` p ON p.id = ap.planned_day_id
WHERE gu.group_id = 213
  AND u.type = 'student'  
  AND STR_TO_DATE(CONCAT(p.year, '-', p.month, '-', p.day), '%Y-%m-%d') BETWEEN STR_TO_DATE('2016-12-01', '%Y-%m-%d') AND STR_TO_DATE('2018-04-13', '%Y-%m-%d')

If you want to get detail information only for notComplete you can put all the necessary conditions into WHERE

SELECT
  gu.user_id,
  a.* -- full answer row
FROM `groups_users` gu
JOIN `users` u ON u.id = gu.user_id
LEFT JOIN `answers` a ON a.created_by = gu.user_id
LEFT JOIN `answer_plannedday` ap ON ap.answer_id = a.id
LEFT JOIN `plannedday` p ON p.id = ap.planned_day_id
WHERE gu.group_id = 213
  AND u.type = 'student'  
  AND STR_TO_DATE(CONCAT(p.year, '-', p.month, '-', p.day), '%Y-%m-%d') BETWEEN STR_TO_DATE('2016-12-01', '%Y-%m-%d') AND STR_TO_DATE('2018-04-13', '%Y-%m-%d')
  -- you say here that you want to get only these rows
  AND a.deleted_at IS NULL AND a.completed_at IS NULL AND a.checked_at IS NULL

Upvotes: 1

Related Questions