Fetching recent/historical queries run on a database from an application - oracle

I'm getting some database errors from an application related to unique constraints however none of the records have any duplicate values so I want to check what query was run that produced the error.
How can I check a history log of queries to pinpoint the problem? I'm using Oracle 12c

You can query the Oracle V$SQL view:
SELECT *
FROM v$sql s
WHERE PARSING_SCHEMA_NAME = 'FOO'
ORDER BY s.LAST_ACTIVE_TIME DESC nulls last;
In this example the result is already filtered by PARSING_SCHEMA_NAME which is the user name that executed the sql.
See Oracle Documentation on V$SQL for details

Related

query ORACLE database during springboot application no response

A sql like "SELECT COUNT(1) FROM tablename..." was executed by my springboot application with mybatis,but no response during a long time.
But when I executed the same sql directly in ORACLE,it worked and returned the response.
With executing these sql:
select event,count(*) from v$session_wait group by event order by 2 desc;
I found the 'latch:cache buffers chains' event.
I asked my frined and he told me that I can execute analyze table tablename compute statistics.
After executed the sql,the problem resolved,the select count sql executed by springboot application worked !
I feel puzzled,why the analyze sql worked?
Thank you.

how to find xml query in bi_publisher

I create a data_model in Bi_Publisher and set it to my RTF_template report.
the question is : how to find the query in database when I running my report?
mean:
I want to find the query that Database executing , not query in my data_model.
( or I want the query behind of report)
the query behind report:
select /* QUERY_SRC('datamodel: __weblogic_PYN_QSN_ACT_IND_IND_NEW_xdm,dataset:Person_IN') */ from P_crm.vw_person where id = 1 *
Database, how to find relation between data sets in bi_publisher?
Assuming you are running in an Oracle database, you can use v$sql.
select * from v$sql
Details on that here:
https://docs.oracle.com/cd/B19306_01/server.102/b14237/dynviews_2113.htm#REFRN30246
All the SQLs being run can be seen in the logs. Increase the logging level on OBIEE, and this will generate detailed logs, containing the presentation logs, but also detailed , performance improved logs running on the database. Its a bit hard to read, but you can search with the table names, if you know them.
There are some instructions here

get runtime execution plan of a query

I have an application that executes some sql queries. How can I get execution plan for currently executing query from sqlplus or some other oracle client? I can amend oracle session that is used in application, if that is necessary.
I do not want to use explain plan and execute that query by hand, I need to get actual execution plan that is used for query.
You can run explain plain on historical queries from the SGA -examples
And listing B.
Example:
SELECT username, prev_sql_id
FROM v$session
WHERE username = 'RDEVALL' -- example user
SQL ID returned
RDEVALL a1d4z5ruduzrd
RDEVALL avmbutuknvb6j
RDEVALL 75g0tqd9x743y
RDEVALL 5fnkh6c8mqqt3
RDEVALL 75g0tqd9x743y
Pick query ID and use here:
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('a1d4z5ruduzrd')); -- replace with sql ID as needed

How does oracle execute an sql statement?

such as:
select country
from table1
inner join table2 on table1.id=table2.id
where table1.name='a' and table2.name='b'
group by country
after the parse, which part will be executed first?
It looks like you want to know the execution plan chosen by Oracle. You can get that ouput from Oracle itself:
set serveroutput off
< your query with hint "/*+ gather_plan_statistics */" inserted after SELECT >
select * from table(dbms_xplan.display_cursor(null, null, 'last allstats'));
See here for an explanation how to read a query plan: http://download.oracle.com/docs/cd/E11882_01/server.112/e16638/ex_plan.htm#i16971
Be aware however that the choice of a query plan is not fixed. Oracle tries to find the currently best query plan, based on available statistics data.
There are plenty of places you can find the order in which SQL is executed:
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause
ORDER BY clause
But note that this is the "theoretical" order - SQL engines are allowed to perform the operations in other orders, provided that the end result appears to have been produced by using the above order.
If you install the free tool SQL*Developer from Oracle, then you can click a button to get the explain plan.
A quick explanation is at http://www.seeingwithc.org/sqltuning.html

Nhibernate Generate wrong SQL for Oracle with locking

yesterday I've been trying to make this code work inspite the fact it's just working fine with nhibernate and SQL server but when it come to oracle it generate wrong sql
UnitOfWork.Current.CreateCriteria<Bill>().Add(Restrictions.IsEmpty("ReqId"))
.SetMaxResults(BatchSize).SetLockMode(LockMode.Upgrade).List<Bill>();
the generated SQL will something like
Select * from
(Select bill_0.id,bill_0.BillNo ...... from Bill bill_0 where bill_0.reqId is Not null )
where ROWNUM < 10 for UPDATE of bill_0.ID
so i wont run because the allies bill_o is defined inside the inner sql statement so who got the solution ?
the correct sql would be something like this which i tried and worked on oracle db
Select bill_0.id,bill_0.BillNo ...... from Bill bill_0
where bill_0.reqId is Not null and ROWNUM < 10 for UPDATE of bill_0.ID
Since, as you say, NHibernate is generating invalid Oracle SQL, I suggest you file a bug with the NHibernate people. The SQL would work if the in-line view had been assigned an alias of "bill_0", or if the FOR UPDATE clause didn't use a table alias ("for UPDATE of ID"). Whether you can modify your NHibernate calls to make either of these happen I'm afraid I have no idea.

Resources