X

Everything about Table Scans including offload to Exadata, Big Data SQL, and External Tables

  • March 2, 2020

Smart Scan and Recursive queries

Roger Macnicol
Software Architect

Since Christmas I have been asked to investigate two different "failures to use Smart Scan". It turns out they both fell into the same little known restriction on the use of Direct Read. Smart Scan critically depends on Direct Read in order to read the synthetic output blocks into private buffers in PGA so with Direct Read disabled Smart Scan is also disabled. In these two cases the restriction is on using Direct Read on Serial Recursive queries.

Case 1: Materialized View Refresh

A customer asked me to investigate why his MView refresh was running slowly and was failing to use Smart Scan. He had used 'trace[NSMTIO] disk=highest' which showed the cause as:

Direct Read for serial qry: disabled(::recursive_call::kctfsage:::)

but that didn't help him understand why that was happening and the terse but accurate reply to their SR "force parallel query" didn't help him understand why either. Good DBAs want to understand, not just have rules to apply and I appreciate that.

Case 2: From Containers() clause

The From Containers() clause is a less well known feature of Containers databases that lets you query a table in one or more a Pluggable databases. So, for example, if I connect to my CDB directly:

sqlplus sys/***********@cdb1_pdb0 as sysdba

SQL> show pdbs
    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 CDB1_PDB1                      READ WRITE NO
         4 CDB1_PDB2                      READ WRITE NO

SQL> select count(*) from obj$;

  COUNT(*)
----------
     75130

SQL> select count(*) from containers(obj$);
  COUNT(*)
----------
    226726

SQL> select count(*) from containers(obj$) where con_id=3;
  COUNT(*)
----------
     75671
 

Checking the NSMTIO tracing we see:

Direct Read for serial qry: disabled(::recursive_call::kctfsage::always DR:::)

The From Containers() clause scans a fixed table of PDBs, if there is a predicate on CON-ID only those PDBs that match the predicate and used otherwise all PDBs are used. A recursive query is issued against each PDB in the fixed table that meets the CON_ID clause and the query results are the Union All of the results from each matching PDB. Now if we force the query to run in parallel either by setting the session parameter to force parallel:

SQL> alter session force parallel QUERY parallel ;

or by using the Containers hint to pass the hint to each recursive query in turn:

SQL> select /*+ CONTAINERS(DEFAULT_PDB_HINT='PARALLEL 2') */ count(*) from containers(obj$) where con_id=3;

and check the tracing we see:

NSMTIO: NSMTIO:kkfdtsc:DirectRead:

Problem fixed.

How to get Smart Scan to engage

When Direct Read was introduced a short list of exceptions was added and one was on recursive serial queries, we have filed ER Bug 30905358 - FROM CONTAINERS() TABLE SCANS ARE NOT OFFLOADED  to track the request to relax the restriction on recursive queries in a future release. In the meantime the correct solution is to add a parallel hint to the query or, if you are unable to modify the query, use the Force Parallel Query parameter.

Roger

Join the discussion

Comments ( 4 )
  • Thomas Baby Wednesday, March 4, 2020
    Do you need to have both session setting and the parallel hint? See your example.


    SQL> alter session force parallel QUERY parallel ;

    SQL> select /*+ parallel 2*/ count(*) from containers(obj$) where con_id=3;
  • Roger Thursday, March 5, 2020
    Thanks Thomas, you can use either the hint or by forcing session parallel query. But I realized the hint was incorrect in what I posted and have updated it to the correct hint.
  • Franky Monday, March 9, 2020
    Hey Roger,

    What about using _serial_direct_read = always at the session level for this execution? If the problem is the direct read on serial execution I guess that changing the parameter would also work, right?

    Regards,

    Franky
  • Roger Monday, March 9, 2020
    Unfortunately the serial recursive logic overrides the serial_direct_read param, forcing it to be PQ is the only way around the restriction.

    Roger
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.