0

I have an SQL (MariaDB, version: 10.3.22-MariaDB-0+deb10u1):

SELECT
    v.employee_id AS id,
    REPLACE(
        FORMAT(
         COALESCE((
            SUM(COALESCE((COALESCE(v.amount, 0) / COALESCE(ash.value, a.standard, 0)), 0))
            /
            (
               SUM(COALESCE(v.worktime, 0)) 
               +
               (
                  SELECT SUM(COALESCE(t.break, 0)) 
                  + COALESCE(SUM(t.assigned_task1), 0) 
                  + COALESCE(SUM(t.assigned_task2), 0) 
                  + COALESCE(SUM(t.assigned_task3), 0) 
                  + COALESCE(SUM(t.assigned_task4), 0) 
                  + COALESCE(SUM(t.assigned_task5), 0) 
                  FROM times t 
                  WHERE t.employee_id = v.employee_id 
                  AND t.day BETWEEN '2022-02-03' AND '2022-02-03' 
               )
            ) 
         ), 0) * 100 
       , 0 
     ), ',', '') AS p_real 
FROM volumes v 
INNER JOIN activities a 
ON 
   v.activity_id = a.id 
   AND a.is_deleted = 0 
   AND a.id IN (-1, 145, 218, 227, 286) /* There are much more IDs in real SQL. */
LEFT JOIN activity_standard_histories ash 
ON ash.activity_id = a.id AND ash.created = (
    SELECT created 
    FROM activity_standard_histories 
    WHERE 
        DATE(activity_standard_histories.created) <= v.day 
        AND activity_standard_histories.activity_id = a.id 
    ORDER BY activity_standard_histories.created DESC 
    LIMIT 1
)
WHERE 
    v.employee_id IN ('552','553','554','2113','2991') /* There are much more employee_ids in real SQL. */
    AND v.day BETWEEN '2022-02-03' AND '2022-02-03' 
GROUP BY v.employee_id

volumes table has 486.2Mb size and growth up all the time.

This query is performed for a very long time. What I've noticed:

  1. Query EXPLAIN:

    enter image description here

  2. State for this query is "Creating sort index" for a long time.

Tables indexes:

volumes
enter image description here

activities
enter image description here

activity_standard_histories
enter image description here

times
enter image description here

I've seen a lot of sources: this, this, this, this and so on. However, I'm not sure what exactly should I apply. Maybe it could be some additional complex index, but I'm not sure which one, given that each table already has certain indexes.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Boolean_Type
  • 1,146
  • 3
  • 13
  • 40
  • In your query is "There are much more IDs in real SQL." Please define "much more", 10 more or 1000 more ? – Luuk Feb 25 '22 at 17:17
  • @Luuk `a.id` count is 76 and `v.employee_id` count is 215. Those counts are very dynamic. – Boolean_Type Feb 25 '22 at 17:27
  • I would try to give those 76 or 215 id's using a query from the database, in stead of a list of numbers. – Luuk Feb 25 '22 at 18:06
  • @Luuk Those ids come from POST (ie from form). – Boolean_Type Feb 26 '22 at 16:31
  • Pretty complex form, if it produces 76 id's + 215 other id's. I think that problem needs more attention than the performance of this query. – Luuk Feb 26 '22 at 19:21

0 Answers0