<?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 Ravi Kumar on Medium]]></title>
        <description><![CDATA[Stories by Ravi Kumar on Medium]]></description>
        <link>https://medium.com/@rk.personal06?source=rss-93287e7ee520------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4qR3fFNy09deWOkjCF-_0g.png</url>
            <title>Stories by Ravi Kumar on Medium</title>
            <link>https://medium.com/@rk.personal06?source=rss-93287e7ee520------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 02:39:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rk.personal06/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[Best Practices to design storing Gold Dataset into your Data Lake using AWS Redshift]]></title>
            <link>https://medium.com/@rk.personal06/best-practices-to-design-storing-gold-dataset-into-your-data-lake-using-aws-redshift-a3cfc143bf5e?source=rss-93287e7ee520------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3cfc143bf5e</guid>
            <dc:creator><![CDATA[Ravi Kumar]]></dc:creator>
            <pubDate>Sun, 09 Apr 2023 14:00:01 GMT</pubDate>
            <atom:updated>2023-04-09T14:05:41.426Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><h4>Problem Statement:</h4><p>Queries that come up along with generation of gold dataset are it’s storage , concurrent access ,data refresh ,optimum performance and automation of deployment. Considering that the <strong>gold</strong> dataset is stored as <strong>AWS Redshift</strong> tables and views are created on top of it. There are challenges to achieve uninterrupted concurrent access with data being refreshed in almost near time with source. Another challenge would be to have an optimum performance when the <strong>gold</strong> dataset is being queried by BI tool.</p><h4>Theme of Blog</h4><p><strong>Gold</strong> Dataset is one which is the most accurate and reliable of its kind.Business scenarios that involve storing data as a single source of truth are recommended to have the features mentioned above in the problem statement. In case the design for storing gold dataset is not efficient , it may result in data inconsistencies with respect to refresh, impact in performance and interrupted concurrent access.</p><p>This blog covers an efficient way to design the gold dataset layer on AWS Redshift following the best practices.</p><h4>Expectation from this blog</h4><p>One can have an in depth understanding to design the gold dataset on AWS Redshift.</p><h3>Amazon Redshift Cost:</h3><p>Amazon redshift is a fully managed petabytes scale data warehouse service in the cloud.</p><p>Costing varies from $ 0.25 per hour to $ 1000 / TB / Year.</p><p><a href="https://aws.amazon.com/redshift/pricing/">Cloud Data Warehouse - Amazon Redshift Pricing- Amazon Web Services</a></p><h4>Architecture</h4><p>Leader Node manages the communication with client programs and compute nodes.</p><p>Compute Node executes the compiled code and sends the intermediate results back to leader node for final aggregation.A compute node is partitioned into slices.Each slice owns a portion of the disk space.</p><p><a href="https://docs.aws.amazon.com/redshift/latest/dg/c_high_level_system_architecture.html"><em>https://docs.aws.amazon.com/redshift/latest/dg/c_high_level_system_architecture.html</em></a></p><p>Below diagram shows a high level overview of AWS Redshift Architecture.</p><p><strong>High Level AWS Redshift Architecture</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/0*cPsXaIefahDZo2SJ" /></figure><h4>Storage</h4><p>Block — Amazon Redshift uses a block size of 1 MB. It is immutable and can have any of 1–13 encodings.</p><p><a href="https://docs.aws.amazon.com/redshift/latest/dg/c_columnar_storage_disk_mem_mgmnt.html"><em>https://docs.aws.amazon.com/redshift/latest/dg/c_columnar_storage_disk_mem_mgmnt.html</em></a></p><p>Zone Map — It exists for each 1 MB block and consists of in-memory metadata that tracks the minimum and maximum values in the block.</p><h4>SORT KEY</h4><p>Without Sort Key Vs With Sort key — add a diagram</p><p><em>SELECT count(*) FROM TABLE where date = ‘08-JAN-2021’ ;</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/0*CQVRv_ELeLz0ipVS" /></figure><h4>Data Distribution:</h4><p>Distribution style is a property which directs how data is distributed throughout the cluster.</p><p>Objective:</p><p>Distribute data evenly for parallel processing.</p><p>Minimize data movement during query processing.</p><p>KEY: Value is hashed, same value goes to same location(slice)</p><p>ALL: Full table data goes to the first slice o every node</p><p>EVEN: Round robin</p><p>AUTO: Combines EVEN and ALL</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/0*YkLevTjswJBMyNlR" /></figure><h3>Design to store Gold Dataset under Redshift</h3><p>Redshift Tables (Table — A1 …. C4)</p><ul><li>Distkey AUTO</li><li>Sortkey AUTO</li></ul><p>Materialized views ( MAT View-A …. C)</p><ul><li>AUTO REFRESH YES</li><li>Only Inner join &amp; no subqueries/cte’s/nested queries</li><li>Cascade while dropping views (to drop dependent views)</li><li>No aggregations in the query</li><li>Must not be built on top of Materialized view.</li></ul><p>Normal Views</p><ul><li>Left/Right/Outer join MAT views / Tables</li><li>Aggregations</li><li>Nested queries</li></ul><p><strong>High Level Diagram — Redshift Tables to Normal Views</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/0*dY0wHoBLBxHVl7rY" /></figure><h3>Best Practices to reduce the query cost displayed in Explain Plan</h3><p>Sample Explain Plan</p><pre><br><br>XN Hash Left Join DS_BCAST_INNER  (cost=2063596927.91..2189539244.17 rows=259 width=0)<br>  Hash Cond: ((&quot;outer&quot;.distributionid)::numeric = &quot;inner&quot;.distributionidnum1)<br>  Filter: ((&quot;outer&quot;.org_id IS NOT NULL) OR (((&quot;inner&quot;.seg3)::text &gt;= &#39;1000&#39;::text) AND ((&quot;inner&quot;.seg3)::text &lt;= &#39;9999&#39;::text)))<br>  -&gt;  XN Hash Left Join DS_BCAST_INNER  (cost=605586288.90..696008599.33 rows=259 width=16)<br>        Hash Cond: (&quot;outer&quot;.org_id = &quot;inner&quot;.org_id)<br>        -&gt;  XN Hash Right Join DS_DIST_INNER  (cost=605586271.60..647048576.12 rows=259 width=16)<br>              Inner Dist Key: tv.elem_id<br>              Hash Cond: (&quot;outer&quot;.prj_id = &quot;inner&quot;.elem_id)<br>              -&gt;  XN Seq Scan on task tv2  (cost=0.00..6862.13 rows=686213 width=8)<br>              -&gt;  XN Hash  (cost=605586270.95..605586270.95 rows=259 width=24)<br>.......<br>.......<br>                                                   -&gt;  XN Hash Right Join DS_DIST_BOTH  (cost=4014153.62..396574325.99 rows=259 width=40)<br>                                                         Outer Dist Key: (pn.per_id)::bigint<br>                                                         Inner Dist Key: i.emp_id<br>                                                         Hash Cond: (&quot;outer&quot;.&quot;?column2?&quot; = &quot;inner&quot;.emp_id)<br>                                                         -&gt;  XN Seq Scan on per pn  (cost=0.00..32.34 rows=3234 width=15)<br>                                                         -&gt;  XN Hash  (cost=4014153.58..4014153.58 rows=16 width=48)<br>                                                               -&gt;  XN Hash Join DS_DIST_INNER  (cost=164598.26..4014153.58 rows=16 width=48)<br>                                                                     Inner Dist Key: d.inv_id<br>                                                                     Hash Cond: (&quot;outer&quot;.inv_id = &quot;inner&quot;.inv_id)<br>                                                                     -&gt;  XN Seq Scan on inv i  (cost=0.00..559.16 rows=55360 width=24)<br>                                                                           Filter: (inv_date IS NULL)<br>                                                                     -&gt;  XN Hash  (cost=164598.22..164598.22 rows=16 width=40)<br>                                                                           -&gt;  XN Hash Join DS_BCAST_INNER  (cost=0.22..164598.22 rows=16 width=40)<br>                                                                                 Hash Cond: (&quot;outer&quot;.books_id = &quot;inner&quot;.l_id)<br>                                                                                 -&gt;  XN Seq Scan on ide d  (cost=0.00..4594.48 rows=269 width=48)<br>                                                                                       Filter: ((flag)::text = &#39;Y&#39;::text) <br>                                                                                 -&gt;  XN Hash  (cost=0.21..0.21 rows=1 width=8)<br>                                                                                       -&gt;  XN Seq Scan on led l  (cost=0.00..0.21 rows=1 width=8)<br>                                                                                             Filter: ((l_name)::text ~~ &#39;XXX%Primary&#39;::text)<br><br></pre><h4>1. Distribution</h4><p>Manage to keep the distributions with the best possible performance rank from the below chart.</p><p>Links: <a href="https://docs.aws.amazon.com/redshift/latest/dg/c_data_redistribution.html">https://docs.aws.amazon.com/redshift/latest/dg/c_data_redistribution.html</a></p><p>Below chart shows distributions In the order of best to lowest performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4VbOocOpLXFIzo_dYy3tww.png" /></figure><h4>2. Cartesian Product</h4><p>Review the explain plan to check if there is any cartesian product. It may be because of inappropriate or missing join conditions.Add join condition to remove cartesian product.</p><h4>3. Bubble up the Heavy Joins</h4><p>Keep the heavy joins at the top followed by decreasing volume of data involved in joins.</p><h4>4. Join Columns Datatype</h4><p>Make sure to have the same datatype for both the join attributes. If required do convert the data types so that the join is compatible.</p><h4>5. Filters</h4><p>Incorporate as many filters as possible in the left join conditions.</p><p>Keep multiple similar OR conditions under single braces and concatenate it using AND.</p><pre>Example - <br>WHERE <br>col1 &gt; 100 <br>OR col2 = ‘IND’ <br>OR col3 = ‘RUPEE’<br>(col1 &gt; 100 OR col2 = ‘IND’ OR col3 = ‘RUPEE’)<br>AND col4 = ‘COUNTRY’<br>AND (col5 IS NOT NULL OR col6 between 0 and 9)</pre><h3>Design to drop and create views automatically</h3><ul><li>Generate Checksum</li></ul><p>For each run a checksum is generated for the ddl (create view query) and is inserted into an audit table.</p><ul><li>Match Checksum</li></ul><p>Program checks if the view exists and its checksum exists in the audit table.</p><ul><li>View does not exist — New deployment view needs to be created.</li><li>Checksum exists — No action require</li><li>Checksum does not match — View needs to be dropped and created.</li></ul><p>Cascade Materialized Views</p><ul><li>Cascade while dropping materialized views to make sure all its dependent views are dropped.</li></ul><p><strong>Flow Diagram for the design</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/0*rk62PmWvFM2bLqwe" /></figure><h4>Design to preserve the permission on views, in case views are been cascaded / dropped</h4><ul><li>Permission logging</li></ul><p>Insert the latest permissions of normal views / materialized views and all its dependent views into the permission log table.</p><ul><li>Drop Views</li></ul><p>Drop the normal views / materialized views with the cascade option.</p><ul><li>Create Views</li></ul><p>Create views as per design mentioned above.</p><ul><li>Recreate permissions</li></ul><p>Using the permission log , execute the grants for the normal views / materialized &amp; its dependent views.</p><ul><li>Delete old logs</li></ul><p>Delete the old logs from the permission log table to make sure that log table has only the latest permissions.</p><ul><li>Flow Diagram</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/0*lDyTUULiMcFSxRqx" /></figure><h4>Conclusion</h4><p>After going through this blog you would have a better understanding of the design for storing gold dataset (AWS Redshift) following the best practices.Though this blog has been written considering the gold dataset , it would assist in several other scenarios which requires storing dataset into AWS Redshift.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3cfc143bf5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>