2012-03-17 11 views
3

と最適化のJOINMYSQL 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をやってみた後、フォルダIDとフォルダ名を取得するt_foldersで結果を参加左ます。

0

異なるテーブルから出てくるので、この方法で結合する必要があります。 CASEを使用して、問合せが出てくる表を切り替えることはできません。比較する値がある前に、その文を解析する必要があります(データ・ソースを含む)。

さらに、CASEは値を返しますが、テーブル名は..私は専門用語を知りません..クエリの構造要素。これは、テーブル「Cool_Stuff」から次のものを選択できないのと同じ理由です。

select * from "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文を使用して