码迷,mamicode.com
首页 > 数据库 > 详细

查询复杂sql的表的大小

时间:2015-07-11 06:48:11      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:oracle 性能优化

1.先explain plan for 目标sql:

explain plan for WITH sales_countries AS
 (SELECT /*+ gather_plan_statistics */
   cu.cust_id, co.country_name
    FROM sh.countries co, sh.customers cu
   WHERE cu.country_id = co.country_id),
top_sales AS
 (SELECT p.prod_name,
         sc.country_name,
         s.channel_id,
         t.calendar_quarter_desc,
         s.amount_sold,
         s.quantity_sold
    FROM sh.sales s
    JOIN sh.times t
      ON t.time_id = s.time_id
    JOIN sh.customers c
      ON c.cust_id = s.cust_id
    JOIN sales_countries sc
      ON sc.cust_id = c.cust_id
    JOIN sh.products p
      ON p.prod_id = s.prod_id),
sales_rpt AS
 (SELECT prod_name product,
         country_name country,
         channel_id channel,
         substr(calendar_quarter_desc, 6, 2) quarter,
         SUM(amount_sold) amount_sold,
         SUM(quantity_sold) quantity_sold
    FROM top_sales
   GROUP BY prod_name,
            country_name,
            channel_id,
            substr(calendar_quarter_desc, 6, 2))
SELECT *
  FROM (SELECT product, channel, quarter, country, quantity_sold
          FROM sales_rpt) pivot(SUM(quantity_sold) FOR(channel, quarter) IN((5, ‘02‘) AS
                                                                            catalog_q2,
                                                                            (4, ‘01‘) AS
                                                                            internet_q1,
                                                                            (4, ‘04‘) AS
                                                                            internet_q4,
                                                                            (2, ‘02‘) AS
                                                                            partners_q2,
                                                                            (9, ‘03‘) AS
                                                                            tele_q3))
 46   ORDER BY product, country
 47  /
Explained.
Elapsed: 00:00:00.37

SQL>

2.用以下sql可以查询出相关表的大小:

 

SQL> col segment_name for a20
select owner, segment_name,segment_type, sum(bytes / 1024 / 1024) "Size(Mb)"
  from dba_segments
 where owner in (select /*+ no_unnest */ object_owner from plan_table)
   and segment_name in (select /*+ no_unnest */   object_name from plan_table)
 group by owner,segment_type, segment_name
UNION
----table in the index
select owner, ‘*‘||segment_name ,segment_type, sum(bytes / 1024 / 1024) "Size(Mb)"
  from dba_segments
 where owner in (select  table_owner
          from dba_indexes
         where owner in (select /*+ no_unnest */  object_owner from plan_table)
           and index_name in (select /*+ no_unnest */  object_name from plan_table))
   and segment_name in (select /*+ no_unnest */  table_name
          from dba_indexes
         where owner in (select /*+ no_unnest */  object_owner from plan_table)
           and index_name in (select /*+ no_unnest */  object_name from plan_table))
 group by owner,segment_type, segment_name
   order by 3,4;

OWNER          SEGMENT_NAME     SEGMENT_TYPE  Size(Mb)
------------------------------ -------------------- ------------------ ----------
SH          COUNTRIES     TABLE      .0625
SH          PRODUCTS      TABLE      .0625
SH          TIMES      TABLE         .5
SH          CUSTOMERS     TABLE         12
SH          SALES      TABLE PARTITION       128

Elapsed: 00:00:05.35

 

查询复杂sql的表的大小

标签:oracle 性能优化

原文地址:http://7642644.blog.51cto.com/7632644/1673047

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!