Tactics for Pushing SQL to the Relational Databases
Total Page:16
File Type:pdf, Size:1020Kb
Tactics for Pushing SQL to the Relational Technical Paper Databases December 2008 Tactics for Pushing SQL to Relational Databases Table of Contents Introduction ....................................................................................................................1 Audience for this Paper .............................................................................................1 Purpose of the Paper.................................................................................................1 Scope of This Paper ..................................................................................................1 Overview....................................................................................................................1 Background for This Paper........................................................................................2 Important Concepts ...................................................................................................3 SAS/ACCESS® Software ....................................................................................3 SAS/ACCESS® Interface to ODBC.....................................................................3 SAS/ ACCESS Interface to OLE DB...................................................................3 Databases, Relational Databases, and Relational Database Management Systems (RDBMS) ............................................................................................................................4 Using SAS to Analyze Relational Database Information ....................................4 SAS/ACCESS Connectivity via LIBNAME Statements.......................................5 Standard SAS/ACCESS Connectivity versus Java Database Connectivity (JDBC) 5 Establishing SAS and Relational Database Connectivity..........................................6 Verify Compatibility of the Versions of the Operating System, the Relational Database, and SAS ...................................................................................................................................6 Relational Database Server ................................................................................6 Network ...............................................................................................................7 SAS® System.......................................................................................................7 Connect to the Relational Database with a Simple Relational Database Client Tool7 Oracle Connectivity.............................................................................................8 DB2 UDB Connectivity........................................................................................8 Teradata Connectivity .........................................................................................9 Connect to the Relational Database with SAS ..........................................................9 SAS to Oracle Connectivity.................................................................................9 SAS to DB2 Connectivity ..................................................................................10 SAS to Teradata Connectivity...........................................................................10 Common Problems You Might Encounter.........................................................10 Run a SAS Verification Program .............................................................................10 Testing Connectivity to a Relational Database Using a LIBNAME Statement .11 PROC SQL “connect to”....................................................................................11 Encoding Passwords.........................................................................................12 Determining Relational Database Client and Server Versions................................12 i Tactics for Pushing SQL to Relational Databases Oracle Client and Server Versions....................................................................12 DB2 Client and Server Versions .......................................................................13 Teradata Client and Server Versions................................................................13 The Basics: Things to Consider .................................................................................15 Introduction..............................................................................................................15 Minimizing Data Transfer.........................................................................................15 Limiting Columns...............................................................................................15 Limiting Rows....................................................................................................17 Improving Throughput by Using Multi-Row Operations...........................................20 Using the READBUFF Option...........................................................................20 Multi-Row Output Techniques...........................................................................21 Using the INSERTBUFF Option........................................................................21 Using the UPDATEBUFF Option ......................................................................23 Restricting SAS Functions.......................................................................................23 Selecting the SAS Function That SAS/ACCESS Can Pass to the Database...23 Using Expressions Instead of Functions...........................................................25 Assigning a SAS Function Value to a SAS Macro When Possible...................26 Constructing a WHERE Clause Correctly ...............................................................28 Sorting in the Relational Database..........................................................................29 Naming Issues.........................................................................................................30 SAS Names and Support for Database Names................................................30 PROC SQL Reserved Words............................................................................32 Database Reserved Words...............................................................................32 Differences in Behavior...............................................................................................32 NULL Values and MISSING Values ........................................................................32 Matching Differences with NULL or MISSING Values ......................................33 NULL and MISSING Sort Order........................................................................35 Character MISSING Represented as SAS Blank String and Its Ramifications 35 SAS Special MISSING Values..........................................................................36 Repeatable Observation or Row Order ...................................................................36 Repeatable Observation Order and SAS/ACCESS Spooling...........................37 Understanding the Differences between SAS and Database Native Data Types...38 Differences between Database and SAS Data Types ......................................38 Appropriate Choices for Database Columns that Contain Large Numeric Values 39 Handling Large Numeric Key Columns with SAS .............................................39 How Numeric Precision Can Affect WHERE clause or Join Evaluation ...........45 SAS Formats and SAS/ACCESS Default Formatting Rules.............................46 Relational Database VARCHAR Types ............................................................48 Date, Datetime, and Other Temporal Values....................................................49 ii Tactics for Pushing SQL to Relational Databases Large Object Data Types ..................................................................................52 Basic Tracing................................................................................................................53 Trace Information Shows Where Work Performed and Amount of Time Taken.....53 Basic SASTRACE SQL Trace ..........................................................................53 SASTRACE Threaded Read trace....................................................................57 Implicit Pass-Through .................................................................................................58 Introduction..............................................................................................................58 SAS and SQL Generation .......................................................................................58 SQL Generation through SAS/ACCESS...........................................................59 Using SAS PROC SQL .....................................................................................62 Criteria for Implicit Pass-Through............................................................................64 Select DISTINCT...............................................................................................65 Query Contains an SQL Aggregate or COALESCE Function ..........................66 Query Containing a GROUP BY Clause...........................................................66 Query Containing a HAVING Clause................................................................66 Query Containing an ORDER BY Clause.........................................................66 Query Contains an SQL Join ............................................................................66