HP Nonstop SQL/MX Release 3.1 Query Guide
Total Page:16
File Type:pdf, Size:1020Kb
TP663851.fm Page 1 Monday, October 17, 2011 11:48 AM HP NonStop SQL/MX Release 3.1 Query Guide Abstract This guide describes how to understand query execution plans and write optimal queries for an HP NonStop™ SQL/MX database. It is intended for database administrators and application developers who use NonStop SQL/MX to query an SQL/MX database and who have a particular interest in issues related to query performance. Product Version NonStop SQL/MX Releases 3.1 Supported Release Version Updates (RVUs) This publication supports J06.12 and all subsequent J-series RVUs and H06.23 and all subsequent H-series RVUs, until otherwise indicated by its replacement publications. Part Number Published 663851-001 October 2011 TP663851.fm Page 2 Monday, October 17, 2011 11:48 AM Document History Part Number Product Version Published 640323-001 NonStop SQL/MX Release 3.0 February 2011 663851-001 NonStop SQL/MX Release 3.1 October 2011 TP663851.fm Page 1 Monday, October 17, 2011 11:48 AM Legal Notices Copyright 2011 Hewlett-Packard Development Company L.P. Confidential computer software. Valid license from HP required for possession, use or copying. Consistent with FAR 12.211 and 12.212, Commercial Computer Software, Computer Software Documentation, and Technical Data for Commercial Items are licensed to the U.S. Government under vendor's standard commercial license. The information contained herein is subject to change without notice. The only warranties for HP products and services are set forth in the express warranty statements accompanying such products and services. Nothing herein should be construed as constituting an additional warranty. HP shall not be liable for technical or editorial errors or omissions contained herein. Export of the information contained in this publication may require authorization from the U.S. Department of Commerce. Microsoft, Windows, and Windows NT are U.S. registered trademarks of Microsoft Corporation. Intel, Itanium, Pentium, and Celeron are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries. Java is a registered trademark of Oracle and/or its affiliates. Motif, OSF/1, UNIX, X/Open, and the "X" device are registered trademarks and IT DialTone and The Open Group are trademarks of The Open Group in the U.S. and other countries. Open Software Foundation, OSF, the OSF logo, OSF/1, OSF/Motif, and Motif are trademarks of the Open Software Foundation, Inc. OSF MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THE OSF MATERIAL PROVIDED HEREIN, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. OSF shall not be liable for errors contained herein or for incidental consequential damages in connection with the furnishing, performance, or use of this material. © 1990, 1991, 1992, 1993 Open Software Foundation, Inc. This documentation and the software to which it relates are derived in part from materials supplied by the following: © 1987, 1988, 1989 Carnegie-Mellon University. © 1989, 1990, 1991 Digital Equipment Corporation. © 1985, 1988, 1989, 1990 Encore Computer Corporation. © 1988 Free Software Foundation, Inc. © 1987, 1988, 1989, 1990, 1991 Hewlett-Packard Company. © 1985, 1987, 1988, 1989, 1990, 1991, 1992 International Business Machines Corporation. © 1988, 1989 Massachusetts Institute of Technology. © 1988, 1989, 1990 Mentat Inc. © 1988 Microsoft Corporation. © 1987, 1988, 1989, 1990, 1991, 1992 SecureWare, Inc. © 1990, 1991 Siemens Nixdorf Informationssysteme AG. © 1986, 1989, 1996, 1997 Sun Microsystems, Inc. © 1989, 1990, 1991 Transarc Corporation. This software and documentation are based in part on the Fourth Berkeley Software Distribution under license from The Regents of the University of California. OSF acknowledges the following individuals and institutions for their role in its development: Kenneth C.R.C. Arnold, Gregory S. Couch, Conrad C. Huang, Ed James, Symmetric Computer Systems, Robert Elz. © 1980, 1981, 1982, 1983, 1985, 1986, 1987, 1988, 1989 Regents of the University of California. Printed in the US TP663851.fm Page 2 Monday, October 17, 2011 11:48 AM TP663851.fm Page i Monday, October 17, 2011 11:48 AM HP NonStop SQL/MX Release 3.1 Query Guide Index Examples Figures Tables Legal Notices What’s New in This Manual ix Manual Information ix New and Changed Information ix Changes to the SQL/MX 3.1 manual: ix Changes to the H06.22/J06.11 manual: ix Changes to the H06.20/J06.09 manual: x Changes to the H06.19/J06.08 Manual x Changes to the H06.16/J06.05 Manual x Changes to the H06.05 Manual xi About This Manual xiii Audience xiii Organization xiii Related Documentation xiv Notation Conventions xvii Hypertext Links xvii General Syntax Notation xvii Change Bar Notation xix 1. Compiling and Executing a Query Overview 1-1 How the Compiler Works 1-2 Compilation Steps 1-2 Parsing, Binding, and Normalizing 1-3 Query Plan Caching 1-5 Improving Query Performance 1-6 Factors That Can Affect Compile Time 1-19 How the Executor Processes the Plan 1-19 2. Accessing SQL/MX Data Access Methods 2-1 Hewlett-Packard Company—663851-001 i TP663851.fm Page ii Monday, October 17, 2011 11:48 AM Contents 3. Keeping Statistics Current Storage-Key Access 2-1 Index-Only Access 2-2 Alternate Index Access 2-4 Full Table Scan 2-5 Understanding Unexpected Access Paths 2-9 MultiDimensional Access Method (MDAM) 2-13 Specifying MDAM 2-13 Comparing MDAM With Single Subset Access 2-14 How MDAM Processes Queries 2-15 Influencing the Optimizer to Use MDAM 2-17 Controlling the Number of Key Columns Used by MDAM 2-18 MDAM’s Use of DENSE and SPARSE Algorithms 2-18 3. Keeping Statistics Current Histogram Statistics 3-1 Updating Histogram Statistics 3-2 Sampling and UPDATE STATISTICS 3-5 Performance Issues and Accuracy in Sampling 3-7 Collecting Statistics for Multiple Columns 3-8 Testing the Results of UPDATE STATISTICS 3-8 Testing the Results for SQL/MP Tables 3-8 Testing the Results for SQL/MX Tables 3-9 4. Reviewing Query Execution Plans Displaying Execution Plans 4-1 Using the EXPLAIN Function 4-1 Using the EXPLAIN Statement Shortcut 4-2 Using the Visual Query Planner 4-2 The Optimizer and Executor 4-3 Description of the EXPLAIN Function Results 4-3 Displaying Selected Columns of the Execution Plan 4-5 Extracting EXPLAIN Output From Embedded SQL Programs 4-6 Using the EXPLAIN Statement to Review the Execution Plan 4-6 Optimization Tips 4-9 Verifying DAM Access 4-13 Using the Visual Query Planner 4-14 Visual Query Planner Requirements 4-14 Getting Help for Visual Query Planner 4-14 Graphically Displaying Execution Plans 4-14 Reviewing Run-Time Statistics 4-21 HP NonStop SQL/MX Release 3.1 Query Guide—663851-001 ii TP663851.fm Page iii Monday, October 17, 2011 11:48 AM Contents 5. Forcing Execution Plans Simple Query Example 4-22 Using Measure 4-22 5. Forcing Execution Plans Why Force a Plan? 5-1 Checklist for Forcing Plans 5-2 Displaying the Optimized Plan 5-2 Reviewing the Optimized Plan 5-3 Translating the Operator Tree to Text Format 5-5 Using SHOWSHAPE and SET SHOWSHAPE to View the Text Format 5-5 Using Visual Query Planner to Get the Shape 5-6 Manually Writing the Shape 5-7 Writing the Forced Shape Statement 5-8 Scope of CONTROL QUERY SHAPE 5-8 Shaping Portions of an Operator Tree 5-9 Using Logical and Physical Specifications 5-9 Forcing Shapes on Views 5-10 What Happens if No Plan Is Returned? 5-10 Migrating Forced Shapes From NonStop SQL/MP 5-10 Forcing Group By Operations to the Data Access Manager 5-10 Forcing Parallel Plans 5-13 6. Query Plan Caching Overview 6-1 Types of Cacheable Queries 6-2 Examples of Cacheable Expressions 6-3 Examples of Queries That Are Not Cacheable 6-4 Choosing an Appropriate Size for the Query Cache 6-6 Query Plan Caching Statistics 6-6 SYSTEM_DEFAULTS Table Settings for Query Plan Caching Attributes 6-7 QUERYCACHE Function 6-9 QUERYCACHEENTRIES Function 6-11 Querying the Query Plan Caching Virtual Tables 6-13 Reviewing the Query Plan Caching Statistics With the DISPLAY_QC and DISPLAY_QC_ENTRIES Commands 6-15 7. SQL/MX Operators Operators 7-5 BLOCKED_UNION Operator 7-5 CALL Operator 7-6 HP NonStop SQL/MX Release 3.1 Query Guide—663851-001 iii TP663851.fm Page iv Monday, October 17, 2011 11:48 AM Contents 7. SQL/MX Operators CURSOR_DELETE Operator 7-8 CURSOR_UPDATE Operator 7-9 ESP_ACCESS Operator 7-11 ESP_EXCHANGE Operator 7-12 EXPLAIN Operator 7-16 EXPR Operator 7-16 EXPLAIN_CMD Operator 7-17 FILE_SCAN Operator 7-17 FILE_SCAN_UNIQUE Operator 7-19 FirstN Operator 7-22 HASH_GROUPBY Operator 7-22 HASH_PARTIAL_GROUPBY_LEAF Operator 7-24 HASH_PARTIAL_GROUPBY_ROOT Operator 7-25 HYBRID_HASH_ANTI_SEMI_JOIN Operator 7-26 HYBRID_HASH_JOIN Operator 7-27 HYBRID_HASH_SEMI_JOIN Operator 7-28 INDEX_SCAN Operator 7-29 INDEX_SCAN_UNIQUE Operator 7-31 INSERT Operator 7-33 INSERT_VSBB Operator 7-34 LEFT_HYBRID_HASH_JOIN Operator 7-35 LEFT_MERGE_JOIN Operator 7-36 LEFT_NESTED_JOIN Operator 7-38 LEFT_ORDERED_HASH_JOIN Operator 7-39 MATERIALIZE Operator 7-40 MERGE_ANTI_SEMI_JOIN Operator 7-41 MERGE_JOIN Operator 7-42 MERGE_SEMI_JOIN Operator 7-44 MERGE_UNION Operator 7-45 MultiUnion Operator 7-46 NESTED_ANTI_SEMI_JOIN Operator 7-46 NESTED_JOIN Operator 7-47 NESTED_SEMI_JOIN Operator 7-49 NEXTVALUEFOR Operator 7-50 NESTED_SEMI_JOIN Operator 7-50 7-50 ORDERED_HASH_JOIN Operator 7-52 ORDERED_HASH_SEMI_JOIN Operator 7-53 ORDERED_UNION Operator 7-54 HP NonStop SQL/MX Release 3.1 Query Guide—663851-001 iv TP663851.fm Page v Monday, October 17, 2011 11:48 AM Contents 8. Parallelism PACK Operator