- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 989 Articles for Software & Coding
1K+ Views
Problem:You want to identify the SQL statements consuming more resources in Oracle.Solution“V$SQLSTATS" view displays performance statistics for SQL statements that have recently executed. You can also use "V$SQL” and “V$SQLAREA" to report on SQL resource usage. "V$SQLSTATS” is faster and retains information for a longer period of time, but contains only a subset of the columns in “V$SQL" and "V$SQLAREA”.Exampleselect * from( select sql_text ,buffer_gets ,disk_reads ,sorts ,cpu_time/1000000 cpu_sec ,executions ,rows_processed from v$sqlstats order by cpu_time DESC) where rownum < 20;
2K+ Views
Problem:You want to view where Oracle SQL is taking time within a SQL execution plan.SolutionWith Oracle 11g version, we can view SQL execution plan progress while the SQL is running. The “V$SQL_PLAN_MONITOR” view contains a row for each step of a SQL statement’s execution plan. Below SQL will help to view the execution plan along with the progress.The “V$SQL_PLAN_MONITOR" provides you with information on the steps that are using the most resources. The statistics in "V$SQL_PLAN_MONITOR” are updated every second.We can also generate a real time text, HTML, or even a XML report of query progress within an execution plan ... Read More
833 Views
Problem:You want to know how much longer a long running SQL might ake to finish.SolutionWe can use “V$SESSION_LONGOPS" view to know the approximate time of a query left to execute. "V$SESSION_LONGOPS” view displays the status of various database operations that have been running for longer than six seconds. Please note that this view give you only a rough estimate of when a SQL might complete.Exampleselect a.username , a.opname , b.sql_text , to_char(a.start_time, 'DD-MON-YY HH24:MI') start_time , a.elapsed_seconds how_long , a.time_remaining secs_left , a.sofar , a.totalwork , round(a.sofar/a.totalwork*100, 2) percent from v$session_longops a ,v$sql ... Read More
2K+ Views
Problem:You want to monitor currently executing SQL statistics in Oracle.SolutionIf your database is Oracle Database 11g, you can use the following query to select from the “V$SQL_MONITOR” to monitor the near real time resource consumption of SQL queries.The statistics in “V$SQL_MONITOR” are updated every second. This helps us to view the resource consumption as it updates. These statistics are gathered by default when a SQL statement runs in parallel or consumes more than 5 seconds of CPU or I/O time.The “V$SQL_MONITOR" view includes a subset of statistics contained in the "V$SQL”, “V$SQLAREA", and "V$SQLSTATS” views.The “V$SQL_MONITOR" view displays real-time statistics ... Read More
2K+ Views
Problem:You want to limit the amount of resources a user can consume in your database.SolutionTo limit the resources, we can follow below steps.We can use below SQL statement to view the current setting of RESOURCE_LIMIT in our database.select name, value from v$parameter where name='resource_limit';Create a profile to limit the resources and assign it to a user. It won’t limit the CPU utilization though.ExampleCREATE PROFILE test_profile LIMIT SESSIONS_PER_USER 2 CPU_PER_SESSION UNLIMITED CPU_PER_CALL 300000 CONNECT_TIME ... Read More
436 Views
Problem:You want to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle.SolutionAny DML statement (an INSERT, UPDATE, or DELETE) executed on tables defined with integrity, helps to ensures that the rows in the tables maintain their integrity.Let’s look at some examples that show the enforcement of a primary key constraint. The customers table’s primary key is the customer_id column, which means that every value stored in the customer_id column must be unique. If you try to insert a row with a duplicate value for a primary key, the database returns the error ORA-00001, as in ... Read More
1K+ Views
Problem:You want to store the results of a SQL temporarily.SolutionWe can use the CREATE GLOBAL TEMPORARY TABLE statement to create a table that stores data temporarily for a session. Further, you can specify whether to retain temporary table data for a session or until a transaction commits. We can further Use ON COMMIT PRESERVE ROWS clause to specify the data be deleted at the end of the user’s session.ExampleCREATE GLOBAL TEMPORARY TABLE tmp_players ON COMMIT PRESERVE ROWS AS SELECT * FROM players WHERE coach_id IS NOT NULL;Global temporary tables store session private data that exists only for the duration of ... Read More
4K+ Views
Problem:You want to perform case-insensitive search in Oracle.SolutionOne way to deal with case issues is to use the built in UPPER and LOWER functions. These functions let you force case conversion on a string for a single operationExampleDECLARE full_name1 VARCHAR2(30) := 'roger federer'; full_name2 VARCHAR2(30) := 'ROGER FEDERER'; BEGIN IF LOWER(full_name1) = LOWER(full_name2) THEN DBMS_OUTPUT.PUT_LINE( full_name1 || ' and ' || full_name2 || ' are the same.'); END IF; END;In the above example the full_name1 and full_name2 are first converted into LOWER CASE then compared with each other resulting the outputroger federer ... Read More
5K+ Views
Problem:You want to define a function in Oracle using WITH clause.SolutionStarting with Oracle Database 12.1, you can define functions as well as procedures within the same SQL statement in which the SELECT statement appears. This allows the context switch between the PL/SQL and SQL engines by allowing both steps to take place in the SQL engine and, in turn, provides for a performance gain.The function or procedure needs to be defined using the WITH clause. Remember, In previous versions of the Oracle platform, only subqueries could be defined in the WITH clause.ExampleWITH FUNCTION func_amount(p_emailid IN VARCHAR2) RETURN NUMBER IS l_amt ... Read More
3K+ Views
Problem:You want to capture Oracle errors in PL/SQL.SolutionAll Oracle errors can be trapped with the help of the OTHERS exception handler. Let us take an example. We will be using students table to demonstrate the usage.Let us first look at the table structure.ExampleDESC students;OutputName Null Type ------------- -------- ------------ STUDENT_ID NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(40) PHONE_NUMBER VARCHAR2(20) JOIN_DATE ... Read More