0

Im having trouble with the performance of this query, especificallly with the case statements. How would yo optimize it?

SELECT a.t,
  a.c,
  a.id,
  a.id_r,
  a.s,
  a.n,
  a.n_s,
  a.c,
  a.co,
  a.d,
  a.dom,
  a.dom_,
  TIMESTAMPDIFF(YEAR, TIME_PARSE(a.nac, 'yyyy-MM-dd HH:mm:ss'), CURRENT_TIMESTAMP) AS edad,
  MAX(t.__t_px) AS t_px,
  MAX(c.__prom) AS prom,
  MAX(b.__avg) AS avg, 
     CASE
    WHEN MAX(b.average) >= 15 AND MAX(b.average) < 30 THEN 'a'
    WHEN MAX(b.average) >= 30 AND MAX(b.average) < 45 THEN 'b'
    WHEN MAX(b.average) >= 45 AND MAX(b.average) < 60 THEN 'c'
    WHEN MAX(b.average) >= 60 AND MAX(b.average) < 90 THEN 'e'
    WHEN MAX(b.average) >= 90 THEN 'f'
    ELSE 'g'
  END AS column,

And: 
  CASE 
    WHEN LATEST(v.value) FILTER(WHERE w.test LIKE '%1%') = 0 THEN -1
    ELSE LATEST(v.value) FILTER(WHERE w.test LIKE '%1')
  END AS 1,
  CASE 
    WHEN LATEST(v.value) FILTER(WHERE  w.test LIKE '2%') = 0 THEN -1
    ELSE LATEST(v.value) FILTER(WHERE  w.test LIKE '2%')
  END AS 2,
  CASE 
    WHEN LATEST(v.value) FILTER(WHERE  w.test LIKE '3%' AND a.n_prueba NOT LIKE '%4') = 0 THEN -1
    ELSE LATEST(v.value) FILTER(WHERE  w.test LIKE '3%' AND a.n_prueba NOT LIKE '%4%')
  END AS 3,
  CASE 
    WHEN LATEST(v.value) FILTER(WHERE  w.test LIKE '%4%') = 0 THEN -1
    ELSE LATEST(v.value) FILTER(WHERE w.test LIKE '%4%')
  END AS 4,
  CASE 
    WHEN LATEST(v.value) FILTER(WHERE  w.test LIKE '5%') = 0 THEN -1
    ELSE LATEST(v.value) FILTER(WHERE  w.test LIKE '5%')
  END AS 5,
  CASE 
    WHEN LATEST(v.value) FILTER(WHERE  w.test LIKE '6%') = 0 THEN -1
    ELSE LATEST(v.value) FILTER(WHERE  w.test LIKE '6%')
  END AS 6,
  CASE 
    WHEN LATEST(v.value) FILTER(WHERE  w.test LIKE '7%') = 0 THEN -1
    ELSE LATEST(v.value) FILTER(WHERE  w.test LIKE '7%')
  END AS 7,
  CASE 
    WHEN LATEST(v.value) FILTER(WHERE  w.test LIKE '8%') = 0 THEN -1
    ELSE LATEST(v.value) FILTER(WHERE  w.test LIKE '8%')
  END AS 8, 
CONCAT('<a link') AS extra
FROM CSSONSAP a,
  (SELECT COUNT(DISTINCT ced) AS __t_px FROM table) t
LEFT JOIN (
  SELECT 
    ced,
    AVG(value) AS avg
  FROM table
  WHERE __time >= CURRENT_TIMESTAMP - INTERVAL '1' YEAR
    AND value != -1
    AND ced NOT LIKE '%0619%'
    AND test LIKE '%name%'
  GROUP BY 1
) b ON b.ced = a.ced
LEFT JOIN (
  SELECT 
    ced,
    AVG(value) AS prom
  FROM table
  WHERE __time >= CURRENT_TIMESTAMP - INTERVAL '1' YEAR
    AND value != -1
    AND ced NOT LIKE '%0619%'
    AND test LIKE '%3%' 
    AND test NOT LIKE '%4%'
  GROUP BY 1
) c ON c.ced = a.ced
WHERE b.avg > 0
  AND a.value != -1
  AND a.ced NOT LIKE '%0619%'
  AND avg IS NOT NULL
  AND c.prom IS NOT NULL
GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12

EDIT: I added the full script, also im working with DRUID SQL syntax in an imply database

4
  • Can you add the full script and also the actual query plan.
    – Peter
    Commented Jan 16, 2023 at 14:20
  • 1
    Please consider reading this advice. Why do you think the case expressions are the problem?
    – mustaccio
    Commented Jan 16, 2023 at 14:25
  • Thank you for the advice, already edited the question, please consider to read the question again.
    – KodaCode
    Commented Jan 16, 2023 at 14:56
  • This is not Microsoft SQL Server syntax, so I removed that unrelated tag. Please tag which database system (and version) you're using. Also, please add the EXPLAIN ANALYZE to your Post.
    – J.D.
    Commented Jan 16, 2023 at 15:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.