T3.3: Uses SQL with InterSystems IRIS

Knowledge Review - InterSystems IRIS Development Professional

1. Differentiates between embedded and dynamic SQL

Key Points

  • Embedded SQL: Compiled at class compile time with in-line code generation for optimal performance
  • Dynamic SQL: Prepared and executed at runtime using %SQL.Statement class
  • Privilege checking: Dynamic SQL enforces privileges; Embedded SQL does not
  • Use cases: Embedded for static queries, Dynamic for runtime-constructed queries
  • Performance: Embedded faster initially; both use cached queries for re-execution
  • Parameters: Dynamic uses ? or :var; Embedded uses :var host variables

Detailed Notes

Overview

Embedded SQL and Dynamic SQL represent two fundamental approaches to executing SQL in InterSystems IRIS.

Embedded SQL

  • Syntax: Uses the &sql() syntax
  • Compilation: Compiled at class compilation time, generating in-line ObjectScript code for maximum execution efficiency
  • Validation: Compiler validates syntax and checks for table existence at compile time, allowing early error detection
  • Privilege Checking: Does NOT perform privilege checking; assumes applications handle authorization before executing queries

Dynamic SQL

  • Implementation: Through the %SQL.Statement class, prepares and executes queries at runtime
  • Similar To: ODBC or JDBC programming but executes within the same process context as the database engine
  • Compile-Time Validation: Cannot validate at compile time; preprocessor macros cannot be used within them
  • Advantage: Flexibility to construct specialized queries based on user input or runtime conditions

Performance Comparison

  • Cached Queries: Both approaches support cached queries for improved re-execution performance
  • Initial Execution: Dynamic SQL slightly less efficient due to lack of in-line code generation
  • Subsequent Executions: Both benefit from query caching

Privilege Checking Behavior

  • Dynamic SQL: Enforces SQL privilege checking for ODBC, JDBC, and SQL Shell interfaces
  • Embedded SQL: Bypasses privilege checks entirely

Parameter Handling

  • Dynamic SQL: Accepts literal input values through ? placeholders and :var host variables
  • Embedded SQL: Uses input and output host variables exclusively (:var syntax)

When to Use Each Approach

  • Embedded SQL: When queries are known at compile time
  • Dynamic SQL: When queries need runtime construction

Internal Usage

Dynamic SQL is used internally by the SQL Shell, Management Portal Execute Query interface, and data import/export utilities.

Documentation References

2. Leverages IRIS-specific SQL features

Key Points

  • Arrow syntax (->): Navigate object references and child tables without explicit JOINs
  • TOP clause: Limit result set size with optimization benefits
  • Bitmap indexing: High-performance indexing for data warehousing applications
  • Object integration: Seamless mixing of relational and object access patterns
  • %CHECKPRIV: Verify SQL privileges programmatically before execution
  • TUNE TABLE: Gather table statistics for query optimizer

Detailed Notes

Overview

InterSystems IRIS provides several SQL extensions beyond SQL-92 standards that leverage its object-oriented architecture and multidimensional database engine.

Arrow Syntax (->)

  • Purpose: Enables implicit joins by navigating object references directly in SQL queries
  • Example: "SELECT Name, Company->Name FROM Sample.Employee" retrieves employee names and their company names without explicit JOIN
  • Compound Navigation: Supports chained references like Film->Category->CategoryName
  • Child Tables: Also works with child table references
  • Privilege Requirements: Requires SELECT privileges on referenced data in both tables, including column-level privileges on the ID field of referenced tables

TOP Clause

  • Purpose: Controls result set size and provides query optimization benefits
  • Optimization Change: When combined with ORDER BY, changes optimization from "fastest time to return all data" to "fastest time to return first row"
  • Ideal For: Paginated displays
  • Accepted Values: Integer literals, Dynamic SQL input parameters (?), or Embedded SQL host variables (:var)
  • TOP ALL: Allows ORDER BY in subqueries and CREATE VIEW statements without restricting row count
  • Caching Behavior: TOP values without parentheses are cached as parameter variables, allowing query reuse with different limits without re-preparation

Object Technology Integration

InterSystems IRIS integrates SQL tightly with its object technology, allowing relational and object access without sacrificing performance.

Additional IRIS-Specific Features

  • Bitmap Indexing: Optimized for data warehouse applications, providing exceptional performance for complex analytical queries
  • %CHECKPRIV Command: Enables programmatic privilege verification before executing queries

```sql -- %CHECKPRIV Example: SELECT %CHECKPRIV('SELECT') FROM Sample.Person -- Returns 1 if user has SELECT privilege, 0 otherwise ```

  • TUNE TABLE: Gathers table statistics (selectivity, extent size, map size) for the query optimizer
  • No Table Compression: Unlike traditional relational databases, IRIS applications do not require periodic table compression in deployed environments

3. Interprets query plans for optimization

Key Points

  • Show Plan: Display query execution strategy without running the query
  • Execution steps: Understand table access methods, index usage, and join strategies
  • Optimization indicators: Identify full table scans vs. index lookups
  • Performance metrics: Analyze costs, row estimates, and selectivity values
  • Validation: Test Embedded SQL syntax and verify table existence
  • SET executemode=deferred: Prepare queries without execution for plan review

Detailed Notes

Overview

Query plans are essential tools for understanding how InterSystems IRIS executes SQL statements and identifying optimization opportunities.

Show Plan Feature

  • Purpose: Displays the query execution strategy without actually running the query
  • Usage: In Management Portal SQL interface, input a query and click "Show Plan"
  • Validation: Validates syntax, checks entity existence, and views the planned execution strategy
  • Dynamic SQL: Set executemode=deferred, issue the query, then use SHOW STATEMENT to display the prepared statement and execution plan without consuming execution resources

Information Revealed by Query Plans

  • Table Access Methods: Full table scan vs. index scan
  • Index Selection Decisions: Which indexes the optimizer chose
  • Join Strategies: Nested loop, hash join, merge join
  • Estimated Row Counts: At each step of the plan
  • Order of Operations: Which tables are accessed first and how intermediate results are processed

Selectivity Values

  • Definition: Percentage of rows expected to satisfy each condition
  • Interpretation: Lower selectivity values indicate more selective conditions that filter more data
  • Source: The optimizer uses table statistics gathered by TUNE TABLE to estimate these values accurately

Interpreting Key Indicators

  • Full Table Scans: Appear when no suitable index exists or when scanning is more efficient than index lookups for high-selectivity conditions
  • Index Usage: Shows as "Index Used: indexname" in the plan
  • Cost Estimates: Help compare alternative query formulations

Embedded SQL Validation

  • Syntax Checking: Show Plan checks syntax and verifies referenced entities exist
  • Error Reporting: Issues appropriate SQLCODE errors if problems are detected
  • INTO Clause: Show Plan will not error on missing INTO clauses in Embedded SQL since they may appear in FETCH statements

SHOW STATEMENT Command

The %SYSTEM.SQL.Shell() interface provides the SHOW STATEMENT command to display prepared statements along with their execution plans, enabling iterative query refinement and optimization.

Documentation References

4. Uses SQL Statement Index statistics for performance tuning

Key Points

  • Statement tracking: Monitor execution frequency and performance of SQL queries
  • Performance metrics: Capture execution time, row counts, and resource usage
  • Index effectiveness: Identify which indexes are being used by queries
  • Query optimization: Find slow-running queries requiring attention
  • SHOW STATEMENT: Display prepared statement details and statistics
  • Cached query analysis: Review query cache utilization and efficiency

Detailed Notes

Overview

SQL Statement Index statistics provide visibility into query execution patterns and performance characteristics across the system.

SHOW STATEMENT Command

  • Purpose: Displays information about prepared SQL statements in the SQL Shell
  • Information Shown: Statement text, preparation status, and execution metadata
  • Value: Particularly valuable for identifying performance bottlenecks and understanding which queries consume the most resources
  • Usage: Execute a query, then issue SHOW STATEMENT to view detailed information without re-executing
  • Deferred Mode: Setting executemode=deferred allows preparing queries and examining statements without incurring execution costs

Performance Metrics Tracked

  • Execution Frequency: How many times a query has been executed
  • Total Execution Time: Cumulative time across all executions
  • Average Execution Time: Per-run execution time
  • Row Counts: Number of rows returned

Index Utilization Analysis

  • Index Usage Visibility: Statistics reveal which indexes are being utilized during query execution
  • Unused Index Identification: Find indexes that waste storage and maintenance overhead
  • Missing Index Detection: Identify where new indexes could improve query performance

Ongoing Performance Tuning

  • Performance Degradation Detection: Analyze execution patterns over time
  • Data Growth Impact: Identify queries affected by data growth
  • Optimization Validation: Validate the effectiveness of optimization efforts

Cached Query Analysis

  • Mechanism: InterSystems IRIS stores prepared statements for reuse
  • Cache Utilization: Statement statistics help understand cache utilization
  • Optimization Opportunities: Queries prepared repeatedly that could share cached statements

Data-Driven Decisions

Understanding statement statistics allows developers to make informed decisions about:

  • Index Creation: Where to add new indexes
  • Query Reformulation: How to rewrite inefficient queries
  • Schema Design: Changes to improve overall system performance

Documentation References

5. Implements table statistics gathering strategies

Key Points

  • TUNE TABLE command: Gather statistics on map size, extent size, and field selectivity
  • Representative data: Run on populated tables with production-like data distribution
  • Query plan optimization: Statistics enable optimizer to choose efficient execution plans
  • Automatic recompilation: Cached queries updated automatically when statistics change
  • Sampling options: %SAMPLE_PERCENT controls data sampling percentage
  • Execution methods: SQL command, Management Portal, or $SYSTEM.SQL.Stats.Table.GatherTableStats()

Detailed Notes

Table statistics are fundamental to query optimization in InterSystems IRIS. The TUNE TABLE command gathers critical statistics based on current table data, calculating map size, extent size, and selectivity for each field. These statistics must be based on representative data that reflects the distribution expected in production. TUNE TABLE analyzes the data and sets values that the query optimizer uses to estimate costs and choose optimal execution plans. The optimizer relies heavily on selectivity values to determine which indexes to use, which table to access first in joins, and whether to use full table scans or index lookups.

When TUNE TABLE executes, it updates both the SQL table definition and typically the corresponding persistent class definition, allowing gathered statistics to be used without requiring class recompilation. For deployed classes, TUNE TABLE updates only the SQL table definition, and the optimizer accesses statistics indirectly. If statistics change, TUNE TABLE automatically recompiles all cached queries that reference the table, ensuring they use updated values. However, if running TUNE TABLE does not change any values (for example, when data distribution is unchanged), cached queries are not purged and classes are not flagged for recompilation, avoiding unnecessary overhead.

TUNE TABLE offers several options for fine-tuning the gathering process. The %SAMPLE_PERCENT option specifies what percentage of table rows to sample, useful when outlier values are not evenly distributed. For tables with fewer than 1000 rows, TUNE TABLE always samples the entire extent regardless of this setting. The %CLEAR_VALUES option removes existing statistics from class and table definitions. TUNE TABLE requires %ALTER_TABLE administrative privilege and %ALTER privilege on the specific table. Table owners automatically have %ALTER privilege. The command can be executed via SQL (TUNE TABLE tablename), the Management Portal SQL interface Actions menu, or programmatically using $SYSTEM.SQL.Stats.Table.GatherTableStats(). Best practices include running TUNE TABLE after significant data loads, when query performance degrades unexpectedly, after schema changes that affect data distribution, and periodically in production environments to maintain accurate statistics as data evolves.

6. Evaluates SQL security considerations

Key Points

  • SQL vs system privileges: SQL privileges are more granular (table/column level)
  • Privilege enforcement: ODBC, JDBC, Dynamic SQL, SQL Shell check privileges
  • Embedded SQL exemption: No privilege checking; application handles authorization
  • Table-level privileges: SELECT, INSERT, UPDATE, DELETE, ALTER on tables/views
  • Column-level privileges: Fine-grained control over specific table columns
  • Administrative privileges: %ALTER_TABLE for schema modifications like TUNE TABLE

Detailed Notes

InterSystems IRIS implements comprehensive SQL security that complements system-level security with additional granular protections. SQL privileges provide table-level and column-level access control beyond database-level protections. A critical distinction is that SQL privileges can be granted directly to users or to roles, while system-level privileges are assigned only to roles. Holding an SQL privilege implicitly grants related system privileges required to perform the SQL action, but the reverse is not true: system-level privileges do not imply table-level privileges. This separation allows precise control over data access patterns.

SQL privilege checking is enforced for ODBC connections, JDBC connections, Dynamic SQL execution, and SQL Shell operations. However, Embedded SQL statements do not perform privilege checking under the assumption that applications using Embedded SQL implement their own authorization logic before executing queries. Similarly, direct invocation of class queries that do not involve %SQL.Statement objects is considered application access and bypasses SQL privilege checks. This design allows trusted applications to operate efficiently while enforcing security for external connections and ad-hoc queries.

SQL privileges operate at multiple levels of granularity. Table-level privileges (SELECT, INSERT, UPDATE, DELETE, ALTER, REFERENCES) control operations on entire tables or views. Column-level privileges provide finer control, allowing SELECT privilege on specific columns rather than the entire table. This is particularly important for arrow syntax queries, which require SELECT privilege on the ID of referenced tables plus the referenced columns. Administrative privileges like %ALTER_TABLE enable schema modification operations such as TUNE TABLE. Users who create tables automatically receive owner privileges on those tables. The %CHECKPRIV command allows programmatic verification of privileges before executing operations, enabling applications to provide appropriate feedback when users lack necessary permissions. The GRANT and REVOKE commands manage privilege assignments, and privileges can be granted WITH GRANT OPTION to allow recipients to grant those privileges to others. Roles can combine both SQL and system-level privileges, providing flexible security models. Best practices include following the principle of least privilege, using roles for privilege management, implementing column-level privileges for sensitive data, and auditing privilege usage through SQL audit features.

Exam Preparation Summary

Critical Concepts to Master:

  1. Embedded vs Dynamic SQL: Understand compilation timing, privilege checking differences, and appropriate use cases
  2. IRIS-Specific Features: Arrow syntax for implicit joins, TOP clause optimization, bitmap indexing advantages
  3. Query Plan Interpretation: Identify table scans, index usage, join strategies, and optimization opportunities
  4. Statement Statistics: Monitor query performance, execution frequency, and resource consumption
  5. TUNE TABLE Strategy: When to gather statistics, sampling options, and impact on cached queries
  6. SQL Security Model: Privilege granularity, enforcement points, and differences from system-level security

Common Exam Scenarios:

  • Choosing between Embedded SQL and Dynamic SQL for a given requirement
  • Using arrow syntax to simplify queries and understanding privilege requirements
  • Interpreting query plans to identify performance bottlenecks
  • Deciding when to run TUNE TABLE and with what options
  • Understanding which SQL execution paths enforce privilege checking
  • Implementing column-level privileges for sensitive data protection

Hands-On Practice Recommendations:

  • Write queries using both Embedded SQL and Dynamic SQL approaches
  • Use arrow syntax for object reference navigation and compare to explicit JOINs
  • Generate and interpret query plans for complex queries
  • Execute TUNE TABLE with different sampling percentages and observe effects
  • Test privilege enforcement differences between Embedded and Dynamic SQL
  • Use SHOW STATEMENT to examine prepared queries and their execution plans
  • Practice %CHECKPRIV for programmatic privilege verification
  • Experiment with TOP clause optimization for paginated result sets

Key Exam Tips:

  • Remember that Embedded SQL does NOT enforce privilege checking
  • Arrow syntax requires privileges on both tables including ID columns
  • TUNE TABLE requires %ALTER_TABLE and %ALTER privileges
  • TOP with ORDER BY changes optimization to "fastest first row"
  • Dynamic SQL uses ? or :var for parameters; Embedded uses :var only
  • Show Plan validates Embedded SQL without executing it
  • TUNE TABLE only recompiles cached queries if statistics actually change

Report an Issue