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 1kEJBU-0003Fu-Sr for pgsql-performance@arkaria.postgresql.org; Fri, 04 Sep 2020 21:23:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kEJBT-0004or-Nr for pgsql-performance@arkaria.postgresql.org; Fri, 04 Sep 2020 21:23:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kEJBT-0004oj-Eg for pgsql-performance@lists.postgresql.org; Fri, 04 Sep 2020 21:23:47 +0000 Received: from mout.gmx.net ([212.227.17.20]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kEJBP-0007lT-6X for pgsql-performance@lists.postgresql.org; Fri, 04 Sep 2020 21:23:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1599254619; bh=DcndqOw4Fl7k6GIQGbIGO53MLToKbWAuoYt8abSuSm8=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=gd3qWGOKU1ZHo7qatE7tdm0fvRCtnFgI7RjS8t/NWL9UKB9FPSJf5xEp7x6NVzWj5 aGaWjkwS5/uYvJE0tdGkZYbqF9NdugHPIQZXTi+XHnSmB95PA6RQvxj61aLaDc0Lgt hgUJz6ncU0Ledhsxk9D1H1vURK6B79/gHPlRHQ8U= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.20] ([188.174.164.189]) by mail.gmx.com (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1M8hZJ-1kAARf4AfR-004lFm for ; Fri, 04 Sep 2020 23:23:39 +0200 Subject: Re: Query performance issue To: Pgsql Performance References: <975305787.3395625.1599254321500.ref@mail.yahoo.com> <975305787.3395625.1599254321500@mail.yahoo.com> From: Thomas Kellerer Message-ID: Date: Fri, 4 Sep 2020 23:23:38 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 MIME-Version: 1.0 In-Reply-To: <975305787.3395625.1599254321500@mail.yahoo.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:tX3Kkd7+dA5UX489gZs5Qmd66N1sQ1jQnImU/1bhtq2ruPR4Dfa zgDeilTY/38QTFhvPm2qSned79CsmPqqUnglZKraNybCKCJt1pCHRIWutSHDNPGKgrERjR8 zxzFuDf3YxbLUxBdBrP5KcJKmRqeQnHom9HCoAewmEtVNyqf791NDVEXGFbpn1DugSAOoLL wHqKxjD6730Ng86M0bZ1g== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:AVXqk2Srn2M=:pR3+7P6TAH/lm01IJVJw8I b2Iv7Yu/5gBOIt8lKqOTnUhPX56UVnvj99yT5oNDYJLpStHUtqX17Hafm2CSdLx9J7IIraKRe cCvdT9X6agxnUMtabAAf2hi8rIAP4zFxUIBLGKVS1zxcFwOQjbxdryhfkDAxEznyfy3XoJcwN kYM+MCOGg3E+h+P+EEuoIk73/BRf/VymdhbPqzsuBaTjTQ+zsXN/j0USigZZYjem+/nDN1EYu 918SzNJYpmRYBHOBOuQAwJLROW74Fok9uJmYUHTbZ9Nj7VYwY43q20U7BJ/VQOu+5q0yg1+CC FwTRj72/cxsZIuu2cagWIUxgvDxEFsmMjB0FwFfKq9hC2InMf+xb5q/7THi7o91VmQwgweWC7 KMAfXbQxKNQxy2MHTcEapdRKb2AUna/aohDUU4ts+DUhGpnboxGCSthgDMuUMMGE5DWEbS5or DE5x4bXawCNeh6QOyPcb8npeKx7RfGZe8VviVtLhzPQx+O6Kbtv9XhsaOGY0PKnu9EIhRlW1s 2zNerWvMvw+pfBUPWn88Mt47MF7akO9ulyPzNLCZNM0+RTI54AqwUlYlfKpNpNpkZzXNIX1Dc bb3+r6M0ix/VI4Q68sp52NT1CHX1zgku5utQ3+CJSheK8vOn+FzL4jRfUFYe0I+Mp8iklDbPy A4kUqd+i9HlXc8oyAG3AJfUoLMYZEHjZECnww7TH2uBrLZ/q4hkvF0dFFEm+LlUcRCs3pdKyD hlp+pA1t+hSVvHKSe6Ea0ghfwrCgLLQqAwV/cxWhUk43qOuDr/Dz3TP8ivasva8V1gVGoGkOB XHG6CaQUPz2uGY6hDesblK6Qu8u0TK96mFBCiJydStdNYIC2g3BG0YQKtCEJ0sR3RiyZ2ebX2 G91XHc8oTg5n3Zxrhh6tCp3FjSEA7WtyiMMVKlhzulYgGyxDSAxNYTDH95gutpOG3DNncb43j 0TU/8HeBEgVxKsCZYiDyPYfinCoUlLzVcgM39wbh4oRvYO252HG2mcnPWJ88UIbOW+EimlPh9 L9LCcRWFTJAEeVRvfbmfOz68VbtwCOZmr5OTltVadXMto1X0wbj98gjYMaVsWuDuc5qK3F78b VzH50f6m0Z0X6B4tKlqSnYEvHg4dhUP/f3ln1og6qaUjcVodj+mWrRPk3pt9GUC49aU+5Ygck 979iczef0Xydm0tazuKYBLY9Te+St9o5mcU15qsHCMs/w2xW5/ggo38pKgJ9BDA8il6WYThIB qZJAcc9bQwMFa5m8d List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Nagaraj Raj schrieb am 04.09.2020 um 23:18: > I have a query which will more often run on DB and very slow and it > is doing 'seqscan'. I was trying to optimize it by adding indexes in > different ways but nothing helps. > > EXPALIN ANALYZE select serial_no,receivingplant,sku,r3_eventtime > from (select serial_no,receivingplant,sku,eventtime as r3_eventtime, row= _number() over (partition by serial_no order by eventtime desc) as mpos > from receiving_item_delivered_received > where eventtype=3D'LineItemdetailsReceived' > and replenishmenttype =3D 'DC2SWARRANTY' > and coalesce(serial_no,'') <> '' > ) Rec where mpos =3D 1; > > > Query Planner: > > "Subquery Scan on rec=C2=A0 (cost=3D70835.30..82275.49 rows=3D1760 width= =3D39) (actual time=3D2322.999..3451.783 rows=3D333451 loops=3D1)" > "=C2=A0 Filter: (rec.mpos =3D 1)" > "=C2=A0 Rows Removed by Filter: 19900" > "=C2=A0 ->=C2=A0 WindowAgg=C2=A0 (cost=3D70835.30..77875.42 rows=3D35200= 6 width=3D47) (actual time=3D2322.997..3414.384 rows=3D353351 loops=3D1)" > "=C2=A0 =C2=A0 =C2=A0 =C2=A0 ->=C2=A0 Sort=C2=A0 (cost=3D70835.30..71715= .31 rows=3D352006 width=3D39) (actual time=3D2322.983..3190.090 rows=3D353= 351 loops=3D1)" > "=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Sort Key: receiving_it= em_delivered_received.serial_no, receiving_item_delivered_received.eventti= me DESC" > "=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Sort Method: external = merge=C2=A0 Disk: 17424kB" > "=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ->=C2=A0 Seq Scan on r= eceiving_item_delivered_received=C2=A0 (cost=3D0.00..28777.82 rows=3D35200= 6 width=3D39) (actual time=3D0.011..184.677 rows=3D353351 loops=3D1)" > "=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 F= ilter: (((COALESCE(serial_no, ''::character varying))::text <> ''::text) A= ND ((eventtype)::text =3D 'LineItemdetailsReceived'::text) AND ((replenish= menttype)::text =3D 'DC2SWARRANTY'::text))" > "=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 R= ows Removed by Filter: 55953" > "Planning Time: 0.197 ms" > "Execution Time: 3466.985 ms" The query retrieves nearly all rows from the table 353351 of 409304 and th= e Seq Scan takes less than 200ms, so that's not your bottleneck. Adding indexes won't change that. The majority of the time is spent in the sort step which is done on disk. Try to increase work_mem until the "external merge" disappears and is done= in memory. Thomas