<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>dorkusmalorkus.dev</title>
  <subtitle>A blog about programming, UNIX tools, databases, memes and more.</subtitle>
  <link rel="self" href="https://dorkusmalorkus.dev/feed.xml"/>
  <link rel="alternate" type="text/html" href="https://dorkusmalorkus.dev/"/>
  <id>https://dorkusmalorkus.dev/</id>
  <updated>2026-01-07T00:00:00Z</updated>
  <author><name>Max Van Lyl</name></author>
  <entry>
    <title>Mind the Gap</title>
    <link rel="alternate" type="text/html" href="https://dorkusmalorkus.dev/posts/outbox_publisher_challenges"/>
    <id>https://dorkusmalorkus.dev/posts/outbox_publisher_challenges</id>
    <published>2026-01-07T00:00:00Z</published>
    <updated>2026-01-07T00:00:00Z</updated>
    <summary>A tale of caution when implementing the Transactional Outbox Pattern in a sharded MySQL setup.</summary>
    <category term="event-driven-architecture"/>
    <category term="transactional-outbox-pattern"/>
    <category term="databases"/>
    <content type="html">&lt;p&gt;SQL stories seem to be very popular on this blog... Maybe it&#x27;s because they&#x27;re so &lt;em&gt;relatable&lt;/em&gt; and everyone &lt;em&gt;joins&lt;/em&gt; in.&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;534&quot; height=&quot;414&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher_challenges/husky_pun.webp?v=cbcef6c1&quot; alt=&quot;I crack me up&quot;&gt;
  &lt;figcaption&gt;I crack me up.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Thank you, thank you. I&#x27;ll be here all week. Aaanyway... If you haven&#x27;t read my previous tiny introduction to the Transactional Outbox Pattern, go and &lt;a href=&quot;https://dorkusmalorkus.dev/posts/outbox_publisher&quot;&gt;give that a quick read&lt;/a&gt;. It&#x27;s okay. I&#x27;ll wait.
Okay, caught up?&lt;/p&gt;
&lt;p&gt;Cool. Let&#x27;s dive into some of the challenges, quirks and gotchas that my team and I experienced when implementing this pattern in a fairly large service.&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;840&quot; height=&quot;630&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher_challenges/mindthegap.webp?v=1f86cf20&quot; alt=&quot;Mind the Gap&quot;&gt;
  &lt;figcaption&gt;Pretty much how we felt at one point in this story. (image credit: travelkillsme.com)&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2&gt;Indexs? Indices? Indexi? Indexes! :)&lt;/h2&gt;
&lt;p&gt;As a reminder from the previous post, here are the DB tables we&#x27;re dealing with:&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher/outbox_table.webp?v=35d4d783&quot;&gt;&lt;img style=&quot;--max-width: 1236px&quot; width=&quot;618&quot; height=&quot;301&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher/outbox_table.webp?v=35d4d783&quot; alt=&quot;Outbox Table example&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
  &lt;figcaption&gt;Anatomy of the outbox_table table&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;However, what I didn&#x27;t include in the previous post is additional info about indexes on the &lt;code&gt;outbox_table&lt;/code&gt; table
(and yes, I looked it up, apparently &#x27;indices&#x27; is math-related, whereas &#x27;indexes&#x27; is appropriate for DBs. The more you know and all that).&lt;/p&gt;
&lt;p&gt;Another thing I didn&#x27;t include is that we&#x27;re working with MySQL 8.0 with InnoDB engine. Why does that matter? Oooh, you&#x27;ll see soon enough, dear sweet innocent reader.&lt;/p&gt;
&lt;p&gt;We have this migration for our &lt;code&gt;outbox_table&lt;/code&gt; table:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox_table&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;bigint&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsigned&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AUTO_INCREMENT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;blob&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;varchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;published_at&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DEFAULT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DEFAULT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UTC_TIMESTAMP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INDEX&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx_outbox_table_created_at_published_at&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;published_at&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You&#x27;ll notice that we created a composite index on the &lt;code&gt;created_at, published_at&lt;/code&gt; fields. This bit is important, so remember it for later.&lt;/p&gt;
&lt;h2&gt;Locked in&lt;/h2&gt;
&lt;p&gt;Our outbox_publisher&#x27;s job was the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Fetch all records in the &lt;code&gt;outbox_table&lt;/code&gt; table that were not yet published, with preconfigured batch limit (let&#x27;s say 25). So, any rows within our limit that had a &lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;published_at&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Publish all the events for any rows fetched.&lt;/li&gt;
&lt;li&gt;Update the published records with the appropriate &lt;code&gt;published_at&lt;/code&gt; timestamp.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As most of you will know, developers add indexes to DB tables to speed up certain queries. In this case, our select query looked like this:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox_table&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;published_at&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;FOR&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SKIP&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LOCKED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Those DBAs-in-waiting among you will notice a few things right away:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;…&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FOR&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;UPDATE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A &lt;code&gt;select for update&lt;/code&gt; statement does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It acquires exclusive row locks on the selected rows&lt;/li&gt;
&lt;li&gt;As such, it guarantees that no other transaction can modify or delete those rows until the current transaction is committed or rolled back.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Prevents:
So this is a protection mechanism against:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;two (or more) workers selecting the same unpublished rows simultaneously before either worker updates them.&lt;/li&gt;
&lt;li&gt;race conditions between the SELECT and UPDATE statements&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;n&quot;&gt;SKIP&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LOCKED&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This tells the DB to not wait up by the phone if this row is ~~out with the bois~~, I mean, locked by another transaction.
It prevents workers from blocking each other, and also allows you to scale horizontally by adding more workers since they can read from the same table effectively.&lt;/p&gt;
&lt;p&gt;Without &lt;code&gt;SKIP LOCKED&lt;/code&gt;, the second worker would either block until the first worker commits or even worse, we&#x27;d have a &lt;code&gt;deadlock&lt;/code&gt; on our hands.&lt;/p&gt;
&lt;h2&gt;Shards and Sharts&lt;/h2&gt;
&lt;p&gt;Why do we need all this complexity? Why not just do a simple select and update? Glad you asked!
See, we&#x27;re not dealing with your average, run-of-the-mill, small-scale, grandmother&#x27;s application here. We&#x27;re dealing with a high-throughput system with a &lt;em&gt;sharded&lt;/em&gt; database setup.&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;568&quot; height=&quot;428&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher_challenges/kermit_shart.webp?v=009f41ca&quot; alt=&quot;I said SHARD&quot;&gt;
  &lt;figcaption&gt;I said sharD. Stop being gross.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Potential accidental underwear soiling jokes aside, a sharded database means that our data is partitioned across multiple database instances, or shards, based on a certain key (like a user ID). This allows us to distribute the load and improve performance, but it also introduces some challenges when it comes to ensuring data consistency and avoiding conflicts between workers.&lt;/p&gt;
&lt;p&gt;I can already hear some of you thinking: &amp;quot;Why not just have one worker per shard?&amp;quot;
Well, yeah... that&#x27;s what we have. However, in this wonderful world of microservices and container orchestration, things can get a bit... unpredictable.
As such, there is a non-zero chance that two workers could end up working on the same shard at the same time. Hence, the need for &lt;code&gt;SELECT FOR UPDATE SKIP LOCKED&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Gaps &amp;amp; Ghosts&lt;/h2&gt;
&lt;p&gt;Alright, so we have our select query with &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;, and we&#x27;re ready to roll. Or so we thought. After shipping this setup to production, we started noticing some weird behavior, mostly with latency spikes on the &lt;code&gt;INSERT&lt;/code&gt; operations on the outbox table.&lt;/p&gt;
&lt;p&gt;After doing the initial checks, we started to suspect our old friend the &lt;code&gt;SELECT FOR UPDATE&lt;/code&gt; query. To confirm our suspicions, we enabled the general query log in MySQL and started analyzing the logs. And sure enough, we&#x27;re seeing quite a bit of locking happening on some of these rows. But wait, didn&#x27;t we instruct the SELECT query to skip locked rows? Yes, we did. So what does this have to do with the &lt;code&gt;INSERT&lt;/code&gt; operations?&lt;/p&gt;
&lt;p&gt;Turns out, there&#x27;s a little-known behavior in MySQL&#x27;s InnoDB engine related to gap locks and the way indexes work. A &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/innodb-locking.html#innodb-gap-locks&quot;&gt;gap lock&lt;/a&gt; is a lock on a gap between index records OR a gap before the first or after the last index record. This last one is important.&lt;/p&gt;
&lt;p&gt;When we have a composite index on &lt;code&gt;created_at&lt;/code&gt; and &lt;code&gt;published_at&lt;/code&gt;, the &lt;code&gt;SELECT FOR UPDATE&lt;/code&gt; query can acquire a gap lock on the index ranges, not just the rows themselves. It does this to prevent &lt;em&gt;phantom reads&lt;/em&gt;.&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;251&quot; height=&quot;201&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher_challenges/luigi_phantom.webp?v=5f2f9515&quot; alt=&quot;Luigi Phantoms&quot;&gt;
&lt;/figure&gt;
&lt;p&gt;What&#x27;s a phantom read? It&#x27;s a concurrency phenomenon where a transaction reads a set of rows that satisfy a certain condition, but another transaction inserts new rows that also satisfy that condition, leading to &#x27;phantom&#x27; rows that weren&#x27;t there on the initial &lt;code&gt;SELECT&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Typically, range selection phantom issues are handled by setting the &lt;code&gt;REPEATABLE_READS&lt;/code&gt; at the MySQL server level. With the InnoDB engine, this configuration uses something called &#x27;next-key locking&#x27;. &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/innodb-locking.html#:~:text=For%20the%20last%20interval%2C%20the,following%20the%20largest%20index%20value&quot;&gt;According to the docs&lt;/a&gt;, this strategy combines index-row locks with &amp;quot;gap locks&amp;quot; to lock the space between rows, preventing other transactions from inserting new rows within that range.&lt;/p&gt;
&lt;p&gt;But are we dealing with range selections? Well, not really.&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox_table&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;published_at&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;FOR&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SKIP&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LOCKED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So what gives? Not every single &lt;code&gt;INSERT&lt;/code&gt; was getting issues, just some of them. Once again turning to the &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/innodb-transaction-isolation-levels.html#isolevel_repeatable-read&quot;&gt;almighty docs&lt;/a&gt;, it turns out that the &lt;code&gt;REPEATABLE_READ&lt;/code&gt; isolation level and next-key locking can still lead to gap locks being acquired in certain situations, even if we&#x27;re not doing range selections. InnoDB will lock the space between the records in the index, in order to protect against phantom inserts. But we&#x27;re using the &lt;code&gt;skip locked&lt;/code&gt; clause, so why are we still seeing issues?``&lt;/p&gt;
&lt;p&gt;After digging deeper, we found out about something called the &#x27;supremum lock&#x27;. I&#x27;ll let the official documentation explain it best:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Suppose that an index contains the values 10, 11, 13, and 20. The possible next-key locks for this index cover the following intervals, where a round bracket denotes exclusion of the interval endpoint and a square bracket denotes inclusion of the endpoint:

(negative infinity, 10]
(10, 11]
(11, 13]
(13, 20]
(20, positive infinity)

For the last interval, the next-key lock locks the gap above the largest value in the index and the “supremum” pseudo-record having a value higher than any value actually in the index. The supremum is not a real index record, so, in effect, this next-key lock locks only the gap following the largest index value.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is particularly relevant to us because it can cause locking when attempting to insert records at the end of an index range. So we basically had our insert and select queries doing the following:&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;500&quot; height=&quot;501&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher_challenges/paul_v_joshua.webp?v=97a0a729&quot; alt=&quot;Jake Paul scared of Anthony Joshua&quot;&gt;
  &lt;figcaption&gt;C&#x27;mon. You know I had to.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2&gt;MAX to the rescue?&lt;/h2&gt;
&lt;p&gt;What do we know so far about the issue?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We have &lt;code&gt;INSERT&lt;/code&gt; operations that are getting blocked due to next-key locks, since they affect the index on the table.&lt;/li&gt;
&lt;li&gt;InnoDB is doing its job in protecting against phantom reads and offering reliable transaction isolation.&lt;/li&gt;
&lt;li&gt;This causes issues for our &lt;code&gt;INSERT&lt;/code&gt; operations, leading to deadlocks and just generally a bad time.&lt;/li&gt;
&lt;li&gt;Thankfully, thanks to the outbox_publisher design, no events are lost since the entire transaction rolls back if the insert fails.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So how did we fix this? (yeah, we did fix this, otherwise I wouldn&#x27;t be writing this post, now would I?)&lt;/p&gt;
&lt;p&gt;Given our problem relates to the ranges covered by the composite index on &lt;code&gt;created_at&lt;/code&gt; and &lt;code&gt;published_at&lt;/code&gt;, we decided to try and do something about that instead. What if, instead of selecting the entire range covered by the index, we ... you know... don&#x27;t? We basically figure out the last committed transaction&#x27;s primary key and select everything before that?&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;MAX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox_table&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;published&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(the keen readers among you might have noticed we no longer care about the &lt;code&gt;published_at&lt;/code&gt; column, in favour of the &lt;code&gt;published&lt;/code&gt; boolean column. This was part of the multiple attempts to improve the situation, but this post is already getting too long so let&#x27;s just run with it, k? k!)&lt;/p&gt;
&lt;p&gt;Anyway, with that max ID in hand, we can now do a select like this:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox_table&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_unpublished_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;published&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;FOR&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SKIP&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LOCKED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By using our &lt;code&gt;max_id&lt;/code&gt;, we are explicitly pruning the search space the DB has to do. So, InnoDB does not need to lock the gap above the highest ID in the table, only the rows below it.&lt;/p&gt;
&lt;p&gt;So, by using the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;LIMIT&lt;/code&gt; clause&lt;/li&gt;
&lt;li&gt;id &amp;lt; &lt;code&gt;max_unpublished_id&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SKIP LOCKED&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;proper indexing&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Another crucial point is that since we&#x27;re working with &lt;code&gt;id&lt;/code&gt;, which is an auto-incremented primary key.
Because of this predictable range, we can now prevent InnoDB from locking the gap above our selected rows and no longer stalling our INSERT operations. Yay and such!&lt;/p&gt;
&lt;h2&gt;So what&#x27;s next?&lt;/h2&gt;
&lt;p&gt;Well, ideally, we shouldn&#x27;t even have had to do any of this. The keen among you may have noticed that the main reason we were running into these issues is because of that darn &lt;code&gt;SELECT FOR UPDATE&lt;/code&gt; itself. If we could avoid using it, we could avoid all these issues. So... did we remove it? That, my friends, is a story for another day.&lt;/p&gt;
&lt;p&gt;Stay tuned for the SQL! (haha, get it? SQL? Sequel? sigh. Never mind...I&#x27;ll see myself out.)&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Goin&#x27; Postal</title>
    <link rel="alternate" type="text/html" href="https://dorkusmalorkus.dev/posts/outbox_publisher"/>
    <id>https://dorkusmalorkus.dev/posts/outbox_publisher</id>
    <published>2025-12-10T00:00:00Z</published>
    <updated>2025-12-10T00:00:00Z</updated>
    <summary>Increase resiliency and consistency in your event driven architecture with the Transactional Outbox Pattern</summary>
    <category term="event-driven-architecture"/>
    <category term="transactional-outbox-pattern"/>
    <content type="html">&lt;h2&gt;Intro: Dopamine &amp;amp; Distractions&lt;/h2&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;500&quot; height=&quot;756&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher/two_buttons.webp?v=379bd90b&quot; alt=&quot;Choices.. choices&quot;&gt;
  &lt;figcaption&gt;Welcome to my life&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;As the year draws to a close, the usual talk of New Year&#x27;s resolutions starts buzzing around.&lt;/p&gt;
&lt;p&gt;I never understood these motivational videos on Youtube or self-help blogs that talk about &#x27;sharing your plans&#x27; with your friends.
I guess the idea is to create accountability so you&#x27;ll actually follow through. For me though, this has the opposite effect.
I get the dopamine hit from telling everyone, then my brain full-on backstabs my ambitions and witholds the sweet sweet happy hormones. So naturally, I don&#x27;t do the thing I talked about.&lt;/p&gt;
&lt;p&gt;Far-fetched analogy, I know, just bear with me ok? At least it&#x27;s somewhat better than your average LLM (not really but hey).&lt;/p&gt;
&lt;p&gt;Why am I rambling on about this? Well, this pattern is basically what my backend services were stuck in.
They would tell everyone about their cool sh*t (aka publishing event messages) and get their little dopamine hit, but
then their actual work (DB transaction) would fail.&lt;/p&gt;
&lt;p&gt;This was always &amp;quot;fun&amp;quot;, since it led to lovely bugs. Much like you and your friends, where you continued living your life assuming your cool friend
was off doing all the cool things they were bragging about, the downstream consumers of the event messages were now assuming that the publisher service
had successfully completed their transaction. When it then turns out their dopamine addicted *ss didn&#x27;t actually do the work, we had some neat issues on our hands. Partial failures, data inconsistencies across
services, retries causing deduplications, inconsistent reads in downstream systems... you name it!&lt;/p&gt;
&lt;h2&gt;Transactional Outbox Pattern&lt;/h2&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;500&quot; height=&quot;500&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher/pepperidge.webp?v=2425e19c&quot; alt=&quot;Pepperide Farm Remembers&quot;&gt;
  &lt;figcaption&gt;Your downstream consumer service be like&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Because we got pretty sick of debugging these type of issues, we set about investigating how to avoid some of these issues in the first place.
After all, why would you emit an event when your main DB transaction might fail and be rolled back?&lt;/p&gt;
&lt;p&gt;Enter the OutBox Publisher Pattern. One of the key guarantees this pattern provides is to use the primary database transaction
to record both the domain change AND the intent to publish the event.&lt;/p&gt;
&lt;p&gt;It does this by writing both the initial record (e.g. a croissant order) AND the event message metadata and payload to the DB.
Next, an asynchronous process can then poll or stream entries in the outbox table, handle the publishing of the event and finally mark the event as dispatched.
A secondary process can then periodically clean up the dispatched records so your outbox table doesn&#x27;t grow too large.&lt;/p&gt;
&lt;h2&gt;Anatomy of the Outbox Table&lt;/h2&gt;
&lt;p&gt;Let&#x27;s dive into a simplified example. Lets say we are running a bakery frontend with an async event system to process the orders in our backend bakery (just roll with it okay, I&#x27;m running low on creative analogies here)&lt;/p&gt;
&lt;p&gt;In our primary service we have a database with two tables:&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher/outbox_table.webp?v=35d4d783&quot;&gt;&lt;img style=&quot;--max-width: 1236px&quot; width=&quot;618&quot; height=&quot;301&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher/outbox_table.webp?v=35d4d783&quot; alt=&quot;Outbox Table example&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
  &lt;figcaption&gt;Basic table setup&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The main constraint for the outbox_publisher table is that each payload is IMMUTABLE. We don&#x27;t want to mess with event payloads or metadata at all.
IF the original record (aka the croissant order) is updated or deleted, we enter a new event in the outbox_publisher table which the downstream consumer
services can then handle separately.&lt;/p&gt;
&lt;h2&gt;Publish &amp;amp; Purge&lt;/h2&gt;
&lt;p&gt;Now that we have our primary record AND event record successfully written to the database, we can kick off our little postal worker process.
This worker can poll for any new entries to the publisher table, handle the publishing of the event, then mark the event as published using the boolean column).&lt;/p&gt;
&lt;p&gt;This works beautifully. However, over time and in high-throughput systems, this outbox table can grow quite large. Sure, now we can look at partitioning or other ways to handle that read load,
but for simplicity we can just delete any of the dispatched event records.&lt;/p&gt;
&lt;p&gt;To do this, we can have a second worker run periodically, check for all outbox records marked as published and delete them. To give ourselves some margin we can
only delete records that were published more than 2 weeks ago. This is neat because it allows us more visibility for debugging.&lt;/p&gt;
&lt;h2&gt;Computer Says No&lt;/h2&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;220&quot; height=&quot;220&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/outbox_publisher/otter.webp?v=5ebea4a4&quot; alt=&quot;Otter doesn&#x27;t fit&quot;&gt;
&lt;/figure&gt;
&lt;p&gt;So when does something like the outbox pattern not fit the bill? When dealing with relational databases that have strong transactional guarantees, this pattern is amazing. For non-transactional systems or in systems
where ordering is critical, this might not work very well. But then again, if ordering is critical, what are you doing with async event publishing anyway? :)&lt;/p&gt;
&lt;p&gt;In that case, it&#x27;s better to consider things like CDC pipelines, streaming pipelines or log-based setups. We won&#x27;t dive into those here.&lt;/p&gt;
&lt;h2&gt;Enough talk. Show Code&lt;/h2&gt;
&lt;p&gt;Okay okay, chill. Here it is:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;__tablename__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;orders&quot;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;primary_key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;product_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nullable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nullable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;price&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nullable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utcnow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Outbox&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;__tablename__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;outbox_table&quot;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;primary_key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LargeBinary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nullable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nullable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utcnow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;published&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;published_at&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;# Create both the order and outbox record and commit to DB&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# fake session from SQLAlchemy for brevity&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;product_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;price&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;price&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# order.id now available in SQLAlchemy session object&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;event&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;OrderCreated&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;s2&quot;&gt;&quot;order_id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;s2&quot;&gt;&quot;product_name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;s2&quot;&gt;&quot;quantity&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;s2&quot;&gt;&quot;price&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;37&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;s2&quot;&gt;&quot;created_at&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isoformat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;38&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;39&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;outbox_entry&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Outbox&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;content_type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;46&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox_entry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;                   &lt;span class=&quot;c1&quot;&gt;# atomic&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;48&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;51&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;c1&quot;&gt;# NOTE: entire transaction gets rolled back, so entity AND event, even if only one of them fails&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rollback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;55&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;56&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next, we have our OutboxPublisher worker that runs continiously in a separate deployment.&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;OutboxPublisher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outbox&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;publisher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delay_sec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outbox&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;publisher&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;publisher&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delay_sec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delay_sec&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should_stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should_stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;process_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;elapsed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delay_sec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elapsed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;process_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;messages&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_unpublished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;published&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;publisher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;publish&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                    &lt;span class=&quot;n&quot;&gt;published&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;                    &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mark_published&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;published&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# simply updates the outbox_record to `published = 1` and the `published_at with current timestamp`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Lastly, a cleanup worker that can be configured to delete published records that are of a certain age.&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;delete_published_before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outbox&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;before_date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;limit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;n&quot;&gt;deleted&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outbox&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delete_published_before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;before_date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;limit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Closing Notes&lt;/h2&gt;
&lt;p&gt;So, with just a handful of code and a bit of infra / DB work, we&#x27;re now in a much better place where we can
reliably tell the world about the cool shit we&#x27;re doing, and make sure we&#x27;ve actually done it! :)&lt;/p&gt;
&lt;p&gt;Even if our publisher crashes mid-flow, this pattern should allow enough resilience to never lose messages and give our developer team
same extra buffer to debug and investigate.&lt;/p&gt;
&lt;p&gt;In the next part of this two-part series, we&#x27;ll dive into some interesting operational considerations and gotcha&#x27;s that
have bitten us in the backside since implementing this pattern in some of our high-throughput services and how we mitigated some
of the most common issues. Stay tuned for that, hopefully it won&#x27;t take another year!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Facing the SnowStorm: Part 2</title>
    <link rel="alternate" type="text/html" href="https://dorkusmalorkus.dev/posts/deferred-merge-snowflake-performance-part-2"/>
    <id>https://dorkusmalorkus.dev/posts/deferred-merge-snowflake-performance-part-2</id>
    <published>2024-12-27T00:00:00Z</published>
    <updated>2024-12-27T00:00:00Z</updated>
    <summary>The Adventure Continues! In this chapter we dive into our Snowflake table data partitions, and we&#x27;re starting to suspect our good friend MERGE might have something to do with it. After some trial and error (and a few too many hairs lost), we managed to boost query speed by 90% (!!!). Read on to find out how!</summary>
    <category term="databases"/>
    <category term="performance"/>
    <category term="snowflake-db"/>
    <content type="html">&lt;h2&gt;Recap from Part 1&lt;/h2&gt;
&lt;p&gt;Welcome back! &lt;a href=&quot;https://dorkusmalorkus.dev/posts/deferred-merge-snowflake-performance-part-1&quot;&gt;In part 1 of this two-part series on Snowflake Query Performance&lt;/a&gt;, we learned that simply upping the size of our Snowflake virtual warehouse didn’t do much for improving query performance. So, if it&#x27;s not compute power, what&#x27;s going on? The issue might actually lie in how the data is structured. In this part, we’re going to dig into clustering and partitioning to see if these could be the missing pieces to the puzzle. Let&#x27;s see if we can finally get to the bottom of this.&lt;/p&gt;
&lt;h2&gt;Snowflake Micro-Partitions and Data-Clustering&lt;/h2&gt;
&lt;h3&gt;Micro-partitions&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;All data in Snowflake tables is automatically divided into micro-partitions, which are contiguous units of storage.&lt;/em&gt;
&lt;em&gt;Groups of rows in tables are mapped into individual micro-partitions, organized in a columnar fashion. This size and structure allows for extremely granular pruning of very large tables, which can be comprised of millions, or even hundreds of millions, of micro-partitions.&lt;/em&gt;
&lt;em&gt;Snowflake stores metadata about all rows stored in a micropartition, including:&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;- The range of values for each of the columns in the micro-partition.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;- The number of distinct values.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;- Additional properties used for both optimization and efficient query processing.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/tables-clustering-micropartitions&quot;&gt;According to Snowflake&lt;/a&gt;, these micropartitions are applied automatically to any data inserted into any table, and is primarily used for the goal of &lt;code&gt;Query Pruning&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The micro-partition metadata maintained by Snowflake enables precise pruning of columns in micro-partitions at query run-time, including columns containing semi-structured data. In other words, a query that specifies a filter predicate on a range of values that accesses 10% of the values in the range should ideally only scan 10% of the micro-partitions.&lt;/em&gt;&lt;/p&gt;
&lt;p class=&quot;text-2xl  &quot;&gt;Rabbit Hole Interlude: Partitions vs Indexes&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Snowflake&#x27;s concept of micro-partitions (let&#x27;s just call them &#x27;partitions&#x27;) and the index of most traditional Relational Database Management Systems (RDBMS) serve different but complementary purposes in that they both handle data access patterns.&lt;/p&gt;
&lt;p&gt;Partitions in Snowflake use a &lt;code&gt;columnar&lt;/code&gt; storage format, and play a huge role in how Snowflake optimizes its underlying data storage and retrieval.
Think of them as physical chunks of data allocated together (or closeby) on a disk. Snowflake also keeps metadata about which data is stored in each partition (think of it like a directory of physical stores in a shopping center). This metadata is part of the partition and is stored alongside the actual data.&lt;/p&gt;
&lt;p&gt;This helps the &lt;a
 href=&quot;https://docs.snowflake.com/user-guide/performance-query-exploring&quot;&gt;Snowflake Query Optimizer&lt;/a&gt; with data pruning, aka deciding which parts of a disk are irrelevant to the query it&#x27;s looking to run based on the metadata (directory) that it knows about each partition.
Suppose if you have a &lt;code  &gt;WHERE&lt;/code&gt; clause that specifies a particular &lt;code  &gt;CUSTOMER_ID&lt;/code&gt;, all the query optimizer has to do is go to the location on disk that has that particular &lt;code  &gt;CUSTOMER_ID&lt;/code&gt; as a key, therefore discarding all the other partitions that are irrelevant to its query. This is an oversimplification, which we&#x27;ll discuss a bit later when we talk about data-clustering.&lt;/p&gt;
&lt;p&gt;Similarly in theme (but not in implementation), indexes in MySQL are mainly used to speed up the retrieval or rows from a table.
Think of a data structure that exists separately from the actual table data (&lt;a
 href=&quot;https://medium.com/@MakeComputerScienceGreatAgain/understanding-b-trees-an-in-depth-guide-dcd77f5e995e#:~:text=A%20B%2Dtree%20is%20a,nature%20of%20the%20tree%20structure.&quot;&gt;typically a B-Tree&lt;/a&gt;) that stores the values
for a particular column (or group of columns). The query optimizer can therefore quickly locate the data without having to scan every single row in a table.&lt;/p&gt;
&lt;p&gt;So while both approaches serve the same purpose of speeding up queries by eliminating irrelevant pieces of the data, they go about things quite differently.
To remember is that Snowflake&#x27;s partitions are designed to scan huge amounts of data quickly (across different datasets even), while indexes are much smaller in scope and are usually restricted to a single table.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;Data Clustering&lt;/h3&gt;
&lt;p&gt;In addition to micro-partitions, Snowflake further organizes data in &lt;code&gt;data-clusters&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Typically, data stored in tables is sorted/ordered along natural dimensions (e.g. date and/or geographic regions). This “clustering” is a key factor in queries because table data that is not sorted or is only partially sorted may impact query performance, particularly on very large tables.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;In Snowflake, as data is inserted/loaded into a table, clustering metadata is collected and recorded for each micro-partition created during the process. Snowflake then leverages this clustering information to avoid unnecessary scanning of micro-partitions during querying, significantly accelerating the performance of queries that reference these columns.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;As an analogy, a cluster is a directory of directories (partitions) that allows the Query Optimizer to do an initial pruning of all the micro-partitions it knows about. &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/tables-clustering-micropartitions&quot;&gt;More information here&lt;/a&gt;&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/snowflake_clusters.webp?v=9c5484f6&quot;&gt;&lt;img style=&quot;--max-width: 2754px&quot; width=&quot;1377&quot; height=&quot;734&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/snowflake_clusters.webp?v=9c5484f6&quot; alt=&quot;Snowflake Data Clustering&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
  &lt;figcaption&gt;Source: Snowflake&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p class=&quot;text-2xl  &quot;&gt;Interlude: Clustering Keys&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Before we dive in further, I should briefly mention &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/tables-clustering-keys&quot;&gt;Clustering Keys&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As a table grows in size, &lt;em&gt;the data in some table rows might no longer cluster optimally on desired dimensions.&lt;/em&gt;&lt;br /&gt;
In order to improve the clustering of your table, you can designate one or more columns as the clustering key for your table, which can help improve the clustering of your underlying table micro-partitions. Since you, the developer, (hopefully) understand the access and query patterns in your data a lot better than the query optimizer, you can use clustering keys to provide Snowflake with hints on how to best organize your data.&lt;/p&gt;
&lt;p&gt;&lt;a
 href=&quot;https://medium.com/@datasmiles/unlocking-snowflakes-potential-a-deep-dive-into-micro-partitions-and-clustering-2fd083d83aef&quot;&gt;This excellent article by Harry Tan&lt;/a&gt; does a wonderful job further explaining some of these concepts.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A core design principle of Snowflake is to store metadata alongside its tables for fast lookups. So it is with data clusters. The key metric we&#x27;re interested in for our problem is &lt;code&gt;Clustering Depth&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We&#x27;re about to do a deep-dive here, so strap in and hold on to your butts.&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;320&quot; height=&quot;173&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/butts.webp?v=e46fa410&quot; alt=&quot;Hold on to your butts&quot;&gt;
  &lt;figcaption&gt;What he said&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;&lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/tables-clustering-micropartitions#clustering-depth-illustrated&quot;&gt;According to Snowflake:&lt;/a&gt;
&lt;em&gt;The clustering depth for a populated table measures the average depth (1 or greater) of the overlapping micro-partitions for specified columns in a table. The smaller the average depth, the better clustered the table is with regards to the specified columns.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Because multiple micro-partitions can store different types of metadata about the same row or record in a table, there is a potential for overlap between two or more partitions. This means the Query Optimizer needs to check multiple partitions when planning a query, increasing its planning (Snowflake calls this &lt;code  &gt;compilation&lt;/code&gt;, a remnant from the early Oracle days from its founders) latency. We can reduce this overlap by analysing the &lt;code&gt;clustering depth&lt;/code&gt; of a table.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;- In the beginning, the range of values in all the micro-partitions overlap.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;- As the number of overlapping micro-partitions decreases, the overlap depth decreases.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;- When there is no overlap in the range of values across all micro-partitions, the micro-partitions are considered to be in a constant state (i.e. they cannot be improved by clustering).&lt;/em&gt;&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/clustering_depth_illustration.webp?v=ce8c4f7e&quot;&gt;&lt;img style=&quot;--max-width: 2680px&quot; width=&quot;1340&quot; height=&quot;813&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/clustering_depth_illustration.webp?v=ce8c4f7e&quot; alt=&quot;Snowflake Clustering Depth&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
  &lt;figcaption&gt;Source: Snowflake&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;So, to put in terms our dear friend &lt;a
 href=&quot;https://grugbrain.dev/&quot;&gt;Grug Developer&lt;/a&gt; would be happy with:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Partitions good.&lt;/li&gt;
&lt;li&gt;Clusters also good. Clusters contain partition data. This moar good.&lt;/li&gt;
&lt;li&gt;Small Clustering Depth Number &amp;gt; Large Clustering Depth Number&lt;/li&gt;
&lt;li&gt;Moar &lt;a
href=&quot;https://grugbrain.dev/#grug-on-saying-no&quot;&gt;black think juice&lt;/a&gt; (aka coffee) equals moar thinking power.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We can conclude that our slow query performance must be related to these partitions and clustering. However, Snowflake&#x27;s SnowSight UI does not offer any dashboards or metrics that can tell us more.&lt;/p&gt;
&lt;p&gt;We dig deeper to find that &lt;a
 href=&quot;https://docs.snowflake.com/en/sql-reference/functions/system_clustering_information&quot;&gt;Snowflake does expose us some useful information in the form of &lt;code&gt;SYSTEM$CLUSTERING_INFORMATION&lt;/code&gt; call&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Given that most of our data lives in the &lt;code  &gt;ORDERS_FINAL&lt;/code&gt; table, let&#x27;s focus our efforts here. We run a query on the metadata Snowflake has on our table and look at the results.&lt;/p&gt;
&lt;p&gt;&lt;code  &gt;select system$CLUSTERING_INFORMATION(&#x27;ORDERS_FINAL&#x27;)&lt;/code&gt;;&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;cluster_by_keys&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;CUSTOMER_ID&quot;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;notes&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Clustering key columns contain high cardinality key CUSTOMER_ID which might result in expensive re-clustering. Consider reducing the cardinality of clustering keys. Please refer to https://docs.snowflake.net/manuals/user-guide/tables-clustering-keys.html for more information.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;total_partition_count&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;total_constant_partition_count&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;average_overlaps&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;40.2857&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;average_depth&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;27.1429&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;partition_depth_histogram&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00000&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00001&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00002&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00003&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00004&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00005&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00006&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00007&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00008&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00009&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00010&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00011&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00012&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00013&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00014&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00015&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;00016&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;clustering_errors&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Disclaimer: For illustration purposes only. Yes, I made up these numbers to prove a point lol.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Let&#x27;s explain some of these concepts:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class=&quot;text-l  &quot;&gt;CLUSTER_BY_KEYS&lt;/p&gt;
Indicates that the table is clustered by the CUSTOMER_ID field,
so Snowflake uses this field to organize the data across micro-partitions.
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p class=&quot;text-l  &quot;&gt;NOTES&lt;/p&gt;
The notes mention that the clustering key (BUSINESS_UUID) has high cardinality,
which could lead to expensive re-clustering operations.
High cardinality means the column has a large number of unique values,
which can result in a broad spread of data across many micro-partitions,
potentially reducing the effectiveness of clustering
(granted, this is less likely for an INT data type, but you get the idea)
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p class=&quot;text-l  &quot;&gt;TOTAL_PARTITION_COUNT&lt;/p&gt;
The total number of micro-partitions in the table is 60.
This indicates how the data is divided at the storage level.
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p class=&quot;text-l  &quot;&gt;TOTAL_CONSTANT_PARTITION_COUNT&lt;/p&gt;
Out of 60 partitions, only 12 are constant.
Constant means that these partitions do not overlap with others based on the clustering key.
 This is typically a bad sign, suggesting that most of the data is poorly-clustered.
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p class=&quot;text-l  &quot;&gt;AVERAGE_OVERLAPS AND AVERAGE_DEPTH&lt;/p&gt;
These metrics show that on average, partitions overlap with about 40 other partitions
Additionally, the average depth of these overlaps is around 27.
This suggests severe overlapping and depth, indicating that re-clustering could be beneficial
especially for those partitions that significantly deviate from this average.
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p class=&quot;text-l  &quot;&gt;PARTITION_DEPTH_HISTOGRAM&lt;/p&gt;
This histogram indicates that most partitions
have at least some depth, which is not great.
This likely means that the data is too spread out in most partitions.
&lt;/blockquote&gt;
&lt;p&gt;It looks like our partitioning on the &lt;code  &gt;ORDERS_FINAL_TABLE&lt;/code&gt; (our fact table) is less than optimal, which could be a possible explanation for our slow queries. We can also run this for every single one of our tables by cobbling together a quick script using the &lt;a
 href=&quot;https://docs.snowflake.com/en/developer-guide/snowpark/index&quot;&gt;SnowPark API&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;itertools&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;permutations&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ast&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;snowflake.snowpark&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;snowpark&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;snowflake.snowpark.functions&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;n&quot;&gt;TABLES&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ORDERS_FACT_FINAL&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;CUSTOMER_DIMENSION&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;SHIPPING_DIMENSION&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# rest of tables... ]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snowpark&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_clustering_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;table_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;SELECT SYSTEM$CLUSTERING_INFORMATION(&#x27;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;table_name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&#x27;)&quot;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Executing query: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;clustering_info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;literal_eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clustering_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;total_partition_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clustering_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;total_partition_count&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# prevent div by zero error&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;total_constant_partition_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clustering_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;total_constant_partition_count&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# prevent div by zero error&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;average_depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clustering_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;average_depth&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;average_overlaps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clustering_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;average_overlaps&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;table_name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;total_partition_count&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_partition_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;total_constant_partition_count&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_constant_partition_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;clustering_depth&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;average_depth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;clustering_depth_percentage&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;average_depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_partition_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;clustering_overlap&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;average_overlaps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;clustering_overlap_percentage&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;average_overlaps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_partition_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;            &lt;span class=&quot;s2&quot;&gt;&quot;partition_depth_histogram&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clustering_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;partition_depth_histogram&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;clustering_results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_clustering_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;table_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TABLES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;results_df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clustering_results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;37&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;results_df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#x27;total_partition_count&#x27;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#x27;clustering_depth_percentage&#x27;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;38&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;results_df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;39&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;results_df&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    TOTAL PARTITION COUNT: &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    Number of micro-partitions in the table. Higher count means a more fragmented table&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    TOTAL_CONSTANT_PARTITION_COUNT:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;46&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    Total number of micro-partitions for which the value of the specified columns &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    have reached a constant state (i.e. the micro-partitions will not benefit &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;48&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    significantly from reclustering). The number of constant micro-partitions in a &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    table has an impact on pruning for queries. The higher the number, the more &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    micro-partitions can be pruned from queries executed on the table, which has a &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;51&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    corresponding impact on performance.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    CLUSTERING DEPTH AVG:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    Micro-partitions scanned per key range Lower = better clustering&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;55&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;56&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    CLUSTERING DEPTH:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    (DEPTH / TOTAL PARTITIONS) * 100. A lower percentage == efficient clustering&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;58&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;59&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    CLUSTERING OVERLAP AVG:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    Overlapping micro-partitions per key range. A lower number == less redundancy&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;61&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;62&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    CLUSTERING OVERLAP:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;63&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    (OVERLAP / TOTAL PARTITIONS) * 100. A lower percentage == efficient storage&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;65&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    PARTITION DEPTH HISTOGRAM:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    Distribution of micro-partitions across depth levels, &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;    providing a visual clustering assessment&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;sd&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It looks like most of our tables are well partitioned, with the exception of our &lt;code  &gt;ORDERS_FINAL&lt;/code&gt; table.&lt;/p&gt;
&lt;p&gt;Given that this is our main FACT table, we&#x27;re pretty confident at this point that this is our bottleneck.&lt;/p&gt;
&lt;p&gt;While we can tell that the partitioning on this table is bad, we still don&#x27;t know WHY.&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;657&quot; height=&quot;640&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/y_tho.webp?v=59e14d8f&quot; alt=&quot;Y Tho meme&quot;&gt;
&lt;/figure&gt;
&lt;p&gt;After sufficient place-forehead-on-desk-at-high-velocity (aka bang head on desk), we decided to ask the good folks at Snowflake, who were awesome enough to let us sit down with their Field CTO team and have us barrage them with the laundry list of questions we gathered after trying to reverse engineer some of the mechanics of their platform.&lt;/p&gt;
&lt;h2&gt;Snowflake Micro-Partitions: The Plot Thickens&lt;/h2&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;1024&quot; height=&quot;768&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/charlie_sunny_conspiracy.webp?v=8cf33eb8&quot; alt=&quot;Sunny In Philadelphia Conspiracy&quot;&gt;
  &lt;figcaption&gt;The team&#x27;s mental state at this point&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;With the help of the Snowflake Field CTO team, we investigated the partitioning on our tables. Using some internal tooling, the Snowflake team was quickly able to find that our tables were adding more and more micro-partitions by the minute! Y Tho?&lt;/p&gt;
&lt;p&gt;The answer: because micro-partitions are &lt;code  &gt;immutable&lt;/code&gt;. The immutable nature of micro-partitions allows for the lookup and usage of said partitions to be MUCH more performant at query-time. But the flipside of this performance is that, whenever data gets updated or deleted, Snowflake will add new micro-partitions instead of modifying existing ones.&lt;/p&gt;
&lt;p&gt;To offset inefficiencies caused by this immutability (and thus the need to create new partitions), Snowflake also has periodic asynchronous automatic re-clustering tasks running in the background, which reorganizes the data across its different nodes and helps the queries remain performant.&lt;/p&gt;
&lt;p&gt;This is further confirmed by &lt;a
 href=&quot;https://www.snowflake.com/en/blog/data-vault-technique-immutable-storage/&quot;&gt;this Snowflake Blog article from May 2022&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Snowflake micro-partitions immutable files that are not visible to the Snowflake user; rather, the table acts as a container of micro-partitions and that is what the user will interact with. Some of the interactions are:&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;- A SQL INSERT operation that will load new records as new micro-partitions....&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Looking back at our initial ingestion architecture, which component makes continuous updates to the tables? Right, THE TRIGGERED TASK.&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;680&quot; height=&quot;674&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/triggered_thomas.webp?v=ed31da0b&quot; alt=&quot;Thomas the Tank Triggered&quot;&gt;
  &lt;figcaption&gt;Thomas is done f*cking around&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2&gt;Triggered Task == VILLAIN?&lt;/h2&gt;
&lt;p&gt;Let&#x27;s look again at our triggered task definition:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;c1&quot;&gt;-- ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MERGE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;USING&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;                    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_STAGING_STREAM&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUALIFY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ROW_NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OVER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- this QUALIFY statement is important - see below.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PARTITION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MATCHED&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- If relevant records are found in ORDERS_FINAL, update values with the stream&#x27;s values, as we know that&#x27;s the latest version of the record&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;c1&quot;&gt;-- ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We know that this task runs each time it detects new data in the stream it&#x27;s &amp;quot;listening&amp;quot; to. If the &lt;code  &gt;ORDERS_STAGING_STREAM&lt;/code&gt; is constantly receiving data, even duplicates or minor changes, the &lt;code  &gt;MERGE&lt;/code&gt; operation in this task will repeatedly attempt to update the same records.&lt;/p&gt;
&lt;p&gt;Additionally, the &lt;code  &gt;QUALIFY ROW_NUMBER() OVER (...) = 1&lt;/code&gt; statement selects the latest record for each &lt;code  &gt;CUSTOMER_ID&lt;/code&gt; and &lt;code  &gt;ORDER_ID&lt;/code&gt;. When the underlying data changes, this is fine.
But what if it&#x27;s just a minor, irrelevant change (e.g. timestamp) and all other data remains the same? Still, the &lt;code&gt;MERGE&lt;/code&gt; statement sees this as a new record and will update the &lt;code  &gt;ORDERS_FINAL&lt;/code&gt; entry accordingly.
Since Snowflake will create new immutable micro-partitions on a record update, frequent updates (caused by the task) will lead to an &lt;code  &gt;exponential increase&lt;/code&gt; in the number of micro-partitions created for a table.&lt;/p&gt;
&lt;p&gt;Because of this excessive number of micro-partitions, the overlapping data between these partitions will also increase dramatically - which explains the large &lt;code  &gt;overlap depth&lt;/code&gt; from our previous analysis.&lt;/p&gt;
&lt;p&gt;So now, &lt;em&gt;the queries now have to scan a lot more micro-partitions in order to retrieve the data, slowing down the entire query!&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/tables-auto-reclustering&quot;&gt;Snowflake&#x27;s auto-clustering background processes&lt;/a&gt; are also having a much harder time keeping up with reconciling the partitions to optimize the data organization, which grinds the whole thing to a near crawl.&lt;/p&gt;
&lt;p&gt;This was confirmed by the Snowflake team:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;quot;One potentially challenging pattern that some customers have encountered is with a MERGE or UPDATE against a random subset of the rows in the target table. Even if only an extremely small percentage of the rows in the target table are updated, if those rows are distributed across a high number of micro-partitions, then  these micro-partitions (since micro-partitions are immutable) all rows within will need to be completely rewritten to new micro-partitions. At a minimum, this can result in poor DML performance in the data pipeline, and in some cases cause extremely high data latency. This can also adversely affect the clustering of the table, negatively impacting query performance against the target table due to poor pruning.&amp;quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Mystery solved!&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;500&quot; height=&quot;666&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/scooby_doo.webp?v=41fc7251&quot; alt=&quot;Scooby Doo villain revealed&quot;&gt;
  &lt;figcaption&gt;&quot;And I would&#x27;ve gotten away with it if it wasn&#x27;t for those meddling micro-partitions!&quot;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2&gt;The solution: Deferred Merge Strategy&lt;/h2&gt;
&lt;p&gt;Great! Root cause identified and proven. Now, what do we do about it?&lt;/p&gt;
&lt;p&gt;The main issue we saw earlier is not necessarily the &lt;code  &gt;MERGE&lt;/code&gt; statement of the triggered task itself, but rather the &lt;em&gt;frequency&lt;/em&gt; with which the task executes said &lt;code  &gt;MERGE&lt;/code&gt; operations.&lt;/p&gt;
&lt;p&gt;The basic idea of a &lt;code  &gt;Deferred Merge&lt;/code&gt; approach is a slight variation of our initial setup, with the main difference being instead of having a triggered task continuously running &lt;code  &gt;MERGE&lt;/code&gt; statements (and hence causing the explosion in micro-partitions), we &amp;quot;defer&amp;quot; the merging of data to a scheduled task instead, which runs at a regular pre-defined interval.&lt;/p&gt;
&lt;p&gt;But how do you handle keeping your data de-duplicated and up to date?&lt;/p&gt;
&lt;p&gt;The proposed solution is to replace our initial setup from this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Node 1: Staging table (entrypoint)&lt;/p&gt;
&lt;p&gt;Node 2: Staging Stream (captures new data being inserted into node 1)&lt;/p&gt;
&lt;p&gt;Node 3: Triggered Task (checks for new data entering Node 2 and executes MERGE)&lt;/p&gt;
&lt;p&gt;Node 4: Destination (aka Final) table with deduplicated records holding latest version -&amp;gt; targeted by queries.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/initial_setup_diagram.svg?v=ea5c3ee7&quot;&gt;&lt;img style=&quot;--max-width: 1258px&quot; width=&quot;629&quot; height=&quot;2323&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/initial_setup_diagram.svg?v=ea5c3ee7&quot; alt=&quot;Initial Setup&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
&lt;/figure&gt;
&lt;p&gt;To this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Node 1: Staging table (entrypoint)&lt;/p&gt;
&lt;p&gt;Node 2: Staging Stream (captures new data being inserted into node 1)&lt;/p&gt;
&lt;p&gt;Node 3: Triggered Task (checks for new data entering Node 2 and executes MERGE)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Node 4: DELTA table with deduplicated records holding latest version.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Node 5: Scheduled task running at a set interval which MERGES the data from Node 4 into the final table (aka node 6) AND deletes the processed records from the Delta table.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Node 6: Destination (aka Final) table with deduplicated records holding latest version.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/deferred_merge_setup.svg?v=58eb8f3c&quot;&gt;&lt;img style=&quot;--max-width: 2324px&quot; width=&quot;1162&quot; height=&quot;2403&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/deferred_merge_setup.svg?v=58eb8f3c&quot; alt=&quot;Deferred Merge Setup&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
&lt;/figure&gt;
&lt;h3&gt;Data Freshness Concerns&lt;/h3&gt;
&lt;p&gt;What about the queries? How to ensure data freshness if our &lt;code  &gt;FINAL&lt;/code&gt; table only gets updated on a schedule?&lt;/p&gt;
&lt;p&gt;The queries should now target both Node 4 (Delta table) and Node 6 (Final table) using a &lt;code  &gt;UNION ALL NOT EXISTS&lt;/code&gt; clause. According to Snowflake:&lt;/p&gt;
&lt;p  &gt;This approach uses a UNION ALL to blend the delta data with the base, with a NOT EXISTS added to ensure that a given key (ID) is only emitted from the base data if it is not emitted by the delta data.&lt;/p&gt;
In our case, the `base` data is coming from Node 6, and joined via a UNION ALL with Node 4 to ensure we only return the latest unique records.
&lt;p&gt;So how does this solve our problem?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Because the triggered task no longer continuously updates our FINAL table (Node 6) and instead we use a scheduled task, the MERGE operation happens a LOT less frequently, allowing Snowflake and its auto-clustering to operate efficiently and actually keep the number of micro-partitions low.&lt;/li&gt;
&lt;li&gt;We are still using a triggered task to get data into Node 4 (Delta table), but the scheduled task is also responsible for periodically deleting rows from this delta table after they&#x27;ve been processed. This means that this DELTA table will stay small, with at most a few thousand records (depending on the time interval used by Node 5).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Final Query&lt;/h3&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;WITH&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DELTA_DATA&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_DELTA&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUALIFY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ROW_NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OVER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PARTITION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_FINAL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;EXISTS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DELTA_DATA&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;D&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ALL&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DELTA_DATA&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;37&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;38&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;39&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;&quot;Shipped At&quot;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_DIMENSION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_DIMENSION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_DIMENSION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_DIMENSION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;46&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASE_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The full document with the proposed Deferred Merge approach as outlined by Snowflake &lt;a
 href=&quot;https://quickstarts.snowflake.com/guide/connectors_example_push_based_java/img/e5a0394034661f7.pdf&quot;&gt;can be found here&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Final verdict&lt;/h2&gt;
&lt;p&gt;We implemented the new proposed deferred merge architecture to run side by side with the initial setup and pointed our queries to the new setup.&lt;/p&gt;
&lt;p&gt;The results:&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/query_after.webp?v=be937afa&quot;&gt;&lt;img style=&quot;--max-width: 3440px&quot; width=&quot;1720&quot; height=&quot;252&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/query_after.webp?v=be937afa&quot; alt=&quot;Query After&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
&lt;/figure&gt;
&lt;p&gt;From 21 seconds to 2.1 seconds. That&#x27;s a whopping &lt;span class=&quot;text-xl  &quot;&gt;90% performance boost&lt;/span&gt; !!!&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;150&quot; height=&quot;150&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/self-high-five.webp?v=748ef17f&quot; alt=&quot;Self High Five&quot;&gt;
&lt;/figure&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;After a few weeks of blood, sweat, tears and a h*ll of a lot of reverse engineering, you finally did it.&lt;/p&gt;
&lt;p&gt;The performance improvements gained by Deferred Merge range from 70% on the slower end of the spectrum to 90% on the faster end.&lt;/p&gt;
&lt;p&gt;The queries are now humming along beautifully and the team can continue building some pretty awesome products on top of some pretty awesome data solutions.
At the same time, this work is never done, so continuous monitoring and tweaking is always appropriate.&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;160&quot; height=&quot;64&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/frodo_its_done.webp?v=f20fa8a4&quot; alt=&quot;Frodo It is done&quot;&gt;
&lt;/figure&gt;
&lt;blockquote&gt;
&lt;p&gt;The average durations of these queries are used as an example from our actual implementations, and they do represent real production results.&lt;/p&gt;
&lt;p&gt;Remember these are averages so the range of values will vary depending on system load, time of day, etc. AKA your mileage may vary.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Roll Credits&lt;/h2&gt;
&lt;p&gt;A massive thanks to my team at FreshBooks and the team at Snowflake. You all know who you are!&lt;/p&gt;
&lt;h2&gt;What&#x27;s next?&lt;/h2&gt;
&lt;p&gt;I&#x27;ll be writing more in the near future about Snowflake&#x27;s architecture, as well as some other measures my team and I have taken to ensure our data pipeline is as fast and reliable as possible. Stay tuned!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Facing the SnowStorm: Part 1</title>
    <link rel="alternate" type="text/html" href="https://dorkusmalorkus.dev/posts/deferred-merge-snowflake-performance-part-1"/>
    <id>https://dorkusmalorkus.dev/posts/deferred-merge-snowflake-performance-part-1</id>
    <published>2024-12-22T00:00:00Z</published>
    <updated>2024-12-22T00:00:00Z</updated>
    <summary>MERGE operations in Snowflake can really slow things down, especially when you&#x27;re dealing with high churn tables that end up all fragmented. In part 1 of this 2-part series, we’re diving into the problem and take you on a tale of woe, despair, lots of coffee and some glimmering nuggets of wisdom in the land of Snowflakes.</summary>
    <category term="databases"/>
    <category term="performance"/>
    <category term="snowflake-db"/>
    <content type="html">&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In this post, we&#x27;ll explore how frequent MERGE operations in Snowflake can drastically degrade query performance, and how implementing a &lt;code&gt;Deferred Merge strategy&lt;/code&gt; improved our production query speeds by up to 90%.&lt;/p&gt;
&lt;p&gt;Over the past few months, my team and I have had the opportunity to work on implementing a new data pipeline in order to provide large-scale reports to an end-user.
The design of this data pipeline was mostly an event-driven architecture, with the final destination being a Snowflake Cloud instance.&lt;/p&gt;
&lt;p&gt;This article will discuss some of the pitfalls the team encountered, the largest one being that executing frequent (continuous) MERGE updates on Snowflake tables lead to fragmented data, inefficient partitioning and horrible query compilation/execution latency.
If you are a Snowflake user managing large datasets that require very low latency from ingestion to retrieval and have to deal with frequent updates to source tables, read on!&lt;/p&gt;
&lt;h2&gt;Initial Setup&lt;/h2&gt;
&lt;p&gt;As stated in the introduction, the goal of the data pipeline was to capture data from different source applications, leveraging events as the primary mechanism, the details of which will be the subject of a future post.
The main way we are inserting data into the initial Snowflake table is via the &lt;a
 href=&quot;https://github.com/snowflakedb/snowflake-ingest-java&quot;&gt;Snowflake Java Ingest SDK&lt;/a&gt;, which leverages &lt;a href=&quot;https://docs.snowflake.com/en/user-guide/data-load-snowpipe-intro&quot;&gt;SnowPipe Streaming&lt;/a&gt; under the hood.&lt;/p&gt;
&lt;p&gt;For the sake of simplicity (and to not get into hot water with my employer) we&#x27;ll be using a fictional data model to illustrate, namely that of an e-commerce platform &lt;em&gt;(bonus points for originality /s)&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;Data Model&lt;/h3&gt;
&lt;p&gt;We&#x27;ll be modelling our data using the tried-and-true &lt;code&gt;dimensional model&lt;/code&gt; (aka &amp;quot;Star Schema&amp;quot;), as popularized by Ralph Kimball et al in the excellent classic &lt;a
 href=&quot;https://www.kimballgroup.com/data-warehouse-business-intelligence-resources/books/data-warehouse-dw-toolkit&quot;&gt;Data Warehouse Toolkit&lt;/a&gt; book.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The data model we&#x27;re creating here mainly serves as illustration, since the focus of this post is on how our data moves through the Snowflake Ingestion Pipeline.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;Defining the Staging table&lt;/h3&gt;
&lt;p&gt;Our first table in Snowflake has the following schema:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;orders_staging&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_uuid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;BINARY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;customer_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_date&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;TIMESTAMP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- e.g., &#x27;Pending&#x27;, &#x27;Shipped&#x27;, &#x27;Delivered&#x27;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shipping_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;TIMESTAMP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_uuid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A few things to note right away:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This table has a &lt;code  &gt;_staging&lt;/code&gt; suffix.&lt;/li&gt;
&lt;li&gt;This is an &lt;code  &gt;APPEND-ONLY&lt;/code&gt; table. Why, you ask? Good question! We&#x27;ll get to that later. For now, just know that this means that every new &lt;code  &gt;INSERT&lt;/code&gt; will have a globally unique &lt;code  &gt;record_uuid&lt;/code&gt;, which is generated at the application-level during initial ingestion and is just added as a row to the table without any further checks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Leveraging Snowflake Streams&lt;/h3&gt;
&lt;p&gt;To detect changes in the &lt;code  &gt;ORDERS_STAGING&lt;/code&gt; table, we can leverage &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/streams-intro&quot;&gt;Snowflake Streams&lt;/a&gt;. According to Snowflake&#x27;s official documentation:
&lt;em&gt;A stream object records data manipulation language (DML) changes made to tables, including inserts, updates, and deletes, as well as metadata about each change, so that actions can be taken using the changed data. This process is referred to as change data capture (CDC).&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let&#x27;s create an append-only stream:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_STAGING_STREAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_STAGING&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append_only&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;APPEND-ONLY STREAMS in SNOWFLAKE&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;An append-only stream exclusively tracks row &lt;em&gt;inserts&lt;/em&gt; on the underlying table. Update, delete, and truncate operations are not captured by append-only streams. &lt;em&gt;An append-only stream specifically returns the appended rows, making it notably more performant than a standard stream for extract, load, and transform (ELT), and similar scenarios reliant solely on row inserts.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Essentially, &lt;code  &gt;ORDERS_STAGING_STREAM&lt;/code&gt; acts as a &lt;em&gt;listener&lt;/em&gt; for any &lt;code  &gt;INSERT&lt;/code&gt; operations on the &lt;code  &gt;ORDERS_STAGING&lt;/code&gt; table. So far, so good. But what do we do with this tracked delta on the data?&lt;/p&gt;
&lt;h3&gt;Snowflake Triggered Tasks&lt;/h3&gt;
&lt;p&gt;Recall the &lt;code  &gt;APPEND-ONLY&lt;/code&gt; nature of the staging table?
The reason for this is that we&#x27;re dealing with an event-driven architecture. Typically, event messages in such systems have the following characteristics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We cannot control the order in which the events are processed in a distributed system with multiple publishers and consumers.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We cannot guarantee uniqueness of messages, so we need must assume that events might be published and consumed &lt;em&gt;at least once&lt;/em&gt;, potentially multiple times.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, how do we handle potential duplicate messages or out-of-order messages when querying for the data?&lt;/p&gt;
&lt;p&gt;That&#x27;s where two more elements come into play: &lt;code  &gt;triggered tasks&lt;/code&gt; and a &lt;code  &gt;ORDERS_FINAL&lt;/code&gt; table. Let&#x27;s first discuss the &lt;code  &gt;_FINAL&lt;/code&gt; table.&lt;/p&gt;
&lt;p&gt;This table essentially has the same schema as the &lt;code  &gt;_STAGING&lt;/code&gt; table we&#x27;ve seen before:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;orders_final&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_uuid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;BINARY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;customer_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_date&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;TIMESTAMP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shipping_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;created_at&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;TIMESTAMP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_uuid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The purpose of the &lt;code  &gt;ORDERS_FINAL&lt;/code&gt; table is to hold the latest, de-duplicated, and normalized version of the records for our final queries.&lt;/p&gt;
&lt;p&gt;So, how do we maintain the most-up-to-date and deduplicated version of each record? This is where &lt;code  &gt;triggered tasks&lt;/code&gt; come in.
According to the &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/tasks-intro&quot;&gt;Snowflake Documentation on Triggered Tasks&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Tasks use user defined functions to automate and schedule business processes. With a single task you can perform a simple to complex function in your data pipeline. (...) You can also combine tasks with table streams for continuous ELT workflows to process recently changed data. (...) Tasks can also be used independently to generate periodic reports by inserting or merging rows into a report table or perform other periodic work.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let&#x27;s define our triggered task:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_STAGING_TO_FINAL_TASK&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USER_TASK_MINIMUM_TRIGGER_INTERVAL_IN_SECONDS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- leave at least 30 seconds between each run&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream_has_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#x27;ORDERS_STAGING_STREAM&#x27;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- only run if the underlying stream has data captured&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MERGE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;USING&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;                    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_STAGING_STREAM&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUALIFY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ROW_NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OVER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- this QUALIFY statement is important - see below.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PARTITION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MATCHED&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- If relevant records are found in ORDERS_FINAL, update values with the stream&#x27;s values, as we know that&#x27;s the latest version of the record&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_DATE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_DATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MATCHED&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- if no match found in ORDERS_FINAL, records are new&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INSERT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_DATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;VALUES&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RECORD_UUID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;37&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;38&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;39&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_DATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&#x27;s highlight some crucial parts of the task definition once more for the cool kids in the back:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;n&quot;&gt;USER_TASK_MINIMUM_TRIGGER_INTERVAL_IN_SECONDS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/* Sets the minimum interval between consecutive task triggers, &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;which prevents the task from executing too frequently. &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;This can help manage performance and limit unnecessary runs. */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream_has_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#x27;ORDERS_STAGING_STREAM&#x27;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/* Task should only run when there is new data available in the &#x27;ORDERS_STAGING_STREAM&#x27; &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;which tracks changes to the staging table and ensures that the &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;task only processes new or updated records. */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;n&quot;&gt;MERGE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;USING&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/* Synchronizes the &#x27;ORDERS_FINAL&#x27; table with the latest data &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;from the `ORDERS_STAGING_STREAM`. Checks for matches based on &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;specified keys and decides whether to update existing &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;records or insert new ones. */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;n&quot;&gt;QUALIFY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ROW_NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OVER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PARTITION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/* This clause is critical for ensuring that only the most recent &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;record for each unique combination of &#x27;CUSTOMER_ID&#x27; and &#x27;ORDER_ID&#x27; &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;is used in the merge, by filtering out older duplicates */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STG_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/* Set conditions under which records from the stream and &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;the final table are considered a match. */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MATCHED&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;UPDATE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/* If matching records are found, update the matches in the &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;final table with the data from the staging stream. &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;This is an UPDATE IN PLACE operation. */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MATCHED&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INSERT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/* If no matching record is found, insert the new record into the final table.*/&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With this, we now have a fully functional end-to-end ingestion process, ensuring we always have the most up-to-date and relevant records in our &lt;code  &gt;ORDERS_FINAL&lt;/code&gt; table, which we can now target with our final end-user queries.&lt;/p&gt;
&lt;p&gt;Let&#x27;s illustrate this process visually:&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/initial_setup_diagram.svg?v=ea5c3ee7&quot;&gt;&lt;img style=&quot;--max-width: 1258px&quot; width=&quot;629&quot; height=&quot;2323&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/initial_setup_diagram.svg?v=ea5c3ee7&quot; alt=&quot;Initial Setup&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
&lt;/figure&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Additional note on Snowflake Streams&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Once the Triggered Task has consumed the data from the underlying append-only stream, that data is &lt;code&gt;deleted&lt;/code&gt; from the stream.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Satisfied with this approach, you now repeat the process for all other tables in your dimensional model.&lt;/p&gt;
&lt;p&gt;The final step is to define our end-user query. Suppose we want a basic report of shipped orders with relevant customer metadata.
We&#x27;ll write a simple SELECT statement on the &lt;code  &gt;ORDERS_FINAL&lt;/code&gt; table with &lt;code  &gt;JOIN&lt;/code&gt; clauses into the relevant dimension tables:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDER_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUANTITY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CREATED_AT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;&quot;Shipped At&quot;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_DIMENSION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_DIMENSION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_DIMENSION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STATUS_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_DIMENSION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUSTOMER_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ORDERS_FINAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SHIPPING_ID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Winter is Coming for your Query Performance&lt;/h2&gt;
&lt;p&gt;Everything seems to be running smoothly. Your pipeline is efficient, reports are accurate, and stakeholders are pleased. Executives are patting you on the back, and your fellow engineers&#x27; songs of your heroic deeds echo across the open floorplan office.&lt;/p&gt;
&lt;p&gt;Suddenly, your pager alerts you to increasing latency. Observability metric dashboards show the end-user query latency climbing dramatically. What is happening?&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;716&quot; height=&quot;402&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/wake_up.webp?v=662e94d5&quot; alt=&quot;Wake Up&quot;&gt;
  &lt;figcaption&gt;That familiar pager sound at 3AM...&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Using &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/ui-snowsight&quot;&gt;Snowflake&#x27;s SnowSight UI&lt;/a&gt;&#x27;s excellent cascading GUI interface for query profiling, you notice something unusual:&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/query_before.webp?v=2e06d017&quot;&gt;&lt;img style=&quot;--max-width: 3468px&quot; width=&quot;1734&quot; height=&quot;248&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/query_before.webp?v=2e06d017&quot; alt=&quot;Snowflake initial query performance&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
&lt;/figure&gt;
&lt;p&gt;Excuse me? The query duration shows &lt;code&gt;21.0 seconds&lt;/code&gt; (!!!!). For a simple SELECT query with a handful of inner joins? That is excessively long for an end-user query, before even considering network latency and application processing time. Well, this is not good.&lt;/p&gt;
&lt;p&gt;Examining the query details in the &lt;a
 href=&quot;https://docs.snowflake.com/user-guide/ui-snowsight-activity&quot;&gt;SnowSight Query Profiler&lt;/a&gt; reveals:&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/query_metrics_detailed.webp?v=6d2d39fa&quot;&gt;&lt;img style=&quot;--max-width: 492px&quot; width=&quot;246&quot; height=&quot;555&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/query_metrics_detailed.webp?v=6d2d39fa&quot; alt=&quot;Snowflake initial query detailed view&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
&lt;/figure&gt;
&lt;p&gt;Our query is spending an awful lot of time on &lt;a
 href=&quot;https://docs.snowflake.com/user-guide/ui-snowsight-activity#data-access-and-generation-operators&quot;&gt;&lt;code&gt;TableScan&lt;/code&gt;&lt;/a&gt; operations.&lt;/p&gt;
&lt;p&gt;This means the query spends a lot of time going through the tables in order to find the relevant data. What gives? Our data model is so clean and our query is not very complex.&lt;/p&gt;
&lt;p&gt;While you furrow your brow examining these query statistics, your stakeholders come tapping you on the (virtual) shoulder with that familiar look on their face:&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;250&quot; height=&quot;215&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/samuel_l_jackson_stare.webp?v=861b3ee3&quot; alt=&quot;Samuel L Jackson Stare&quot;&gt;
  &lt;figcaption&gt;Just kidding. The PMs I work with are just absolute wonderful human beings ^_^ &lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Your breathing intensifies. Beads of sweat start rolling down your face. You may be on to something, but we have real users out there with super slow loading reports! Quickly, you huddle with the team and compile a list of potential solutions. You split your team of engineers and divide and conquer:&lt;/p&gt;
&lt;p class=&quot;text-xl  &quot;&gt;Simplify Query Complexity&lt;/p&gt;
&lt;p&gt;Even though our original query is pretty simple, we can reduce the number columns in our SELECT statement and further refine our WHERE clauses to better leverage our newfound knowledge about micro-partitions. The more we can help the Query Optimizer do its job, the better.&lt;/p&gt;
&lt;p class=&quot;text-xl  &quot;&quot;&gt;Enable/Disable Query Acceleration Performance&lt;/p&gt;
&lt;p&gt;Topic for a future post! In the meantime, &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/query-acceleration-service&quot;&gt;have some tasty docs.&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;text-xl  &quot;&quot;&gt;Enable/Disable Search Optimization Service&lt;/p&gt;
&lt;p&gt;Again, stay tuned! &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/search-optimization-service&quot;&gt;Here&#x27;s some moar yummy docs.&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;text-xl  &quot;&quot;&gt;Split ingestion and querying activities into separate warehouses&lt;/p&gt;
&lt;p&gt;Separate the workloads by dedicating distinct Virtual Warehouse instances to ingestion and querying tasks, respectively. Wait, what&#x27;s a warehouse? Right! Good question. We&#x27;ll get to this in a minute.&lt;/p&gt;
&lt;p class=&quot;text-xl  &quot;&quot;&gt;Use Materialized Views as pre-compute for query&lt;/p&gt;
&lt;p&gt;We actually did spend some time on this one. While it was a neat experiment and did provide some speedup on the query (since we essentially just pre-computed the data into a giant wide table), it made ingestion, business logic and maintenance unnecessarily complex. Again, stay tuned for more on this in a future post, where we&#x27;ll be talking materialized views and task graphs!&lt;/p&gt;
&lt;p class=&quot;text-xl   whitespace-pre-wrap break-words overflow-auto&quot;&gt;Increased USER_TASK_MINIMUM_TRIGGER_INTERVAL_IN_SECONDS&lt;/p&gt;
&lt;p&gt;Another theory: maybe the ingestion processes are in resource contention with the query processes? If you&#x27;ll recall, the &lt;code  &gt;USER_TASK_MINIMUM_TRIGGER_INTERVAL&lt;/code&gt; &lt;em&gt;sets the minimum interval between consecutive task triggers, which prevents the task from executing too frequently.&lt;/em&gt; So, if we force a larger pause between task runs, maybe that will leave some breathing room for the queries and reduce resource contention between compute nodes? Sadly, this did not prove to be all that effective.&lt;/p&gt;
&lt;blockquote&gt;&lt;/blockquote&gt;
&lt;p class=&quot;text-xl  &quot;&quot;&gt;Added RELY to primary key constraints&lt;/p&gt;
&lt;p&gt;What if we reduced redundant joins in our query? &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/join-elimination#setting-the-rely-constraint-property-to-eliminate-unnecessary-joins&quot;&gt;&lt;em&gt;Snowflake does not enforce UNIQUE, PRIMARY KEY, and FOREIGN KEY constraints on standard tables&lt;/em&gt;.&lt;/a&gt;. So, if we&#x27;re confident in our data model and its inherent relationships between the different tables, we can use the &lt;code  &gt;RELY&lt;/code&gt; constraint on &lt;code  &gt;UNIQUE&lt;/code&gt;, &lt;code  &gt;PRIMARY KEY&lt;/code&gt;, &lt;code  &gt;FOREIGN KEY&lt;/code&gt; constraints to help the query optimizer eliminate unnecessary joins.&lt;/p&gt;
&lt;p&gt;&lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/join-elimination&quot;&gt;More details here.&lt;/a&gt;.&lt;/p&gt;
&lt;p class=&quot;text-xl  &quot;&quot;&gt;Modify or disable clustering&lt;/p&gt;
&lt;p&gt;This also made very little difference. But what is clustering? That&#x27;s what the other half of our team has been working on, so they&#x27;ll take over from us in a minute!&lt;/p&gt;
&lt;p&gt;First things first, let&#x27;s talk warehouses.&lt;/p&gt;
&lt;h2&gt;Snowflake Virtual Warehouses Explored&lt;/h2&gt;
&lt;p&gt;Remember when I talked about &lt;em&gt;Split ingestion and querying activities into separate warehouses&lt;/em&gt; ? Yeah, that didn&#x27;t work. In order to understand why, we need to take a closer look at what a warehouse actually is.&lt;/p&gt;
&lt;p&gt;A Snowflake Virtual Warehouse is a cluster of compute resources in Snowflake that processes queries and transactions. Unlike a traditional RDBMS system with static resources, warehouses in Snowflake can be scaled dynamically based on the workload. They provide a completely isolated query execution environment and other features like pausing and auto-suspending of warehouses.
This allows Snowflake to do some pretty cool stuff where they can re-use idle resources / nodes / warehouses to buff up compute intensive tasks on other client&#x27;s warehouses, basically &#x27;borrowing&#x27; compute power without charging you, the client, for it. In return, whenever you need a bit of extra juice for a workload, they can do the same for you. What&#x27;s that? You want docs? &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/warehouses&quot;&gt;Of course, here ya go.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;An important consideration when it comes to warehouses is their size, which ranges from XS all the way to 6X-Large as of time of writing.&lt;/p&gt;
&lt;p&gt;According to Snowflake:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The size of a warehouse can impact the amount of time required to execute queries submitted to the warehouse, particularly for larger, more complex queries. In general, query performance scales with warehouse size because larger warehouses have more compute resources available to process queries.&lt;/p&gt;
&lt;p&gt;If queries processed by a warehouse are running slowly, you can always resize the warehouse to provision more compute resources.&lt;/p&gt;
&lt;p&gt;Check out &lt;a
 href=&quot;https://articles.analytics.today/snowflake-virtual-warehouses-what-you-need-to-know&quot;&gt;this post&lt;/a&gt; to learn more about Snowflake Compute layer and its Virtual Warehouse architecture.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Previously, we used a single XS warehouse for both ingestion and querying workloads. Instead, we decided to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Assign an &lt;code  &gt;XS&lt;/code&gt; warehouse for data ingestion.&lt;/li&gt;
&lt;li&gt;Assign an &lt;code  &gt;S&lt;/code&gt; warehouse for querying.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Data freshness is not impacted by this split because warehouses are part of the compute layer, which are totally separated from the storage layer. Therefore, data doesn&#x27;t need to be replicated between warehouses.&lt;/p&gt;
&lt;p&gt;The storage layer is fully managed by Snowflake and is built on cloud storage solutions like Amazon S3, Google Cloud Storage or Microsoft Azure Blob Storage. This centralized storage is scalable, secure, and independent of compute resources. In the origin days of Snowflake, they decided to simply offload the storage to cloud vendors so they could specify more on the querying, compute layers and caching layers, which turned out to be a very smart move in the end (no need to build your own data centers if you can rent them for a fraction of the cost, amirite?).&lt;/p&gt;
&lt;p&gt;So, in theory, we can add as many warehouses as we want - they would all have instant access to the same data. We (as the client) just tell Snowflake which cloud provider we want to use and they take care of the provisioning. The data in the respective buckets is usually still their proprietary data format, although they do also support open-source formats like Parquet, Avro, XML, etc...&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/snowflake_warehouse.svg?v=db26659f&quot;&gt;&lt;img style=&quot;--max-width: 2650px&quot; width=&quot;1325&quot; height=&quot;640&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/snowflake_warehouse.svg?v=db26659f&quot; alt=&quot;Snowflake Platform Overview&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
  &lt;figcaption&gt;Snowflake Platform Overview&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;So where did the warehouse resizing leave us?&lt;/p&gt;
&lt;p&gt;In theory, moar compute power == faster queries, right? &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/warehouses-overview#impact-on-query-processing&quot;&gt;After all, that&#x27;s what the docs tell us.&lt;/a&gt;
&lt;code&gt;WRONG. Increasing the warehouse size did not improve query latency.&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;meme-pair&quot;&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;548&quot; height=&quot;455&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/unlimited_power.webp?v=741e6592&quot; alt=&quot;Unlimited Power&quot;&gt;
  &lt;figcaption&gt;EXPECTATION&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;360&quot; height=&quot;202&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/memes/obi_wan_confused.webp?v=9c863d17&quot; alt=&quot;Confused&quot;&gt;
  &lt;figcaption&gt;REALITY&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;p&gt;As it turns out, &lt;a
 href=&quot;https://docs.snowflake.com/en/user-guide/warehouses-overview#query-processingrand-concurrency&quot;&gt;we missed a critical detail&lt;/a&gt; that tells us whether or not it was the available compute resources being the bottleneck for our query speed:
&lt;em&gt;If the warehouse does not have enough remaining resources to process a query, the query is queued, pending resources that become available as other running queries complete.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Snowflake provides insight into this via its &lt;a
 href=&quot;https://community.snowflake.com/s/article/Understanding-Queuing&quot;&gt;&lt;code  &gt;INFORMATION_SCHEMA.QUERY_HISTORY&lt;/code&gt;, which shows query queuing statistics.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A more holistic overview can also be seen in the Warehouses section of the SnowSight Admin UI.&lt;/p&gt;
&lt;figure class=&quot;diagram&quot;&gt;
  &lt;a class=&quot;zoom&quot; href=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/warehouse_activity.webp?v=200d0cfe&quot;&gt;&lt;img style=&quot;--max-width: 4312px&quot; width=&quot;2156&quot; height=&quot;341&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/deferred_merge/warehouse_activity.webp?v=200d0cfe&quot; alt=&quot;Warehouse Overview&quot;&gt;&lt;span class=&quot;visually-hidden&quot;&gt; (open full size)&lt;/span&gt;&lt;/a&gt;
&lt;/figure&gt;
&lt;p&gt;Over the past two weeks, there were no queued queries. So, this means compute resources were sufficient after all?&lt;/p&gt;
&lt;p&gt;Sigh...back to the drawing board we go.&lt;/p&gt;
&lt;p&gt;So, resizing the warehouse didn’t exactly do what we hoped. The query latency didn’t budge, and it turns out compute resources weren’t the issue after all. With no queries getting stuck in a queue, we know it’s something else. What’s actually slowing things down? It might be time to take a closer look at the way our data is organized. &lt;a href=&quot;https://dorkusmalorkus.dev/posts/deferred-merge-snowflake-performance-part-2&quot;&gt;In part 2 of this series&lt;/a&gt;, we’ll dive into clustering and partitioning to see if they hold the key to fixing this. See you there!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>TIL in UNIX tooling - JQ is awesome!</title>
    <link rel="alternate" type="text/html" href="https://dorkusmalorkus.dev/posts/til-unix-tools-jq"/>
    <id>https://dorkusmalorkus.dev/posts/til-unix-tools-jq</id>
    <published>2024-12-06T00:00:00Z</published>
    <updated>2024-12-06T00:00:00Z</updated>
    <summary>Why did the JSON file break up with the CSV file? Because we just couldn&#x27;t parse their differences! ... ahem. Thankfully, this well known UNIX-tool `JQ` can help coerce JSON into other formats!</summary>
    <category term="til"/>
    <category term="unix-tooling"/>
    <content type="html">&lt;p&gt;&lt;i&gt;Disclaimer: this post was originally written in December 2019, but new blog new me amirite?&lt;/i&gt;&lt;/p&gt;
&lt;figure class=&quot;meme&quot;&gt;
  &lt;img width=&quot;509&quot; height=&quot;499&quot; decoding=&quot;async&quot; src=&quot;https://dorkusmalorkus.dev/static/assets/jq/andy.webp?v=d6368946&quot; alt=&quot;Don&#x27;t know unix tools&quot;&gt;
  &lt;figcaption&gt;Ain&#x27;t no shame in admitting it tho&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Once in a while, when working with vendors, I might need to make some
comparisons between what different vendors/ partners offer. Because
documentation can sometimes be incomplete or not completely up to date, I find
myself often turning to the vendor&#x27;s API for answers, since even a handful of
sandbox API endpoints can often give you most, if not all, the answers.&lt;/p&gt;
&lt;p&gt;Let&#x27;s dive into an example.&lt;/p&gt;
&lt;h3&gt;Context&lt;/h3&gt;
&lt;p&gt;I really, really, really like flaky pastries. I don&#x27;t know why, but I&#x27;m not
satisfied until the entire table is covered in flakes. We could dig further into this strange flex, but it&#x27;s pretty irrelevant. Anyway, to maximize my chances of indulging in this preference, I want to know what types of pastries are offered by 3 bakeries that are located closest to my house. Instead of walking over and asking like a normal human being, I decide to find the answer through more devious ways: hitting their baker.... API. ....this problem statement is already falling apart.&lt;/p&gt;
&lt;p&gt;Anyway, let&#x27;s say I curl baker_1&#x27;s GET /pastries endpoint, and get the following
result:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;         &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// irrelevant stuff&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;pastries&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;French Croissant&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;flake_factor&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;bakery_location&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;longitude&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;6.11499&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;latitude&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;50.76891&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;         &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// moar irrelevant stuff&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ideally, if I am making a comparison of multiple bakeries and their pastries,
I might want to share that with my fellow pastry-philes (yes, that&#x27;s
a word). Since spreadsheets are cool and all, I want to do that in a CSV file.&lt;/p&gt;
&lt;h3&gt;Problem&lt;/h3&gt;
&lt;p&gt;This leads me to 2 problems:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I need to get this JSON data in a CSV format&lt;/li&gt;
&lt;li&gt;I need to be able to repeat this process with minimal effort.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Normally, this could potentially take me quite some time to write a script to
parse the json data, filter out the fields I care about and write those to a new
file (or output them elsewhere). I would need to do this again for each
vendor, assuming the response data will look different each time.&lt;/p&gt;
&lt;h3&gt;JQ to the rescue&lt;/h3&gt;
&lt;p&gt;This is where &lt;a
 href=&quot;https://stedolan.github.io/jq/&quot;&gt;jq&lt;/a&gt; comes into play.
A &lt;em&gt;&amp;quot;lightweight and flexible command-line JSON processor&amp;quot;&lt;/em&gt;, it basically lets
you slice and dice up JSON data however you want, with the same ease that
&lt;code&gt;grep&lt;/code&gt;
lets you do it on the command line for regular text.&lt;/p&gt;
&lt;p&gt;Now that we have our JSON response stored locally somewhere, let&#x27;s apply
&lt;code&gt;jq&lt;/code&gt; to
it.&lt;/p&gt;
&lt;p&gt;Since all I care about for these bakeries is their pastries and their flake-factor, most of the top-level data is irrelevant to me. I need to dig into the &lt;code  &gt;pastries&lt;/code&gt; array. I can do that with the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;jq -r &#x27;.data[] | {&amp;quot;type&amp;quot;: .pastries[].name, &amp;quot;flake&amp;quot;: .pastries[].flake_factor}&#x27; bakery.json &amp;gt; baker_filtered.json
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above command will take the entries from the &lt;code  &gt;data&lt;/code&gt; array (&lt;code  &gt;.data[]&lt;/code&gt;) and pass it
further down the filter. There, I can construct a new JSON object with whatever
keys I want (in this case, my standardized pastry ratings), and have the values
be the desired output from the original JSON object (in this case, entries from
the &lt;code  &gt;pastries&lt;/code&gt; array). I then pass this into a new &lt;code  &gt;baker_filtered&lt;/code&gt; json file.&lt;/p&gt;
&lt;p&gt;You can do some very powerful things with these commands. I have mostly used it
to filter out stuff from JSON responses that I don&#x27;t care about, but as you can
see in this case, I created a brand new JSON object with it.&lt;/p&gt;
&lt;p&gt;For more tutorials and the full manual, check out &lt;a
 href=&quot;https://stedolan.github.io/jq/&quot;&gt;jq&#x27;s&lt;/a&gt; homepage.&lt;/p&gt;
&lt;p&gt;Sure, I still need to update the command for each vendor with a different
response data structure, but that can be done in a matter of seconds once I&#x27;ve
sufficiently grokked the &lt;code  &gt;jq&lt;/code&gt; syntax.&lt;/p&gt;
&lt;p&gt;Lastly, I did say I wanted the final output to be CSV. That can be done with a very
quick Python script:&lt;/p&gt;
&lt;pre class=&quot;chroma&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;json&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;csv&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#x27;baker_filtered.json&#x27;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#x27;flaky_pastry_list.csv&#x27;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#x27;w&#x27;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;csv&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;cl&quot;&gt;        &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writerows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And there you have it. I might put together a more real-world example with some
actual useful data instead of this contrived example, but I hope this can
illustrate some of the cool stuff &lt;code  &gt;jq&lt;/code&gt; will let you do. And yes, I know I&#x27;m late to this party, but hey, we can&#x27;t know everything, all right?&lt;/p&gt;
&lt;p&gt;See you next time.&lt;/p&gt;
</content>
  </entry>
</feed>