Karl about the Oracle Database

Some experiences out of my daily oracle practice

Archive for the 'SQL' Category

Month ID generator

Posted by carl.reitschuster on 22nd May 2007

Hi reader,

if you have to deal with datawarehouses you need to implement dimension tables. A dimension could be the time dimension - a table of month Id’s for example to aggregate data per month. How to generate  such month id’s (YYYYMM)?

Simply use the model clause - some Oracle 10g spreadsheet functionality and generate a bunch of cells with specified rules.

To get a stream of years execute following SQL :


SELECT Integer_Value AS YEAR
 FROM Dual
 WHERE 1=2
 MODEL DIMENSION BY(0 AS Key)
       MEASURES(0 AS Integer_Value)
       RULES UPSERT(Integer_Value [ FOR KEY FROM 1900 TO 2100 INCREMENT 1 ] = Cv(KEY))


SQL> SELECT Integer_Value AS YEAR
  2    FROM Dual
  3   WHERE 1 = 2
  4   MODEL DIMENSION BY(0 AS Key)
  5         MEASURES(0 AS Integer_Value)
  6         RULES UPSERT(Integer_Value [ FOR KEY FROM 1900 TO 2100 INCREMENT 1 ] = Cv(KEY))
  7  ;
 
      YEAR
———-
      1900
      1901
      1902
      1903
      1904
      1905
      1906
      1907

To get a stream of months execute following statement


SELECT Integer_Value AS MONTH

  FROM Dual

 WHERE 1 = 2

 MODEL DIMENSION BY(0 AS Key)

       MEASURES(0 AS Integer_Value)

       RULES Upsert(Integer_Value [ FOR KEY FROM 1 TO 12 INCREMENT 1 ] = Cv(Key))

SQL> SELECT Integer_Value AS MONTH
  2    FROM Dual
  3   WHERE 1 = 2
  4   MODEL DIMENSION BY(0 AS Key)
  5         MEASURES(0 AS Integer_Value)
  6         RULES Upsert(Integer_Value [ FOR KEY FROM 1 TO 12 INCREMENT 1 ] = Cv(Key))
  7  ;
 
     MONTH
———-
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
 
12 rows selected
 

To get the combination of both use cartesian product of month and year stream


SELECT YEAR , MONTH, (YEAR * 100) + MONTH AS MONTHID FROM (

  SELECT Integer_Value AS YEAR

    FROM Dual

   WHERE 1 = 2

   MODEL DIMENSION BY(0 AS Key)

         MEASURES(0 AS Integer_Value)

         RULES UPSERT(Integer_Value [ FOR Key FROM 1900 TO 2100 Increment 1 ] = Cv(Key))),

(

  SELECT Integer_Value AS MONTH

    FROM Dual

   WHERE 1 = 2

   MODEL DIMENSION BY(0 AS Key)

         MEASURES(0 AS Integer_Value)

         RULES UPSERT(Integer_Value [ FOR KEY FROM 1 TO 12 INCREMENT 1 ] = Cv(Key))) 

 

SQL> SELECT YEAR , MONTH, (YEAR * 100) + MONTH AS  MONTHID FROM (
  2    SELECT Integer_Value AS YEAR
  3      FROM Dual
  4     WHERE 1 = 2
  5     MODEL DIMENSION BY(0 AS Key)
  6           MEASURES(0 AS Integer_Value)
  7           RULES UPSERT(Integer_Value [ FOR Key FROM 1900 TO 2100 Increment 1 ] = Cv(Key))),
  8  (
  9    SELECT Integer_Value AS MONTH
 10      FROM Dual
 11     WHERE 1 = 2
 12     MODEL DIMENSION BY(0 AS Key)
 13           MEASURES(0 AS Integer_Value)
 14           RULES UPSERT(Integer_Value [ FOR KEY FROM 1 TO 12 INCREMENT 1 ] = Cv(Key)))
 15  ;
 
      YEAR      MONTH    MONTHID
———- ———- ———-
      1900          1     190001
      1900          2     190002
      1900          3     190003
      1900          4     190004
      1900          5     190005
      1900          6     190006
      1900          7     190007
      1900          8     190008
      1900          9     190009
      1900         10     190010
      1900         11     190011
      1900         12     190012
      1901          1     190101

Now you get your monthid without writing one line of PL/SQL or the need of base data.

HTH Karl Reitschuster

 

Posted in 10.2, SQL | No Comments »

A simple GUID() Generator …

Posted by carl.reitschuster on 24th April 2007

Hi Reader,

In a posting in  the Oracle Xing Forum Sven Vetter showed a very simple way to write for example a GUID generator Statement with CONNECT BY;

just

SELECT Sys_Guid()
 FROM  Dual
CONNECT BY LEVEL <= 10
;

SQL> SELECT Sys_Guid()
  2    FROM Dual
  3  CONNECT BY LEVEL <= 10;
 
SYS_GUID()
——————————–
2EDF60AEC8A41F7AE040A98C881B4573
2EDF60AEC8A51F7AE040A98C881B4573
2EDF60AEC8A61F7AE040A98C881B4573
2EDF60AEC8A71F7AE040A98C881B4573
2EDF60AEC8A81F7AE040A98C881B4573
2EDF60AEC8A91F7AE040A98C881B4573
2EDF60AEC8AA1F7AE040A98C881B4573
2EDF60AEC8AB1F7AE040A98C881B4573
2EDF60AEC8AC1F7AE040A98C881B4573
2EDF60AEC8AD1F7AE040A98C881B4573
 
10 rows selected
 
SQL>

Impressed?
Karl Reitschuster

NOTE:

As Laurent Schneider mentioned - this is not normal CONNECT BY behavior and should not work at all - it’s a bug. You must use the PRIOR Operator to describe the parent relationship.

 

Posted in SQL | No Comments »

Flashback Query simple example

Posted by carl.reitschuster on 17th October 2006

Hi reader,

changing a type of a company form internal value 2 to 1 increases the number companies with type 1; Ok thats no rocket science. But i wanted to run the counting query in the past to get the old number companies with company type = 1;

Currently i get following result with 359 Companies :

SQL> SELECT COUNT(*)
  2            FROM Company
  3           WHERE Company.Companytype = 1
  4  ;

  COUNT(*)
———-
       359

with following query i get the result in the past (358 Companies). Undo Blocks were still available for this, look at the AS OF TIMESTAMP Clause:

SELECT *
 
FROM (SELECT COUNT(*)
         
FROM Company
        
WHERE Company.Companytype = 1) AS OF TIMESTAMP SYSDATE - 0.1;

 

 SQL> SELECT *
  2    FROM (SELECT COUNT(*)
  3            FROM Company
  4           WHERE Company.Companytype = 1) AS OF TIMESTAMP SYSDATE - 0.1;

  COUNT(*)
———-
       358

SQL>

For a limited time you could query in the past even with COUNT(*)  - Queries;

HTH Karl

PS.: Unfortunately this works only for the past but not for the future - ;-)

 

 

Posted in 10.2, SQL | No Comments »

Not All OBJECT_TYPES are found in the DBA_OBJECTS View

Posted by carl.reitschuster on 14th September 2006

Hi reader,

If you copy a schema from a database to another you could check the integrity of the operation via the DBA_OBJECTS View. A good idea but the data dictionary view is incomplete - not all object types are supported.

SELECT DISTINCT Do.Object_Type
 
FROM
Dba_Objects Do
 
ORDER BY Do.Object_Type

;

SQL> SELECT DISTINCT Do.Object_Type
  2    FROM Dba_Objects Do
  3   ORDER BY Do.Object_Type

  4  ;

OBJECT_TYPE
——————-
CHAIN
CLUSTER
CONSUMER GROUP
CONTEXT
DATABASE LINK
DIRECTORY
EVALUATION CONTEXT
FUNCTION
INDEX
INDEX PARTITION
INDEXTYPE
JOB
JOB CLASS
LIBRARY
LOB
LOB PARTITION
MATERIALIZED VIEW
OPERATOR
PACKAGE
PACKAGE BODY

PROCEDURE
PROGRAM
QUEUE
RESOURCE PLAN
RULE
RULE SET
SCHEDULE
SEQUENCE
SYNONYM
TABLE
TABLE PARTITION
TRIGGER
TYPE
TYPE BODY
UNDEFINED
VIEW
WINDOW
WINDOW GROUP
XML SCHEMA

39 rows selected

SQL>

In a schema of the same database i queried all object types of the DBA_OBJECTS view i found object types not listed : 


SELECT Uml.Log_Table
  FROM User_Mview_Logs Uml
;


SELECT Urc.NAME,
              
Urc.Refgroup
  FROM User_Refresh_Children Urc
;

SELECT Ur.Refgroup,
       Ur.Rname
  FROM
User_Refresh Ur

;

 

SQL> SELECT Uml.Log_Table
  2    FROM User_Mview_Logs Uml

;

LOG_TABLE
——————————
MLOG$_COMPANY

SQL>

SQL> SELECT Ur.Refgroup,
  2         Ur.Rname
  3    FROM User_Refresh Ur

;

  REFGROUP RNAME
———- ——————————
       141 GMRV_FACTS_RGR_IFX
SQL>

SQL> SELECT Urc.NAME,
  2                 Urc.Refgroup
  3    FROM User_Refresh_Children Urc

;

NAME                             REFGROUP
—————————— ———-
GMRV_FACTS_MAX_PARENT_IFX             141
GMRV_FACTS_COUNTPART_IFX              141
GMRV_FACTS_MAX_VAL_IFX                141

SQL>  

As you can see clearly Refresh groups and Materialized View Logs are not supported and documented via the (DBA|ALL|USER)_OBJECTS Views.

 

HTH KARL

 

Posted in 10.2, SQL | 3 Comments »

Shrinking segments

Posted by carl.reitschuster on 8th September 2006

Hi reader,

with oracle 10.2 you have something like a segment fragment which could release space not needed anymore. First you enable row movement to enable reorganization. Then you start to shrink. With the dba_segments view you can query the segment properties of your table or index.

SELECT Us.Segment_Name,
       Us.Segment_Type
,
       Us.Bytes
,
       Us.Blocks
 
FROM User_Segments Us
 
WHERE Us.Segment_Name = ‘EVENTLOG’

;

SQL>                                      
SQL> SELECT Us.Segment_Name,
  2         Us.Segment_Type,
  3         Us.Bytes,
  4         Us.Blocks
  5    FROM User_Segments Us
  6   WHERE Us.Segment_Name = ‘EVENTLOG’
  7  ;
SEGMENT_NAME                                SEGMENT_TYPE            BYTES     BLOCKS
——————————————- —————— ———- ———-
EVENTLOG                                    TABLE                84934656      10368
SQL>

 

Like a diskdefragmenter must be able to move disk blocks of a file the table defragmentation only works effective if rows are allowed to be moved from one oracle block to another. This means the rowid of a table row changes.

ALTER TABLE EVENTLOG ENABLE ROW MOVEMENT
;
ALTER TABLE EVENTLOG SHRINK SPACE
;

SQL> ALTER TABLE EVENTLOG ENABLE ROW MOVEMENT;
Table altered
SQL> ALTER TABLE EVENTLOG SHRINK SPACE;
Table altered
SQL>

In this example the number blocks could be reduced from 10368 to 128! Also this means that the high water mark which is the end position of the Full Table Scan is reseted too causing reduced resource usage for Full Table Scans.

SQL>                                                                                
SQL> SELECT Us.Segment_Name,                                                        
  2         Us.Segment_Type,                                                        
  3         Us.Bytes,                                                               
  4         Us.Blocks                                                               
  5    FROM User_Segments Us                                                        
  6   WHERE Us.Segment_Name = ‘EVENTLOG’
  7  ;                                           
                                                                                    
SEGMENT_NAME                                SEGMENT_TYPE            BYTES     BLOCKS
——————————————- —————— ———- ———-
EVENTLOG                                    TABLE                 1048576        128
                                                                                    
SQL>                                                                                

How do you detect candidates for shrinking? simply look into Segment Advisor Recommendations of the database Homepage of the DbConsole or do this by SQL using the Dba_Advisor_Findings view :

SELECT Daf.Task_Id,
       Daf.Finding_Id
,
       Daf.Task_Name
,
       Daf.
TYPE,
       Daf.Object_Id
,
       Daf.Message
 
FROM Dba_Advisor_Findings Daf
 
WHERE Daf.Task_Name LIKE ‘SYS_AUTO_SPCADV%’

  
AND Message LIKE ‘%EVENTLOG%’

;


SQL>
SQL> SELECT Daf.Task_Id,
  2         Daf.Finding_Id,
  3         Daf.Task_Name,
  4         Daf.TYPE,
  5         Daf.Object_Id,
  6         Daf.Message
  7    FROM Dba_Advisor_Findings Daf
  8   WHERE Daf.Task_Name LIKE ‘SYS_AUTO_SPCADV%’
  9     AND Message LIKE ‘%EVENTLOG%’
 10  ;

   TASK_ID FINDING_ID TASK_NAME                      TYPE         OBJECT_ID MESSAGE
———- ———- —————————— ———– ———- ——————————————————————————–
     14837         10 SYS_AUTO_SPCADV_250201882006   INFORMATION         28 Zeilenverschiebung der Tabelle EVENTLOG aktivieren und Verkleinerung vorneh
     14870         11 SYS_AUTO_SPCADV_25041982006    INFORMATION         28 Zeilenverschiebung der Tabelle EVENTLOG aktivieren und Verkleinerung vorneh
     15127          7 SYS_AUTO_SPCADV_410202182006   INFORMATION         23 Zeilenverschiebung der Tabelle EVENTLOG aktivieren und Verkleinerung vorneh
     15228          1 SYS_AUTO_SPCADV_921202282006   INFORMATION          1 Zeilenverschiebung der Tabelle EVENTLOG aktivieren und Verkleinerung vorneh

SQL>

HTH Karl


Links

 

Posted in 10.2, SQL | No Comments »

REGEXP_LIKE the better LIKE ;-)

Posted by carl.reitschuster on 31st July 2006

Hi Reader,

I use a pattern to select access indexes of a table defined in our application schema. Now having Oracle 10G i changed the SQL from standard LIKE to REGEXP_LIKE. If you are used to work with regular expression specially on UNIX operating systems you will be very excited about the regular expression engine built in Oracle SQL.

The classic approach :

SELECT Ui.Index_Name
 
FROM User_Indexes Ui
 
WHERE Ui.Table_Name =‘NVDW_FACTS_V02_IFX’
  
AND(   Ui.Index_Name LIKE Ui.Table_Name || ‘\_IX__’ ESCAPE ‘\’
      
OR Ui.Index_Name LIKE Ui.Table_Name || ‘\_FX__’ ESCAPE ‘\’
      
OR Ui.Index_Name LIKE Ui.Table_Name || ‘\_BX__’ ESCAPE ‘\’)

;

The new approach :

SELECT Ui.Index_Name
 
FROM User_Indexes Ui
 
WHERE Ui.Table_Name = ‘NVDW_FACTS_V02_IFX’
  
AND regexp_like(ui.index_name, ui.table_name || ‘_[IFB]X[[:digit:]]{2}’)

;

The output :

SQL> SELECT Ui.Index_Name
  2    FROM User_Indexes Ui
  3   WHERE Ui.Table_Name = ‘NVDW_FACTS_V02_IFX’
  4     AND regexp_like(ui.index_name, ui.table_name || ‘_[IFB]X[[:digit:]]{2}’);

INDEX_NAME
——————————
NVDW_FACTS_V02_IFX_BX01
NVDW_FACTS_V02_IFX_BX02

SQL>

The new appraoach is more exact because the last to characters are exactly defined as two digits ‘[[:digit:]]{2}’, you could do the same with Oracle SQL too but with a very large where clause!

HTH
Karl

 


 

Links :

Posted in 10.2, SQL | No Comments »