Oracle 11g Count查询速度慢

时间:2016-11-11 11:11:31

标签: oracle oracle11g query-optimization sql-execution-plan

我写了一个查询,它使用group by为我提供项目类型的计数。 选择查询,如果我在没有组合的情况下触发并且计数在sql developer上提供不到一秒的结果。

然而,计数查询一直运行,我必须杀死进程。

以下是查询..

 WITH permList
     AS (SELECT user_perm_level,
                item_category,
                active
         FROM   usr_perm_levels
         WHERE  usr_pk = '7'
         UNION ALL
         SELECT home_key,
                'ALL',
                0
         FROM   user_home_list
         WHERE  usr_pk = 7
         UNION ALL
         SELECT home_key,
                'UYT',
                0
         FROM   user_home_list
         WHERE  usr_pk = 7
         UNION ALL
         SELECT home_key,
                'ABR',
                0
         FROM   user_home_list
         WHERE  usr_pk = 7),
     list1
     AS (SELECT a.parent_home_key,
                b.item_category
         FROM   user_hier a
                INNER JOIN indhome b
                        ON a.child_home_key = b.user_perm_level),
     list2
     AS ((SELECT oh.child_home_key,
                 u.item_category
          FROM   indhome U
                 INNER JOIN user_hier oh
                         ON u.user_perm_level = oh.parent_home_key
                            AND u.active = 1)
         UNION ALL
         SELECT user_perm_level,
                item_category
         FROM   indhome)
SELECT type,
       Count(*)
FROM   inventory 
WHERE  is_available = 0
       AND active = 1
       AND EXISTS (SELECT 0
                   FROM   inventory_auth c
                   WHERE  EXISTS (SELECT 0
                                  FROM   orglist1
                                  WHERE  c.user_perm_level = orglist1.parent_home_key
                                         AND c.item_category =
                                             orglist1.item_category
                                         AND c.active = 1)
                           OR EXISTS (SELECT 0
                                      FROM   orglist2
                                      WHERE  c.user_perm_level = orglist2.child_home_key
                                             AND c.item_category =
                                                 orglist2.item_category)
                              AND inventory.item_key = c.item_key
                              AND inventory.item_category = c.item_category)
GROUP  BY type;  

在解释计划中,我看到group by子句的成本是145297221173。

现在这里的库存表有1000万条记录,库存表周围的存在块也可以运行到1500万条记录。 作为视图permList,list1和list2只有100s的记录。

我尝试了各种组合,但似乎都没有带来更好的性能。比如我尝试使用exists子句在库存和内部查询之间进行内部联接,但这没有多大帮助。

0 个答案:

没有答案