Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lB19U-00035L-SE for pgsql-general@arkaria.postgresql.org; Sat, 13 Feb 2021 20:04:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lB19T-0000GK-M9 for pgsql-general@arkaria.postgresql.org; Sat, 13 Feb 2021 20:04:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lB19T-0000GD-Bb for pgsql-general@lists.postgresql.org; Sat, 13 Feb 2021 20:04:23 +0000 Received: from mail-ot1-x331.google.com ([2607:f8b0:4864:20::331]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lB19Q-0003xv-W7 for pgsql-general@lists.postgresql.org; Sat, 13 Feb 2021 20:04:22 +0000 Received: by mail-ot1-x331.google.com with SMTP id a5so2574653otq.4 for ; Sat, 13 Feb 2021 12:04:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=AWWscYYFR2INHbAdFR2uuCjv64JvAEpfDcfu9X3u7yg=; b=Q14w1eQE8MHHzWBx73ZGgLG4ewufeqhCzJ/3mJrGpfW0zb+qEYRUnt40McPlGECgCy vuemzsQGHhceCW6wlU2PVas7Gbc9HHr6R4L0rJekL53c3BH9h43/9nvZ0YE720eE1AcD e/O0w4YK7MRy9Vny1xICF306O1ZGN0UbuFdGzs17KwrXZ8qIikkivCzDFkiPXdHX4fTS F6umjvDM8UjX2mfHkYaayXFr9VMCO4fmUuG9uQ4ayfvEozakJvKbvumFzciHMNoq2CcY uSwQlyPuQLwfukRccKc5ZJwMNzo8DBiHuOrJpLdIHY9r36/MttsnNyoAvVNDiohsJf8B peYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=AWWscYYFR2INHbAdFR2uuCjv64JvAEpfDcfu9X3u7yg=; b=RNTFglA/20OTLm/U0Ogo8vtY57G6sCr1taxh0/CByRR3/meB3ZHESkgJ5vgJI1WjqW FBOd5HmwJ9BhbDwKHAhBbDQ4dQva9iK4aK97gf+TB25XQW+ABmckNKLcorpvThErowc9 Yj5T9cfG6IJyJyugVotYCuEQ4bON/1TiyEICsOiSf4MmnYsFVgaNAbjU9ZjItZvyNF9V KCcovlGMmMOoEb1rgE3JsxpmO/8JiweMsNsxGNksjA6IG5XXZlubnQSu8Dn6WH02MAbK mcBZAMaiFhIgrmINjWSxCKbGFyXFSlnrpKr1XDkZbsihF4PsARD9DiqI/Mqc4L5zMznZ qmEA== X-Gm-Message-State: AOAM533EAwB/ggdYT5CXReQXvxFhO+FmtjgCpcWJ0r+8axL6eKs5oCjw Yn1pbBLAxxcoW3VpIlVIbQRBUohnOlJzIQ== X-Google-Smtp-Source: ABdhPJwU8b0wFBiexIFqw2wKmu/l7guW+hZ5TfqrK2zVSF8TmolcaMcfBbKDPAaLjL/DH5RdoJ4unQ== X-Received: by 2002:a9d:24c9:: with SMTP id z67mr6248041ota.273.1613246658904; Sat, 13 Feb 2021 12:04:18 -0800 (PST) Received: from [192.168.88.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id i22sm974411oot.3.2021.02.13.12.04.18 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 13 Feb 2021 12:04:18 -0800 (PST) Subject: Re: Insert into on conflict, data size upto 3 billion records To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <2a876340-e227-c96a-1d79-f8a74aa45e4a@gmail.com> Date: Sat, 13 Feb 2021 14:04:17 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------4EF2BDAED04B1DAE05BC7E25" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------4EF2BDAED04B1DAE05BC7E25 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 2/12/21 12:46 PM, Karthik Kumar Kondamudi wrote: > Hi, > > I'm looking for suggestions on how I can improve the performance of the > below merge statement, we have a batch process that batch load the data > into the _batch tables using Postgres and the task is to update the main > target tables if the record exists else into it, sometime these batch > table could go up to 5 billion records. Here is the current scenario > > |target_table_main| has 700,070,247  records and is hash partitioned into > 50 chunks, it has an index on |logical_ts| and the batch table has > 2,715,020,546 close to 3 billion records, so I'm dealing with a huge set > of data so looking of doing this in the most efficient way. Many times, I have drastically sped up batch processing by #1 partitioning on the same field as an index, and #2 pre-sorting the input data by that field. That way, you get excellent "locality of data" (meaning lots of writes to the same hot bits of cache, which later get asynchronously flushed to disk).  Unfortunately for your situation, the purpose of hash partitioning is to /reduce/ locality of data.  (Sometimes that's useful, but *not* when processing batches.) -- Angular momentum makes the world go 'round. --------------4EF2BDAED04B1DAE05BC7E25 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 2/12/21 12:46 PM, Karthik Kumar Kondamudi wrote:
Hi, 

I'm looking for suggestions on how I can improve the performance of the below merge statement, we have a batch process that batch load the data into the _batch tables using Postgres and the task is to update the main target tables if the record exists else into it, sometime these batch table could go up to 5 billion records. Here is the current scenario

target_table_main has 700,070,247  records and is hash partitioned into 50 chunks, it has an index on logical_ts and the batch table has 2,715,020,546 close to 3 billion records, so I'm dealing with a huge set of data so looking of doing this in the most efficient way.

Many times, I have drastically sped up batch processing by #1 partitioning on the same field as an index, and #2 pre-sorting the input data by that field.

That way, you get excellent "locality of data" (meaning lots of writes to the same hot bits of cache, which later get asynchronously flushed to disk).  Unfortunately for your situation, the purpose of hash partitioning is to reduce locality of data.  (Sometimes that's useful, but not when processing batches.)

--
Angular momentum makes the world go 'round.
--------------4EF2BDAED04B1DAE05BC7E25--