Jump to content
Fórum Script Brasil
  • 0

Lentidão em consulta


Question

Olá pessoal, onde trabalho temos um sistema de coleta de dados ambientais e esse sistema possui uma procedure que demora muito para retornar os dados.

Tenho conhecimento muito básico em SQL e não tenho noção por onde tentar melhorar. Ficaria agradecido se puderem me sugerir por onde começar a melhorar esta procedure.

Abaixo está o código da pocedure.

Desde já agradeço!

DELIMITER $$

DROP PROCEDURE IF EXISTS `PesquisaAmbientalLinha2Diario` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `PesquisaAmbientalLinha2Diario`(IN Val1 Datetime)
BEGIN
  SELECT
    t1.A AS "Data Hora",
    t1.B AS "O2 (%)",
    t1.C AS "CO (ppm)",
    t1.D AS "NOx (mg/Nm3)",
    t1.E AS "THC (ppmv)",
    t1.F AS "Mat. Part. (mg/Nm3)",    
    t1.H AS "SOx (mg/m3)",
    t2.B AS "Alim. Farinha (t/h)",    
    t2.D AS "Combustível Precalcinador (t/h)",    
    t2.F AS "Combustível Maçarico (t/h)",
    t2.G AS "Balança 04r36M (t/h)",
    t2.H AS "Balança R14 (t/h)",
    t2.I AS "Balança R22 (t/h)"
FROM

(SELECT * FROM

(SELECT datetime AS A FROM ambientallinha2_horario
WHERE
    YEAR(datetime) = YEAR(Val1) AND
    MONTH(datetime) = MONTH(Val1) AND
    DAY(datetime) = DAY(Val1)
GROUP BY
    YEAR(datetime),
    MONTH(datetime),
    DAY(datetime),
    HOUR(datetime)
ORDER BY (datetime)) t11

LEFT OUTER JOIN

(SELECT
    (datetime) AS X,
    IF(CONTADOR >= 0.75*F10, ROUND(O2/CONTADOR,2), NULL) AS B,
    IF(CONTADOR >= 0.75*F10, ROUND(CO/CONTADOR,2), NULL) AS C,
    IF(CONTADOR >= 0.75*F10, ROUND(NOX/CONTADOR,2), NULL) AS D,
    IF(CONTADOR >= 0.75*F10, ROUND(THC/CONTADOR,2), NULL) AS E,
    IF(CONTADOR2 >=0.75*F10, ROUND(OPAC/CONTADOR2,2), NULL) AS F,
    IF(CONTADOR >= 0.75*F10, ROUND(SO2/CONTADOR,2), NULL) AS H    
FROM
    ambientallinha2_horario
WHERE
    YEAR(datetime) = YEAR(Val1) AND
    MONTH(datetime) = MONTH(Val1) AND
    DAY(datetime) = DAY(Val1)
GROUP BY
    YEAR(datetime),
    MONTH(datetime),
    DAY(datetime),
    HOUR(datetime)
ORDER BY (datetime))t12

ON YEAR(t11.A) = YEAR(t12.X) AND
   MONTH(t11.A) = MONTH(t12.X) AND
   DAY(t11.A) = DAY(t12.X) AND
   HOUR(t11.A) = HOUR(t12.X)) t1

LEFT OUTER JOIN


(SELECT * FROM

(SELECT
    (datetime) AS A,
    ROUND(FARINHA/CONTADOR,2) AS B,
    ROUND(COMB_PRECAL/CONTADOR,2) AS D,
    ROUND(COMB_MACARICO/CONTADOR,2) AS F,
    ROUND(COPROCESSAMENTO/CONTADOR,2) AS G
FROM
    producaolinha2_param00_horario
WHERE
    YEAR(datetime) = YEAR(Val1) AND
    MONTH(datetime) = MONTH(Val1) AND
    DAY(datetime) = DAY(Val1)
GROUP BY
    YEAR(datetime),
    MONTH(datetime),
    DAY(datetime),
    HOUR(datetime)
ORDER BY (datetime)) t21

LEFT OUTER JOIN

(SELECT
    (datetime) AS X,
    ROUND(AVG(R22),2) AS H,
    ROUND(AVG(R14),2) AS I
FROM
    producaolinha2_param01
WHERE
    YEAR(datetime) = YEAR(Val1) AND
    MONTH(datetime) = MONTH(Val1) AND
    DAY(datetime) = DAY(Val1)
GROUP BY
    YEAR(datetime),
    MONTH(datetime),
    DAY(datetime),
    HOUR(datetime)
ORDER BY (datetime)) t22

ON YEAR(t21.A) = YEAR(t22.X) AND
   MONTH(t21.A) = MONTH(t22.X) AND
   DAY(t21.A) = DAY(t22.X) AND
   HOUR(t21.A) = HOUR(t22.X)) t2

ON YEAR(t1.A) = YEAR(t2.A) AND
   MONTH(t1.A) = MONTH(t2.A) AND
   DAY(t1.A) = DAY(t2.A) AND
   HOUR(t1.A) = HOUR(t2.A);

END $$

DELIMITER ;

 

 

Link to post
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Cloud Computing


  • Forum Statistics

    • Total Topics
      148691
    • Total Posts
      644531
×
×
  • Create New...