0

パフォーマンスが非常に悪い次のSQLステートメントがあります。

SELECT  
frmInstLastModifiedDate AS last_modified 
, frmInstID AS proj_id 
, frmInstIsApproved 
, frmInstStatus AS proj_sts 
, (CASE 
    WHEN frmInstCreator = 294  THEN 'M' 
    WHEN status = 'f'  THEN 'F' 
    WHEN (frmInstID IN (SELECT shr.frmInstID from tbl_frm_share as shr WHERE shr.shrMember = 294 AND shr.shrType = 'LIKE'  AND shr.frmID = inst.frmID)) THEN 'L' 
    WHEN (frmInstID IN (SELECT shr.frmInstID from tbl_frm_share as shr WHERE shr.shrMember = 294 AND shr.shrType = 'SHARE' AND shr.frmID = inst.frmID)) THEN 'S' 
    ELSE 'O' END) as proj_grp 
, (SELECT lkpCode FROM tbl_frm_lookup WHERE lkpID = (SELECT ansValue FROM tbl_itm_answer WHERE frmInstID = proj_id AND itmID = (select itmID from tbl_frm_item where itmName = 'prjStatus' AND frmID = inst.frmID))) as prjStatus 
, (SELECT lkpCode FROM tbl_frm_lookup WHERE lkpID = (SELECT ansValue FROM tbl_itm_answer WHERE frmInstID = proj_id AND itmID = (select itmID from tbl_frm_item where itmName = 'ProjectType' AND frmID = inst.frmID))) as ProjectType 
, (SELECT itmID FROM tbl_itm_answer where itmID in (828,829,830,831) and frmInstID = proj_id AND SUBSTRING(ansValue,1,2) = 'on') as primIRWMObj 
, (SELECT lkpCode FROM tbl_frm_lookup WHERE lkpID = (SELECT ansValue FROM tbl_itm_answer WHERE frmInstID = proj_id AND itmID = (select itmID from tbl_frm_item where itmName = 'PrjPSubReg'))) as ProjectSubReg 
, frmInstCreator AS proj_creatorID 
, frmInstCode AS proj_code 
    FROM tbl_frm_instance inst 
WHERE status not like 'd'   
HAVING (proj_sts like 'c' AND ('PROJECT PROPONENT' = 'ADMIN' or proj_creatorID = 294 )) 
    or (proj_sts like 'a') 
    or (proj_sts like 't' AND proj_creatorID = 294)  
    OR (proj_grp = 'S')  
ORDER BY frmInstCreateDate DESC ;

ステートメントは、ユーザーが選択したオプションに基づいて動的に作成されます。ネストされたselectステートメント(次のような)が問題であることは知っていますが、どのように置き換えることができるかわかりません

    (SELECT lkpCode FROM tbl_frm_lookup WHERE lkpID = (SELECT ansValue FROM tbl_itm_answer WHERE frmInstID = proj_id AND itmID = (select itmID from tbl_frm_item where itmName = 'prjStatus' AND frmID = inst.frmID))) as prjStatus 

どんな助けでも大歓迎です。

4

2 に答える 2

4

使用しないでくださいnested selects。最も遅く、データベースが汚れています。最善のアプローチは、可能な限り常にJOIN使用することです。JOIN

優れたパフォーマンスで作成したい場合IS(それはあなたの仕事であり、あなたの名前です)、常に最も効果的で、絶食し、最も安全なアプローチを決定する必要がありJOINます。

JOINどこでも使用することをお勧めします。

クエリ実行プランについてを読むことをお勧めします。

注:「手順のように」ではなく、「データベースのように」と考える必要がある場合があります。

于 2012-06-19T19:08:02.673 に答える
2

これを試して::

私にできる最低限のこと::

SELECT  
frmInstLastModifiedDate AS last_modified 
, frmInstID AS proj_id 
, frmInstIsApproved 
, frmInstStatus AS proj_sts 
, (CASE 
    WHEN frmInstCreator = 294  THEN 'M' 
    WHEN status = 'f'  THEN 'F' 
    WHEN (frmInstID IN (SELECT shr.frmInstID from tbl_frm_share as shr WHERE shr.shrMember = 294 AND shr.shrType = 'LIKE'  AND shr.frmID = inst.frmID)) THEN 'L' 
    WHEN (frmInstID IN (SELECT shr.frmInstID from tbl_frm_share as shr WHERE shr.shrMember = 294 AND shr.shrType = 'SHARE' AND shr.frmID = inst.frmID)) THEN 'S' 
    ELSE 'O' END) as proj_grp 
, 
(SELECT lkpCode 
FROM tbl_frm_lookup tfl
inner join tbl_itm_answer tia on (tfl.lkpID= tia.ansValue)
inner join tbl_frm_item tfi on (tia.itmID= tfi.itmID)
 WHERE frmInstID = proj_id and itmName = 'prjStatus' AND frmID = inst.frmID) as prjStatus 
, frmInstCreator AS proj_creatorID 
, frmInstCode AS proj_code
于 2012-06-19T19:08:20.393 に答える