Monday, 1 August 2016

Application Developer must know SQL

Changing the password of  user
--------------------------------------
  Navigate to path  C:\oraclexe\app\oracle\product\11.2.0\server\bin

1.  login to the  sqlplus as sysdba

2. alter user system identified by <new_pass>

 Changing the password of the sysdba account
-----------------------------------------------
1. login as sqlplus   /nolog
2. con /as sysdba
3.alter user sys indetified by sysdba123

Unlocking the user acccount
==================
ALTER USER system ACCOUNT UNLOCK



Creating sequence  on the column
============================

http://sathyam-soa.blogspot.in/2012/07/adf-db-sequence-using-db-trigger.html

create sequence vendor_sequence
starts with 1
incremented by 1
nonmaxvalue;

-- Define the DB sequence at ADF level is the best choice
http://sathyam-soa.blogspot.in/2012/07/adf-db-sequence-using-db-trigger.html

http://waslleysouza.com.br/en/2014/09/using-database-sequence-adf/

 Creating the trigger on the table
==========================

create trigger  vendor_trigger
before insert on vendor
for each row begin
select vendor_sequence.nextval   into :new.vendorid from dual;


: SELECT SQL_ID,ELAPSED_TIME,(ELAPSED_TIME/1000000) as TimeInSec,buffer_gets, sql_text FROM SQLREP_SQLSET_STATEMENTS WHERE SQLSET_NAME='R13_PRC_VIN_SQL_REPORT'
AND SQL_ID IN ('31amya9qm5q4u',
'993hvbpudj04u',
'dw9p9wwt59ac4',
'59yhd8nwm3f17',
'37mvdw8bhfzw6',
'81srdc545ram5',
'4258878w8hjhv',
'2jnmh4qqf8s68',
'20yqp8z2m793v',
'97b93wxyqfsah',
'gnwj2r6jyd9rn',
'7gan7r6341udh',
'd171tkna91dw6')
order by TimeInSec;


 How many times function get executes
===============================




 begin
  
      dbms_application_info.set_client_info(0);
     end;
   
      select
   dbms_utility.get_cpu_time-:cpu cpu_hsecs,
    userenv('client_info')
    from dual;

 Code in function :
 dbms_application_info.set_client_info
       (userenv('client_info')+1 );




analytic_function([ arguments ]) OVER (analytic_clause)
 
 
The analytic_clause breaks down into the following optional elements.
[ query_partition_clause ] [ order_by_clause [ windowing_clause ] ]


  GRANT ALTER,DEBUG,DELETE,FLASHBACK,INDEX,INSERT,ON COMMIT REFRESH,QUERY REWRITE,REFERENCES,SELECT,UPDATE ON yogesh_tax TO fusion_runtime;
 


Saturday, 20 February 2016

REST Vs SOAP

'Unless you have a definitive reason to use SOAP use REST'


REST :  REST is sweat spot when you are exposing the public API overt internet to handle the CRUD operation on the data.REST focuses on assessing the named resources through the  single consistent  Interface.


  SOAP:   SOAP brings it's own protocol and focuses on the exposing the pieces of application logic (not the data).SOAP focuses on accessing the named operation, which implements some business logic through different interfaces.
Though SOAP is commonly referred to as “web services” this is a misnomer. SOAP has very little if anything to do with the Web. REST provides true “Web services” based on URIs and HTTP.
 -Language, Platform ,protocol indepedent

Why REST ?
  1. REST  ( lightweight, ) has better performance and scalability .
  2.REST supports multiple data format XML,JSON etc.
  3.REST reads can be cached, while SOAP based read can not be cached.

Why SOAP ?

1. WS_SECURITY
  While SOAP supports SSL (just like REST) it also supports WS-Security which adds some enterprise security features. Supports identity through intermediaries, not just point to point (SSL). It also provides a standard implementation of data integrity and data privacy. Calling it “Enterprise” isn’t to say it’s more secure, it simply supports some security tools that typical internet services have no need for, in fact they are really only needed in a few “enterprise” scenarios.

2. WS_AUTOMIC_TRANSACTION

Rest doesn’t have a standard messaging system and expects clients to deal with communication failures by retrying. SOAP has successful/retry logic built in and provides end-to-end reliability even through SOAP intermediaries.
3. WS_MESSAGE_RELIBILITY 

4.WS_CO_ORDINATIOND


What is Integration ?

  Integration is process by which information is passed between tow or more distinct software entities.

THE CHOICE OF INTEGRATION MEDIA IS CATEGORICALLY IS DETERMINED WITH HELP OF FOLLOWING QUESTIONNAIRES.

1. Is transfer of information is synchronous or asynchronous ?
2. Is transfer of information acknowledged ?
3. Is transfer of information transactional ?
4. Does transfer of information requires message-level or transport-level  encryption ?
5.Does transfer of information occurs in batches composed of multiple message or one message at time ?
6. Does transfer of information occurs between system build using same technology or different technology ?
7. Does transfer of information use the technology-specific/transport protocol specific ?.


Java to Java  Integration
--------------------------
1. JMS  
1. JMS is interinsically designed for Asynchronous communication between between to java aplication.
   Features:
          1. publish subscribe and point to point messaging model.
          2. Message Delivery  Acknowledgement
          3.Message Level Encryption
          4.Distributed Transaction (JTA)

Java to Non-Java Integration :
------------------------------
  1.Web service are intrinsically designed to facilitate the integration of heterogeneous systems.
      1. Using web service is truly technology  independent .
     Which one embrace :
             1. SOAP
             2. Self Describing mesaage format Such as XML
         
    SOAP VS REST.   
 
https://dzone.com/articles/put-vs-post
https://knpuniversity.com/screencast/rest/put-versus-post

  Deciding between Put and POST

=======================

1. if the end point is idempotent .
2. URI must be addressed to resource being updated.


Patch :
  Update the resource without sending  all  the attributes in the request.

 







Friday, 19 February 2016

Java Memory Structure

JVM memory areas / components
    - Heap area
      Objects and array stored
      Created when JVM started  define fix size or vary between min and max size  –Xms -Xmx
       Yong Generation
        Eden Memory
        Survivor Memory
        Most of the newly created objects are located in the Eden memory space.
            When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one of the survivor spaces.
           
       Old Generation
         after many rounds of minor GC object is moved to the Old generation space.
       
    --  Perm Gem  :
      Permanent Generation or “Perm Gen” contains the application metadata required by the JVM to describe the classes and methods used in the application. Note that Perm Gen is not part of Java Heap memory.
    - Method area and runtime constant pool
      field, method data, code, constructor ,
      created on JVM started its part of Heap Memory
       The method area may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger method area becomes unnecessary
      
    - JVM stack
      Each of the JVM threads has a private stack created at the same time as that of the thread. The stack stores frames. A frame is used to store data and partial results and to perform dynamic linking, return values for methods, and dispatch exceptions.
     
       If the computation in a thread requires a larger Java Virtual Machine stack than is permitted, the Java Virtual Machine throws a StackOverflowError.
      
       f Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java Virtual Machine stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.
      
    - Native method stacks
        Native method stacks is called C stacks; it support native methods (methods written in a language other than the Java programming language), typically allocated per each thread when each thread is created. Java Virtual Machine implementations that cannot load native methods and that do not themselves rely on conventional stacks need not supply native method stacks.

The size of native method stacks can be either fixed or dynamic.
    - PC registers
   
--    Memory Pool
      To store immutable object
     
     
--  Runtime Constant Pool

Runtime constant pool is per-class runtime representation of constant pool in a class. It contains class runtime constants and static methods. Runtime constant pool is the part of method area.



VM Switch     VM Switch Description
-Xms     For setting the initial heap size when JVM starts
-Xmx     For setting the maximum heap size.
-Xmn     For setting the size of the Young Generation, rest of the space goes for Old Generation.
-XX:PermGen     For setting the initial size of the Permanent Generation memory
-XX:MaxPermGen     For setting the maximum size of Perm Gen
-XX:SurvivorRatio     For providing ratio of Eden space and Survivor Space, for example if Young Generation size is 10m and VM switch is -XX:SurvivorRatio=2 then 5m will be reserved for Eden Space and 2.5m each for both the Survivor spaces. The default value is 8.
-XX:NewRatio     For providing ratio of old/new generation sizes. The default value is 2.


Resource Link :
http://howtodoinjava.com/core-java/garbage-collection/jvm-memory-model-structure-and-components/
 http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html  
  http://www.journaldev.com/2856/java-jvm-memory-model-and-garbage-collection-monitoring-tuning

Saturday, 6 February 2016

Count(1) Vs Count(*) Vs Count(experession) in Oracle.

1. Number of Records:

   The count(1)  and count(*)  returns same number of records, as its return the number rows in the table
   including the NULL records.
 
    While Count(Expression)  return the number of null records for which expression evalutes.
2. Performance Factor.
    There is no significance difference  in performance onwards release R8 oracle. 
  
  I will say don't invest too much time on this topics.

Source  doc Link
http://www.oracledba.co.uk/tips/count_speed.htm
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:1156159920245

 

IN Vs EXIST and NOT IN VS NOT EXIST in oracle

1. IN condition Vs EXIST   condition
==========================

1.Use IN with list of sub-query (Where servey_date  in ('20015','2016'))
2. EXIST  looking for at least one row to return return true
3.If inner query has less records then;then use the IN
4. If inner query has more records then; the outer query then use EXIST.
 (Thumb rule to use the IN and EXIST)

IN query processed as
===============

Select * from T1 where x in ( select y from T2 )
is typically processed as:

select *
  from t1, ( select distinct y from t2 ) t2
 where t1.x = t2.y;

Exist Processed as folowing
===================
select * from t1 where exists ( select null from t2 where y = x )

That is processed more like:


   for x in ( select * from t1 )
   loop
      if ( exists ( select null from t2 where y = x.x )
      then 
         OUTPUT THE RECORD
      end if
   end loop

 
 - It always result into FULL_TABLE scan on the T1.
  -If your goal is the FIRST row exists might totally blow away IN this is 
the exception to thumb rule.

 Link to source page:  IN_VS_EXIST_ASK_TOM
 
 
 
 
 
NOT IN can be just as efficient as NOT EXISTS -- many orders of magnitude BETTER 
even -- "anti-join" can be used (if the subquery is known to not return nulls) 


Anti-Join fails when NULL;Therefore, a NOT IN operation would fail
 if the result set being probed returns a NULL. In such a case, 
the results of a NOT IN query is 0 rows  while a NOT EXISTS query
 would still show the rows present in the one table but not in the other table. 

Tuesday, 24 November 2015

Performance Booster BULK COLLECT /BULK BIND/ CURRENT OF

While  doing programming   in PL/SQL  following are the common scenario of data manipulation


1. selecting multiple  records from the cursor or table (BULK COLLECT)
2. Inserting multiple record in the database(BULK  BIND )
3. Updating the multiple records to the cursor latest row fetched  from the cursor  (CURRENT OF )

 1. selecting multiple  records from the cursor or table (BULK COLLECT)
 
 Concept of Context Switching
=======================
All most every PL/SQL developer writes the SQL and  PL/SQL statement in code.  The  SQL statement are  executed by the SQL engine and PL/SQL statement are executed  by the PL/SQL engine. When PL/SQL engine encounter the SQL statement then it pass the control to the  SQL engine and  again control come backs to PL/SQL engine when PL/SQL  statement encounters.
This is called as context switching.

Following is the procedure  which   will accept the departmentId and salary percentage increase and gives to each employee in the provided department

The following procedure use the cursor (FOR LOOP cursor ) to fetch the employee for the provided departmentId and then update the employee table with increased salary


increase_salary procedure with FOR loop

PROCEDURE increase_salary (
   department_id_in   IN employees.department_id%TYPE,
   increase_pct_in    IN NUMBER)
IS
BEGIN
   FOR employee_rec
      IN (SELECT employee_id
            FROM employees
           WHERE department_id =
                    increase_salary.department_id_in)
   LOOP
      UPDATE employees emp
         SET emp.salary = emp.salary + 
             emp.salary * increase_salary.increase_pct_in
       WHERE emp.employee_id = employee_rec.employee_id;
   END LOOP;
END increase_salary;


Suppose there are 100 employees in department 15. When I execute this block,

BEGIN
   increase_salary (15, .10);
END;
 

When we are executing the above procedure there will be 100 context switching between SQL and PL/SQL engine this is row by row switching  which is performance overhead.




Simplified increase_salary procedure without FOR loop

PROCEDURE increase_salary (
   department_id_in   IN employees.department_id%TYPE,
   increase_pct_in    IN NUMBER)
IS
BEGIN
   UPDATE employees emp
      SET emp.salary =
               emp.salary
             + emp.salary * increase_salary.increase_pct_in
    WHERE emp.department_id = 
             increase_salary.department_id_in;
END increase_salary;



Single context switch to execute update statement  all work is done in single  context switch. By default the update statement is BULK update.


In Real time the code is not that much simple need to perform sever data manipulation operation then updating the data .Suppose that, for example, in the case of the increase_salary procedure, I need to check employees for eligibility for the increase in salary and if they are ineligible, send an e-mail notification. My procedure might then look like below.


PROCEDURE increase_salary (
   department_id_in   IN employees.department_id%TYPE,
   increase_pct_in    IN NUMBER)
IS
   l_eligible   BOOLEAN;
BEGIN
   FOR employee_rec
      IN (SELECT employee_id
            FROM employees
           WHERE department_id =
                    increase_salary.department_id_in)
   LOOP
      check_eligibility (employee_rec.employee_id,
                         increase_pct_in,
                         l_eligible);

      IF l_eligible
      THEN
         UPDATE employees emp
            SET emp.salary =
                     emp.salary
                   +   emp.salary
                     * increase_salary.increase_pct_in
          WHERE emp.employee_id = employee_rec.employee_id;
      END IF;
   END LOOP;
END increase_salary;

Now no longer  everything in single context switch.


Bulk Processing in PL/SQL

The bulk processing features of PL/SQL are designed specifically to reduce the number of context switches required to communicate from the PL/SQL engine to the SQL engine.

Use the BULK COLLECT clause to fetch multiple rows into one or more collections with a single context switch.

Use the FORALL statement when you need to execute the same DML statement repeatedly for different bind variable values. The UPDATE statement in the increase_salary procedure fits this scenario; the only thing that changes with each new execution of the statement is the employee ID.



 CREATE OR REPLACE PROCEDURE increase_salary (
 2     department_id_in   IN employees.department_id%TYPE,
 3     increase_pct_in    IN NUMBER)
 4  IS
 5     TYPE employee_ids_t IS TABLE OF employees.employee_id%TYPE
 6             INDEX BY PLS_INTEGER;
 7     l_employee_ids   employee_ids_t;
 8     l_eligible_ids   employee_ids_t;
 9
10     l_eligible       BOOLEAN;
11  BEGIN
12     SELECT employee_id
13       BULK COLLECT INTO l_employee_ids
14       FROM employees
15      WHERE department_id = increase_salary.department_id_in;
16
17     FOR indx IN 1 .. l_employee_ids.COUNT
18     LOOP

19        check_eligibility (l_employee_ids (indx),
20                           increase_pct_in,
21                           l_eligible);
22
23        IF l_eligible
24        THEN
25           l_eligible_ids (l_eligible_ids.COUNT + 1) :=
26              l_employee_ids (indx);
27        END IF;
28     END LOOP;
29
30     FORALL indx IN 1 .. l_eligible_ids.COUNT
31        UPDATE employees emp
32           SET emp.salary =
33                    emp.salary
34                  + emp.salary * increase_salary.increase_pct_in
35         WHERE emp.employee_id = l_eligible_ids (indx);
36  END increase_salary;



The highlighted green part is the BULK select which will fetch all employee id  in  l_employee_ids 
collection .

The code snippet highlighted in yellow FORALL BULK update
 Rather than move back and forth between PL/SQL and SQL Engine  the FORALL handles all the updates and passes them to the sql engine in single context switch.


IMPORTANT THINK to know when starting to use the advantage of the  BULK COLLECT

Trade of the BULK COLLECT run faster consume more memory .

 There are two types of memory SGA (System Global Area) and PGA(Program Global Area)
the SGA is shared for all the session  connected to the database.  whie PGA is alloacted for each session .  Memory for collection is stored in the PGA.

Thus, if a program requires 5MB of memory to populate a collection and there are 100 simultaneous connections, that program causes the consumption of 500MB of PGA memory, in addition to the memory allocated to the SGA.


The PL/SQL made the life of developer easy  by by introducing the  LIMIT clause on the   BULK COLLECT to control amount of memory used.

  FETCH employees_cur 
            BULK COLLECT INTO l_employees LIMIT limit_in;


 Due to LIMIT it will fetch the number_record specified in the limit_in parameter at a time .PL/sQL resuse the same limit_in and same memory for  subsequent fetch. Even though the table size grows the PGA consumption will be constant.

When you are using BULK COLLECT and collections to fetch data from your cursor, you should never rely on the cursor attributes to decide whether to terminate your loop and data processing.
EXIT WHEN 
l_table_with_227_rows.COUNT = 0; 




Generally, you should keep all of the following in mind when working with BULK COLLECT: 

  1.  The collection is always filled sequentially, starting from index value 1.
  2.  It is always safe (that is, you will never raise a NO_DATA_FOUND exception) to iterate through a collection from 1 to collection .COUNT when it has been filled with BULK COLLECT.
  3.  The collection is empty when no rows are fetched.
  4.  Always check the contents of the collection (with the COUNT method) to see if there are more rows to process.
  5.  Ignore the values returned by the cursor attributes, especially %NOTFOUND.




Saturday, 21 November 2015

Leading Ranks and Lagging Percentages: Analytic Functions

This post explains 7 analytical functions to manipulate the  way you display the result

1.DENSE RANK  : over (partition by department_id order by salary desc)
2. RANK :  over (partition by department_id order by salary desc)

3. FIRST_VALUE : FIRST(column)  over (partition by department_id order by salary desc)
4. LAST_VALUE

5.LEAD  LAG(column | expression, offset, default)   over (partition by department_id order by salary desc)
6. LAG  LAG(column | expression, offset, default)  over (partition by department_id order by salary desc)  offset :  how many previous rows it should go back

7.RATIO_TO_REPORT


1. Rank the data

        Display the three employees with highest salaries by department

 The query that retrieves top or bottom N rows from the database that satisfy the certain condition refered as the TOP N query .
  Business Requirement : the most highly paid employees are or which department has the lowest sales figures 


Example :

select department_id, last_name, first_name, salary,
          DENSE_RANK() over (partition by department_id
                                 order by salary desc) dense_ranking
      from employee
      order by department_id, salary desc, last_name, first_name;
 
=========================================================================================
 
 
 
 
 
DEPARTMENT_ID LAST_NAME    FIRST_NAME                    SALARY DENSE_RANKING
————————————— ———————————  —————————————————————————     —————— —————————————
           10 Dovichi      Lori                                             1
           10 Eckhardt     Emily                         100000             2
           10 Newton       Donald                         80000             3
           10 Michaels     Matthew                        70000             4
           10 Friedli      Roger                          60000             5
           10 James        Betsy                          60000             5
           20 peterson     michael                        90000             1
           20 leblanc      mark                           65000             2
           30 Jeffrey      Thomas                        300000             1
           30 Wong         Theresa                        70000             2
              Newton       Frances                        75000             1

11 rows selected.
 
 This result revels the an interesting analytical function DENSE RANK  . When query 
uses a descending order   a NULL  value can affect the outcome of analytical function.
 
By default , with descending sort , SQL views NULL as being higher than any other value .
 
In the result the record Dovichi Lori  has an NULL salary and the DENSE RANK analytical function 
assigns the highest RANK 1 in Department 10.
 
-- You can  eliminate the null by adding the where clause salary is not null.
 
 Alternatively  , yo can use  the   NULL last as the extension to the  Order by clause 
==========================================================================================
 
select department_id, last_name, first_name, salary,
        DENSE_RANK() over (partition by department_id
                               order by salary desc NULLS LAST) dense_ranking
      from employee
    order by department_id, salary desc, last_name, first_name;

DEPARTMENT_ID LAST_NAME    FIRST_NAME                    SALARY DENSE_RANKING
————————————— ———————————  —————————————————————————     —————— —————————————
           10 Dovichi      Lori                                             5
           10 Eckhardt     Emily                         100000             1
           10 Newton       Donald                         80000             2
           10 Michaels     Matthew                        70000             3
           10 Friedli      Roger                          60000             4
           10 James        Betsy                          60000             4
           20 peterson     michael                        90000             1
           20 leblanc      mark                           65000             2
           30 Jeffrey      Thomas                        300000             1
           30 Wong         Theresa                        70000             2
              Newton       Frances                        75000             1

11 rows selected. 
 
 Still the NULL record appears at the top due to the outer order by clause.
  
 --  Quick notes :
 
1. In the query outcome the highlighted  two rows have the same salary 
    and ranked the same rank as 4.
 
2. The next record having just less than salary will have the next rank i.e 5 highlighted in green
 
  DENS_RANK return the ranking number without any gaps , regardless of any records that 
   have same  value for expression in the order by clause.
   In contrast the rank analytical function find the same value records and assign the same rank 
    the subsequent rank number take in account of this by skipping ahead.
 
 
 
 select department_id, last_name, first_name, salary,
           RANK() over (partition by department_id
                            order by salary desc NULLS LAST) regular_ranking
      from employee
    order by department_id, salary desc, last_name, first_name;

DEPARTMENT_ID LAST_NAME    FIRST_NAME                SALARY REGULAR_RANKING
————————————— ———————————  ———————————————————————   —————— ———————————————
           10 Dovichi      Lori                                           6
           10 Eckhardt     Emily                     100000               1
           10 Newton       Donald                     80000               2
           10 Michaels     Matthew                    70000               3
           10 Friedli      Roger                      60000               4
           10 James        Betsy                      60000               4
           20 peterson     michael                    90000               1
           20 leblanc      mark                       65000               2
           30 Jeffrey      Thomas                    300000               1
           30 Wong         Theresa                    70000               2
              Newton       Frances                    75000               1

11 rows selected.

  In department 10 the highlighted  rows shows ranking difference in RANK and DENSE_RANK 
 Same value row got the same rank 4 but subsequent record got the rank 6 instead of 5 in DENSE_RANK.


FINISHING FIRST  OR LAST :

For reporting purpose it might occasionally  useful to include the first value obtained in the perticular group or window.
 then you can use FIRST_VALUE analytical function

Display the first value returned per window, using FIRST_VALUE 
SQL> select last_name, first_name, department_id, hire_date, salary,
  2       FIRST_VALUE(salary)
  3       over (partition by department_id order by hire_date) first_sal_by_dept
  4   from employee
  5  order by department_id, hire_date;

LAST_NAME     FIRST_NAME   DEPARTMENT_ID HIRE_DATE  SALARY FIRST_SAL_BY_DEPT
————————— ——————————————  —————————————— ————————— ——————— —————————————————
Eckhardt      Emily                   10 07-JUL-04  100000            100000
Newton        Donald                  10 24-SEP-06   80000            100000
James         Betsy                   10 16-MAY-07   60000            100000
Friedli       Roger                   10 16-MAY-07   60000            100000
Michaels      Matthew                 10 16-MAY-07   70000            100000
Dovichi       Lori                    10 07-JUL-11                    100000
peterson      michael                 20 03-NOV-08   90000             90000
leblanc       mark                    20 06-MAR-09   65000             90000
Jeffrey       Thomas                  30 27-FEB-10  300000            300000
Wong          Theresa                 30 27-FEB-10   70000            300000
Newton        Frances                    14-SEP-05   75000             75000


In the Lead and Lagging Behind

Its  common requirement to get access to record which is precedes or follow the current row .  By using the lead and lagging function one can obtain the side by side view of current row

SQL> select last_name, first_name, department_id, hire_date,    
  2         LAG(hire_date, 1, null) over (partition by department_id
  3                                 order by hire_date) prev_hire_date
  4    from employee
  5  order by department_id, hire_date, last_name, first_name;

LAST_NAME     FIRST_NAME              DEPARTMENT_ID HIRE_DATE PREV_HIRE
————————— —————————————— —————————————————————————— ————————— —————————
Eckhardt      Emily                              10 07-JUL-04
Newton        Donald                             10 24-SEP-06 07-JUL-04
Friedli       Roger                              10 16-MAY-07 24-SEP-06
James         Betsy                              10 16-MAY-07 16-MAY-07
Michaels      Matthew                            10 16-MAY-07 16-MAY-07
Dovichi       Lori                               10 07-JUL-11 16-MAY-07
peterson      michael                            20 03-NOV-08
leblanc       mark                               20 06-MAR-09 03-NOV-08
Jeffrey       Thomas                             30 27-FEB-10
Wong          Theresa                            30 27-FEB-10 27-FEB-10
Newton        Frances                               14-SEP-05

11 rows selected.

LAG(column | expression, offset, default)

Offset is a positive integer that defaults to a value of 1. This parameter tells the LAG function how many previous rows it should go back. A value of 1 means, “Look at the row immediately preceding the current row within the current window.” Default is the value you want to return if the offset value (index) is out of range for the current window. For the first row in a group, the default value will be returned.

RATIO_TO_RAPORT :
Business usage often need to report on percentage .sales  amounts, overall cost and annual  salries.
“What percentage of the total annual salary allotment does each employee receive?” The syntax for the RATIO_TO_REPORT analytic function is 
RATIO_TO_REPORT( column | expression)

Code Listing 9: Use RATIO_TO_REPORT to obtain the percentage of salaries 
SQL> select last_name, first_name, department_id, hire_date, salary, 
round(RATIO_TO_REPORT(salary) over ()*100, 2) sal_percentage
  2    from employee
  3  order by department_id, salary desc, last_name, first_name;

LAST_NAME      FIRST_NAME    DEPARTMENT_ID  HIRE_DATE  SALARY  SAL_PERCENTAGE
———————————  ————————————   —————————————— ——————————  ——————  ——————————————
Dovichi        Lori                     10  07-JUL-11
Eckhardt       Emily                    10  07-JUL-04  100000          10.31
Newton         Donald                   10  24-SEP-06   80000           8.25
Michaels       Matthew                  10  16-MAY-07   70000           7.22
Friedli        Roger                    10  16-MAY-07   60000           6.19
James          Betsy                    10  16-MAY-07   60000           6.19
peterson       michael                  20  03-NOV-08   90000           9.28
leblanc        mark                     20  06-MAR-09   65000            6.7
Jeffrey        Thomas                   30  27-FEB-10  300000          30.93
Wong           Theresa                  30  27-FEB-10   70000           7.22
Newton         Frances                      14-SEP-05   75000           7.73

 Note the analytical function in this query entire set of rows as window , because over doesn't specify any order by clause

or additional windowing clause.

Use RATIO_TO_REPORT to obtain the percentage of salaries, by department 
SQL> select last_name, first_name, department_id, hire_date, salary, 
round(ratio_to_report(salary)
  2         over(partition by department_id)*100, 2) sal_dept_pct
  3    from employee
  4  order by department_id, salary desc, last_name, first_name;

LAST_NAME      FIRST_NAME    DEPARTMENT_ID  HIRE_DATE  SALARY  SAL_DEPT_PCT
——————————  —————————————  ———————————————  —————————  ——————  ————————————
Dovichi        Lori                     10  07-JUL-11
Eckhardt       Emily                    10  07-JUL-04  100000        27.03
Newton         Donald                   10  24-SEP-06   80000        21.62
Michaels       Matthew                  10  16-MAY-07   70000        18.92
Friedli        Roger                    10  16-MAY-07   60000        16.22
James          Betsy                    10  16-MAY-07   60000        16.22
peterson       michael                  20  03-NOV-08   90000        58.06
leblanc        mark                     20  06-MAR-09   65000        41.94
Jeffrey        Thomas                   30  27-FEB-10  300000        81.08
Wong           Theresa                  30  27-FEB-10   70000        18.92
Newton         Frances                      14-SEP-05   75000          100

11 rows selected.
 
 
 In this query the analytical function use the row set from window created on departmentId 
aover defines the window on Department-id
 
 




Saturday, 7 November 2015

Having Sums, Averages, and Other Grouped Data

When you strive to get an  average 

1. The business requirement is what is current average salary for all Employees.

select AVG(salary) from employee;
 
  The avg aggregate function sum up the salary values and then divides it by total number of employee
  records those doesn't have the NULL salary .
 
  Thus the avg function ignores the NULL values 
 
  To get the business required answer substitute the Non NULL value for NULL value.
 
   
select AVG(NVL(salary,0)) from employee;
 
  This will return the exact average salary of all employee.
 
 
The Difference between Count(*) and  Count(Coulmn_name) 
 

 The count (*) return the all the records which satisfy the  query condition and count(*)  does not ignore  the null value, however the count(Column_name)  ignores the null records from the count.


Categorization and aggregation  of data.

  The group by clause enables  you to collect the data from multiple records and tclub it by one or more columns.
 The Aggregate function and the group by clause used to tandem  to determine the aggregate value for every group.

 count of employees in each department

select COUNT(employee_id), department_id
    from employee
    GROUP BY department_id
    ORDER BY department_id;


When the group by is followed by the order by then the clumn listed in the order should be listed in the select  , otherwise it will flag an error message.
  similarly  if the column listed in the group by should be listed in the select.

--,ASC, DESC, NULLS FIRST, and NULLS LAST options behave and how null values are handled by default in an ORDER BY clause


HAVING the last word   
   
 Just like the select list can use the where clause to filter records from the result set those satisfying the condition 
  mentioned in the where clause , similarly to filter the result of group by clause (Categorized data)   the having function is used .
 

 

Friday, 6 November 2015

Purging the SOA Infra

This is very useful and straight forward process to clean up SOA database schema. In real world , server are receiving millions of requests in a day and keeping these all data as instances in SOA Suite database schema is very costly. It can affect a performance of the server up to some extent. After few days or month probably you will start receiving table space errors as allotted all the table sapce is already been used by the instances created within SOA database schema. For this reason you need to plan your tablesapce accordingly and generally it should be in between 50 GB - 80 GB in loaded server. And still it requires regular purging for data on the SOA database.

What data does Oracle SOA Suite 11g (PS6 11.1.1.7) store?

Composite instances utilising the SOA Suite Service Engines (BPEL, mediator, human task, rules, BPM, OSB, EDN etc.) will write data to tables residing within the SOAINFRA schema. Each of the engines will either write data to specific engine tables (e.g. the CUBE_INSTANCE table is used solely by the BPEL engine) or common tables that are shared by the SOA Suite engines such as the AUDIT_TRAIL table.

Which data will be purged by the Oracle SOA Suite 11g (PS6 11.1.1.7) purge script?

The purge script will delete composite instances that are in the following states:

Completed

Faulted
Terminated by user
Stale
Unknown
The purge script will NOT delete composite instances that are in the following states:

Running (in-flight)
Suspended
Pending Recovery

1. First of all you will required Repository creation utility for 11.1.1.4. This installable contain the all required purging script provided by oracle to purge the database schema  You can find the purge script at location RCU_HOME/rcu/integration/soainfra/sql/soa_purge


2. In SQL*Plus, connect to the database AS SYSDBA:

3. Execute the following SQL commands:
                      GRANT EXECUTE ON DBMS_LOCK to dev_soainfra;
                      GRANT CREATE ANY JOB TO dev_soainfra;

4. RCU_HOME/rcu/integration/soainfra/sql/soa_purge/soa_purge_scripts.sql


6.  execute below SQL block and description of each variable is given below

    min_creation_date : minimum date when instance was created
    max_creation_date : Maximum date when instance was created
    batch_size :Batch size used to loop the purge. The default value is 20000.
    max_runtime :Expiration at which the purge script exits the loop. The default value is 60. This value is specified in minutes.
    retention_period :Retention period is only used by the BPEL process service engine only (in addition to using the creation time parameter). The default value is null
    purge_partitioned_component  : Users can invoke the same purge to delete partitioned data. The default value is false


DECLARE

   MAX_CREATION_DATE timestamp;
   MIN_CREATION_DATE timestamp;
   batch_size integer;
   max_runtime integer;
   retention_period timestamp;

BEGIN

   MIN_CREATION_DATE := to_timestamp('2011-06-23','YYYY-MM-DD');
   MAX_CREATION_DATE := to_timestamp('2011-07-03','YYYY-MM-DD');
    max_runtime := 15;
    retention_period := to_timestamp('2011-07-04','YYYY-MM-DD');
   batch_size := 5000;
     soa.delete_instances(
     min_creation_date => MIN_CREATION_DATE,
     max_creation_date => MAX_CREATION_DATE,
     batch_size => batch_size,
     max_runtime => max_runtime,
     retention_period => retention_period,
     purge_partitioned_component => false);
  END;


 Here is very important to note that this script provided is able to delete instances from database schema however it will not free up the memory of that table / tablespace.

For freeing up the memory you can try this option below on tables.

alter table enable row movement.
alter table shrink space;

JBO-27024: Failed to validate a row with key oracle.jbo.Key

Seems that the key you use is a composite key and the first attribute in this key is null, which is why the row cannot be retrieved



JBO-27024: Failed to validate a row with key oracle.jbo.Key usually occurs
1. Not providing value for a mandatory attribute.
2. Incorrect value for an attribute of different data type
3. any of your other validation rules failed on EO.




the validation is failing because the id attribute may not be getting created properly. Because of this it could not validate the row as the id is null. row.validateEntity() will help to identify the error by calling it when you commit the record and check where exactly the error pops up.
4. when the primary key is not based on a sequence and depends on the composite. The solution is to create a surrogate key. If you want to override the db constraint you must have a surrogate key populated programmatic way every time a new row is created. So that the data is unique all the time and proceed with a customized error message for each attribute, make the surrogate key hidden and read only.
if you want to suppress the validation then use skipValidation=true in pageDef or have the immediate set to true.

use the thread.dumpstack(); which will let you know which attribute is updating with nulll value