Found 975 Articles for Software & Coding

How to UNPIVOT results in Oracle?

Kiran P
Updated on 05-Dec-2020 06:02:10

4K+ Views

Problem:You want to UNPIVOT results in Oracle.SolutionThe UNPIVOT clause is new for Oracle Database 11g and enables you to flip the columns into rows in the output from a query, and, at the same time, allow you to run an aggregation function on the data.Consider a table called customer which has below data stored inside.ExampleSELECT * FROM customers;Output1   tammy.bryant@internalmail   Tammy Bryant 2   roy.white@internalmail      Roy White 3   gary.jenkins@internalmail   Gary Jenkins 4   victor.morris@internalmail  Victor Morris 5   beverly.hughes@internalmail Beverly HughesIn the customer table we can see that full name has First name and last ... Read More

How to PIVOT results in Oracle?

Kiran P
Updated on 05-Dec-2020 06:00:42

15K+ Views

Problem:You want to PIVOT results in Oracle.SolutionThe PIVOT clause is new for Oracle Database 11g and enables you to flip the rows into columns in the output from a query, and, at the same time, allow you to run an aggregation function on the data.PIVOT is especially useful to look at overall trends in large amounts of data.We will be using sales data to demonstrate the usage.ExampleSELECT * FROM sales;Output12008   12  1998-02-28  6   37  552 1898.88 12008   18  1998-02-28  6   37  463 1592.72 12008   20  1998-02-28  6   37  2430    8359.2 12008   25 ... Read More

How to monitor temporary tablespace usage in Oracle?

Kiran P
Updated on 05-Dec-2020 05:58:55

10K+ Views

Problem:You want to monitor the usage of the temporary tablespace in Oracle.Solution:We can use the following query to find out the used and free space in a temporary tablespace.We will begin with identifying temporary table space names.ExampleSELECT tablespace_name FROM dba_tablespaces WHERE contents = 'TEMPORARY';OutputTEMPNext up we will use below SQL to identify the used and free space in temporary tablespace.ExampleSELECT * FROM   (SELECT a.tablespace_name,     SUM(a.bytes/1024/1024) allocated_mb   FROM dba_temp_files a   WHERE a.tablespace_name = 'TEMP'   GROUP BY a.tablespace_name   ) x,   (SELECT SUM(b.bytes_used/1024/1024) used_mb,     SUM(b.bytes_free      /1024/1024) free_mb   FROM v$temp_space_header b ... Read More

How to identify recent WAIT events in a Oracle database ?

Kiran P
Updated on 05-Dec-2020 05:57:43

9K+ Views

Problem:You want to find out the recent important waits in your database, as well as the users, SQL statements, and objects that are responsible for most of those waits.SolutionOracle provides us with V$ACTIVE_SESSION_HISTORY to get information about the most common wait events, and the SQL statements, database objects, and users responsible for those waits.SQL to find the wait eventsExampleSELECT event,   SUM(wait_time + time_waited) total_wait_time FROM v$active_session_history GROUP BY event ORDER BY total_wait_time DESC;To find out the users who experienced the most waits we can use below SQL.ExampleSELECT s.sid,   s.username,   SUM(a.wait_time + a.time_waited) total_wait_time FROM v$active_session_history a,   ... Read More

How to identify blocked and blocking sessions in Oracle ?

Kiran P
Updated on 05-Dec-2020 05:56:52

26K+ Views

Problem:You’d like to identify the blocking and the blocked sessions in your database.SolutionWhen we see an enqueue wait event in an Oracle database, the chances are that there is some thing locking or holding up some sessions from executing their SQL statements. When a session waits on an “enqueue” wait event, that session is waiting for a lock that is held by a different session. We can issue the following command to view information about the blocked and the blocking sessions in ORacle.ExampleSELECT DECODE(request, 0, 'Holder: ', 'Waiter: ') || sid sess,   id1,   id2,   lmode,   request, ... Read More

How to use Grouping clause to handle NULLS in Oracle?

Kiran P
Updated on 05-Dec-2020 05:55:47

1K+ Views

Problem:You want to use GROUPING() with ROLLUP, CUBE and SETS functions.SolutionThe GROUPING() function accepts a column and returns 0 or 1. This function returns 1 when the column value is null and returns 0 when the column value is non null. However, GROUPING() is used only in queries that use ROLLUP or CUBE. GROUPING() is very useful when you want to display a value when a null would otherwise be returned.GROUPING() with a Single Column in a ROLLUPSQL to pass a column to ROLLUPThe last row in the below example will result total/sum of fees. This total is represented as ... Read More

How to identify SQL queries with the most waits in Oracle?

Kiran P
Updated on 05-Dec-2020 07:15:11

2K+ Views

Problem:You want to identify the SQL statements responsible for the most waits in your database.SolutionWe can use below SQL statement to identify SQL causing problem.The below query will rank SQL statements that ran during the past 30 minutes and display them as per the total time waited by each query.ExampleSELECT ash.user_id,   u.username,   s.sql_text,   SUM(ash.wait_time + ash.time_waited) ttl_wait_time FROM v$active_session_history ash,   v$sqlarea s,   dba_users u WHERE ash.sample_time BETWEEN sysdate - 60/2880 AND sysdate AND ash.sql_id  = s.sql_id AND ash.user_id = u.user_id GROUP BY ash.user_id,   s.sql_text,   u.username ORDER BY ttl_wait_time ;When you have a performance ... Read More

How to gather extended query execution stats in Oracle?

Kiran P
Updated on 05-Dec-2020 05:52:55

517 Views

Problem:You want to gather extended explain plan statistics for a specific query.SolutionWe can use the GATHER_PLAN_STATISTICS hint. This hint when placed within a query at runtime, will generate extended runtime statistics. It basically have two steps.Execute the query with the gather_plan_statistics hint.Use dbms_xplan.display_cursor to display the results.ExampleSELECT /*+ gather_plan_statistics */       city,       round(avg(fees)) avg_fees,       min(fees) min_fees,       max(fees) max_fees  FROM students e, departments d, locations l WHERE e.department_id = d.department_id   AND l.location_id = d.location_id GROUP BY city;OutputSouthlake   5760    4200    9000 Seattle     10701   6900 ... Read More

How to change the JOIN Method in Oracle ?

Kiran P
Updated on 05-Dec-2020 05:49:23

576 Views

Problem:You have a performance issue with a queryhaving JOIN conditions on multiple tables, and you wish to override the join type by placing the appropriate hint in the query.Solution:There are three possible types of joins: nested loops, hash, and sort merge.Nested Loops Join Hint:  To invoke a nested loops join, use the USE_NL hintExampleEXPLAIN PLAN FOR SELECT /*+ use_nl(students, departments)  */  first_name, department_name   FROM students e, departments d  WHERE e.department_id = d.department_id;   SELECT * FROM table(DBMS_XPLAN.DISPLAY (FORMAT=>'BASIC'));Output---------------------------------------------------------- | Id  | Operation                    | Name             ... Read More

How to change the JOIN order in Oracle?

Kiran P
Updated on 04-Dec-2020 11:07:18

2K+ Views

Problem:You have a performance issue with a queryhaving JOIN conditions on multiple tables, and the Oracle optimizer is not choosing the join order you wanted.Solution:Oracle has two hints the ORDERED hint, and the LEADING hint that can be used to influence the join order used within a query.ORDERED HintYou are running a SQL to join two tables, student and departments, as you want to get the department names for each student. By placing an ORDERED hint into the query, you can see how the hint alters the execution access path.ExampleEXPLAIN PLAN FOR SELECT first_name, department_name   FROM students e, departments ... Read More

Advertisements