0% found this document useful (0 votes)
5 views18 pages

Oracle PLSQL Int Class Notes

The document provides a comprehensive overview of various SQL and PL/SQL concepts, including analytical functions like ROW_NUMBER, RANK, and DENSE_RANK, as well as cursor operations and performance tuning techniques. It also covers database normalization rules, materialized views, triggers, and table functions, along with examples of SQL queries and optimization hints. Additionally, it discusses job scheduling in AutoSys and UNIX shell scripting commands relevant to database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Oracle PLSQL Int Class Notes

The document provides a comprehensive overview of various SQL and PL/SQL concepts, including analytical functions like ROW_NUMBER, RANK, and DENSE_RANK, as well as cursor operations and performance tuning techniques. It also covers database normalization rules, materialized views, triggers, and table functions, along with examples of SQL queries and optimization hints. Additionally, it discusses job scheduling in AutoSys and UNIX shell scripting commands relevant to database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Analytical functions

 ROW_NUMBER – Returns the sequential number of a row within a partition of a


result set, without any gaps in the ranking. The rank of a row is one plus the
number of distinct ranks that come before the row in question.
 RANK – Returns the rank of each row within the partition of a result set. The rank
of a row is one plus the number of ranks that come before the row in question.
 DENSE_RANK – Returns the rank of rows within the partition of a result set,
without any gaps in the ranking. The rank of a row is one plus the number of
distinct ranks that come before the row in question.
 NTILE – Distributes the rows in an ordered partition into a specified number of
groups. The groups are numbered, starting at one. For each row, NTILE returns
the number of the group to which the row belongs.
Select NTILE(3) over ( order by Grade) from student_grades;

 LEAD – Lead (expression, offset (1), out of bounds value(null) ) over (partition by
expression order by expression)
 LAG - Lag (expression, offset (1), out of bounds value(null) ) over (partition by
expression order by expression)
Cursor

 Cursor – BULK COLLECT, passing parameters to cursors

BULK COLLECT with FORALL and NESTED tables

L_eligible_ids (indx).employee_id := 1;  to refer to a column of an index in pl/sql table

BULK COLLECT with RECORD


BULK COLLECT with VARRAY
VARRAYs are BOUNDED (Limited rows) and DENSE (no gaps between rows)

begin

End;
Reference Cursor & Cursor attributes:

****** SYS_REFCURSOR is a oracle provided weak cursor ******


Passing parameters to Dynamic SQL
Materialized views & refresh types

Create or replace FORCED view …. What do you mean by forced?

What is a query re-write on materialized view?

The first method matches the SQL text of the query to the SQL text of the materialized view definition. If the first
method fails, then the optimizer uses the more general method in which it compares joins, selections, data columns,
grouping columns, and aggregate functions between the query and materialized views.

 Query rewrite must be enabled for the session.


 A materialized view must be enabled for query rewrite.
 The rewrite integrity level should allow the use of the materialized view. For example, if a materialized view is
not fresh and query rewrite integrity is set to ENFORCED, then the materialized view is not used.
PL/SQL

Exceptions – pragma init exception

Oracle provided exceptions (few examples)

o TOO_MANY_ROWS
o DIVIDE_BY_ZERO
o NO_DATA_FOUND
o INVALID_CURSOR
o CURSOR_ALREADY_OPEN
SQL performance tuning

Driving_site()

o The driving_site() hint forces query execution to be done at a different site than the initiating instance.
Especially when the remote table is much larger than local table.
o This will save the back-and-forth network traffic and enhances SQL query performance

Fast full table scan - index_ffs()

 Nested loops – each row of outer table will be matched against target table
 Dbms_metadata.get_ddl(‘OBJECT_NAME’,’OBJECT_TYPE’,’OBJECT_OWNER’);
 Dbms_stats.gather_table_stats(owner,schema_name,% of sample size)
 Copy table stats?
dbms_stats.copy_table_stats(Owner, table_name,Source_partition_name ,target_partition_name);
Performance tuning did for TFS:

 Redesign ETL:
o partition copy with CTAS & PEL
o introduce staging area for aggr
o implement NOLOGGING & PARALLEL for temp table creation
o replace delete with truncate
o Drop and recreate indexes as part of ETL pre and post processes
o Convert informatica sequence into DB sequence using oracle 12c feature
o Use table partitions

 Rewrite SQL – ex: replace update statements with Merge statements

 DB optimization –
o implement PARALLEL & NOLOGGING for index creation
o implement DOP on DB tables
 alter table table_name parallel 4
 alter table table_name parallel DEFAULT
 DB configuration parameter PARALLEL_DEGREE_LIMIT
o gather table stats as part of ETL post load process
o copy table stats for initial loads
o incremental stats gathering

 SQL optimization:
o Using optimizer hints: PARALLEL, INDEX, NO_INDEX,NO_NL,USE_NL, USE_HASH,
NO_USE_HASH,USE_MERGE, NO_USE_MERGE
DB concepts:

 Normalization rules

First Normal Form (1NF)

As per First Normal Form, no two Rows of data must contain repeating group of information i.e each set of
column must have a unique value, such that multiple columns cannot be used to fetch the same row. Each
table should be organized into rows, and each row should have a primary key that distinguishes it as unique.

Second Normal Form (2NF)

As per the Second Normal Form there must not be any partial dependency of any column on primary key. It
means that for a table that has concatenated primary key, each column in the table that is not part of the
primary key must depend upon the entire concatenated key for its existence. If any column depends only on
one part of the concatenated key, then the table fails Second normal form

Third Normal Form (3NF)

Third Normal form applies that every non-prime attribute of table must be dependent on primary key, or we
can say that, there should not be the case that a non-prime attribute is determined by another non-prime
attribute. So this transitive functional dependency should be removed from the table and also the table must
be in Second Normal form. For example, consider a table with following fields.

4th Normal form (4NF)

Remove NULL attributes from parent entity


Type 3 dimension

Triggers
Table Functions

Table functions are used to return PL/SQL collections that mimic tables. They can be queried like a regular table by using the TABLE
function in the FROM clause. Regular table functions require collections to be fully populated before they are returned. Since
collections are held in memory, this can be a problem as large collections can waste a lot of memory and take a long time to return
the first row. These potential bottlenecks make regular table functions unsuitable for large Extraction Transformation Load (ETL)
operations. Regular table functions require named row and table types to be created as database objects.
Instead of Triggers

When you issue a DML statement such as INSERT, UPDATE, or DELETE to a non-updatable view, it will automatically
skip the DML statement and execute other DML statements instead.

Note that an INSTEAD OF trigger is fired for each row of the view that gets modified. you can create an INSTEAD
OF trigger for a view only. You cannot create an INSTEAD OF trigger for a table.

External tables

What are some of stand-out things you did with pl/sql?

 Customer CARE design & implementation


 batch jobs & microservices telemetry dashboard telemetry dashboard ** Denado
 Application modernization of commissions calculations
 Health check dashboard
 JIRA delivery status dashboard
 control table driven 300+ DCS_HIST fact table updates as part of USGCI compliance

Any other applications that you have designed?

 Generic framework for flattening JSON file into a CSV

How do you make sure that your jobs are running within SLA?

identify top SQL which are most time taking/most executed/most resource intensive:

SELECT
sql_fulltext,
sql_id,
elapsed_time,
child_number,
disk_reads,
executions,
first_load_time,
last_load_time
FROM v$sql
ORDER BY elapsed_time DESC

SELECT * FROM table (DBMS_XPLAN.DISPLAY_CURSOR ('&sql_id', &child));


Example of start with connect by prior SQL query:

SELECT last_name "Employee", CONNECT_BY_ISCYCLE "Cycle",


LEVEL, SYS_CONNECT_BY_PATH(last_name, '/') "Path"
FROM employees
WHERE level <= 3 AND department_id = 80
START WITH last_name = 'King'
CONNECT BY NOCYCLE PRIOR employee_id = manager_id AND LEVEL <= 4;

 START WITH specifies the root row(s) of the hierarchy


 CONNECT BY specifies the relationship between parent rows and child rows of the hierarchy
 The NOCYCLE parameter instructs Oracle Database to return rows from a query even if
a CONNECT BY LOOP exists in the data
 In a hierarchical query, one expression in condition must be qualified with the PRIOR operator to refer to the
parent row

Some of the optimizer hints:

 ALL_ROWS
 FIRST_ROWS
 PARALLEL
 FULL
 INDEX
 NO_INDEX
 INDEX_FFS
 NO_NL
 USE_NL
 USE_HASH
 NO_USE_HASH
 USE_MERGE
 NO_USE_MERGE
 DRIVING_SITE

What is a LISTAGG function?

LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the
measure column

SELECT LISTAGG(last_name, '; ')


WITHIN GROUP (ORDER BY hire_date, last_name) "Emp_list",
MIN(hire_date) "Earliest"
FROM employees
WHERE department_id = 30;

Emp_list Earliest
------------------------------------------------------------ ---------
Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares 07-DEC-02
What are some of the DICT tables that you have used?

V$SQL, V$SESSION, ALL_OBJECTS, ALL_TABLES, ALL_TAB_COLUMNS, ALL_CONSTRAINTS, ALL_TAB_PARTITIONS,


ALL_INDEXES, ALL_IND_COLUMNS, ALL_SEQUENCES, ALL_SYNONYMS, ALL_TRIGGERS, ALL_TRIGGER_COLS,
TAB_COL_STATISTICSs

What is TKPROF?

What are Common Table Expression (CTE) in Oracle?

A common table expression (CTE) is a named temporary result set that exists within the scope of a single statement and
that can be referred to later within that statement, possibly multiple times.

 A derived table can be referenced only a single time within a query. A CTE can be referenced multiple times. To
use multiple instances of a derived table result, you must derive the result multiple times.

 A CTE can be self-referencing (recursive).

 One CTE can refer to another.

 A CTE may be easier to read when its definition appears at the beginning of the statement rather than
embedded within it.

WITH RECURSIVE employee_paths (id, name, path) AS


(
SELECT id, name, CAST(id AS CHAR(200))
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.id, e.name, CONCAT(ep.path, ',', e.id)
FROM employee_paths AS ep JOIN employees AS e
ON ep.id = e.manager_id
)
SELECT * FROM employee_paths ORDER BY path;

SET SESSION cte_max_recursion_depth = 10;


SET SESSION max_execution_time = 10000000 (milli seconds);

What is a COALESCE function?

COALESCE function takes two or more compatible arguments and returns the first argument that is not null
select coalesce (smallintcol, bigintcol) from temp;

What is AUTHID CURRENT_USER in Oracle?


indicates that a package executes with the privileges of the current user

How is ROW SECURITY enabled in Oracle?

Define access groups  define data access role  assign access groups to data access roles  assign data access roles
to users

Select numbers 1 .. 100 from dual table

Select Rownum r
From dual
Connect By Rownum <= 100

How does a watcher work as per Autosys? Any specific scripting language or AutoSys does it?

JIL – Job Information Language is used for scripting in Autosys

insert_job: APP_BATCH_FW_JOB
job_type: FW
machine: some@hostname
days_of_week: mon,tue,wed,thru,fri
start_times: "09:00"
watch_file: /app/input/infeed.txt
watch_interval: 60 # every 60 sec it would check for the file
term_run_time: 15 # after 15 mins the job would be terminated if file is not found

insert_job: APP_BATCH_START_JOB
job_type: CMD
machine: some@hostname
command: bash /app/script/start.bash
condition: SUCCESS(APP_BATCH_FW_JOB)

insert_job: APP_INFORMARICA_BATCH_START_JOB
job_type: PMCMD
machine: some@hostname
command: pmcmd startworkflow -sv Integration_Service_Name -d Domain_Name -u User_Name -p Password -f
Folder_Name Workflow_Name
condition: SUCCESS(APP_INFORMATICA_BATCH_ JOB)

Any experience to create new UNIX shell scripts

#!/bin/bash
sqlplus -s user/pass@oracle_sid @file.sql

 Echo “Hello”
 cp source_file /path/to/target_file
 mv file_or_directory [target_directory]
 file [file_name] - checks a file type, such as TXT, PDF, or other
 zip [options] zip_file_name file1 file2
 unzip [options] zip_file_name
 tar [options] tar_file_name file1 file2
 tar -cfz archive.tar.gz fle1.txt file2.txt
 tar [options] tar_file_name
 cat file_name
 ls | grep "file.txt"
 sed 's/red/blue' colors.txt hue.txt
 head [options] file_name
 tail [options] file_name
 cut -d',' -f3-5 list.txt - extracts the third to fifth field from a comma-separated list
 diff file_name1 file_name2
 find path/to/folder -type f -name "file"

Difference between Count (*) and count (1)

COUNT(*) takes all columns to count rows and COUNT(1) counts using the first column: Primary Key. Thanks to
that, COUNT(1) is able to use index to count rows and it's much faster

Delimited file how we can fetch 1000 records single command

Option 1:

 Create external table


 Select from external table & bulk fetch

Option 2:
CALL SYSCS_UTIL.SYSCS_IMPORT_TABLE (null,'STAFF','c:\output\myfile.del',';','%',null,0);

SYSCS_UTIL.SYSCS_IMPORT_TABLE (
IN SCHEMANAME VARCHAR(128),
IN TABLENAME VARCHAR(128), IN FILENAME VARCHAR(32672),
IN COLUMNDELIMITER CHAR(1), IN CHARACTERDELIMITER CHAR(1),
IN CODESET VARCHAR(128), IN REPLACE SMALLINT
);

You might also like