title: Is there any way to optimize the lateral flatten json query in Snowflake? My query is taking too much time to execute
body: <p>I have json with nested arrays data vary for different jsons. I am putting my json parsing code and sample json file here. Query only got slow when there are many objects for rows tags as you can see below for this json query executes within a minute but when I have 100 objects then it will take up to 50 minutes. you can see the json below</p>
  <pre><code>    {
    &quot;page_desktop_image&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 5,
          &quot;ctr&quot;: 0.003048780487804878,
          &quot;impressions&quot;: 1640,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 10.207317073170731
        },
        {
          &quot;clicks&quot;: 2,
          &quot;ctr&quot;: 0.010638297872340425,
          &quot;impressions&quot;: 188,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 28.324468085106382
        },
        {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 4,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 237.5
        }
      ]
    },
    &quot;page_desktop_video&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 1,
          &quot;ctr&quot;: 0.038461538461538464,
          &quot;impressions&quot;: 26,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 4.5
        },
        {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 19,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 6.947368421052632
        }
      ]
    },
    &quot;page_desktop_web&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 8578,
          &quot;ctr&quot;: 0.28393631458740193,
          &quot;impressions&quot;: 30211,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 1.7217900764622156
        },
        {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 22,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 12.318181818181818
        }
      ]
    },
    &quot;page_mobile_image&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 3,
          &quot;ctr&quot;: 0.028037383177570093,
          &quot;impressions&quot;: 107,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 17.018691588785046
        },
        {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 37,
          &quot;keys&quot;: [
            &quot;abcx&quot;
          ],
          &quot;position&quot;: 38.4054054054054
        }
      ]
    },
    &quot;page_mobile_video&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 2,
          &quot;ctr&quot;: 0.05128205128205128,
          &quot;impressions&quot;: 39,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 6.487179487179487
        },
        {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 64,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 4.3125
        }
      ]
    },
    &quot;page_mobile_web&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 3604,
          &quot;ctr&quot;: 0.1579385599719532,
          &quot;impressions&quot;: 22819,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 2.3936193522941407
        },
              {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 12,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 6.583333333333333
        }
      ]
    },
    &quot;page_tablet_image&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 1,
          &quot;ctr&quot;: 0.005649717514124294,
          &quot;impressions&quot;: 177,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 5.112994350282486
        },
              {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 6,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 33.5
        }
      ]
    },
    &quot;page_tablet_video&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 1,
          &quot;ctr&quot;: 0.1,
          &quot;impressions&quot;: 10,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 18.7
        },
             {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 1,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 10
        }
      ]
    },
    &quot;page_tablet_web&quot;: {
      &quot;responseAggregationType&quot;: &quot;byPage&quot;,
      &quot;rows&quot;: [
        {
          &quot;clicks&quot;: 639,
          &quot;ctr&quot;: 0.2729602733874413,
          &quot;impressions&quot;: 2341,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 1.5262708244340026
        },
       {
          &quot;clicks&quot;: 0,
          &quot;ctr&quot;: 0,
          &quot;impressions&quot;: 27,
          &quot;keys&quot;: [
            &quot;abc&quot;
          ],
          &quot;position&quot;: 60.55555555555556
        }
      ]
    }
  }
  </code></pre>
  <p>and this is my json parsing code:</p>
  <pre><code>    SELECT 
                   JSON_FILE:page_desktop_image:responseAggregationType::String as responseAggregationType_desk_image
                     , r_desk_image.value:clicks as clicks_desk_image
                     , r_desk_image.value:ctr as ctr_desk_image
                     , r_desk_image.value:impressions as impressions_desk_image
                     , array_to_string(r_desk_image.value:keys , '') as keys_desk_image
                     , r_desk_image.value:position as position_desk_image
                     ,JSON_FILE:page_desktop_video:responseAggregationType::String as responseAggregationType_desk_video
                     , r_desk_video.value:clicks as clicks_desk_video
                     , r_desk_video.value:ctr as ctr_desk_video
                     , r_desk_video.value:impressions as impressions_desk_video
                     , array_to_string(r_desk_video.value:keys,'') as keys_desk_video
                     , r_desk_video.value:position as position_desk_video
                     ,JSON_FILE:page_desktop_web:responseAggregationType::String as responseAggregationType_desk_web
                     , r_desk_web.value:clicks as clicks_desk_web
                     , r_desk_web.value:ctr as ctr_desk_web
                     , r_desk_web.value:impressions as impressions_desk_web
                     , array_to_string(r_desk_web.value:keys,'') as keys_desk_web
                     , r_desk_web.value:position as position_desk_web
                     ,JSON_FILE:page_mobile_image:responseAggregationType::String as responseAggregationType_mob_image
                     , r_mob_image.value:clicks as clicks_mob_image
                     , r_mob_image.value:ctr as ctr_mob_image
                     , r_mob_image.value:impressions as impressions__mob_image
                     , array_to_string(r_mob_image.value:keys,'') as keys_mob_image
                     , r_mob_image.value:position as position_mob_image
                     ,JSON_FILE:page_mobile_video:responseAggregationType::String as responseAggregationType_mob_video
                     , r_mob_video.value:clicks as clicks_mob_video
                     , r_mob_video.value:ctr as ctr_mob_video
                     , r_mob_video.value:impressions as impressions_mob_video
                     , array_to_string(r_mob_video.value:keys,'') as keys_mob_video
                     , r_mob_video.value:position as position_mob_video
                     ,JSON_FILE:page_mobile_web:responseAggregationType::String as responseAggregationType_mob_web
                     , r_mob_web.value:clicks as clicks_mob_web
                     , r_mob_web.value:ctr as ctr_mob_web
                     , r_mob_web.value:impressions as impressions_mob_web
                     , array_to_string(r_mob_web.value:keys,'') as keys_mob_web
                     , r_mob_web.value:position as position_mob_web
                     ,JSON_FILE:page_tablet_image:responseAggregationType::String as responseAggregationType_tab_image
                     , r_tab_image.value:clicks as clicks_tab_image
                     , r_tab_image.value:ctr as ctr_tab_image
                     , r_tab_image.value:impressions as impressions_tab_image
                     , array_to_string(r_tab_image.value:keys,'') as keys_tab_image
                     , r_tab_image.value:position as position_tab_image
                     ,JSON_FILE:page_tablet_video:responseAggregationType::String as responseAggregationType_tab_video
                     , r_tab_video.value:clicks as clicks_tab_video
                     , r_tab_video.value:ctr as ctr_tab_video
                     , r_tab_video.value:impressions as impressions_tab_video
                     , array_to_string(r_tab_video.value:keys,'') as keys_tab_video
                     , r_tab_video.value:position as position_tab_video
                     ,JSON_FILE:page_tablet_web:responseAggregationType::String as responseAggregationType_tab_web
                     , r_tab_web.value:clicks as clicks_tab_web
                     , r_tab_web.value:ctr as ctr_tab_web
                     , r_tab_web.value:impressions as impressions_tab_web
                     , array_to_string(r_tab_web.value:keys ,'') as keys_tab_web
                     , r_tab_web.value:position as position_tab_web
                from GSC_JSONS
                     ,lateral flatten(input =&gt; JSON_FILE:page_desktop_image:rows) as r_desk_image
                     ,lateral flatten(input =&gt; JSON_FILE:page_desktop_video:rows) as r_desk_video
                     ,lateral flatten(input =&gt; JSON_FILE:page_desktop_web:rows) as r_desk_web
                     ,lateral flatten(input =&gt; JSON_FILE:page_mobile_image:rows) as r_mob_image
                     ,lateral flatten(input =&gt; JSON_FILE:page_mobile_video:rows) as r_mob_video
                     ,lateral flatten(input =&gt; JSON_FILE:page_mobile_web:rows) as r_mob_web
                     ,lateral flatten(input =&gt; JSON_FILE:page_tablet_image:rows) as r_tab_image
                     ,lateral flatten(input =&gt; JSON_FILE:page_tablet_video:rows) as r_tab_video
                     ,lateral flatten(input =&gt; JSON_FILE:page_tablet_web:rows) as r_tab_web
  </code></pre>
  <p>If anyone knows the workaround then please let me know. I know why it is slow because it makes cross joins for each object but I want to execute it faster.</p>