INSERT EXPLAIN Is Not Valid Within a Multistatement Request - Teradata Database

SQL Data Manipulation Language

Product
Teradata Database
Release Number
15.10
Language
English (United States)
Last Update
2018-10-06
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata® Database

You cannot submit an INSERT EXPLAIN request as part of a multistatement request, though you can submit an INSERT EXPLAIN request for a multistatement request. If you attempt to submit a multistatement request that contains an INSERT EXPLAIN request, the multistatement request aborts and returns an error.

Note that while the INSERT EXPLAIN request in the following example superficially appears to be a valid statement in a multistatement request, it actually captures the query plan for a multistatement request that follows it, and is not itself part of that multistatement request. As a result, the request is treated like any other INSERT EXPLAIN and completes successfully.

     INSERT EXPLAIN INTO qcd SELECT * FROM d1.t1
     ;SELECT * FROM d1.t1;
 
       *** Insert completed. One row added.
       *** Total elapsed time was 1 second.

When a data parcel is submitted with an INSERT EXPLAIN request, the plan might be generated with peeking at USING and CURRENT_DATE or DATE values, or both. If any of these values are peeked, then the query plan shows them.

If no data parcel is submitted with an INSERT EXPLAIN request, the resulting plan is generated without peeking at USING, CURRENT_DATE, or DATE values, so it is a generic plan by definition. Note that the Visual Explain, Teradata System Emulation Tool, and Teradata Index Wizard tool do not accept USING data as input while capturing query plans using INSERT EXPLAIN requests unless those requests are submitted using BTEQ or Teradata SQL Assistant.

The Teradata Index Wizard internally generates plans for workload queries for the purpose of estimating workload costs, which are used to determine optimal index recommendations. When queries in the workloads specify USING request modifiers, the plan is generated without peeking at USING, CURRENT_DATE, or DATE values. Because of these factors, Request Cache peeking has no impact on the resulting index. Given that workload analyses should be independent of USING values, this behavior is correct.

The IN XML option for INSERT EXPLAIN provides a functionally equivalent representation of a query plan created by an INSERT EXPLAIN request that does not produce XML output.

INSERT EXPLAIN operations that require several minutes to be processed when you do not specify the IN XML option take only a few seconds to complete when you capture the information as an XML file. This is because an INSERT EXPLAIN … IN XML request performs only one insert operation by storing the entire output of the request as one or more 31,000 byte slices of a single XML file. When INSERT EXPLAIN inserts XML data into the QCD table XMLQCD, it also updates the QCD table SeqNumber, but the update of SeqNumber is a trivial operation.

When you specify the IN XML option, Teradata Database does not update any other QCD tables beyond XMLQCD and SeqNumber. This is unlike a standard INSERT EXPLAIN request where inserts must be made into multiple QCD tables.

In a single QCD table, XMLQCD has a marked performance advantage over a standard INSERT EXPLAIN QCD data collection operation. For the definition of the XMLQCD table, see SQL Request and Transaction Processing

A similar option for the BEGIN QUERY LOGGING and REPLACE QUERY LOGGING statements optionally creates a compact XML version of QCD information that can be stored in a single DBQL table, DBC.DBQLXMLTbl, making the features complementary to one another.

XMLPLAN query logging is not an alternative method for capturing the information captured by INSERT EXPLAIN requests.

  • In addition to differences in the content of the documents produced, these two methods have an important difference regarding the execution of the query.
  •  

    BEGIN QUERY LOGGING … XMLPLAN

    INSERT EXPLAIN …

    logs query plans for executed queries.

    captures query plans without executing the query.

    XMLPLAN logging is ideal when you want to record query plans for your executing workloads and using INSERT EXPLAIN requests to capture query plans for those workloads is too slow for your needs.

    If you are only tuning a query and do not want to execute it, XMLPLAN logging is not as useful as capturing the query plan for a request using INSERT EXPLAIN requests.

    In this case, executing an INSERT EXPLAIN INTO QCD_name IN XML request or an EXPLAIN IN XML SQL_request (see “Capturing EXPLAIN Text Output in XML Format” on page 554) is a more viable alternative.

  • Runtime information from the traditional DBQL tables is also captured for a logged plan.
  • XMLPLAN logging is more of an extension to query logging than an extension to the Query Capture Facility.
  • The following examples, with some exceptions, use the same SELECT request to show how the different syntax options of INSERT EXPLAIN produce different results.

    Each INSERT EXPLAIN request inserts the data for the query plan produced by the Optimizer for the SELECT request that it modifies directly into the specified QCD database (see “Query Capture Facility” in SQL Data Definition Language for documentation of the QCD tables).

    The output of this INSERT EXPLAIN request is referenced under the query plan name EmployeeSmithQuery in the TLE_Queries database. The system collects neither statistics nor demographics for this request.

         INSERT EXPLAIN 
         INTO TLE_Queries AS EmployeeSmithQuery
         SELECT emp_id, emp_address
         FROM employee
         WHERE emp_name = 'Smith';

    The output of this INSERT EXPLAIN request has a null query plan name in the TLE_Queries database because there is no query plan naming clause. The system collects neither statistics nor demographics for this request.

         INSERT EXPLAIN 
         INTO TLE_Queries
         SELECT emp_id, emp_address
         FROM employee
         WHERE emp_name = 'Smith';

    The output of this INSERT EXPLAIN request is referenced under the query plan name Employee Smith Query in the QCD database. The system collects neither statistics nor demographics for this request.

         INSERT EXPLAIN 
         INTO QCD AS "Employee Smith Query"
         SELECT emp_id, emp_address
         FROM employee
         WHERE emp_name = 'Smith';

    The output of this INSERT EXPLAIN request is referenced under the query plan name Wizard Test in the Wizard_QCD database. Statistics are collected for table_1 and column statistics are collected for column_2 and column_3 in table_1. The frequency of performance of the specified SQL SELECT request is 10 times per workload. No demographics are collected.

         INSERT EXPLAIN WITH STATISTICS FOR table_1 
         INTO Wizard_QCD AS "Wizard Test" FOR 10
         SELECT *
         FROM table_1
         WHERE table_1.column_2 = 10
         AND table_1.column_3 = 20
         FOR 10;

    The output of this INSERT EXPLAIN request is saved under the query plan name WizardTest2 in the Wizard_QCD database. Statistics are collected for the employee table and column statistics are collected for the emp_lastname column. No statistics are captured for the department table because the request specifies they are to be collected only for the employee table in the FOR table_name clause.

         INSERT EXPLAIN WITH STATISTICS FOR employee 
         INTO Wizard_QCD AS WizardTest2
         SELECT employee.first_name, department.dept_num
         FROM employee, department
         WHERE employee.emp_lastname = 'Smith' 
         AND   department.dept_name IN ('Engineering', 'Support');

    This INSERT EXPLAIN request returns an error because none of the tables specified in the table list are referenced in the specified query.

         INSERT EXPLAIN WITH STATISTICS FOR employee1 
         INTO Wizard_QCD AS WizardTest2
         SELECT employee.first_name, department.dept_num
         FROM employee, department
         WHERE employee.emp_lastname = 'Smith' 
         AND department.dept_name IN ('Engineering', 'Support');
     
        ****Failure 5644: No match found for specified tables in the request.

    This example uses an explicit sampling percentage of 80 to capture QCD statistics for the specified SELECT request:

         INSERT EXPLAIN WITH STATISTICS
         USING SAMPLE 80 PERCENT 
         INTO MyQCD AS query1
         SELECT t2.y3, t1.x3  
         FROM t1, t2  
         WHERE  t1.pi = t2.y2;

    Single‑column statistics recommendations are represented by a single row in the StatsRecs table. In this example there are two separate recommendations, one for the primary index on column a1 and one for the nonindexed column c1.

    After you run this request, you can then apply the generated recommendations for collecting statistics by running the two COLLECT STATISTICS requests that the system stores in the StatsDDL column of the StatsRec table in the QCD database.

    The definition for the only table referenced by the SQL request to be analyzed is as follows:

         CREATE TABLE t1 (
           a1 INTEGER, 
           b1 INTEGER, 
           c1 INTEGER);

    The following INSERT EXPLAIN request generates the single‑column statistics recommendations for the SQL request SELECT * FROM t1 WHERE c1=5.

         INSERT EXPLAIN INTO QCD 
         CHECK STATISTICS 
         SELECT * FROM t1 WHERE c1 = 5;

    The following SELECT request selects all the columns that contain the statistics recommendations for QueryID 1, which is the system‑assigned unique identifier for the row set in the QCD StatsRecs table that contains the recommendations:

         SELECT * 
         FROM QCD.StatsRecs 
         WHERE QueryID = 1 
         ORDER BY StatsID;

    The query returns the following report:

         QueryID           1
         StatsID           0
    DatabaseName D1
       TableName t1
         FieldID        1025
       FieldName A1
          Weight           3
        StatsDDL COLLECT STATISTICS D1.t1 COLUMN A1 ;
         QueryID           1
         StatsID           1
    DatabaseName D1
       TableName t1
         FieldID        1027
       FieldName C1
          Weight           3
        StatsDDL COLLECT STATISTICS D1.t1 COLUMN C1 ;

    Multicolumn statistics recommendations are represented by multiple rows in the StatsRecs table. Each column of the recommendation is stored in a separate row, sharing its StatsID value with the other columns in the set, each of which is represented by its own row.

    Note how the COLLECT STATISTICS request text is stored in the row corresponding to the lowest FieldID value. The StatsDDL value for the other rows is null. In this example, there is one recommendation for the multicolumn primary index.

    After you run this request, you can then apply the generated recommendations for collecting statistics by running the COLLECT STATISTICS request that the system stores in the StatsDDL column of the StatsRec table in the QCD database.

    The definition for the only table referenced by the SQL request to be analyzed is as follows:

         CREATE TABLE t2 (
           a2 INTEGER, 
           b2 INTEGER, 
           c2 INTEGER, 
           d2 INTEGER) 
         PRIMARY INDEX (a2, b2, c2);

    The following INSERT EXPLAIN request generates the multicolumn statistics recommendations for the SQL request SELECT * FROM t2.

         INSERT EXPLAIN INTO QCD 
         CHECK STATISTICS 
         SELECT * FROM t2;

    The following SELECT request selects all the columns that contain statistics recommendations for QueryID 2, which is the system‑assigned unique identifier for the row set in the QCD StatsRecs table that contains the recommendations:

         SELECT * 
         FROM QCD.StatsRecs 
         WHERE QueryID = 2 
         ORDER BY StatsID, FieldID;

    The query returns the following report:

         QueryID           2
         StatsID           0
    DatabaseName D1
       TableName t2
         FieldID        1025
       FieldName A2
          Weight           3
        StatsDDL COLLECT STATISTICS D1.t2 COLUMN (A2 ,B2 ,C2 ) ;
         QueryID           2
         StatsID           0
    DatabaseName D1
       TableName t2
         FieldID        1026
       FieldName B2
          Weight           3
        StatsDDL ?
         QueryID           2
         StatsID           0
    DatabaseName D1
       TableName t2
         FieldID        1027
       FieldName C2
          Weight           3
        StatsDDL ?

    There are three rows in the report: one for each of the columns that make up the primary index for the table. You can tell these are multicolumn statistics because all three rows share the same QueryId value (2) and the same StatsID value (0). Another sign that these are multicolumn statistics is that there is only one column of DDL text, and it is stored in the row with the lowest FieldID value (1025).

    This example produces one row of query capture output and stores it in XML format in the QCD table myqcd.XMLQCD.

         INSERT EXPLAIN INTO myqcd IN XML 
         SELECT * 
         FROM DBC.DBCInfoV;

    The following output shows a fragment of the row inserted into myqcd.XMLQCD table.

                 Id            1
               Kind            Q
                Seq            1
             Length         4501
               Text  <?xml version="1.0" encoding="UTF-8"?><QCF>
    <ElapsedSeconds>0.030<ElapsedSeconds><QueryID>8<QueryID>
    <User_Database><UDB_Key>1</UDB_Key><UDB_ID>256</UDB_ID>
    <MachineName>localhost</MachineName><UDB_Name>DBC</UDB_Name>
    </User_Database><Query><UDB_Key>1</UDB_Key><MachName>localhost
    </MachName><NumAMPs>2</NumAMPs><NumPEs>1<NumPEs><NumNodes>1
    <NumNodes><ReleaseInfo>13w.00.00.00</ReleaseInfo><VersionInfo>
    13w.00.00.00</VersionInfo>…

    This example produces one row of query capture output and stores it in XML format in the QCD table myqcd.XMLQCD. Because you specified the NODDLTEXT option, the system does not store the DDL SQL text related to the request.

         INSERT EXPLAIN INTO myqcd IN XML NODDLTEXT
         SELECT * 
         FROM DBC.DBCInfoV;

    For more information about index analysis, see:

  • “BEGIN QUERY LOGGING” and “REPLACE QUERY LOGGING” in SQL Data Definition Language.
  • “COLLECT DEMOGRAPHICS” on page 534
  • “COLLECT STATISTICS (QCD Form)” on page 537
  • “DUMP EXPLAIN” on page 547
  • “EXPLAIN Request Modifier” on page 552
  • “INITIATE INDEX ANALYSIS” on page 560
  • “RESTART INDEX ANALYSIS” on page 590
  • SQL Request and Transaction Processing
  • Database Design
  • Teradata Index Wizard User Guide
  • Teradata Viewpoint User Guide, chapter on Stats Manager
  • Teradata Visual Explain User Guide
  • Teradata System Emulation Tool User Guide