Re: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables

Поиск
Список
Период
Сортировка
От Holger Jakobs
Тема Re: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables
Дата
Msg-id 6e9caa48-5e29-a493-d54d-3a97f3893029@jakobs.com
обсуждение исходный текст
Ответ на RE: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables  ("Pierson Patricia L (Contractor)" <Patricia.L.Pierson@irs.gov>)
Список pgsql-admin
Am 12.07.22 um 20:13 schrieb Pierson Patricia L (Contractor):
@font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;}@font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4;}@font-face {font-family:Consolas; panose-1:2 11 6 9 2 2 4 3 2 4;}p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; font-size:11.0pt; font-family:"Calibri",sans-serif;}a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;}pre {mso-style-priority:99; mso-style-link:"HTML Preformatted Char"; margin:0in; font-size:10.0pt; font-family:"Courier New";}span.HTMLPreformattedChar {mso-style-name:"HTML Preformatted Char"; mso-style-priority:99; mso-style-link:"HTML Preformatted"; font-family:Consolas;}span.EmailStyle21 {mso-style-type:personal-reply; font-family:"Verdana",sans-serif; color:windowtext;}.MsoChpDefault {mso-style-type:export-only; font-family:"Calibri",sans-serif;}div.WordSection1 {page:WordSection1;}

Hello,

 

Do a count on the primary key.  Will force index access and you don’t access the entire row which may be very long.

LIKE : select count(ID) from my_table;

If you do a COUNT(*) the database won't do anything different. That's a myth.

Just compare the execution plans.


db=# explain select count(anr) from auftrag;
                                        QUERY PLAN                                        
------------------------------------------------------------------------------------------
 Finalize Aggregate  (cost=11614.55..11614.56 rows=1 width=8)
   ->  Gather  (cost=11614.33..11614.54 rows=2 width=8)
         Workers Planned: 2
         ->  Partial Aggregate  (cost=10614.33..10614.34 rows=1 width=8)
               ->  Parallel Seq Scan on auftrag  (cost=0.00..9572.67 rows=416667 width=8)
(5 Zeilen)

db=# explain select count(*) from auftrag;
                                        QUERY PLAN                                        
------------------------------------------------------------------------------------------
 Finalize Aggregate  (cost=11614.55..11614.56 rows=1 width=8)
   ->  Gather  (cost=11614.33..11614.54 rows=2 width=8)
         Workers Planned: 2
         ->  Partial Aggregate  (cost=10614.33..10614.34 rows=1 width=8)
               ->  Parallel Seq Scan on auftrag  (cost=0.00..9572.67 rows=416667 width=0)
(5 Zeilen)

-- 
Holger Jakobs, Bergisch Gladbach, Tel. +49-178-9759012
Вложения

В списке pgsql-admin по дате отправления:

Предыдущее
От: "Pierson Patricia L (Contractor)"
Дата:
Сообщение: RE: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables
Следующее
От: Laurenz Albe
Дата:
Сообщение: Re: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables