0
SELECT 
    lpn.TC_LPN_ID 
FROM LPN lpn 
WHERE lpn.inbound_Outbound_Indicator = 'O' 
    AND lpn.tc_company_id = 1403 
    AND lpn.TC_REFERENCE_LPN_ID **IN**  
        (SELECT 
            l.tc_lpn_id 
         FROM lpn l 
         WHERE l.TC_COMPANY_ID = 1403 
             AND l.inbound_Outbound_Indicator = 'I' 
             AND (l.tc_lpn_id= '000990950' OR l.TC_REFERENCE_LPN_ID = '000990950'))

想将我的查询修改为EXISTS

EXISTS提高性能。

4

2 回答 2

1
SELECT lpn.tc_lpn_id
  FROM lpn lpn
 WHERE lpn.inbound_outbound_indicator = 'O'
   AND lpn.tc_company_id = 1403
   AND EXISTS (
          SELECT 1
            FROM lpn l
           WHERE lpn.tc_reference_lpn_id = l.tc_lpn_id
             AND l.tc_company_id = 1403
             AND l.inbound_outbound_indicator = 'I'
             AND (   l.tc_lpn_id = '000990950'
                  OR l.tc_reference_lpn_id = '000990950'
                 ))
于 2013-04-01T06:19:18.690 回答
0

JOIN 进一步提高了性能。

您可以将查询表达为对其自身的标准内部联接:

SELECT 
    lpn.TC_LPN_ID 
FROM LPN
JOIN lpn l
    ON l.TC_COMPANY_ID = lpn.l.TC_COMPANY_ID
    AND l.tc_lpn_id = lpn.TC_REFERENCE_LPN_ID
    AND l.inbound_Outbound_Indicator = 'I'
    AND l.tc_lpn_id = '000990950' OR l.TC_REFERENCE_LPN_ID = '000990950')
WHERE lpn.inbound_Outbound_Indicator = 'O' 
AND lpn.tc_company_id = 1403

请注意,我删除了lpn对自身的无意义别名 - 即FROM LPN lpn与简单相同FROM LPN:将表别名为其自己的名称无效。

于 2013-04-04T13:00:56.857 回答