1

actualmente estoy teniendo problemas para cargar esta informacion, ya que es bastante lento y al parecer el problema es mi consulta.

mi pregunta es como puedo otimizar los tiempos de carga en esta consulta.

SELECT distinct  T0.emp_pin, T0.emp_firstname, CONVERT(date, T1.punch_time, 4) AS Fecha, CONVERT(varchar,
                             (SELECT        MIN(punch_time) AS Expr1
                               FROM            dbo.att_punches AS TX
                               WHERE        (employee_id = T0.id) AND (CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1))), 24) AS Entrada, 
                               CONVERT(varchar,
                             (SELECT        MAX(punch_time) AS Expr1
                               FROM            dbo.att_punches AS TX
                               WHERE        (employee_id = T0.id) AND (CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1))), 24) AS Salida,
                               CONVERT(varchar,
                                 (SELECT        MIN(punch_time) AS Expr1
                               FROM            dbo.att_punches AS TX
                               WHERE    DATEPART(HOUR,punch_time) * 100 + DATEPART(minute,punch_time) between 1145 and 1230 and    (employee_id = T0.id) AND (CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1))), 24) AS Salida_Almuerzo,
                               CONVERT(varchar,
                             (SELECT        MAX(punch_time) AS Expr1
                               FROM            dbo.att_punches AS TX
                               WHERE       DATEPART(HOUR,punch_time) * 100 + DATEPART(minute,punch_time) between 1250 and 1330 and   (employee_id = T0.id) AND (CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1))), 24) AS Entrada_Almuerzo
                                
                                
                                
                        


FROM            dbo.hr_employee AS T0 LEFT JOIN
                dbo.att_punches AS T1 ON T0.id = t1.employee_id
GO
3
  • ya intentaste en vez de hacer 4 sub queries, hacer uno solo agrupado?
    – MrDonPh0x
    Commented el 16 jun. 2023 a las 15:23
  • la converti en una sola y ahora si puedo verla super rapido, gracias bro Commented el 16 jun. 2023 a las 15:37
  • ¿MySQL o SQL Server? Elimina la etiqueta que no corresponda de tu pregunta.
    – padaleiana
    Commented el 16 jun. 2023 a las 15:38

2 respuestas 2

3

Podemos simplificar mucho esta consulta. Esto sirve para simplificar su lectura como para mejorar su rendimiento. Lo primero que podemos hacer es mover la conversión de la columna punch_time fuera de la lista de columnas para no tener que repetirla.

SELECT E.emp_pin, 
       E.emp_firstname, 
       CONVERT(DATE, P.punch_time, 4)        AS Fecha, 
       H.hora
FROM dbo.hr_employee AS E
LEFT JOIN dbo.att_punches AS P ON E.id = P.employee_id
OUTER APPLY (SELECT CONVERT(char(8), P.punch_time, 24) AS hora) H;

Así, ya podemos hacer los agregados. Para la Entrada y la salida es muy sencillo porque solo necesitamos el máximo y mínimo por día y empleado.

SELECT E.emp_pin, 
       E.emp_firstname, 
       CONVERT(DATE, P.punch_time, 4)        AS Fecha, 
       MIN( H.Hora) AS Entrada, 
       MAX( H.Hora) AS Salida 
FROM dbo.hr_employee AS E
LEFT JOIN dbo.att_punches AS P ON E.id = P.employee_id
OUTER APPLY (SELECT CONVERT(char(8), P.punch_time, 24) AS hora) H
GROUP BY E.emp_pin, 
       E.emp_firstname, 
       CONVERT(DATE, P.punch_time, 4) ;

Después, usamos un agregado condicional que permite usar solo los valores que requerimos.

SELECT E.emp_pin, 
       E.emp_firstname, 
       CONVERT(DATE, P.punch_time, 4)        AS Fecha, 
       MIN( H.Hora) AS Entrada, 
       MAX( H.Hora) AS Salida, 
       MIN( CASE WHEN H.hora BETWEEN '11:45' AND '12:30' THEN H.hora END)  AS Salida_Almuerzo, 
       MAX( CASE WHEN H.hora BETWEEN '12:50' AND '13:30' THEN H.hora END)  AS Entrada_Almuerzo
FROM dbo.hr_employee AS E
LEFT JOIN dbo.att_punches AS P ON E.id = P.employee_id
OUTER APPLY (SELECT CONVERT(char(8), P.punch_time, 24) AS hora) H
GROUP BY E.emp_pin, 
       E.emp_firstname, 
       CONVERT(DATE, P.punch_time, 4) ;

No necesitamos hacer otro cálculo matemático para la comparación porque la comparación de cadenas funcionará correctamente.

1
  • Excelente funciona muy bien, muchas gracias Commented el 16 jun. 2023 a las 16:43
0

La solucion fue convertir los subqwerys en uno solo

SELECT
    T0.emp_pin,
    T0.emp_firstname,
    CONVERT(date, T1.punch_time, 4) AS Fecha,
    MIN(CASE
        WHEN CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1) THEN CONVERT(varchar, punch_time, 24)
        ELSE NULL
    END) AS Entrada,
    MAX(CASE
        WHEN CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1) THEN CONVERT(varchar, punch_time, 24)
        ELSE NULL
    END) AS Salida,
    MIN(CASE
        WHEN DATEPART(HOUR, punch_time) * 100 + DATEPART(minute, punch_time) BETWEEN 1145 AND 1230
            AND CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1) THEN CONVERT(varchar, punch_time, 24)
        ELSE NULL
    END) AS Salida_Almuerzo,
    MAX(CASE
        WHEN DATEPART(HOUR, punch_time) * 100 + DATEPART(minute, punch_time) BETWEEN 1250 AND 1330
            AND CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1) THEN CONVERT(varchar, punch_time, 24)
        ELSE NULL
    END) AS Entrada_Almuerzo
FROM dbo.hr_employee AS T0
LEFT JOIN dbo.att_punches AS T1 ON T0.id = T1.employee_id
GROUP BY T0.emp_pin, T0.emp_firstname, CONVERT(date, T1.punch_time, 4)
1
  • No necesitas incluir CONVERT(date, punch_time, 1) = CONVERT(date, T1.punch_time, 1) porque es exactamente la misma columna. Commented el 16 jun. 2023 a las 15:49

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.