0

I had a large sql query that had a nested select in the from clause.

Similar to this:

SELECT * FROM 
     ( SELECT * FROM SOME_TABLE WHERE some_num = 20) 
    WHERE some_num = 20

In my sql query if I remove the outer "some_num" = 20 it takes 5 times as long . Shouldent these querys run in almost exactly the same time, if not wouldn't having the the additional where slow it down slightly?

What am I not understanding about how sql querys work?

Here is the original query in question

SELECT a.ITEMNO                                                       AS Item_No,
       a.DESCRIPTION                                                  AS Item_Description,
       UNITPRICE / 100                                                AS Retail_Price,
       b.UNITSALES                                                    AS Units_Sold,
       ( Dollar_Sales )                                               AS Dollar_Sales,
       ( Dollar_Cost )                                                AS Dollar_Cost,
       ( Dollar_Sales ) - ( Dollar_Cost )                             AS Gross_Profit,
       ( Percent_Page * c.PAGECOST )                                  AS Page_Cost,
       ( Dollar_Sales - Dollar_Cost - ( Percent_Page * c.PAGECOST ) ) AS Net_Profit,
       Percent_Page * 100                                             AS Percent_Page,
       ( CASE
           WHEN UNITPRICE = 0 THEN NULL
           WHEN Percent_Page = 0 THEN NULL
           WHEN ( Dollar_Sales - Dollar_Cost - ( Percent_Page * c.PAGECOST ) ) > 0 THEN 0
           ELSE ( ceiling(abs(Dollar_Sales - Dollar_Cost - ( Percent_Page * c.PAGECOST )) / ( UNITPRICE / 100 )) )
         END )                                                        AS Break_Even,
       b.PAGENO                                                       AS Page_Num
FROM   (SELECT PAGENO,
               OFFERITEM,
               UNITSALES,
               UNITPRICE,
               ( DOLLARSALES / 100 )                                      AS Dollar_Sales,
               ( DOLLARCOST / 10000 )                                     AS Dollar_Cost,
               (( CAST(STUFF(PERCENTPAGE, 2, 0, '.') AS DECIMAL(9, 6)) )) AS Percent_Page
        FROM   OFFERITEMS
        WHERE  LEFT(OFFERITEM, 6) = 'CH1301'
               AND PERCENTPAGE > 0) AS b
       INNER JOIN ITEMMAST a
         ON a.EDPNO = 1 * RIGHT(OFFERITEM, 8)
       LEFT JOIN OFFERS c
         ON c.OFFERNO = 'CH1301'
WHERE  LEFT(OFFERITEM, 6) = 'CH1301'
ORDER  BY Net_Profit DESC 

Notice the two

WHERE left(OFFERITEM,6) = 'CH1301'

If I remove the outer Where then the query takes 5 times as long

As requested the Execution plan excuse the crappy upload

http://i.imgur.com/1PqmpVf.png

4

1 回答 1

1

OFFERITEM 列是否在索引中,但 PERCENTPAGE 不是?

在您的内部查询中,您引用这两个列,在外部查询中,您只引用 OFFERITEM。

很难说没有看到执行计划,但可能是外部查询导致优化器运行“索引扫描”,而内部查询会导致全表扫描。

在单独的说明中,您绝对应该修改:

WHERE left(OFFERITEM,6) ='CH1301' 

至:

where offeritem like 'CH1301%'

因为如果 offeritem 上有索引,这将允许索引查找。

于 2013-09-13T15:19:40.363 回答