Professional Documents
Culture Documents
Using Netezza Query Plan
Using Netezza Query Plan
As with most database management systems, Netezza generates a query plan for all queries executed in
the system. The query plan details the optimal execution path as determined by Netezza to satisfy each
query. The Netezza component which generates and determines the optimal query path from the
available alternatives is called the query optimizer and it relies on number of statistics available about the
database objects involved in the query executed. The Netezza query optimizer is a cost based optimizer
i.e. the optimizer determines a cost for the various execution alternatives and determines the path with
the least cost as the optimal execution path for a particular query.
Optimizer
The optimizer relies on number of statistics about the objects in the query executed like the
Given that the optimizer relies heavily on the statistics to determine the best execution plan, it is
important to keep the database statistics up to date using the “GENERATE STATISTICS” commands.
Along with the statistics the optimizer also takes into account the FPGA capabilities of Netezza when
determining the optimal plan.
When coming up with the plan for execution, the optimizer looks for
The best method for data scan operations i.e. to read data from the tables
The best method to join the tables in the query like hash join, merge join, nested loop join
The best method to distribute data in between SPUs like redistribute or broadcast
The order in which tables can be joined in a query join involving multiple tables
Opportunities to rewrite queries to improve performance like
o Pull up of sub-queries
o Push down of tables to sub-queries
o De-correlation of sub-queries
o Expression rewrite
Query Plan
Netezza breaks query execution into units of work called snippets which can be executed on the host or
on the snippet processing units (SPU) in parallel. Queries can contain one or many snippets which will
be executed in sequence on the host or the SPUs depending on what is done by the snippet code.
Scanning and retrieving data from a table, joining data retrieved from tables, performing data
aggregation, sorting of data, grouping of data, dynamic distribution of data to help query performance
are some of the processes which can be accomplished by the snippet code generated for query execution.
© asquareb llc 1
Using Netezza Query Plan to Improve Performance
The following is a sample query plan which shows the various snippets executed in sequence, what they
accomplish, in what order the snippets are executed and where they get executed when the query is run.
QUERY VERBOSE PLAN: Snippet 1 reading data from table “B” and
Table from which data is
this runs on all the SPUs
retrieved in this snippet
Node 1.
[SPU Sequential Scan table "ORGANIZATION_DIM" as "B" {}]
-- Estimated Rows = 22193, Width = 4, Cost = 0.0 .. 843.4, Conf = 100.0
Restrictions:
((B.ORG_ID NOTNULL) AND (B.CURRENT_IND = 'Y'::BPCHAR))
Projections:
1:B.ORG_ID
Cardinality:
B.ORG_ID 22.2K (Adjusted) Snippet 2 reading data from table “A” and
[HashIt for Join] this runs on all the SPUs
Node 2.
[SPU Sequential Scan table "ORGANIZATION_FACT" as "A" {}]
-- Estimated Rows = 11568147, Width = 4, Cost = 0.0 .. 24.3, Conf = 90.0 [BT:
MaxPages=19 TotalPages=1748] (JIT-Stats)
Projections:
1:A.ORGANIZATION_ID
Cardinality: Snippet 3 performing a hash join on the data
A.ORGANIZATION_ID 22.2K (Adjusted) returned from snippet 1 & 2. Runs on all SPUs
Node 3.
[SPU Hash Join(right exists) Stream "Node 2" with Temp "Node 1" {}]
-- Estimated Rows = 11568147, Width = 0, Cost = 843.4 .. 867.7, Conf = 57.6
Restrictions:
(A.ORGANIZATION_ID = B.ORG_ID) AND (A.ORGANIZATION_ID = B.ORG_ID)
Projections:
Snippet 4 performs count of the number of rows
Node 4. from the query snippet 3. Runs on all SPUs
[SPU Aggregate]
-- Estimated Rows = 1, Width = 8, Cost = 930.6 .. 930.6, Conf = 0.0
Projections:
1:COUNT(1) Each SPU returns the count to host which aggregates the
numbers and return the total count to user
[SPU Return]
[HOST Merge Aggs]
[Host Return]
The Netezza snippet scheduler determines which snippets to add to the current work load based on the
session priority, estimated memory consumption versus available memory, and estimated SPU disk time.
Plan Generation
When a query is executed, Netezza dynamically generates the execution plan and C code to execute the
each snippet of the plan. All recent execution plans are stored in the data.<ver>/plan directory under
the /nz/data directory on the host. The snippet C code is stored under the /nz/data/cache directory
and this code is used to compare against the code for new plans so that the compilation of the code can
be eliminated if the snippet code is already available there by improving performance.
Apart from Netezza generating the execution plan dynamically during query execution, users can also
generate the execution plan (without the C snippet code) using the “EXPLAIN” command. This process
will help users identify any potential performance issues by reviewing the plan and making sure that the
path chosen by optimizer is inline or better than expected. During the plan generation process, the
optimizer may perform statistics generation dynamically to prevent issues due to out of date statistics
data particularly when the tables involved store large volume of data. The dynamic statistics generation
process uses sampling which is not as perfect as generating statistics using the “GENERATE
© asquareb llc 2
Using Netezza Query Plan to Improve Performance
STATISTICS” command which scans the tables. The following are some variations of the “EXPLAIN”
command
EXPLAIN [VERBOSE] <SQL QUERY>; //Detailed query plan including cost estimates
EXPLAIN PLANTEXT <SQL QUERY>; //Natural language description of query plan
EXPLAIN PLANGRAPH <SQL QUERY>; //Graphical tree representation of query plan
Data Points in Query Plan
Following are some of the key data points in the query plan which will help identify any performance
issues in queries
Number of rows estimated to be This is the estimated cost of this This is the confidence in
returned by this snippet query snippet percentage on the estimated cost
…
Node 2.
[SPU Sequential Scan table "ORGANIZATION_FACT" as "A" {}]
-- Estimated Rows = 10007, Width = 4, Cost = 0.0 .. 24.3, Conf = 54.0 [BT:
MaxPages=19 TotalPages=1748] (JIT-Stats)
Projections:
1:A.ORGANIZATION_ID
Cardinality:
A.ORGANIZATION_ID 22.2K (Adjusted)
…
If the estimated number of rows is less than what is expected, it is an indication that the statistics on the
table is not current. It will be good to schedule “generate statistics” against the table to get the current
statistics. If the estimated cost is high it is a good candidate to look further and make sure that the higher
cost is not due to query coding inefficiency or inefficient database object definitions.
The restrictions (where clause) applied to the
QUERY VERBOSE PLAN: data read from the table in this snippet
Node 1.
[SPU Sequential Scan table "ORDERS" as "O" {(O.O_ORDERKEY)}]
-- Estimated Rows = 500000, Width = 12, Cost = 0.0 .. 578.6, Conf = 64.0
Restrictions:
((O.O_ORDERPRIORITY = ‘HIGH’::BPCHAR) AND (DATE_PART('YEAR'::"VARCHAR",
O.O_ORDERDATE) = 2012))
Relevant table columns retrieved in this
Projections: snippet. The other columns are discarded
1:O.O_TOTALPRICE 2:O.O_CUSTKEY
Cardinality:
The resulting data is redistributed to all SPUs
O.O_CUSTKEY 500.0K (Adjusted) with CUSTKEY as distribution key
[SPU Distribute on {(O.O_CUSTKEY)}]
[HashIt for Join] The CUSTKEY value is hashed so that it can be
used to perform a hash join
…
Each SPU executing the snippet retrieves only the relevant columns from the table and applies the
restrictions using FPGA reducing the data to be processed by the CPU in the SPU which in turn helps
with the performance. The columns retrieved and the restrictions applied are detailed in the projections
and restrictions sections of the execution plan. The optimizer may decide to redistribute data retrieved in
a snippet if the tables joined in the query are not distributed on the same key value as in this snippet. If
large volume of data is being distributed by a snippet like redistribution of a fact table, it is a good
candidate to check whether the table is physically distributed on optimal keys. Note that if the snippet is
projecting a small set of columns from a table with many columns, when it redistributes the amount of
data will be small – (number of rows * total size of columns it projects). If the amount of data is small,
redistributing dynamically by a snippet execution may not be a major overhead since it is done using the
high performing/high bandwidth IP based network between the SPUs without the involvement of the
NZ host.
© asquareb llc 3
Using Netezza Query Plan to Improve Performance
…
Node 2.
[SPU Sequential Scan table "STATE" as "S" {(S.S_STATEID)}]
-- Estimated Rows = 50, Width = 100, Cost = 0.0 .. 0.0, Conf = 100.0
Projections:
1:S.S_NAME 2:S.S_STATEID Data retrieved from the snippet is
broadcast to all the snippets
[SPU Broadcast]
[HashIt for Join]
…
Like redistribution, data from a snippet can also be broadcast to all the snippets. What that means is that
the data retrieved from all the SPUs executing the snippet returns the data to the host and the host
consolidates the data and sends all the consolidated data to every SPU in the server. Broadcast is efficient
in case of small tables joined with a large table and both can’t be distributed physically on the same key.
As in the example above, the state code is used in the join to generate the name of the state in the query
result. Since the fact and STATE table are not distributed in the same key the optimizer broadcasts the
STATE table to all the SPUs so that the joins can be done locally on each SPUs. Broadcasting of
STATE table is much efficient since the data stored in the table is miniscule compared to what NZ can
handle. This snippet performs a join of the data
from the previous snippets
…
Node 5.
[SPU Hash Join Stream "Node 4" with Temp "Node 1" {(C.C_CUSTKEY,O.O_CUSTKEY)}]
-- Estimated Rows = 5625000, Width = 33, Cost = 578.6 .. 1458.1, Conf = 51.2
Restrictions:
(C.C_CUSTKEY = O.O_CUSTKEY)
Projections:
1:O.O_TOTALPRICE 2:N.N_NAME
Cardinality:
C.C_NATIONKEY 25 (Adjusted)
…
NZ supports hash, merge and nested loop joins and hash joins are the most efficient. The key data
points to check at in a snippet performing a join is to see whether the correct join is selected by the
optimizer and whether the number of estimated rows is reasonable. For e.g. if a column participating in a
join is defined as floating point instead of an integer type, the optimizer can’t use hash join when the
expectation was to use hash join. The problem can be fixed by defining the table column to use integer
type. Similarly if the estimated number of rows is very high or the estimated cost is high, it may point to
not having the correct join conditions or missing a join condition by mistake resulting in incorrect join
result set.
The following are some of the common reasons for performance issues
Not having correct distribution keys resulting in certain data slice storing more data resulting in
data skew. Performance of a query depends on the performance of the slice storing the most
data for the tables involved in a query.
Even if the data is distributed uniformly, not taking into account processing patterns in
distribution results in process skew. For e.g. data is distributed by month but if the process looks
© asquareb llc 4
Using Netezza Query Plan to Improve Performance
for a month of data, then the performance of the query will be degraded since the processing
need to be handled by a subset of SPUs and not all of the SPUs in the system.
Performance gets impacted if a large volume of data (fact table) gets re-distributed or broadcast
during query execution.
Not having zone maps of columns since the data types are not the ones for which NZ can
generate Zone Maps like numeric(x, y), char, varchar etc.
Not having the table data organized optimally for multi table joins as in the case of multi-
dimensional joins performed in a data warehouse environment.
Inefficient SQL code like using cursor based processing instead of set based processing.
blog.asquareb.com
https://github.com/bijugs
@gsbiju
http://www.slideshare.net/bijugs
© asquareb llc 5