Tengo esta consulta en mysql, mi problema surge cuanto uso muchos CASE WHEN
, para no alargar el codigo, elimine los demas y solo deje estos que muestro a continuacion
SELECT D551.TipoOperacion,D551.ClaveDocumento, D551.FechaP,
(CASE WHEN D551.Ped=(
SELECT D554.Ped FROM Tabla554 D554 WHERE D554.idEmp = 4
AND D554.Ped = D551.Ped AND D554.ClaveC = 'DH' AND D554.Fraccion = D551.Fraccion LIMIT 1)
THEN (SELECT D554.ComplementoC FROM Tabla554 D554 WHERE D554.idEmp = 4
AND D554.Ped = D551.Ped AND D554.ClaveC = 'DH' LIMIT 1) ELSE '0' END) AS 'DH',
(CASE WHEN D551.Ped=(
SELECT D554.Ped FROM Tabla554 D554 WHERE D554.idEmp =4
AND D554.Ped = D551.Ped AND D554.ClaveC = 'EN' AND D554.IdentificadorCaso = 'U' AND D554.Fraccion = D551.Fraccion LIMIT 1)
THEN (SELECT D554.ComplementoC FROM Tabla554 D554 WHERE D554.idEmp = 4
AND D554.Ped = D551.Ped AND D554.ClaveC = 'EN' LIMIT 1) ELSE '0' END) AS 'EN U'
(CASE WHEN D551.Ped=(
SELECT D554.Ped FROM Tabla554 D554 WHERE D554.idEmp = 4
AND D554.Ped = D551.Ped AND D554.ClaveC = 'TG' AND D554.Fraccion = D551.Fraccion LIMIT 1)
THEN (SELECT D554.ComplementoC FROM Tabla554 D554 WHERE D554.idEmp = 4
AND D554.Ped = D551.Ped AND D554.ClaveC = 'TG' LIMIT 1) ELSE '0' END) AS 'XD',
(CASE WHEN D551.Ped=(
SELECT D554.Ped FROM Tabla554 D554 WHERE D554.idEmp =4
AND D554.Ped = D551.Ped AND D554.ClaveC = 'FP' AND D554.IdentificadorCaso = 'U' AND D554.Fraccion = D551.Fraccion LIMIT 1)
THEN (SELECT D554.ComplementoC FROM Tabla554 D554 WHERE D554.idEmp = 4
AND D554.Ped = D551.Ped AND D554.ClaveC = 'FP' LIMIT 1) ELSE '0' END) AS 'EN X'
FROM Tabla551 D551 WHERE D551.idEmp = 4
AND D551.FechaP BETWEEN "2022-04-01" AND "2022-04-30";
las tablas no tienen relacion alguna, pero tiene algo en comun, que son D554.Ped
, D554.idEmp
,D554.Fraccion
, estos datos son iguales en ambas tablas, cuanto son 2 mil registros la consulta se demora mas de 5 minutos en devolver los resultados.
Tambien intente usar un INNER JOIN, LEFT JOIN
, pero obtengo mas resultados de lo esperado, que son 604 registros al hacer JOINS, tambien intente agruparlos de por Ped, IdEm, Fraccion, pero obtengo menos resultados esperados, GROUP BY D551.Ped,D551.PedCompleto,D551.Fraccion
SELECT SELECT D551.TipoOperacion,D551.ClaveDocumento, D551.FechaP,
IF(D554.ClaveC = 'DH' AND D554.Ped = D551.Ped AND D554.Fraccion = D551.Fraccion, D554.ComplementoC,'') AS 'DH',
IF(D554.ClaveC = 'EN' AND D554.IdentificadorCaso = 'U' AND D554.Ped = D551.Ped AND D554.Fraccion = D551.Fraccion , D554.ComplementoC,'') AS 'EN U',
IF(D554.ClaveC = 'EN' AND D554.IdentificadorCaso = 'X' AND D554.Ped = D551.Ped AND D554.Fraccion = D551.Fraccion, D554.ComplementoC,'' ) AS 'XD',
IF(D554.ClaveC = 'EN' AND D554.IdentificadorCaso = 'X' AND D554.Ped = D551.Ped AND D554.Fraccion = D551.Fraccion, D554.ComplementoC,'' ) AS 'EN X'
FROM Tabla551 D551 INNER JOIN Tabla554 D554 ON D551.PedCompleto = D554.PedCompleto
AND D551.idEmp = D554.idEmp WHERE D551.idEmp = 4 AND D554.idEmp = 4
AND D554.ClaveC IN("DH","EN","MA","MC","PO","PT","TL","XP")
AND D554.Ped = D551.Ped AND D554.Fraccion = D551.Fraccion
AND D551.FechaP BETWEEN "2022-04-01" AND "2022-04-30" ;
cuanto en mi consulta a la tabla 551 obtengo 204 registros que es el resultado esperado
SELECT * FROM Tabla551 WHERE idEmp = 4 and FechaP BETWEEN "2022-04-01" AND "2022-04-30"
como podria optimizar esta consulta