MySQL Join и Union Optimization - PullRequest
       10

MySQL Join и Union Optimization

0 голосов
/ 03 июля 2011

У меня есть этот SQL, и он идет так, как он думал, но я думаю, что есть лучший способ написать его, чтобы избежать повторяющихся предложений и повысить производительность.

Есть предложения?

CREATE OR REPLACE VIEW tx_oriconvocatorias_consultas AS
SELECT pubs.*, pais.nombre_es as area,
GROUP_CONCAT(DISTINCT linea_es ORDER BY linea_es SEPARATOR '\n') as linea_es,
GROUP_CONCAT(DISTINCT modalidad_es ORDER BY modalidad_es SEPARATOR '\n') as modalidad_es,
GROUP_CONCAT(DISTINCT orgs.nombre_es ORDER BY orgs.nombre_es SEPARATOR '\n') as organizacion_es
FROM tx_oriconvocatorias_publicadas AS pubs

INNER JOIN tx_oriconvocatorias_publicadas_lineas_mm as ml
ON ml.uid_local=pubs.uid

INNER JOIN tx_oritablascomunes_lineas as lins
ON ml.uid_foreign=lins.uid

INNER JOIN tx_oriconvocatorias_publicadas_modalidades_mm as mm
ON mm.uid_local=pubs.uid

INNER JOIN tx_oritablascomunes_modalidades as mods
ON mm.uid_foreign=mods.uid

INNER JOIN tx_oriconvocatorias_publicadas_organizaciones_mm as mo
ON mo.uid_local=pubs.uid

INNER JOIN tx_oritablascomunes_organizaciones as orgs
ON mo.uid_foreign=orgs.uid

INNER JOIN tx_oritablascomunes_paises AS pais
ON pubs.carea = CONCAT('tx_oritablascomunes_paises_',pais.uid)
GROUP BY uid

UNION

SELECT pubs.*, regs.nombre_es as area,
GROUP_CONCAT(DISTINCT linea_es ORDER BY linea_es SEPARATOR '\n') as linea_es,
GROUP_CONCAT(DISTINCT modalidad_es ORDER BY modalidad_es SEPARATOR '\n') as modalidad_es,
GROUP_CONCAT(DISTINCT orgs.nombre_es ORDER BY orgs.nombre_es SEPARATOR '\n') as organizacion_es

FROM tx_oriconvocatorias_publicadas AS pubs

INNER JOIN tx_oriconvocatorias_publicadas_lineas_mm as ml
ON ml.uid_local=pubs.uid

INNER JOIN tx_oritablascomunes_lineas as lins
ON ml.uid_foreign=lins.uid

INNER JOIN tx_oriconvocatorias_publicadas_modalidades_mm as mm
ON mm.uid_local=pubs.uid

INNER JOIN tx_oritablascomunes_modalidades as mods
ON mm.uid_foreign=mods.uid

INNER JOIN tx_oriconvocatorias_publicadas_organizaciones_mm as mo
ON mo.uid_local=pubs.uid

INNER JOIN tx_oritablascomunes_organizaciones as orgs
ON mo.uid_foreign=orgs.uid

INNER JOIN tx_oritablascomunes_regiones AS regs
ON pubs.carea = CONCAT('tx_oritablascomunes_regiones_',regs.uid)
GROUP BY uid

1 Ответ

0 голосов
/ 03 июля 2011

Да.Используйте представление, чтобы собрать все sql, общие для обоих запросов:

CREATE VIEW PUB_ORGS AS
SELECT *
FROM tx_oriconvocatorias_publicadas AS pubs
INNER JOIN tx_oriconvocatorias_publicadas_lineas_mm as ml ON ml.uid_local=pubs.uid
INNER JOIN tx_oritablascomunes_lineas as lins ON ml.uid_foreign=lins.uid
INNER JOIN tx_oriconvocatorias_publicadas_modalidades_mm as mm ON mm.uid_local=pubs.uid
INNER JOIN tx_oritablascomunes_modalidades as mods ON mm.uid_foreign=mods.uid
INNER JOIN tx_oriconvocatorias_publicadas_organizaciones_mm as mo ON mo.uid_local=pubs.uid
INNER JOIN tx_oritablascomunes_organizaciones as orgs ON mo.uid_foreign=orgs.uid;

, а затем создайте объединенные запросы из этого

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...