[20180928]如何能在11g下執行.txt

lfree發表於2018-09-29

[20180928]如何能在11g下執行.txt

--//連結問的問題: http://www.itpub.net/thread-2105467-1-1.html

create table test(t_id int,t_name varchar2(50));
create table test2(t_id int,t_name varchar2(50));

insert into test values(1,'a');
insert into test values(2,'b');
insert into test values(3,'c');

insert into test2 values(1,'a');
insert into test2 values(2,'b');
insert into test2 values(3,'c');

SELECT a.t_id, a.t_name
    FROM test a
         LEFT JOIN (  SELECT t_id, t_name
                        FROM test2
                    GROUP BY t_name) b
            ON a.t_id = b.t_id AND a.t_name = b.t_name
   WHERE a.t_id = 1
GROUP BY a.t_id, a.t_name;

--//同樣的sql ,在10g下就能執行,在11g下就會報錯 ora-00979 不是group by 表示式。紅色部分看起來確實是錯的,可是10g下整句
--//執行就有結果。如何能做到這個語句再11g下也能執行呢。

--//家裡沒有10g,使用12c測試看看,這條語句明顯錯誤(  SELECT t_id, t_name FROM test2 GROUP BY t_name),這裡少寫了 GROUP BY
--//t_id,t_name.

1.環境:
SCOTT@test01p> @ ver1
PORT_STRING                    VERSION        BANNER                                                                               CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0           12.1.0.1.0     Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production              0

2.測試:
SCOTT@test01p> select a.t_id,a.t_name from test a LEFT JOIN
  2  (select t_id,t_name from test2 group by t_name) b
  3  on a.t_id=b.t_id and a.t_name=b.t_name where a.t_id=1
  4  group by a.t_id,a.t_name;
(select t_id,t_name from test2 group by t_name) b
        *
ERROR at line 2:
ORA-00979: not a GROUP BY expression
--//12c 一樣報錯.

SCOTT@test01p> show parameter feature
NAME                                 TYPE                 VALUE
------------------------------------ -------------------- ---------------
optimizer_adaptive_features          boolean              TRUE
optimizer_features_enable            string               12.1.0.1

SCOTT@test01p> alter session set optimizer_features_enable='10.2.0.1';
Session altered.

SCOTT@test01p> SELECT a.t_id, a.t_name
  2      FROM test a
  3           LEFT JOIN (  SELECT t_id, t_name
  4                          FROM test2
  5                      GROUP BY t_name) b
  6              ON a.t_id = b.t_id AND a.t_name = b.t_name
  7     WHERE a.t_id = 1
  8  GROUP BY a.t_id, a.t_name;
      T_ID T_NAME
---------- --------------------------------------------------
         1 a

--//OK透過.不過這句話明顯錯誤.看看執行計劃:

SCOTT@test01p> @ dpc '' advanced
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  0urvg3qakxxcx, child number 0
-------------------------------------
SELECT a.t_id, a.t_name     FROM test a          LEFT JOIN (  SELECT
t_id, t_name                         FROM test2
GROUP BY t_name) b             ON a.t_id = b.t_id AND a.t_name =
b.t_name    WHERE a.t_id = 1 GROUP BY a.t_id, a.t_name

Plan hash value: 1211648783

-----------------------------------------------------------------------------------------------------------
| Id  | Operation             | Name  | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |       |        |       |     9 (100)|          |       |       |          |
|   1 |  HASH GROUP BY        |       |      1 |    40 |     9  (34)| 00:00:01 |  2063K|  2063K|  438K (0)|
|   2 |   VIEW                |       |      1 |    40 |     8  (25)| 00:00:01 |       |       |          |
|   3 |    HASH GROUP BY      |       |      1 |    92 |     8  (25)| 00:00:01 |  1345K|  1345K|  486K (0)|
|*  4 |     HASH JOIN OUTER   |       |      1 |    92 |     7  (15)| 00:00:01 |  1421K|  1421K|  722K (0)|
|*  5 |      TABLE ACCESS FULL| TEST  |      1 |    52 |     3   (0)| 00:00:01 |       |       |          |
|*  6 |      TABLE ACCESS FULL| TEST2 |      1 |    40 |     3   (0)| 00:00:01 |       |       |          |
-----------------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$439AFB4F
   2 - SEL$3DD9CB74 / $vm_view_0@SEL$439AFB4F
   3 - SEL$3DD9CB74
   5 - SEL$3DD9CB74 / A@SEL$3
   6 - SEL$3DD9CB74 / TEST2@SEL$2

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('10.2.0.1')
      DB_VERSION('12.1.0.1')
      ALL_ROWS
      OUTLINE_LEAF(@"SEL$3DD9CB74")
      MERGE(@"SEL$2")
      OUTLINE_LEAF(@"SEL$439AFB4F")
      OUTLINE(@"SEL$F2B2F603")
      OUTLINE(@"SEL$2")
      OUTLINE(@"SEL$CD8351FA")
      MERGE(@"SEL$F1D6E378")
      OUTLINE(@"SEL$4")
      OUTLINE(@"SEL$F1D6E378")
      MERGE(@"SEL$1")
      OUTLINE(@"SEL$3")
      OUTLINE(@"SEL$1")
      NO_ACCESS(@"SEL$439AFB4F" "$vm_view_0"@"SEL$439AFB4F")
      USE_HASH_AGGREGATION(@"SEL$439AFB4F")
      FULL(@"SEL$3DD9CB74" "A"@"SEL$3")
      FULL(@"SEL$3DD9CB74" "TEST2"@"SEL$2")
      LEADING(@"SEL$3DD9CB74" "A"@"SEL$3" "TEST2"@"SEL$2")
      USE_HASH(@"SEL$3DD9CB74" "TEST2"@"SEL$2")
      USE_HASH_AGGREGATION(@"SEL$3DD9CB74")
      END_OUTLINE_DATA
  */


Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("A"."T_NAME"="T_NAME" AND "A"."T_ID"="T_ID")
   5 - filter("A"."T_ID"=1)
   6 - filter("T_ID"=1)

--//抽取outline.

--//編輯整理如下:

begin
dbms_sqltune.import_sql_profile(
   name => 'profile_group_error',
   description => 'SQL profile created manually test',
   sql_text => q'[SELECT a.t_id, a.t_name
    FROM test a
         LEFT JOIN (  SELECT t_id, t_name
                        FROM test2
                    GROUP BY t_name) b
            ON a.t_id = b.t_id AND a.t_name = b.t_name
   WHERE a.t_id = 1
GROUP BY a.t_id, a.t_name]',
   profile => sqlprof_attr(
'IGNORE_OPTIM_EMBEDDED_HINTS',
q'[OPTIMIZER_FEATURES_ENABLE('10.2.0.1')]',
q'[DB_VERSION('12.1.0.1')]',
'ALL_ROWS',
'OUTLINE_LEAF(@"SEL$3DD9CB74")',
'MERGE(@"SEL$2")',
'OUTLINE_LEAF(@"SEL$439AFB4F")',
'OUTLINE(@"SEL$F2B2F603")',
'OUTLINE(@"SEL$2")',
'OUTLINE(@"SEL$CD8351FA")',
'MERGE(@"SEL$F1D6E378")',
'OUTLINE(@"SEL$4")',
'OUTLINE(@"SEL$F1D6E378")',
'MERGE(@"SEL$1")',
'OUTLINE(@"SEL$3")',
'OUTLINE(@"SEL$1")',
'NO_ACCESS(@"SEL$439AFB4F" "$vm_view_0"@"SEL$439AFB4F")',
'USE_HASH_AGGREGATION(@"SEL$439AFB4F")',
'FULL(@"SEL$3DD9CB74" "A"@"SEL$3")',
'FULL(@"SEL$3DD9CB74" "TEST2"@"SEL$2")',
'LEADING(@"SEL$3DD9CB74" "A"@"SEL$3" "TEST2"@"SEL$2")',
'USE_HASH(@"SEL$3DD9CB74" "TEST2"@"SEL$2")',
'USE_HASH_AGGREGATION(@"SEL$3DD9CB74")'
),
   replace => true,
   force_match => TRUE
);
end;
/

SCOTT@test01p> show parameter feature
NAME                                 TYPE                 VALUE
------------------------------------ -------------------- --------------
optimizer_adaptive_features          boolean              TRUE
optimizer_features_enable            string               12.1.0.1

SCOTT@test01p> SELECT a.t_id, a.t_name
  2      FROM test a
  3           LEFT JOIN (  SELECT t_id, t_name
  4                          FROM test2
  5                      GROUP BY t_name) b
  6              ON a.t_id = b.t_id AND a.t_name = b.t_name
  7     WHERE a.t_id = 1
  8  GROUP BY a.t_id, a.t_name;
      T_ID T_NAME
---------- --------------------------------------------------
         1 a

--//換1個數值,SELECT變成sELECT看看.

SCOTT@test01p> sELECT a.t_id, a.t_name
  2      FROM test a
  3           LEFT JOIN (  SELECT t_id, t_name
  4                          FROM test2
  5                      GROUP BY t_name) b
  6              ON a.t_id = b.t_id AND a.t_name = b.t_name
  7     WHERE a.t_id = 2
  8  GROUP BY a.t_id, a.t_name;
      T_ID T_NAME
---------- --------------------------------------------------
         2 b

--//OK解決,最好還是叫開發改程式碼..

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2215299/,如需轉載,請註明出處,否則將追究法律責任。

相關文章