V $ SESSION SQL _ID is blank. SQL _ID and sessionsql_id cannot be found

Source: Internet
Author: User

V $ SESSION SQL _ID is blank. SQL _ID and sessionsql_id cannot be found

Brother, have you ever encountered the embarrassment of querying V $ SESSION. SQL _ID but SQL _ID is null and cannot be found? Too many people have asked this question.

I believe that you do not have any good solutions from Baidu/GOOGLE. Today I will share a method to teach you how to grasp SQL (this method is based on ORACLE11G, and you can wash your bed at 10G ).

First, let's make an experiment:

SQL> select sid from v $ mystat where rownum = 1;
 
SID
----------
1150
 
SQL> update test set owner = 'bigsb 'where object_id <100;
 
98 rows updated

Execute an UPDATE statement in the SESSION 1150. do not submit the UPDATE statement.

SQL> select sid from v $ mystat where rownum = 1;
 
SID
----------
1338
 
SQL> update test set owner = 'SB 'where object_id <10;

Run another UPDATE in 1338. Because 1150 is not submitted, 1138 is in the row lock wait state.

In this case, use the following script to query the database:

SQL> select inst_id,  2         sid,  3         sql_id,  4         event,  5         blocking_session,  6         blocking_instance  7    from gv$session a  8   where blocking_session is not null;    INST_ID        SID SQL_ID        EVENT                                   BLOCKING_SESSION BLOCKING_INSTANCE---------- ---------- ------------- --------------------------------------- ---------------- ----------------         1       1338 852mvmth18w37 enq: TX - row lock contention           1150                 1         SQL> select sql_id from gv$session where inst_id=1 and sid=1150; SQL_ID-------------


Indeed, SQL _ID is null. Some people may say, I will query PREV_ SQL _ID. Well, let's try it. The SQL _ID is the SQL _ID of the transaction, not the SQL _ID of UPDATE.

SQL>  select prev_sql_id from gv$session where inst_id=1 and sid=1150; PREV_SQL_ID-------------9m7787camwh4m SQL> select sql_text from gv$sql where sql_id='9m7787camwh4m'; SQL_TEXT--------------------------------------------------------------------------------begin :id := sys.dbms_transaction.local_transaction_id; end;


So a lot of people suffer from this time and don't know what to do. Another method is provided.

SQL> select PREV_EXEC_START,USERNAME,MODULE,ACTION FROM GV$SESSION WHERE INST_ID=1 AND SID=1150; PREV_EXEC_START     USERNAME    MODULE               ACTION---------------     ------------------------------ -----------------------2015-04-10 18:01:44 SCOTT       PL/SQL Developer    Command Window - NewSQL> SELECT SQL_ID,SQL_TEXT,LAST_ACTIVE_TIME,MODULE,ACTION FROM GV$SQL WHERE INST_ID=1 AND LAST_ACTIVE_TIME=TO_DATE('2015-04-10 18:01:44','YYYY-MM-DD HH24:MI:SS'); SQL_ID        SQL_TEXT                                                                         LAST_ACTIVE_TIME MODULE              ACTION------------- -------------------------------------------------------------------------------- ---------------- ---------------------------------------------------------------- ----------------------------------------------------------------2syvqzbxp4k9z select u.name, o.name, a.interface_version#, o.obj#      from association$ a, us 2015/4/10 18:01:                                                                  6c9wx6z8w9qpu select a.default_selectivity                             from association$ a     2015/4/10 18:01:                    2xyb5d6xg9srh select a.default_cpu_cost, a.default_io_cost             from association$ a     2015/4/10 18:01:                                                                  d1s917pgj7650  update test set owner='BIGSB' where object_id<100                               2015/4/10 18:01: PL/SQL Developer    Command Window - New 


Now you can capture the SQL

Note:

1. In the case of high concurrency, multiple suspicious SQL statements may occur.

2. LAST_ACTIVE_TIME and PREV_EXEC_START may have a small error (in seconds)

In this case, please make your own judgment and never be too stupid.


select a.inst_id, a.sid, a.sql_id, b.sql_id, b.sql_text  from gv$session a, gv$sql b where a.inst_id = b.inst_id   and a.PREV_EXEC_START = b.LAST_ACTIVE_TIME   and a.USERNAME = b.PARSING_SCHEMA_NAME   and a.MODULE_HASH = b.MODULE_HASH   and a.ACTION_HASH = b.ACTION_HASH      select a.inst_id,       a.sid,       a.event,       a.sql_id,       b.sql_text          running_sql,       c.sql_in_session,       c.sql_id_in_v$sql,       c.sql_text          blocking_sql,       a.blocking_session,       a.blocking_instance  from gv$session a,       (select sql_id, sql_text          from (select sql_id,                       sql_text,                       row_number() over(partition by sql_id order by sql_id) as rn                  from gv$sql)         where rn = 1) b,       (select a.inst_id,               a.sid,               a.sql_id   sql_in_session,               b.sql_id   sql_id_in_v$sql,               b.sql_text          from gv$session a, gv$sql b         where a.inst_id = b.inst_id           and a.PREV_EXEC_START =b.LAST_ACTIVE_TIME           and a.USERNAME = b.PARSING_SCHEMA_NAME           and a.MODULE_HASH = b.MODULE_HASH           and a.ACTION_HASH = b.ACTION_HASH) c where a.sql_id = b.sql_id   and a.blocking_session is not null   and a.BLOCKING_SESSION = c.sid   and a.BLOCKING_INSTANCE = c.inst_id;







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.