Home
%3CLINGO-SUB%20id%3D%22lingo-sub-305247%22%20slang%3D%22en-US%22%3EChoosing%20hash%20distributed%20table%20vs.%20round-robin%20distributed%20table%20in%20Azure%20SQL%20DW%20Service%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-305247%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Aug%2011%2C%202015%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CI%3E%20Authored%20by%20Sanjay%20Mishra%20%3C%2FI%3E%3C%2FP%3E%3CBR%20%2F%3E%20This%20topic%20explains%20the%20various%20Azure%20SQL%20Data%20Warehouse%20distributed%20table%20types%2C%20and%20offers%20guidance%20for%20choosing%20the%20type%20of%20distributed%20table%20to%20use%20and%20when.%20There%20are%20two%20types%20of%20distributed%20tables%20in%20Azure%20SQL%20DW%20at%20the%20writing%20of%20this%20article%2C%20hash%20distributed%20table%20and%20round-robin%20distributed%20table.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Designing%20databases%20to%20use%20these%20distributed%20tables%20effectively%20will%20help%20you%20to%20achieve%20the%20storage%20and%20query%20processing%20benefits%20of%20the%20Azure%20SQL%20DW%20Service%20(SQL%20DW).%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20In%20SQL%20DW%20a%20%3CB%3E%20%3CI%3E%20distribution%20%3C%2FI%3E%20%3C%2FB%3E%20is%20an%20Azure%20SQL%20Database%2C%20in%20which%20one%20or%20more%20distributed%20tables%20are%20stored.%20Each%20instance%20of%20SQL%20DW%20has%20many%20distributions.%20Many%20distributions%20can%20reside%20in%20a%20single%20instance%20of%20Azure%20SQL%20instance.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20amount%20of%20distributions%20are%20subject%20to%20change%20and%20not%20important%20for%20understanding%20this%20particular%20topic.%20%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-1476847804%22%20id%3D%22toc-hId-1539649303%22%3EHash%20Distributed%20Table%20Basics%3C%2FH3%3E%3CBR%20%2F%3E%20A%20%3CB%3E%20%3CI%3E%20hash%20distributed%20table%20%3C%2FI%3E%20%3C%2FB%3E%20is%20a%20table%20whose%20rows%20are%20dispersed%20across%20multiple%20distributions%20based%20on%20a%20hash%20function%20applied%20to%20a%20column.%20Each%20SQL%20instance%20contains%20a%20group%20of%20one%20or%20more%20rows.%20The%20following%20diagram%20depicts%20how%20table%20within%20SQL%20DW%20gets%20stored%20as%20a%20hash%20distributed%20table.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F63493iD00CBC73CB1C540A%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20When%20processing%20queries%20involving%20distributed%20tables%2C%20SQL%20DW%20instances%20execute%20multiple%20internal%20queries%2C%20in%20parallel%2C%20within%20each%20SQL%20instance%2C%20one%20per%20distribution.%26nbsp%3B%20These%20separate%20processes%20(independent%20internal%20SQL%20queries)%20are%20executed%20to%20handle%20different%20distributions%20during%20query%20and%20load%20processing.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20A%20%3CB%3E%20distribution%20column%20%3C%2FB%3E%20is%20a%20single%20column%20(specified%20at%20table%20creation%20time)%20that%20SQL%20DW%20uses%20to%20assign%20each%20row%20to%20a%20distribution.%20A%20deterministic%20hash%20function%20uses%20the%20value%20in%20the%20distribution%20column%20to%20assign%20each%20row%20to%20belong%20to%20one%20and%20only%20one%20distribution.%20Two%20identical%20column%20values%20with%20the%20same%20data%20type%20will%20be%20hashed%20the%20same%20and%20thus%20will%20end%20up%20in%20the%20same%20distribution.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20In%20the%20diagram%2C%20each%20row%20in%20the%20original%20file%20is%20stored%20on%20one%20distribution.%20The%20number%20of%20rows%20in%20each%20distribution%20can%20vary%20and%20is%20usually%20not%20identical%20from%20distribution%20to%20distribution.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20There%20are%20performance%20considerations%20for%20the%20selection%20of%20a%20distribution%20column%2C%20such%20as%20minimizing%20data%20skew%2C%20minimizing%20data%20movement%2C%20and%20the%20types%20of%20queries%20executed%20on%20the%20system.%20For%20example%2C%20query%20performance%20improves%20when%20two%20distributed%20tables%20are%20joined%20on%20a%20column%20that%20is%20of%20the%20same%20data%20type%20and%20size.%20This%20is%20called%20a%20distribution%20compatible%20join%20or%20a%20co-located%20join.%20%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1075309157%22%20id%3D%22toc-hId--1012507658%22%3ERound-Robin%20Distributed%20Table%20Basics%3C%2FH3%3E%3CBR%20%2F%3E%20A%20round-robin%20distributed%20table%20is%20a%20table%20where%20the%20data%20is%20evenly%20(or%20as%20evenly%20as%20possible)%20distributed%20among%20all%20the%20distributions%20without%20the%20use%20of%20a%20hash%20function.%20A%20row%20in%20a%20round-robin%20distributed%20table%20is%20non-deterministic%20and%20can%20end%20up%20in%20different%20distributions%20each%20time%20they%20are%20inserted.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Each%20JOIN%20to%20a%20round-robin%20distributed%20table%20is%20a%20data%20movement%20operation.%20The%20data%20movement%20needed%20to%20perform%20join%20operations%20is%20a%20separate%20topic%20and%20will%20be%20published%20as%20a%20separate%20blog%20soon.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Usually%20common%20dimension%20tables%20or%20tables%20that%20doesn%E2%80%99t%20distribute%20evenly%20are%20good%20candidates%20for%20round-robin%20distributed%20table.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20following%20diagram%20shows%20a%20round-robin%20distributed%20table%20that%20is%20stored%20on%20different%20distribution.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F63494i47715A3971B98FEE%22%20%2F%3E%20%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-667501178%22%20id%3D%22toc-hId-730302677%22%3EBest%20Practices%3C%2FH3%3E%3CBR%20%2F%3E%20In%20SQL%20DW%2C%20a%20user%20query%20is%20a%20logical%20query%20that%20gets%20divided%20into%20many%20physical%20queries%20one%20for%20each%20distribution.%20The%20Engine%20Service%20on%20the%20control%20node%20acts%20as%20a%20coordinator%20and%20waits%20for%20each%20of%20these%20individual%20queries%20to%20finish%20before%20returning%20results%20or%20the%20next%20part%20of%20the%20multi-step%20query%20is%20executed.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20When%20creating%20a%20table%20in%20SQL%20DW%2C%20you%20need%20to%20decide%20if%20the%20table%20will%20be%20hash%20distributed%20or%20round-robin%20distributed.%20This%20decision%20has%20implications%20for%20query%20performance.%20Each%20of%20these%20distributed%20tables%20may%20require%20data%20movement%20during%20query%20processing%20when%20joined%20together.%20Data%20movement%20in%20MPP%20RDBMS%20system%20is%20an%20expensive%20but%20sometimes%20unavoidable%20step.%20In%20my%208%20plus%20years%20of%20working%20with%20MPP%20data%20warehouse%20I%20haven%E2%80%99t%20seen%20a%20real%20customer%20workload%20that%20can%20completely%20eliminate%20data%20movement.%20The%20objective%20of%20a%20good%20data%20warehouse%20design%20in%20SQL%20DW%20is%20to%20minimize%20data%20movement%20so%20let%E2%80%99s%20keep%20that%20in%20mind%20while%20choosing%20table%20design.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Here%20are%20considerations%20for%20choosing%20whether%20to%20use%20a%20round-robin%20distributed%20table%20or%20a%20hash%20distributed%20table%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201.%20To%20choose%20a%20good%20distribution%20design%20with%20SQL%20DW%2C%20one%20should%20know%20their%20data%2C%20DDL%20and%20queries.%20This%20is%20not%20unique%20to%20SQL%20DW%20but%20for%20most%20MPP%20RDBMS%20system.%20You%20need%20to%20minimize%20data%20movement%20queries%20but%20also%20watch%20out%20for%20data%20that%20can%20heavily%20skew%20a%20certain%20distribution.%20If%20one%20of%20the%20distribution%20has%20more%20data%20than%20others%2C%20it%20will%20be%20the%20slowest%20performing%20distribution.%20Since%20SQL%20DW%20queries%20are%20as%20fast%20as%20its%20slowest%20distribution%2C%20we%20need%20to%20take%20notes%20of%20any%20data-heavy%20(skewed%20or%20hot)%20distribution%20for%20the%20same%20table.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202.%20A%20nullable%20column%20is%20a%20bad%20candidate%20for%20any%20hash%20distributed%20table.%20All%20null%20columns%20are%20hashed%20the%20same%20and%20thus%20the%20rows%20will%20end%20up%20on%20the%20same%20distribution%20creating%20a%20skewed%20(hot)%20distribution.%20If%20most%20of%20the%20columns%20are%20null%20able%20and%20no%20good%20hash%20distribution%20can%20be%20achieved%2C%20that%20table%20is%20a%20good%20candidate%20for%20round-robin%20distribution.%20Choose%20%E2%80%98not%20null%E2%80%99%20columns%20when%20creating%20table%20that%20will%20be%20hash%20distributed.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203.%20Any%20fact%20tables%20that%20has%20a%20default%20value%20in%20a%20column%20is%20also%20not%20a%20good%20candidate%20to%20create%20a%20hash%20distributed%20table.%20DW%20Developers%20will%20sometime%20assign%20-1%20value%20to%20an%20otherwise%20unknown%20value%20or%20early%20arriving%20values%20for%20a%20fact%20table.%20These%20values%20will%20create%20data%20skew%20on%20a%20particular%20distribution.%20Avoid%20these%20kind%20of%20default%20value%20column%20unless%20you%20know%20for%20sure%20that%20the%20-1%20values%20are%20negligible%20in%20your%20data.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204.%20Large%20fact%20tables%20or%20historical%20transaction%20tables%20are%20usually%20stored%20as%20hash%20distributed%20tables.%20These%20tables%20usually%20have%20a%20surrogate%20key%20that%20is%20monotonically%20increasing%20and%20are%20used%20in%20JOIN%20conditions%20with%20other%20fact%20and%20dimension%20tables.%20These%20surrogate%20keys%20are%20a%20good%20candidate%20for%20distributing%20the%20data%20as%20there%20are%20many%20unique%20values%20in%20that%20column.%20This%20allows%20the%20query%20operations%20to%20be%20performed%20across%20all%20distributions.%20Each%20distribution%20can%20work%20independently%20on%20separate%20subsets%20of%20data.%20This%20takes%20advantage%20of%20the%20processing%20resources%20across%20the%20MPP%20system.%20Queries%20on%20distributed%20tables%20may%20require%20data%20movement%20between%20distributions%20during%20query%20execution%20and%20that%20is%20okay.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205.%20Dimension%20tables%20or%20other%20lookup%20tables%20in%20a%20schema%20can%20usually%20be%20stored%20as%20round-robin%20tables.%20Usually%20these%20tables%20connects%20to%20more%20than%20one%20fact%20tables%20and%20optimizing%20for%20one%20join%20may%20not%20be%20the%20best%20idea.%20Also%20usually%20dimension%20tables%20are%20smaller%20which%20can%20leave%20some%20distributions%20empty%20when%20hash%20distributed.%20Round-robin%20by%20definition%20guarantees%20a%20uniform%20data%20distribution.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%206.%20If%20you%20are%20unsure%20of%20query%20patterns%20and%20data%2C%20you%20can%20start%20with%20all%20tables%20in%20round-robin%20distribution.%20And%20as%20you%20learn%20the%20patterns%20the%20data%20can%20be%20easily%20redistributed%20on%20a%20hash%20key.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%207.%20When%20using%20%E2%80%98group%20by%E2%80%99%20SQL%20DW%20will%20shuffle%20the%20data%20on%20the%20group%20by%20key.%20When%20multiple%20keys%20are%20present%20and%20statistics%20is%20up-to-date%20SQL%20DW%E2%80%99s%20cost%20based%20optimizer%20will%20pick%20the%20right%20key%20to%20shuffle%20the%20data.%20If%20this%20group%20by%20key%20is%20heavily%20non-unique%20then%20the%20query%20will%20be%20slower.%20A%20worst%20case%20example%20would%20be%20grouping%20by%20gender%20of%20a%20large%20customer%20table.%20If%20your%20query%20is%20running%20slower%2C%20look%20into%20explain%20plan%20(add%20the%20word%20%E2%80%98explain%E2%80%99%20before%20your%20query%20and%20execute)%20to%20find%20out%20what%20key%20is%20being%20used%20as%20the%20shuffle%20key.%20There%20may%20or%20may%20not%20be%20anything%20you%20can%20do%20to%20change%20this%20based%20on%20the%20query.%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-305247%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Aug%2011%2C%202015%20Authored%20by%20Sanjay%20MishraThis%20topic%20explains%20the%20various%20Azure%20SQL%20Data%20Warehouse%20distributed%20table%20types%2C%20and%20offers%20guidance%20for%20choosing%20the%20type%20of%20distributed%20table%20to%20use%20and%20when.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-305247%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eazure%20sqldw%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EData%20Warehouse%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Empp%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Esqldw%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Aug 11, 2015

Authored by Sanjay Mishra


This topic explains the various Azure SQL Data Warehouse distributed table types, and offers guidance for choosing the type of distributed table to use and when. There are two types of distributed tables in Azure SQL DW at the writing of this article, hash distributed table and round-robin distributed table.

Designing databases to use these distributed tables effectively will help you to achieve the storage and query processing benefits of the Azure SQL DW Service (SQL DW).

In SQL DW a distribution is an Azure SQL Database, in which one or more distributed tables are stored. Each instance of SQL DW has many distributions. Many distributions can reside in a single instance of Azure SQL instance.

The amount of distributions are subject to change and not important for understanding this particular topic.

Hash Distributed Table Basics


A hash distributed table is a table whose rows are dispersed across multiple distributions based on a hash function applied to a column. Each SQL instance contains a group of one or more rows. The following diagram depicts how table within SQL DW gets stored as a hash distributed table.



When processing queries involving distributed tables, SQL DW instances execute multiple internal queries, in parallel, within each SQL instance, one per distribution.  These separate processes (independent internal SQL queries) are executed to handle different distributions during query and load processing.

A distribution column is a single column (specified at table creation time) that SQL DW uses to assign each row to a distribution. A deterministic hash function uses the value in the distribution column to assign each row to belong to one and only one distribution. Two identical column values with the same data type will be hashed the same and thus will end up in the same distribution.

In the diagram, each row in the original file is stored on one distribution. The number of rows in each distribution can vary and is usually not identical from distribution to distribution.

There are performance considerations for the selection of a distribution column, such as minimizing data skew, minimizing data movement, and the types of queries executed on the system. For example, query performance improves when two distributed tables are joined on a column that is of the same data type and size. This is called a distribution compatible join or a co-located join.

Round-Robin Distributed Table Basics


A round-robin distributed table is a table where the data is evenly (or as evenly as possible) distributed among all the distributions without the use of a hash function. A row in a round-robin distributed table is non-deterministic and can end up in different distributions each time they are inserted.

Each JOIN to a round-robin distributed table is a data movement operation. The data movement needed to perform join operations is a separate topic and will be published as a separate blog soon.

Usually common dimension tables or tables that doesn’t distribute evenly are good candidates for round-robin distributed table.

The following diagram shows a round-robin distributed table that is stored on different distribution.


Best Practices


In SQL DW, a user query is a logical query that gets divided into many physical queries one for each distribution. The Engine Service on the control node acts as a coordinator and waits for each of these individual queries to finish before returning results or the next part of the multi-step query is executed.

When creating a table in SQL DW, you need to decide if the table will be hash distributed or round-robin distributed. This decision has implications for query performance. Each of these distributed tables may require data movement during query processing when joined together. Data movement in MPP RDBMS system is an expensive but sometimes unavoidable step. In my 8 plus years of working with MPP data warehouse I haven’t seen a real customer workload that can completely eliminate data movement. The objective of a good data warehouse design in SQL DW is to minimize data movement so let’s keep that in mind while choosing table design.

Here are considerations for choosing whether to use a round-robin distributed table or a hash distributed table:

1. To choose a good distribution design with SQL DW, one should know their data, DDL and queries. This is not unique to SQL DW but for most MPP RDBMS system. You need to minimize data movement queries but also watch out for data that can heavily skew a certain distribution. If one of the distribution has more data than others, it will be the slowest performing distribution. Since SQL DW queries are as fast as its slowest distribution, we need to take notes of any data-heavy (skewed or hot) distribution for the same table.

2. A nullable column is a bad candidate for any hash distributed table. All null columns are hashed the same and thus the rows will end up on the same distribution creating a skewed (hot) distribution. If most of the columns are null able and no good hash distribution can be achieved, that table is a good candidate for round-robin distribution. Choose ‘not null’ columns when creating table that will be hash distributed.

3. Any fact tables that has a default value in a column is also not a good candidate to create a hash distributed table. DW Developers will sometime assign -1 value to an otherwise unknown value or early arriving values for a fact table. These values will create data skew on a particular distribution. Avoid these kind of default value column unless you know for sure that the -1 values are negligible in your data.

4. Large fact tables or historical transaction tables are usually stored as hash distributed tables. These tables usually have a surrogate key that is monotonically increasing and are used in JOIN conditions with other fact and dimension tables. These surrogate keys are a good candidate for distributing the data as there are many unique values in that column. This allows the query operations to be performed across all distributions. Each distribution can work independently on separate subsets of data. This takes advantage of the processing resources across the MPP system. Queries on distributed tables may require data movement between distributions during query execution and that is okay.

5. Dimension tables or other lookup tables in a schema can usually be stored as round-robin tables. Usually these tables connects to more than one fact tables and optimizing for one join may not be the best idea. Also usually dimension tables are smaller which can leave some distributions empty when hash distributed. Round-robin by definition guarantees a uniform data distribution.

6. If you are unsure of query patterns and data, you can start with all tables in round-robin distribution. And as you learn the patterns the data can be easily redistributed on a hash key.

7. When using ‘group by’ SQL DW will shuffle the data on the group by key. When multiple keys are present and statistics is up-to-date SQL DW’s cost based optimizer will pick the right key to shuffle the data. If this group by key is heavily non-unique then the query will be slower. A worst case example would be grouping by gender of a large customer table. If your query is running slower, look into explain plan (add the word ‘explain’ before your query and execute) to find out what key is being used as the shuffle key. There may or may not be anything you can do to change this based on the query.