调优SQL语句性能

时间:2015-07-31 07:48:40

标签: sql oracle performance query-optimization sql-tuning

我有两个返回相同结果的查询:

1

SELECT DISTINCT 
         cvc.object_id
        , cvc.object_name  
 FROM ems.ibo_sm_cvc_rfs cvc,  
      ems.ibo_alcatel_mse_locale poi, 
      ems.ibo_nbn_csa csa, 
      ems.ibo_sm_ean_service_sites_rfs sites, 
      ems.ibo_sm_ean_service_site_rfs site  
WHERE poi.object_name ='testPoi'  
  AND csa.parent_id = poi.object_id  
  AND cvc.csa_id = csa.csa_id  
  AND sites.parent_id = cvc.object_id  
  AND site.service_site_type = 'testSite'  
  AND site.object_name IN (SELECT mse_chassis.object_name  
                             FROM ems.ibo_alcatel_mse_chassis mse_chassis  
                            WHERE mse_chassis.parent_id = poi.object_id);

2。 :

SELECT cvc.object_id,
       cvc.object_name
  FROM ems.ibo_sm_cvc_rfs cvc
  JOIN ems.ibo_nbn_csa csa
    ON cvc.csa_id = csa.csa_id
  JOIN ems.ibo_sm_ean_service_sites_rfs sites
    ON sites.parent_id   = cvc.object_id
 WHERE csa.parent_id IN (SELECT poi.object_id
                           FROM ems.ibo_alcatel_mse_locale poi
                          WHERE poi.object_id IN (SELECT csa.parent_id FROM ems.ibo_nbn_csa csa)
                             AND poi.object_name = 'testPoi'
                             AND poi.object_id  IN 
                                 (SELECT mse_chassis.parent_id
                                    FROM EMS.ibo_alcatel_mse_chassis mse_chassis
                                   WHERE mse_chassis.object_name IN
                                         (SELECT site.object_name
                                            FROM EMS.ibo_sm_ean_service_site_rfs site
                                           WHERE site.service_site_type = 'testSite')
                                 )
                         );

奇怪的是,第一个声明在0.156秒完成,而第二个声明在0.624秒完成。需要提及的是,所有ems。*对象都是视图,我看起来基本上where子句中的每一列都被编入索引。 如果需要我可以放置视图模式,但我不是因为是公司信息。也许一双经验眼睛可能会看到这两种陈述的改进

2 个答案:

答案 0 :(得分:1)

正如@Mihai指出的那样,你的第二个查询充满了子查询,我还会再向前微调你的第一个发布的查询,如下所示

putSerializable

注意两件事:

  1. 我已将隐式连接语法修改为显式连接语法

  2. 我已将带有SELECT DISTINCT cvc.object_id , cvc.object_name FROM ems.ibo_sm_cvc_rfs cvc JOIN ems.ibo_nbn_csa csa ON cvc.csa_id = csa.csa_id JOIN ems.ibo_alcatel_mse_locale poi ON csa.parent_id = poi.object_id JOIN ems.ibo_sm_ean_service_sites_rfs sites ON sites.parent_id = cvc.object_id JOIN EMS.ibo_alcatel_mse_chassis mse_chassis ON mse_chassis.parent_id = poi.object_id JOIN EMS.ibo_sm_ean_service_site_rfs site ON site.object_name = mse_chassis.object_name WHERE poi.object_name ='testPoi' AND site.service_site_type = 'testSite'; 子句的最后一个子查询修改为IN语句

答案 1 :(得分:1)

SELECT 
    cvc.object_id
    , cvc.object_name  
FROM ems.ibo_sm_cvc_rfs cvc,  
    ems.ibo_alcatel_mse_locale poi, 
    ems.ibo_nbn_csa csa, 
    ems.ibo_sm_ean_service_sites_rfs sites, 
    EMS.ibo_sm_ean_service_site_rfs site,
    EMS.ibo_alcatel_mse_chassis mse_chassis  
WHERE poi.object_name ='testPoi'  
    AND csa.parent_id = poi.object_id  
    AND cvc.csa_id = csa.csa_id  
    AND sites.parent_id = cvc.object_id  
    AND site.service_site_type = 'testSite'  
    AND site.object_name = mse_chassis.object_name  
    AND mse_chassis.parent_id = poi.object_id 
GROUP BY cvc.object_id, cvc.object_name 

针对松散的索引扫描,这是非常有效的,所以添加这个索引

CREATE INDEX objects_idx
  ON ibo_sm_cvc_rfs (object_id, object_name);