It's All About ORACLE

Oracle - The number one Database Management System. Hope this Blog will teach a lot about oracle.

PRAGMA directives in Oracle PL/SQL

In Oracle PL/SQL, PRAGMA refers to a compiler directive or "hint" it is used to provide an instruction to the compiler. The directive restricts member subprograms to query or modify database tables and packaged variables. Pragma directives are processed at compile time where they pass necessary information to the compiler; they are not processed at runtime.


The 5 types of Pragma directives available in Oracle are listed below (Four of them exist since Oracle8i while the last one has been introduced with Oracle11g):

  1. PRAGMA AUTONOMOUS_TRANSACTION: This pragma can perform an autonomous transaction within a PL/SQL block between a BEGIN and END statement without affecting the entire transaction.

  2. PRAGMA SERIALLY_REUSABLE: This directive tels Oracle that the package state is needed only for the duration of one call to the server. After the call is made the package may be unloaded to reclaim memory.

  3. PRAGMA RESTRICT_REFRENCES: Defines the purity level of a packaged program. After Oracle8i this is no longer required.

  4. PRAGMA EXCEPTION_INIT: This directive binds a user defined exception to a particular error number.

  5. PRAGMA INLINE: (Introduced in Oracle 11g) This directive specifies that a subprogram call either is or is not to be inlined. Inlining replaces a subprogram call with a copy of the called subprogram.
Let’s begin with each one by one

PRAGMA EXCEPTION_INIT

This directive allows us to associate an ORA error code to an user-defined PL/SQL exception.
Once the association as been done we’ll be able to manage the exception in our code as it was a predefined exception (just like NO_DATA_FOUND or TOO_MANY_ROWS).
Let’s see an example.

We need a function that converts a string to a date using the ‘YYYY-MM-DD’ format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SQL> create or replace function string2date (str in varchar2) return date is
  2  retDate date;
  begin
  4    retDate := to_date(str,'yyyy-mm-dd');
  5    return retDate;
  end;
  7  /
SQL> select string2date('2010-01-31')
  from dual;
STRING2DA
---------
31-JAN-10
SQL> select string2date('werrwer')
  from dual;
select string2date('werrwer')
       *
ERROR at line 1:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
ORA-06512: at "MAXR.STRING2DATE", line 4


As the example shows, if the input string does not conform to the format we get the ORA-1841 error.
We want to manage this error using the PRAGMA EXCEPTION_INIT directive:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SQL> create or replace function string2date (str in varchar2) return date is
  2  retDate date;
  3  not_valid_date exception;
  4  PRAGMA EXCEPTION_INIT(not_valid_date,-1841);
  begin
  6    retDate := to_date(str,'yyyy-mm-dd');
  7    return retDate;
  8  exception
  9    when not_valid_date then
 10     dbms_output.put_line('Error: the string '||str||' cannot be converted to a date!');
 11     return null;
 12  end;
 13  /
SQL> set serverout on
SQL> select string2date('werrwer')
  from dual;
STRING2DA
---------
Error: the string werrwer cannot be converted to a date!
We’re defining a new exception not_valid_date, but it will be never called if we don’t associate it to the ORA-1841 error using the PRAGMA.
Once we have made the association Oracle knows that, in case of the ORA-1841 error, the not_valid_date exception must be raised.


PRAGMA RESTRICT_REFERENCES 

 It allows us to explicitly declare that a PL/SQL program doesn’t read/write in db objects or in package variables. In some situations, only functions that guarantee those restrictions can be used.
The fewer side-effects a function has, the better it can be optimized within a query, particular when the PARALLEL_ENABLE or DETERMINISTIC hints are used. The same rules that apply to the function itself also apply to any functions or procedures that it calls. 

If any SQL statement inside the function body violates a rule, you get an error at run time (when the statement is parsed). To check for violations of the rules at compile time, you can use the compiler directive PRAGMA RESTRICT_REFERENCES. This pragma asserts that a function does not read and/or write database tables and/or package variables. Functions that do any of these read or write operations are difficult to optimize, because any call might produce different results or encounter errors.

pragma_declaration ::= 
PRAGMA RESTRICT_REFERENCES 
 ({function_name | DEFAULT},
 {RNDS | WNDS| RNPS| WNPS | TRUST} 
 [, {RNDS | WNDS| RNPS| WNPS | TRUST}]...);

DEFAULT
Specifies that the pragma applies to all subprograms in the package spec or object type spec. You can still declare the pragma for individual subprograms. Such pragmas override the default pragma.

RNDS
Asserts that the subprogram reads no database state (does not query database tables).

RNPS
Asserts that the subprogram reads no package state (does not reference the values of packaged variables)

TRUST
Asserts that the subprogram can be trusted not to violate one or more rules. This value is needed for functions written in C or Java that are called from PL/SQL, since PL/SQL cannot verify them at run time.

WNDS
Asserts that the subprogram writes no database state (does not modify database tables).

WNPS
Asserts that the subprogram writes no package state (does not change the values of packaged variables).

You can declare the pragma RESTRICT_REFERENCES only in a package spec or object type spec. You can specify up to four constraints (RNDSRNPSWNDSWNPS) in any order. To call a function from parallel queries, you must specify all four constraints. No constraint implies another.
When you specify TRUST, the function body is not checked for violations of the constraints listed in the pragma. The function is trusted not to violate them. Skipping these checks can improve performance.
If you specify DEFAULT instead of a subprogram name, the pragma applies to all subprograms in the package spec or object type spec (including the system-defined constructor for object types). You can still declare the pragma for individual subprograms, overriding the default pragma.

The following is a simple example:
Let’s define a package made of a single function that updates a db table and returns a number:

1
2
3
4
5
6
7
8
9
10
11
12
13
SQL> create or replace package pack is
  function a return number;
  end;
  4  /
SQL> create or replace package body pack is
  function a return number is
  begin
  4    update emp set empno=0 where 1=2;
  5    return 2;
  end;
  end;
  8  /

If we try to use the function pack.a in a query statement we’ll get an error:
1
2
3
4
5
6
SQL> select pack.a from dual;
select pack.a from dual
       *
ERROR at line 1:
ORA-14551: cannot perform a DML operation inside a query
ORA-06512: a "MAXR.PACK", line 4

PL/SQL functions can be used inside a query statement only if they don’t modify neither the db nor packages’ variables.
This error can be descovered only at runtime, when the select statement is executed.
How can we check for this errors at compile time? We can use PRAGMA RESTRICT_REFERENCES!
If we know that the function will be used in SQL we can define it as follows:

1
2
3
4
5
SQL> create or replace package pack is
  function a return number;
  3  pragma restrict_references(a,'WNDS');
  end;
  5  /

Declaring that the function A will not modify the database state (WNDS stands for WRITE NO DATABASE STATE).
Once we have made this declaration, if a programmer, not knowing that the function has to be used in a query statement, tries to write code for A that violates the PRAGMA:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SQL> create or replace package body pack is
  function a return number is
  begin
  4    update emp set empno=0 where 1=2;
  5    return 2;
  end;
  end;
  8  /
Warning: Package Body created with compilation errors.
SVIL>sho err
Errors for PACKAGE BODY PACK:
LINE/COL ERROR
-------- -----------------------------------------------------------------
2/1      PLS-00452: Subprogram 'A' violates its associated pragma

He(She)’ll get an error at compile time…

NOTE: Pragma RESTRICT_REFERENCE is deprecated and could be removed from future versions of Oracle.

PRAGMA SERIALLY_REUSABLE 

The PRAGMA tells to the compiler that the package’s variables are needed for a single use. After this single use Oracle can free the associated memory. It’s really useful to save memory when a packages uses large temporary space just once in the session.
Let’s see an example.
Let’s define a package with a single numeric variable “var” not initialized:

1
2
3
4
SQL> create or replace package pack is
  2  var number;
  end;
  4  /

If we assign a value to var, this will preserve that value for the whole session:
1
2
3
4
5
6
7
SQL> begin
  2  pack.var := 1;
  end;
  4  /
SQL> exec dbms_output.put_line('Var='||pack.var);
Var=1

If we use the PRAGMA SERIALLY_REUSABLE, var will preserve the value just inside the program that initializes it, but is null in the following calls:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SQL> create or replace package pack is
  2  PRAGMA SERIALLY_REUSABLE;
  3  var number;
  end;
  5  /
SQL> begin
  2  pack.var := 1;
  3  dbms_output.put_line('Var='||pack.var);
  end;
  5  /
Var=1
SQL> exec dbms_output.put_line('Var='||pack.var);
Var=

PRAGMA SERIALLY_REUSABLE is a way to change the default behavior of package variables that is as useful as heavy for memory.

PRAGMA AUTONOMOUS_TRANSACTION 

It declare to the compiler that a given program has to run into a dedicated transaction, ignoring all uncommitted data changes made into the original transaction of the calling program.
The sum of salaries in EMP is:
1
2
3
4
5
SQL> select sum(sal) from emp;
  SUM(SAL)
----------
     29025

Let’s define two functions that do exactly the same thing, read and return the sum of salaries of EMP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SQL> create or replace function getsal return number is
  2  s number;
  begin
  4    select sum(sal) into s from emp;
  5    return s;
  end;
  7  /
SQL> create or replace function getsal_AT return number is
  2  PRAGMA AUTONOMOUS_TRANSACTION;
  3  s number;
  begin
  5    select sum(sal) into s from emp;
  6    return s;
  end;
  8  /
SQL> select sum(sal), getsal, getsal_AT
  from emp;
  SUM(SAL)     GETSAL  GETSAL_AT
---------- ---------- ----------
     29025      29025      29025

The second one uses the PRAGMA AUTONOMOUS_TRANSACTION. Now let’s cut all the salaries:
1
2
3
4
5
6
7
8
SQL>  update emp set sal=10;
SQL> select sum(sal), getsal, getsal_AT
  from emp;
  SUM(SAL)     GETSAL  GETSAL_AT
---------- ---------- ----------
       140        140      29025

GETSAL is seeing uncommitted changed data while GETSAL_AT, defined using PRAGMA AUTONOMOUS_TRANSACTION, reads data as they where before the UPDATE statement.

PRAGMA INLINE

The only PRAGMA recently added (in Oracle11g) is PRAGMA INLINE.
In Oracle11g has been added a new feature that optimizer can use to get better performances, it’s called Subprogram Inlining.
Optimizer can (autonomously or on demand) choose to replace a subprogram call with a local copy of the subprogram.
For example, assume the following code:
1
2
3
4
5
declare
total number;
begin
 total := calculate_nominal + calculate_interests;
end;
Where calculate_nominal and calculate_interests are two functions defined as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function calculate_nominal return number is
s number;
begin
  select sum(nominal)
    into s
    from deals;
     
  return s;
end;
function calculate_interests return number is
s number;
begin
  select sum(interest)
    into s
    from deals;
     
  return s;
end;
Optimizer can change the code to something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
declare
total number;
v_calculate_nominal number;
v_calculate_interests number;
begin
  select sum(nominal)
    into v_calculate_nominal
    from deals;
  select sum(interest)
    into v_calculate_interests
    from deals;
 total := v_calculate_nominal + v_calculate_interests;
end;
Including a copy of the subprograms into the calling program. PRAGMA INLINE is the tool that we own to drive this new feature. 

If we don’t want such an optimization we can do:
1
2
3
4
5
6
7
declare
total number;
begin
 PRAGMA INLINE(calculate_nominal,'NO');
 PRAGMA INLINE(calculate_interests,'NO');
 total := calculate_nominal + calculate_interests;
end;
If we do want subprogram inlining on calculate_nominal we do:
1
2
3
4
5
6
declare
total number;
begin
 PRAGMA INLINE(calculate_nominal,'YES');
 total := calculate_nominal + calculate_interests;
end;

Subprogram inlining behave differently depending on the level of optimization defined through the db initialization variable PLSQL_OPTIMIZE_LEVEL.

If this variable is set to 2 (that’s the default value) optimizer never uses subprogram inlining unless the programmer requests it using PRAGMA INLINE YES.
If PLSQL_OPTIMIZE_LEVEL=3 optimizer can autonomously decide whether to use subprogram inlining or not. In this case PRAGMA INLINE YES does not force the optimizer, it’s just an hint.

Data Warehouse Various Components - 1

Objective of this tutorial are:

  •  Understand data marts and its types
  •  Learn the methods to build data warehouse
  •  Distinguish between types of dimensions
  •  Explain difference between types of facts 
  •  Understand Hierarchy and Data warehouse Architecture
  •  Explain what are dimension and facts
  •  Learn importance of Dimension and fact table.

Data Marts

  •  Data marts is a smallest version of Data warehouse.
  •  Data marts deal with a single project.
  •  Data marts are focussed on one area. Hence they draw data from a limited number of sources.
  •  Time taken to build the data marts is very low compared to the time taken to build a DataWarehouse.
Supply chain, Finance, HR department, Sales are all seperate department operating separately. These department could have their own data need which will be very specific to their data needs so they can have their own data marts. 

Difference between Data Warehouse and Data Marts:

Data warehouse Data Marts
Enterprise wide data          Department wide data
Multiple subject areas          Single Subject Area
Multiple Data sources          Limited data source
Occupies large memory         Occupies limited memory
Longer time to implement         Shorter time to implement

Different types of Data Marts:

Dependent Data Mart:

Loaded from Data warehouse. First you form a Data Warehouse then you form a Data Marts.
"The data is first extracted from the OLTP systems and then populated in the central Data warehouse. From the Data warehouse the data then travel down to the data marts."

Q: Why someone will have a dependent Data Mart:
Ans: You will go for Data mart so that you do not have to access the whole Data warehouse for fetching some reports and you want quick turn around time from your source queries.

Independent Data Mart
Independent data mart is the one that depend on the OLTP source that is available. So the data is loaded directly from the transaction system into the data mart based on the requirement of the reports.
That's is suitable for small organization or small groups within an organization, which can be easily developed. So Independent Data mart is quick solution for quick requirements for small organization.

Hybrid Data Mart:
It is a mix of both Independent and Dependent Data Mart.

A hybrid data mart allows you to combine input from sources other than a data warehouse. This could be useful for many situations, especially when you need ad hoc integration, such as after a new group or product is added to the organization.

Most of the business scenarios will be Hybrid. In this scenario we have to get the file data and combine the report from the file and combine the report from the data warehouse and form a consolidated report.

What to Build First

Whether to build the Data warehouse first or the data mart first. Based on this question two approaches have been defined:

Top Down Approach
  • Data warehouse is build first and then the data marts are built. So Data warehouse is built on top of OLTP system. 
  • It is one big coherent warehouse that is built directly from the OTLP source systems. So it has consolidated data from all the OLTP sources.
  • The main advantage is that the information is available at a central location. various people accessing the data will have same information at a point of time. 
  • This disadvantage is that this becomes a very big project which should be handled by specialists else it can go any wrong.
  • The cost and time involved is big and the result are not quick.
The approach is also called Inmon approach.

Bottom Up Approach
  •  Data marts required by different department or specific report requirements by specific people are built first then Data warehouse is built.
  •  Data marts can be built very quickly, hence the result are seen much faster.
  •  It is easier than Top Down Approach.
  •  Since the entire architecture is broken down in the initial phase itself, it become easier to manage. Operations in one datamart doesn't affect the operations of other data marts.
  •  Usually start up go for such approach where there are limited departments and it is very easy to build data marts and then go for the data warehouse. It is cost effective, easier to implement and you do not need heavy duty resources to implement this data marts.
Top Down Approach Definition:
In this approach you first build a data warehouse and then go down to different department. The data marts are then created from the data warehouse.

Advantages of top-down design are:
Provides consistent dimensional views of data across data marts, as all data marts are loaded from the data warehouse.
This approach is robust against business changes. Creating a new data mart from the data warehouse is very easy.

Disadvantages of top-down design are:
This methodology is inflexible to changing departmental needs during implementation phase.
It represents a very large project and the cost of implementing the project is significant. 

Bottom Up Approach Definition:

In the bottom-up design approach, the data marts are created first to provide reporting capability. A data mart addresses a single business area such as sales, Finance etc. These data marts are then integrated to build a complete data warehouse.  The integration of data marts is implemented using data warehouse bus architecture. In the bus architecture, a dimension is shared between facts in two or more data marts. These dimensions are called conformed dimensions. These conformed dimensions are integrated from data marts and then data warehouse is built.

Advantages of bottom-up design are:
This model contains consistent data marts and these data marts can be delivered quickly.
As the data marts are created first, reports can be generated quickly.
The data warehouse can be extended easily to accommodate new business units. It is just creating new data marts and then integrating with other data marts.

Disadvantages of bottom-up design are:
The positions of the data warehouse and the data marts are reversed in the bottom-up approach design.

Operational Data Store
  •  Operation Data Store(ODS) contains operational data with a very short window. Basically it is a kind of data warehouse but it just store the current data.
  •  The ODS is refreshed frequently so that it contains very current data. It can be updated daily, hourly etc. However Data warehouse is time variant, keep historical data and not updated daily rather updated monthly. Its like a central data that call centre people may need to look before they answer customer query. Call center might need certain summarize data for certain level of statistical view of the OLTP system. That's why we need ODS.
  •  The frequency of refresh depends on how current the data must be for reporting purpose.
  •  This concept comes in between the concept of OLTP and the Datawarehouse. When real time reporting is not possible in a Datawarehouse, ODS is used.
  •  It provides improved access to the critical and current operational data.
Example:
When we place an order on an e-commerce site and we call up the call centre to enquire about the state.
We cannot get information from Data warehouse, as the data might not even have reached the warehouse OLTP system is not ideal to fetch the data as already described
Hence in such case ODS is used.
When the status changes, the OLTP gets updated and the same is refreshed in ODS but only the current data is maintained.

Dimension Table

  •  The Objects of the subject are called Dimensions. The categories under which an information can be split across is a Dimension. All the related information for that category is placed inside the dimension. 
  • The tables that describe the dimensions involved are called "Dimension Table". For example a Hospital related Dimension table will have all information related to Hospital like Hospital Name, Hospital ID, Address, Number of doctor. All the dimension related to hospital will be present in that dimension table. So this information (detail of hospital) shouldn't be kept or repeated in any other table.
  • Basically a Dimension table is a category of information. 
  • Dividing a Data warehouse project into dimensions, provides structured information for reporting purpose. 

It is to be noted that business users or the end users who generate reports, fire queries on these dimension tables, because they contain descriptive information.

What are Facts?

  • A fact is a measure that can be summed, averaged or manipulated. If a fact is manipulated, it has to be a measure that make a business sense.
  • A dimension is lined to a fact. There is a fact table and multiple dimensions will be linked to it.
  • A fact table contain 2 kind of data - a dimension key and a measure. For example, a fact with column ProductID, CustomerID and Quantity. Here productID and CustomerID will be Dimension Key and Quantity will be store the actual measure.
The combination of these two is called a Data Model. 

Conformed Dimension, De-Generated Dimension, Junk Dimension, Slowly Changing Dimension are other Dimensions.

Slowly Changing Dimension:

In a data warehousing concept when we split the data into various category, ( we split into categories so that we maintain category of data in separate tables which is more or less static). Like a customer name will change but it will not change frequently. So these are dimensions which might change but do not change. 
  •  Dimension attributes that change slowly over a period of time rather than changing regularly is grouped as SCD.
Q: Why we cannot keep fact and dimension in same table. 
Ans: If dimension change then we need to change the whole fact table, update everything inside the fact table. e.g if customer name changes and you have change all the record of fact table where that information has been kept.
However if maintain Dimension and fact separately then you will have a customerID and changes to customer name will be in Dimension table only. This way we are able to save ourself from updating Customer information in Fact table.
  • Dimensions might not change frequently but it could change occasionally. That's why it is called Slowly changing Dimension.
  • Let's consider an example of a person changing his/her city from city 1 to city 2 and this change does not happen at regular intervals. Here person information will be in different table and his location information will be in a different Location Dimension table. So only the location Dimension will change in fact table, person information will not change.
Q. How do we maintain these slowly changing dimensions?
Ans. By Data warehousing concept there are three ways in which these types of attributes are handled (Handling means the way you want to keep the new information with keeping historic information also or you want to keep only the latest information):

1. Type 1 Slowly Changing Dimension: You overwrite the old Values. 
You just keep only City 2 information. No information of City 1 neither do you keep when it is changed. So you never know when person was on City 1.

2. Type 2 Slowly Changing Dimension: You add a new record in your dimension table. You do not change the key, but you add a new record in your dimension table. So you will always know where he was at a certain point of time and when he actually change.

3. Type 3 Slowly Changing Dimension: You add a new column in your dimension table. Column as in previous location, current location. So either you will keep two column only or keep adding new column. 

SCD Type 1 - Overwrite the Old Value

  •  The advantage of this approach is that it is very easy to follow and results in huge space savings and hence cost saving.
  •  The disadvantage is no historical data is kept.
SCD Type 2 - Add a New Row(Challenges)
  •  It is relatively difficult to determine which record is the most recent.
  •  A subquery has to be written to fetch the most recent record for each ID.
  • To overcome these, there are 2 approaches to indicate the most current record.
Approach 1 - Use the Start Date and the End Date 
  • Suppose that James started living in New York from 1st Jan, 2000, hence for the very first time the record will look as:
ID Start Date End Date Names City
1 01-01-2000 31-12-9999 James New York
  •  If James moves to Chicago on 1st Jan, 2014, then the record as per the 1st approach will look as below:
ID Start Date End Date Names City
1 01-01-2000 31-12-2003 James New York
1 01-01-2004 31-12-9999 James Chicago

Hence the most recent record can be identified using the condition End Date > Current Date.

Approach 2 - Use an Indicator.
First time when a record arrives with New York as the City, the Indicator will be set as 'Y'
ID Start Date Names City Current Indicator
1 2000 James New York Y

When the person changes the city then the Indicator of the previous record is changed to 'N' and the indicator of the current record is changes to 'Y'.
ID Start Date Names City Current Indicator
1 2000 James New York N
1 2004 James New York Y

Hence to get the most recent record, just use the condition WHERE Current_Indicator = 'Y'

Advantage: You have historical data.
Disadvantage: More space needed.

SCD Type 3 - Add a New Column

Sometimes it is not required to maintain the entire history of a slowly changing dimension.  We might want to retain the current record and the immediate previous record.
In such cases instead of adding rows, it is beneficial to add columns as illustrated below:

For the first time when James moves to New York the old city column will be blank 
ID Start Date Names City Old City Old Year
1 2000 James New York - -

When he moves to Chicago, the city column becomes chicago and the old city column will become New York
ID Start Date Names City Old City Old Year
1 2004 James Chicago New York 2000

The disadvantage of the above approach is clearly seen in the table above. For each attribute, there must be 2 columns, one indicating the current value and other indicating the previous value.

Types of Fact

There are three types of Fact Table:

Additive Fact table:
If the rows can be combined to ge the final output then it is known as Additive Fact Table. For example:
No. of product sold on Day 1 = 500
No. of product sold on Day 2 = 250

Total No. of product sold on two Days = 750

Semi-Additive Fact Table
However same can not be applied in following example:
Balance of Company's account 1 for day 1 = 5000
Balance of Company's account 2 for day 1 = 3000

Two balance in two accounts of a company on Day 1 = 8000 it will be right.

Now for the data:
Balance of Company's account for day 1 = 5000
Balance of Company's account for day 2 = 3000

If a perform additive approach: Total balance in Acc in two days: 8000, Which will be wrong.  This kind of table is called Semi-Additive fact table where we cannot perform addition across all the columns in fact tables.

Non-Additive Fact Table
Similarly if there is a fact table called profit margin:
Profit Margin for Day 1 = 30%
Profit Margin for Day 2 = 70%

Total profit margin for two days = 100%. This will be wrong.

Those fact tables where you cannot perform additive operation across any column then that fact table is called Non-Additive Fact table.

No Fact table
A Fact table where only dimension id's are present and no measurable attribute then this table is called No Fact Table because there must be some measurable attribute. Also called Fact less fact table.

You Might Also Like

Related Posts with Thumbnails

Pages