MySQL Join 和 Union 优化

发布于 2024-11-18 03:08:31 字数 2105 浏览 3 评论 0原文

我有这个 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

I have this SQL and it goes like it was thought, but I guess there is a better way to write it in order to avoid repetitive sentences and increase the performance.

Any suggestions?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2024-11-25 03:08:31

是的。使用视图收集两个查询共有的所有 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;

然后从中构建联合查询

Yes. Use a view to collect up all the sql common to both queries:

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;

then build your unioned queries from this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文