2012-03-17 1 views
3

에 최적화 가입MYSQL LEFT CASE

가 어떻게이 SELECT를 최적화 할 수있다 (지금은) 내가 COLASCE를 (사용하고 그에게 감사) ... 나는 CASE와이 SELECT 작업려고 몇 시간을 보냈다 그러나 나는 실패 CASE/IF 문장을 사용하여? 이것은 필드에 의해 선택된 다른 테이블에서 쿼리하는 빠른 방법입니까?

SELECT a.folderid, a.foldername, a.contenttype, COALESCE(b.descriptor, c.descriptor, d.descriptor, e.descriptor, f.descriptor) as descriptor 
FROM t_folders a 
LEFT JOIN t_files b 
ON a.contenttype = 'file' AND a.contentid = b.fileid 
LEFT JOIN t_links c 
ON a.contenttype = 'link' AND a.contentid = c.linkid 
LEFT JOIN t_extfiles d 
ON a.contenttype = 'extfile' AND a.contentid = d.extfileid 
LEFT JOIN t_videos e 
ON a.contenttype = 'video' AND a.contentid = e.videoid 
LEFT JOIN t_exams f 
ON a.contenttype = 'exam' AND a.contentid = f.examid 
WHERE a.folderid = $folderId 
ORDER BY a.folderid DESC 
+0

정확히 달성하기를 원하십니까? 왜 CASE 또는 IF를 사용합니까? 무엇을 최적화하고 싶습니까? COALESCE가하는 일이 당신에게 분명합니까? 그렇다면 왜 당신이 찾고있는 것이 아닙니다. – fancyPants

+0

CASE/IF에 대해 LEFT JOIN을 대체 할 수 있는지 알고 싶습니다. mysql이 더 빠를 수 있습니까? – kbitdn

+0

@tombom 및 kbitdn :이 질문에 대한 답변이 없지만 그 질문에도 관심이 있습니다 ... 비슷한 문제가 있습니다 ... – Chris

답변

1

을 optmizsed 수 있습니다 조인 있지만, 그것을 요청한 것은 아래처럼 보일 것입니다. t_files, t_links 등 각 테이블은 folder_id 필드가있는 경우

SELECT a.folderid, a.foldername, a.contenttype, 
    (CASE a.contenttype 
     WHEN 'file' THEN b.descriptor 
     WHEN 'link' THEN c.descriptor 
     WHEN 'extfile' THEN d.descriptor 
     WHEN 'video' THEN e.descriptor 
     ELSE f.descriptor 
    END CASE) AS descriptor 
FROM t_folders a 
LEFT JOIN t_files b ON a.contenttype = 'file' AND a.contentid = b.fileid 
LEFT JOIN t_links c ON a.contenttype = 'link' AND a.contentid = c.linkid 
LEFT JOIN t_extfiles d ON a.contenttype = 'extfile' AND a.contentid = d.extfileid 
LEFT JOIN t_videos e ON a.contenttype = 'video' AND a.contentid = e.videoid 
LEFT JOIN t_exams f ON a.contenttype = 'exam' AND a.contentid = f.examid 
WHERE a.folderid = $folderId 
ORDER BY a.folderid DESC 

, 나는 이러한 테이블에 UNION을하고 시도하고 folderid 및 폴더 이름을 얻을 t_folders과 결과에 가입 왼쪽 것이다.

0

다른 테이블에서 나오기 때문에 이러한 방식으로 조인을 수행해야합니다. CASE를 사용하여 쿼리가 나오는 테이블을 전환 할 수 없습니다. 비교할 값이 있기 전에 해당 문을 데이터 소스를 포함하여 파싱해야합니다.

더 많은 점은 CASE가 값을 반환하는 반면 테이블 이름은 .. 쿼리의 구조적 구성 요소에 대한 전문 용어를 알지 못합니다.

select * from "Cool_" + "Stuff" 

희망이 당신의 질문에 대한 답 : 그것은 당신이 테이블 "Cool_Stuff"밖으로 선택할 수 없습니다 같은 이유의 일종! 테이블 A의 결과를 최소화하면서

0

다음과 같은

select master.* , COALESCE(b.descriptor, c.descriptor, d.descriptor, e.descriptor,  f.descriptor) as descriptor 
    from 
    (SELECT a.folderid, a.foldername, a.contenttype 

FROM t_folders a 
WHERE a.folderid = $folderId 
ORDER BY a.folderid DESC) master 
LEFT JOIN t_files b 
ON master.contenttype = 'file' AND master.contentid = b.fileid 
LEFT JOIN t_links c 
ON master.contenttype = 'link' AND master.contentid = c.linkid 
LEFT JOIN t_extfiles d 
ON master.contenttype = 'extfile' AND master.contentid = d.extfileid 
LEFT JOIN t_videos e 
ON master.contenttype = 'video' AND master.contentid = e.videoid 
LEFT JOIN t_exams f 
ON master.contenttype = 'exam' AND master.contentid = f.examid 

을 할 수는는 이후 작업이 빠르게 경우 쿼리를하지 않습니다 case 문을 사용하여