[20131217]pivot应用例子.txt

[20131217]pivot应用例子.txt

PIVOT是11G的新特性,可以把列转换为行,自己写一个例子:

SCOTT@test> @ver

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

SCOTT@test> select name from V$SEGSTAT_NAME order by statistic#;
SCOTT@test> select statistic#,name from V$SEGSTAT_NAME order by statistic#;
STATISTIC# NAME
---------- ------------------------------
         0 logical reads
         1 buffer busy waits
         2 gc buffer busy
         3 db block changes
         4 physical reads
         5 physical writes
         6 physical read requests
         7 physical write requests
         8 physical reads direct
         9 physical writes direct
        11 optimized physical reads
        12 gc cr blocks received
        13 gc current blocks received
        14 ITL waits
        15 row lock waits
        17 space used
        18 space allocated
        20 segment scans

18 rows selected.

--10g
STATISTIC# NAME
---------- ------------------------------
         0 logical reads
         1 buffer busy waits
         2 gc buffer busy
         3 db block changes
         4 physical reads
         5 physical writes
         6 physical reads direct
         7 physical writes direct
         9 gc cr blocks received
        10 gc current blocks received
        11 ITL waits
        12 row lock waits
        14 space used
        15 space allocated
        17 segment scans
15 rows selected.

WITH pivot_stats
     AS (SELECT owner,
                object_name,
                statistic_name,
                VALUE
           FROM v$segment_statistics)
SELECT *
  FROM pivot_stats PIVOT (SUM (VALUE)
                   FOR statistic_name
                   IN (select name from V$SEGSTAT_NAME order by statistic#)
--这样写不行.
ERROR at line 10:
ORA-00936: missing expression

--11G
WITH pivot_stats
     AS (SELECT owner,
                object_name,
                statistic_name,
                VALUE
           FROM v$segment_statistics)
SELECT *
  FROM pivot_stats PIVOT (SUM (VALUE)
                   FOR statistic_name
                  IN  ('logical reads',
                       'buffer busy waits',
                       'gc buffer busy',
                       'db block changes',
                       'physical reads',
                       'physical writes',
                       'physical read requests',
                       'physical write requests',
                       'physical reads direct',
                       'physical writes direct',
                       'optimized physical reads',
                       'gc cr blocks received',
                       'gc current blocks received',
                       'ITL waits',
                       'row lock waits',
                       'space used',
                       'space allocated',
                       'segment scans'))
--适当的选择排序,可以定位那个object_name存在问题.

--如果使用10g写法就比较复杂了.
SELECT owner,
         object_name,
         MAX (DECODE (statistic_name, 'logical reads', VALUE, 0))              "logical reads",
         MAX (DECODE (statistic_name, 'buffer busy waits', VALUE, 0))          "buffer busy waits",
         MAX (DECODE (statistic_name, 'gc buffer busy', VALUE, 0))             "gc buffer busy",
         MAX (DECODE (statistic_name, 'db block changes', VALUE, 0))           "db block changes",
         MAX (DECODE (statistic_name, 'physical reads', VALUE, 0))             "physical reads",
         MAX (DECODE (statistic_name, 'physical writes', VALUE, 0))            "physical writes",
         MAX (DECODE (statistic_name, 'physical read requests', VALUE, 0))     "physical read requests",
         MAX (DECODE (statistic_name, 'physical write requests', VALUE, 0))    "physical write requests",
         MAX (DECODE (statistic_name, 'physical reads direct', VALUE, 0))      "physical reads direct",
         MAX (DECODE (statistic_name, 'physical writes direct', VALUE, 0))     "physical writes direct",
         MAX (DECODE (statistic_name, 'optimized physical reads', VALUE, 0))   "optimized physical reads",
         MAX (DECODE (statistic_name, 'gc cr blocks received', VALUE, 0))      "gc cr blocks received",
         MAX (DECODE (statistic_name, 'gc current blocks received', VALUE, 0)) "gc current blocks received",
         MAX (DECODE (statistic_name, 'ITL waits', VALUE, 0))                  "ITL waits",
         MAX (DECODE (statistic_name, 'row lock waits', VALUE, 0))             "row lock waits",
         MAX (DECODE (statistic_name, 'space used', VALUE, 0))                 "space used",
         MAX (DECODE (statistic_name, 'space allocated', VALUE, 0))            "space allocated",
         MAX (DECODE (statistic_name, 'segment scans', VALUE, 0))              "segment scans"
    FROM v$segment_statistics
GROUP BY owner, object_name

时间: 2024-08-23 04:28:13

[20131217]pivot应用例子.txt的相关文章

[20141126]expr例子.txt

[20141126]expr例子.txt --今天抽空学习linux的expr命令,做一些简单记录,主要是自己不常用的部分. --查看man expr文档: 1. expr length $ expr length " This is a Text" 15 --计算字符串的长度. 2.expr index STRING CHARS #  expr index '123456' '5' 5 --返回CHARS的在 STRING的位置.如果chars是多个字符,返回单个char在STRIN

[20150224]ITL不足的测试例子.txt

[20150224]ITL不足的测试例子.txt --前几天看Apress.Oracle.Database.Transactions.and.Locking.Revealed.1484207610.pdf,发现P39页的测试例子,感觉不对,重复做 --一次. SCOTT@test> @ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- ---

[20160919]sql注入例子.txt

[20160919]sql注入例子.txt --许多开发喜欢拚接sql语句,而不是使用绑定变量,而这样带来一个问题就是给注入攻击提供了可能,从别人的网站炒一个例子: 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- --------------------------------------

[20171125]bash for例子.txt

[20171125]bash for例子.txt #!/bin/bash for i in {1..10} do echo $i done --//学习!!特别是for后面的技巧.我以前喜欢使用seq,感觉上面要好一些,毕竟不使用命令seq. #!/bin/bash for i in $(seq 10) do echo $i done

[20170626]rman merge例子.txt

[20170626]rman merge例子.txt 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- -------------------------------------------------------------------------------- x86_64/Linux

[20170708]tmux script例子.txt

[20170708]tmux script例子.txt --//上个星期解决rac ora-12514问题时,因为要打开多个窗口,非常麻烦,利用星期6,7看了一些文档,写一个tmux script: --//便于以后工作需要: #! /bin/bash tmux has-session -t oracle if [ $? != 0 ]; then   tmux new-session -s oracle -d   tmux new-window -n bashgrid -t oracle   t

[20171031]rman merge例子2.txt

[20171031]rman merge例子2.txt --//以前做的测试 [20170626]rman merge例子.txt --//链接 http://blog.itpub.net/267265/viewspace-2141253/ --//不断应用日志,常用的方式如下: 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER --------------------------

[20171205]bash for例子错误.txt

[20171205]bash for例子错误.txt --//今天写bash for循环,遇到问题.通过例子说明: $ cat tt1.sh #! /bin/bash for i in { 1 .. 5 } do   echo $i done --//本想输出1,2,3,4,5的.而实际上执行输出是: $ . tt1.sh { 1 .. 5 } --//使用这样的方式{ 1 .. 5 }之间不能有任何空格.正确的写法如下: $ cat tt1.sh #! /bin/bash for i in {

[20170703]pivot与order by字段.txt

[20170703]pivot与order by字段.txt --//11G开始支持pivot,上午写一个脚本,来自链接http://blog.itpub.net/267265/viewspace-1063539/ --//做了一点点改写. select * from ( WITH pivot_stats      AS (SELECT owner,                 object_name,                 statistic_name,