Posts

Showing posts with the label using group by

Using GROUP BY

Image
Introduction Used to group rows of data with the same values in specified columns into a single row, with the main purpose being accompanied by Aggregate Functions such as SUM, AVG, COUNT, MAX, MIN . Note that all columns appearing in the SELECT clause that are not part of an aggregate function like SUM, COUNT must be declared in the GROUP BY clause. How it works When you execute a GROUP BY command, the Postgres Optimizer typically chooses one of the following two algorithms depending on the data volume and work_mem : HashAggregate : Uses a hashing algorithm Postgres scans through the data table once, hashes the value of the GROUP BY column for each row into a bucket in a Hash Table located in RAM and computes the aggregate function into that bucket if it already exists. Used when data is unsorted and the hash table can fit entirely within work_mem , which is usually the fastest method with a time complexity of O(N) since it only requires reading the table once after creating t...