<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Pazeto Júnior on Medium]]></title>
        <description><![CDATA[Stories by Daniel Pazeto Júnior on Medium]]></description>
        <link>https://medium.com/@dpazetojr?source=rss-9c98889dc7a3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nDC78tQBU9dCqyfcUqGxCA.jpeg</url>
            <title>Stories by Daniel Pazeto Júnior on Medium</title>
            <link>https://medium.com/@dpazetojr?source=rss-9c98889dc7a3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 17:09:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dpazetojr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Redshift — Distribution styles]]></title>
            <link>https://medium.com/@dpazetojr/redshift-distkey-and-sortkey-d247b01b01f6?source=rss-9c98889dc7a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d247b01b01f6</guid>
            <category><![CDATA[redshift]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[sort-key]]></category>
            <category><![CDATA[distkey]]></category>
            <category><![CDATA[query-optimization]]></category>
            <dc:creator><![CDATA[Daniel Pazeto Júnior]]></dc:creator>
            <pubDate>Mon, 13 Dec 2021 16:53:42 GMT</pubDate>
            <atom:updated>2021-12-13T16:56:04.294Z</atom:updated>
            <content:encoded><![CDATA[<p>As we know the AWS Redshift uses computer nodes to distribute and process data and it CAN do it in parallel, but I said CAN because it is not always an automatic feature. Sometimes when we perform complex queries(and in bigdata we do it a lot) we need to help Redshift to do it.</p><figure><img alt="https://unsplash.com/@chuttersnap" src="https://cdn-images-1.medium.com/max/1000/0*59-V1hzJKtwVSoYl" /></figure><p>There are two major bottlenecks in databases processing, <strong>JOIN </strong>tables and <strong>SORT </strong>things, for those two we have the <strong>DISTKEY </strong>and <strong>SORTKEY</strong></p><h3>Distribuition Styles</h3><p><strong>DISTKEY</strong></p><p>Using the DISTKEY is basically you letting RS know the way it will distribute the data(i.e a table) through the slices, in this case using a column as a KEY and storing the rows where the column has the same values physically in the same place, in the same slice.</p><p>See the sample below, where we have two tables and both are distributed based on a key(column), we are saying to Redshift to <strong>keep in the same slice the rows where the chosen column has the same values, </strong>in that case for both tables (using the fields <strong>person._id</strong> and<strong> employees.person_id</strong>) and on the query where we are joining the tables no further distribution will be needed, in other words, the Redshift knows that all the matches will happen into the slice/node so isn’t needed to one node communicate with other to obtain other rows because all the matches are in there. So we <strong>minimize the data movement and improve the parallel processing</strong>.</p><pre>CREATE TABLE person ( <strong>_id</strong>, name, last_name, address_id )<br><strong>DISTSTYLE KEY<br>DISTKEY (_id)</strong>;<br></pre><pre><strong>CREATE </strong><strong>TABLE</strong> employees ( <strong>person_id</strong>, salary, jobtitle)<br><strong>DISTSTYLE KEY<br>DISTKEY</strong> <strong>(person_id)</strong>;</pre><p>Imagine the following join with those two tables, using the fields we have chosen as key, the row where the values are, for example in the range of 0–100 will be in the same node/slice for both tables, so will not be necessary to ask to another node, avoiding the data movement.</p><p><strong>ALL</strong></p><p>Another style we have is the <strong>ALL</strong>, this style will create a copy for the table in each node. In general, it is used for small tables(less than 2M rows) and tables that aren’t updated so often, once it are in differentes places(nodes) and update it will have a higher cost because we need to updated all those places and also all those copies will take more storage space. Examples of candidates tables are that ones storing City or country codes and names for standardization.</p><p><strong>EVEN</strong></p><p>The style is used when we have tables where we won’t use in JOINs and they are highly de-normalized. The leader node distributes using round-robin style.</p><p><strong>AUTO</strong></p><p>Case you don’t define any of that styles the leader node will try distribute it in a optimal way according to the table size.</p><h3><strong>SORTKEY</strong></h3><p>The other pain point when we are talking about database processing is the <strong>ORDER BY</strong> and functions that need to sort things(i.e <strong>Window functions</strong>) and we also have a feature to improve that, which is the <strong>SORTKEY</strong>, it is similar to the DISTKEY, it will grant that the data will be stored ordered by the chosen field/column. It helps and improves a lot in cases you need to sort or use windows functions, especially in a large amount of data.</p><pre><strong>CREATE</strong> <strong>TABLE</strong> employees ( person_id, salary, jobtitle )<br>DISTSTYLE <strong>KEY<br></strong>DISTKEY (person_id)<br>SORTKEY (salary);</pre><p>If you are looking to improve your queries performance in Redshift, I think the best features to use are these one presented here</p><h3>Bonus tip:</h3><p><strong>What if my table already has a different DISTKEY I need to?</strong></p><p>The important thing to keep in mind is that sometimes we can’t cover all queries/processes just using one DISTKEY and one SORTKEY or maybe we can not change the table storage keys, and for that, we can use the <strong>TEMP TABLES</strong> to improve the query/processes time.</p><p>Now imagine that you have to join two tables but one or both of them aren’t distribute using the column you will use, I mean the data for these tables are distributed over the slices by the an key that isn’t the best for your join, so Redshift will need to make the nodes talk to each other and move data between them in order to join those tables and the query will take too much time to process.</p><p>One attempt to decrease thise query time is use the <strong>CREATE TEMP TABLE </strong>command,<strong> </strong>so basically, instead of using the source table directly, we create a temporary table that points to the source and this temp table has the distribution key we will use in the query. We explicitly <strong>tell Redshift to redistribute it before we start our processing</strong>.</p><pre><strong>CREATE</strong> <strong>TEMPORARY</strong> <strong>TABLE</strong> inter_job_titles DISTKEY(jobtitle_name) <strong>AS</strong> (<br>    <strong>SELECT</strong> * <strong>FROM</strong> international_job_titles <strong>WHERE </strong>country = &#39;US&#39;<br>)<br><strong>SELECT <br>    </strong>e.jobtitle,<br>    ejt.jobtitle_name<br><strong>FROM</strong> employees p<br><strong>LEFT </strong><strong>JOIN</strong> inter_job_titles ijt <strong>ON</strong> ijt.jobtitle_name = e.jobtitle</pre><p>Keep in mind that this “copy”(in quotes because you can already make some filters to bring only relevant data for your scenario) of the table can take some time, so<strong> is your responsibility to see if that strategy is worth for your scenario</strong>, in general when we have a huge amount of data, is worth because the time we take to create the temp table will be compensated when you use it on the join or window functions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d247b01b01f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Redshift — Architecture basics]]></title>
            <link>https://medium.com/@dpazetojr/redshift-architecture-basics-4aae5068b8e3?source=rss-9c98889dc7a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/4aae5068b8e3</guid>
            <category><![CDATA[redshift]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[big-data]]></category>
            <dc:creator><![CDATA[Daniel Pazeto Júnior]]></dc:creator>
            <pubDate>Mon, 27 Sep 2021 20:34:10 GMT</pubDate>
            <atom:updated>2021-12-13T16:54:53.895Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Photo by Ferhat Deniz Fors on Unsplash" src="https://cdn-images-1.medium.com/max/1000/0*Vua3rS9dOzixtoMH" /></figure><p>Used a lot for Bigdata teams with a <strong>huge amount of data</strong>, <strong>Amazon Redshift (RS) </strong>is sometimes treated like other SQL databases and that is why they haven’t extracted the maximum performance of it. The developers which will use that tool to process data need to understand a little bit about the architecture and how RS handles data, just <strong>SQL skills aren’t enough to obtain the total power of it</strong>, is similar to know how to driving, but driving a Formula 1 car takes more than that.</p><p>In the Mysql database usually, we have one machine which has the database on it, for Redshift we have more than that, we have a cluster that is similar to the Mysql machine, but in that cluster, we also have nodes, a leader one and the compute nodes.</p><p>The <strong>cluster</strong> is the unit that will contain the nodes, in this article we won’t cover how to create it, but using the AWS Console or even searching on the internet you can easily create one in just minutes with some clicks.</p><p>The <strong>leader node</strong> is the one responsible for client connections in the database and also manages the processing and data storing in the compute nodes. Every connection, query or data will pass through the leader, for the query, it will compile that and distribute it among the compute nodes, the same for the data, saying which portion of data each computes node will process or store. We have only one leader in a Redshift cluster.</p><p>The <strong>compute nodes</strong> are (we have more than one) responsible for store and processing data, each unit has its own CPU, memory and disk storage. When the client sends a query to Redshift, the leader node breaks down that query into smaller parts and gives each part for one compute node, the same for the data that the client wants to store. In each compute node we also have the node slices which are smaller parts of the node where the distributed data is stored based on a key.</p><p>Thinking in a more practical way, let’s say we have a table and it’s distributed among the nodes/slices based on a key and ordered by another key, once we query it using those keys the RS can basically use each node to process one data portion paralleling and return it faster. Obviously, we need to act to obtain the best from the tool using some features like DISTKEY and SORTKEY.</p><figure><img alt="Redshift Architecture from AWS documentation. https://docs.aws.amazon.com/redshift/latest/dg/c_high_level_system_architecture.html" src="https://cdn-images-1.medium.com/max/616/0*1_SGQIkWtsBLpCOd.png" /><figcaption>Redshift Architecture</figcaption></figure><p>Now we have this brief knowledge, every time you make a table you will think <strong>“How can I optimize that using the Power of Redshift?”</strong> and that is the idea you need to know that architecture because if you and your team are using Redshift is because you need either huge storage or big data processing and both are challenging.</p><p>The next step is to know about and how to use the Redshift DIST KEYS and SORTKEYS <a href="https://medium.com/@dpazetojr/redshift-distkey-and-sortkey-d247b01b01f6">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4aae5068b8e3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>