gpt4 book ai didi

sql - PostgreSQL查询效率

转载 作者:行者123 更新时间:2023-11-29 12:14:06 24 4
gpt4 key购买 nike

我正在使用 PostgreSQL(我是数据库领域的菜鸟),我想知道您对我在使用的代码中发现的这种查询效率的看法。这些查询有很多 JOIN,其中一个(粗体)根据请求有很多行。这迫使我们 GROUP BY request.id 以便按请求获取一行和包含所有这些行数据的字段(粗体)。

我认为这种查询必须花费大量时间来寻找所有这些最大值,但我想不出替代方法。关于它的效率以及如何改进它有什么想法吗?

SELECT
request.id AS id,
max(request_type.name) AS request_type,
to_char(max(request.timestamp),'DD/mm/YYYY HH24:mi') AS timestamp,
to_char(max(request.timestamp),'YYYY-mm-DD') AS timestamp_filtering,
max(state.name) AS request_state,
max(users.name || ' ' || COALESCE(users.surname,'')) AS create_user,
max(request.id_create_user) AS id_create_user,
max(enterprise.name) AS enterprise,
max(cause_issue.name) AS cause,
max(request_movements.id_request_state) AS id_state,
array_to_string(array_agg(DISTINCT act_code.name || '/' || req_res.act_code), ', ') AS act_code, /* here */
max(revised.code) AS state_revised,
max(request_shipment.warehouse) AS warehouse,
max(req_res.id_warehouse) AS id_warehouse
FROM
request
LEFT JOIN users
ON users.id=request.id_create_user
LEFT JOIN enterprise
ON users.id_enterprise=enterprise.id
LEFT JOIN request_movements
ON request_movements.id=request.id_request_movement
LEFT JOIN request_versions
ON request_versions.id = request_movements.id_version
LEFT JOIN state
ON request_movements.id_request_state=state.id
INNER JOIN request_type
ON request.id_request_type=request_type.id
LEFT JOIN cause_issue
ON request.id_cause_issue=cause_issue.id
LEFT JOIN request_reserve req_res
ON req_res.id_request = request.id /* here */
LEFT JOIN act_code
ON req_res.id_act_code=act_code.id
LEFT JOIN request_shipment
ON (request_shipment.id_request=request.id)
LEFT JOIN warehouse_enterprise
ON (warehouse_enterprise.id = request_shipment.id_warehouse_enterprise)
LEFT JOIN revised
ON (revised.id = request_shipment.id_revised)
WHERE
request.id_request_type = "any_type"
GROUP BY
request.id

EXPLAIN 返回 this .

最佳答案

您可以通过聚合 request_reserveact_code before 中的值来大大简化此查询JOIN加入。这避免了对所有其他列使用聚合函数的需要,并且对于更多的行通常应该更快。

SELECT r.id
,rt.name AS request_type
,to_char(r.timestamp, 'DD/mm/YYYY HH24:mi') AS timestamp
,to_char(r.timestamp, 'YYYY-mm-DD') AS timestamp_filtering
,s.name AS request_state
,u.name || COALESCE(' ' || u.surname, '') AS create_user
,r.id_create_user
,e.name AS enterprise
,c.name AS cause
,rm.id_request_state AS id_state
,rr.act_code
,rd.code AS state_revised
,rs.warehouse
,rr.id_warehouse
FROM request r
LEFT JOIN users u ON u.id = r.id_create_user
LEFT JOIN enterprise e ON e.id = u.id_enterprise
LEFT JOIN request_movements rm ON rm.id = r.id_request_movement
LEFT JOIN request_versions rv ON rv.id = rm.id_version
LEFT JOIN state s ON s.id = rm.id_request_state
JOIN request_type rt ON rt.id = r.id_request_type
LEFT JOIN cause_issue c ON c.id = r.id_cause_issue
LEFT JOIN request_shipment rs ON rs.id_request = r.id
LEFT JOIN warehouse_enterprise w ON w.id = rs.id_warehouse_enterprise
LEFT JOIN revised rd ON rd.id = rs.id_revised
LEFT JOIN (
SELECT rr.id_request, rr.id_warehouse
,array_to_string(array_agg(
DISTINCT a.name || '/' || rr.act_code), ', ') AS act_code
FROM request_reserve rr
LEFT JOIN act_code a ON r.id_act_code = a.id
GROUP BY rr.id_request, rr.id_warehouse
) rr ON rr.id_request = r.id
WHERE r.id_request_type = "any_type"; -- use single quotes for values!

对于大型查询,您必须拥有一种人眼可以轻松解析的格式。因此,在改进查询之前我重新格式化了。 我使用表别名来尽可能避免笨重的标识符。

create_user 的小改进:没有尾随空格。如果名称的任何一部分可以是 NULL,我建议这样做以避免悬空空间:

COALESCE(u.name || ' ' || u.surname, u.name, u.surname)

在 PostgreSQL 9.1 或更高版本中,您可以使用 concat_ws() .

关于sql - PostgreSQL查询效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13357541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com