Home Discussions Questions & Answers Anchor Types and Dynamic SQL Optimization

Anchor Types and Dynamic SQL Optimization

Avatar photoCustomer January 6, 2024 at 9:06 am

I see two primary ways to handle optional parameters: the traditional `:bind` variables, and Anchors like `1=1` or `&lexical`. Why does the developer guide recommend using Anchors, especially for optional restrictions?

Viewing 6 reply threads
  • Author
    Replies
    • Support January 7, 2024 at 12:54 pm  

      The recommendation is driven by performance optimization. Blitz Report creates dynamic SQL at run-time, which allows unused parameters to be dropped entirely from the query. This eliminates a frequent source of non-performant SQL, specifically the widespread coding practice of using the inefficient pattern: `column_name=nvl(:bind_variable, column_name)` for optional parameters.

    • Avatar photoCustomer January 8, 2024 at 5:00 pm  

      That makes sense for performance. Now, what’s the difference between the `n=n` anchor (like `1=1`) and the `&lexical` anchor, and when should I use one over the other?

    • Support January 9, 2024 at 1:56 pm  

      The `n=n` anchors (e.g., `1=1`) are specifically WHERE clause SQL anchors. When using these, Blitz Report inserts the associated SQL text *before* the anchor, automatically adding the keyword ‘and’ and a line feed, ensuring valid SQL. However, the `n=n` syntax remains in the run-time SQL, so it can only be used for WHERE clauses.

    • Avatar photoCustomer January 10, 2024 at 1:42 pm  

      So if I need to dynamically change something outside the WHERE clause, like adding columns or an ORDER BY clause, `n=n` won’t work?

    • Support January 11, 2024 at 6:05 am  

      Exactly. The `&lexical` parameter reference is far more flexible. It works like lexical parameters in standard SQL*Plus or Oracle Reports, meaning Blitz Report replaces the placeholder completely with the parameter SQL text at run-time. This makes `&lexical` suitable for injecting almost any SQL snippet, including Dynamic tables and columns, ORDER BY clauses, GROUP BY clauses, HINTS, or Complete subselects or EXISTS clauses.

    • Avatar photoCustomer January 12, 2024 at 4:52 pm  

      If I use an `&lexical` parameter and the user leaves the input blank, does the placeholder still remain in the SQL?

    • Support January 14, 2024 at 7:49 pm  

      No. If a parameter value associated with an `&lexical` anchor is left blank, the corresponding reference is removed entirely before SQL execution, which is another aspect of the optimization.

Viewing 6 reply threads
  • You must be logged in to reply to this post.

Login with: