๐—•๐˜‚๐—ถ๐—น๐—ฑ๐—ถ๐—ป๐—ด ๐—ฉ๐—ถ๐—ฑ๐—ฒ๐—ผ ๐—›๐—ฒ๐—ฎ๐˜๐—บ๐—ฎ๐—ฝ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—›๐˜†๐—ฝ๐—ฒ๐—ฟ๐—Ÿ๐—ผ๐—ด๐—Ÿ๐—ผ๐—ด

Counting unique viewers per second kills your database.

One person watching a 9-minute video creates 540 rows. One million viewers create millions of rows.

Most people use COUNT(DISTINCT). This is slow. It forces your database to sort everything every time you open the analytics tab.

Use HyperLogLog (HLL) in Postgres.

HLL gives a close estimate. It does not give a perfect number. For a heatmap, a close number is enough.

Why use HLL?

The Setup:

The Trade-offs:

The Result:

Your database stays small. Your analytics load in milliseconds. You save money on servers.

Source: https://dev.to/ahmet_gedik778845/building-video-heatmap-analytics-with-hyperloglog-in-postgres-42ah