Professional Documents
Culture Documents
V 1.1
July 18, 2013 © 2013 IBM Corporation
PDA Best Practices Agenda
Data Distribution and join strategies
Datatypes
Zonemaps
Materialized views
Statistics
Groom
Datatypes
Zonemaps
Materialized views
Statistics
Groom
2
Response time
Response Time
3 is affected by
the completion
4 time for all of
the data slices
5 in the MPP
array
6
A distribution method that distributes data fairly evenly across all data slices.
This is the one of the most important factors that can influence overall performance.
Hash Distributions and Data Skew
Data
Slice CPU Disk I/O Network
1
Response Time
3
4
Gender = M or F
5 Distributes all table records on 2 data slices
Feb 2
Response Time
Mar 3
Apr 4
May 5
Jun 6
Jul 7
Using a DATE as the distribution key may distribute rows evenly across all data slices.
However, most analysis (queries) is performed on a date range. Massively parallel
processing won’t be achieved when all of the records to be processed for a given date
range are located on one or a few data slices.
Collocated Tables
Data slice 32 Data slice 33 Data slice 34
customer (c_custkey)
orders (o_custkey)
orders (o_orderkey)
orders (o_orderkey)
orders ()
• Random distributes on round robin so the data is spread across all SPUs.
• Lots of data movement between data slices and SPUs.
• Worse case is if both tables are random and large.
• This will give very poor performance.
Broadcasted Table DBOS/Host
nation (n_nationkey)
... Japan ... ... ... ... US ... ... ... ... UK ... ... ...
... China ... ... ...
• A small table may be broadcast to all data slices (small being a relative term!).
• The data will be joined locally to the data slice.
Broadcasted Table DBOS/Host
nation (n_nationkey)
... US ... ... ... ... US ... ... ... ... US ... ... ...
... UK ... ... ... ... UK ... ... ... ... UK ... ... ...
... Japan ... ... ... ... Japan ... ... ... ... Japan ... ... ...
... China ... ... ... ... China ... ... ... ... China ... ... ...
• This is one case where the use of random for the large table will work..
• Performance will be good if the broadcast data slice does not get large.
Distribution
To create an explicit distribution key, the Netezza SQL syntax is:
usage: CREATE TABLE <tablename> [ ( <column> [, … ] ) ]
DISTRIBUTE ON RANDOM;
Checking Distribution
• You can review actual table distribution using the Netezza Performance Portal.
• Alternatively:
• Using SQL:
Select datasliceid, count(datasliceid) as “Rows”
Always review after implementation to ensure no skew when deployed or skew that
was hidden on development
PDA Best Practices Agenda
Data Distribution and join strategies
Datatypes
Zonemaps
Materialized views
Statistics
Groom
– You will save space – remember on a multi-billion row table saving a few bytes a row will
result in tens of gigs of space savings. Whilst data at rest is compressed it is
uncompressed as it is moved between SPUs or processed.
– The more compact your datatypes the less data has to be moved around the system
Data types
NUMERIC datatypes with a scale of 0 are similar to INTEGER datatypes.
Floating point data types (REAL/DOUBLE PRECISION) are, by definition, lossy in nature.
– Unless you are working with very large numbers or very small fractions are you sure
you want to be using these datatypes?
Inconsistent data types for the same column on different tables impacts performance
– If you join on them, there is additional processing needed to convert to the same type
– If it’s a distribution key, you will not get collocated data for the same VALUE in
different DATATYPES
The nz_best_practices script can be used to flag where a better data type is available.
Datatypes
Zonemaps
Materialized views
Statistics
Groom
Why scan 999 days when the query only wants one day?
Zone Maps
Zone Maps can be used to scan only relevant data
The system knows where data is not and only scans the relevant table extents/pages.
Bad zonemap
…
Extent # | DAT_CREAZIONE (Min) | DAT_CREAZIONE (Max) | ORDER'ed
----------+---------------------+---------------------+--------- …
0 | 2006-10-01 00:21:47 | 2009-10-10 19:02:27 |
1 | 2006-10-02 13:07:21 | 2009-10-10 19:30:32 |
2 | 2006-10-01 21:00:48 | 2009-11-18 23:07:02 |
…
3 | 2006-10-02 11:05:06 | 2009-11-18 23:31:25 |
…
Good zonemap
…
Extent # | DAT_CREAZIONE (Min) | DAT_CREAZIONE (Max) | ORDER'ed
----------+---------------------+---------------------+---------
0 | 2006-10-01 00:21:47 | 2006-10-01 19:02:27 | …
1 | 2006-10-01 19:02:28 | 2006-10-02 19:30:32 | TRUE
.........
247 | 2009-11-17 21:00:48 | 2009-11-18 23:07:02 | TRUE …
248 | 2009-11-18 23:07:02 | 2009-11-21 23:31:25 | TRUE
Zone Maps: Automatic Performance
Zone Maps enable the optimizer to take advantage of inherent ordering of data within a data
slice.
– When stats are run, the zone map is created.
– For every eligible column (Integers, timestamps, dates) in the table.
– Min and Max values per 3Mb extent (or 128k page in NPS 7.0) are gathered.
When a query runs, the system skips over extents/pages where it knows the data does NOT
reside.
Automatically configured
– During stats
– During Loads
– During inserts, updates, loads, and groom (but not deletes!)
Also exploited by clustered base tables.
Zone Maps: Automatic Performance cont.
Design ETL architecture and batch jobs with Zone Maps in mind.
– Load in sets of data (dates, stores, sites, etc).
– Alternatively take advantage of clustered base tables.
Tip: Primary Key lookups. If you have low latency queries that target specific rows..
– Sort your table on the primary key.
– PK lookups will be as optimal as possible.
– Example: if customer is a dimension table, sort by customer_id (pk) and load data..
Zone Maps: Automatic Performance cont.
Validate/Check
– If a table is rebuilt with CTAS with a different DISTRIBUTION then data ordering will change
and zone maps will behave differently.
– Can also happen when data is migrated between different sized systems
– nz_zonemap script will tell you how sorted the data is on your table.
Datatypes
Zonemaps
Materialized views
Statistics
Groom
A CBT is a user table that contains data which is organized using 1 to 4 organizing keys.
An organizing key is a column of the table that you specify for clustering the records.
Netezza uses the organizing keys to group records within the table.
Netezza also creates zone maps for the organizing columns including the first eight
characters of a character organizing field.
This accelerates the performance of queries that restrict using the organizing keys.
Clustered base tables (CBT) cont.
Used generally on large fact tables (millions to billions of rows)
CBTs allow you to incrementally organize data within your user tables in situations where
data cannot easily be accumulated in staging areas for pre-ordering before insertions.
CBTs can help you to eliminate or reduce pre-sorting of new table records prior to a
load/insert operation.
CBTs do not replicate the base table data and do not allocate additional data structures.
CBTs may reduce compression (e.g. sequential rowids) but the impact will be small.
– A standard ordered table will give great performance for the first column, less so for
the second, even less for the third, etc.
This means that in cases where you are only interested in restricting by a single column
(say a date field) a standard table will outperform or give the same performance as a
CBT.
However...
– CBTs only need grooming periodically and will only reorder data that needs it.
– You can change your ordering by altering the ORGANISE ON clause and re-
grooming. No change to any ETL code is needed.
– Groom only needs a small space overhead and locks only at commit time. A CTAS
and rename needs 100% of the table space as an overhead.
CBT in practice
The CBT uses a space filling curve in n-dimensional space to determine the best key
order.
For a single column this may result in slightly poorer performance than a perfectly
ordered zone map, but will still be vastly better than no ordering at all.
Be sure to groom any CBT that is regularly updated (The admin tool will give you a
‘percent organized’ figure).
Consider how ordered the incoming data is and what the nature of the changes will be on
an ongoing basis.
You cannot have a materialized view on a CBT – it’s one or the other.
PDA Best Practices Agenda
Data Distribution and join strategies
Datatypes
Zonemaps
Materialized views
Statistics
Groom
Alternatively, provides zone maps on the first eight values of a character field.
Finally, for a single or small number of rows the mview can be used as an index
– it contains a pointer to the block that holds the base row in the original table.
Materialized Views cont.
Base Table • A MATERIALIZED VIEW reduces the
width of data being scanned in a base
table by creating a thin version of the
Column 305
Column 304
Column 300
Column 303
base table that conatains a small
Column 302
Column 3
Column 4
Column 5
Column n
Column 1
Column 2
Materialized Views
transparently avoid scanning
unreferenced columns
MATERIALIZED VIEW
36 © 2013 IBM Corporation
Materialized Views cont.
They are useful if you have a very wide table and run queries that will use only a subset
of the data.
When data is loaded into the base table, the columns the mview is interested in are
loaded at the tail end of the mview in an unsorted manner.
This means that to keep good performance, the mview needs to periodically be rebuilt.
Mview data is not backed up, only its definition. The mview is recreated when you
restore the table.
Materialized Views: Things to consider
Whilst a useful tool, these objects will add maintenance overhead. These need to be
thought through and built in or you will hit performance issues over time.
Periodic refresh
Consider if a CBT will do what you need before looking at mviews, or if your tables are
appropriately distributed.
PDA Best Practices Agenda
Data Distribution and join strategies
Datatypes
Zonemaps
Materialized views
Statistics
Groom
The more up to date and accurate table statistics are, the more chance the query.
optimizer has to generate an optimal plan.
Statistics should be built into ETL and ELT processing wherever possible.
This does not mean you need to collect statistics on large tables for every load. It
means you need to think carefully about when and how to collect statistics.
‘Mop up jobs to capture or report on any missed statistics are also recommended.
Datatypes
Zonemaps
Materialized views
Statistics
Groom
Guaranteed to be unique
– But not necessarily sequential within a table
– A new record is inserted with the deleted rowid and the xid
value updated
• Rowid is preserved
• Groom tables that receive frequent updates or deletes or if you cancel/abort a load as
this will result in deleted rows.
• As with reclaim, you can groom to remove fully empty leading/training pages (ie there
is no valid data in the entire page). This is the quickest option and suitable if you are,
for example, loading data in day by day or housekeeping to remove old data
regularly..
• You can groom at a record level to remove all deleted records regardless of their
location. This will give you the best space gains but take longer.
• Deciding which option requires knowledge of your specific circumstances.
• Consider using nz_groom to assist with automated grooming.
Grooming for ordering
Grooming to order tables:
• Once you carry out at least one full backup using nzbackup, you need to consider
carefully the impact on groom.
• Groom can only physically delete rows that have been recorded in a backup – this is
to ensure that any differential backups contain a complete record of rows that have
been removed to replay.
• Ensure your groom command follows a differential or full backup to maximize the
space recovered.
• If you need to groom immediately you can specify RECLAIM BACKUPSET NONE to
force groom to reclaim all rows regardless of whether they have been backed up.
• If you do this then the next backup will take a full table backup regardless of whether
you specified a full or differential backup.
Groom
Things to keep in mind: