내 질문은 다음 문서 (테이블과 기능 hierarchy_connect_by_parent_eq_prior_id) http://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/MySQL은, 트리, 계층 적 쿼리 성능
은 (테이블 t_hierarchy이 typ1 (ID와 부모 옆에) 두 개의 추가 필드가 있다고 가정하자 기반으로 char) 및 시간 (int). 필드 typ1은 두 개의 값 A와 B를 가질 수 있습니다. 목표는 기사에서 설명한대로 전체 트리를 표시하는 것입니다. 그러나 현재 노드의 시간을 표시하는 추가 필드가 필요합니다 (typ1 = B 인 경우). 모든 자손 (typ1 = B 인 경우). 그래서 typ1 = B 일 때 특정 노드 (그 자체 포함)에 대한 모든 자손의 시간의 합이 필요합니다.
나는 다음과 같은 해결책을 가지고 있지만, 너무 느린 :
이주요 질의 :
SELECT CONCAT(REPEAT(' ', level - 1), hi.id) AS treeitem, get_usertime_of_current_node_and_descendants(hi.id) as B_time,
hierarchy_sys_connect_by_path('/', hi.id) AS path,
parent, level
FROM (
SELECT hierarchy_connect_by_parent_eq_prior_id(id) AS id,
CAST(@level AS SIGNED) AS level
FROM (
SELECT @start_with := 0,
@id := @start_with,
@level := 0
) vars, t_hierarchy
WHERE @id IS NOT NULL
) ho
JOIN t_hierarchy hi
ON hi.id = ho.id
이 기능 get_usertime_of_current_node_and_descendants (입력 INT)이 :
는BEGIN
DECLARE _id INT;
DECLARE _desctime INT;
DECLARE _nodetime INT;
SET _id = input;
select COALESCE((select sum(time) from (
SELECT hi.id, time,typ1
FROM (
SELECT hierarchy_connect_by_parent_eq_prior_id_2(id) AS id, @levela AS level
FROM (
SELECT @start_witha := _id,
@ida := @start_witha,
@levela := 0,
) vars, t_hierarchy a
WHERE @ida IS NOT NULL
) ho
JOIN t_hierarchy hi
ON hi.id = ho.id
) q where typ1 = 'B'), 0) into _desctime;
select COALESCE((select time from t_hierarchy where id = _id and typ1='B'), 0) into _nodetime;
return _desctime + _nodetime;
END $$
함수 hierarchy_connect_by_parent_eq_prior_id_2가있다 article과 동일하고 hierarchy_connect_by_parent_eq_prior_id 위의 것과 동일하지만 글로벌 변수 이름이 다르게 지정되었습니다. 그래서 그것은 메인 쿼리에서 사용 된 것과 간섭하지 않을 것입니다.
위의 솔루션은 원하는대로 작동하지만 너무 느립니다 (특히 대규모 데이터 세트로 작업하는 경우). 더 나은 솔루션을 제공 할 수 있습니까? 아니면 쿼리를 향상시키는 방법을 제안 할 수 있습니까? 시간과 도움을 미리 보내 주셔서 감사합니다!
중첩 세트를 사용하려고합니다. – Bytemain
안타깝게도 중첩 세트 사용은 옵션이 아닙니다. – ltblueberry
할 수있는 작업은 고정 된 트리 깊이 (예 : 4)를 사용하고 쿼리에서 조인을 사용하는 것입니다. 나는 깊이 4의 인접한 트리에 대해 이렇게했고 recursivley는 트리를 더 빨리 쿼리해야합니다. 물론보기가 좋지 않으며 유연하지도 않습니다. – Bytemain