---
title: "IP-based Geolocation in ClickHouse"
description: "ClickHouse's ip_trie dictionary layout enables low-latency IP-to-location lookups once IP ranges are expressed as CIDR blocks."
url: "https://swe.quest/articles/ip-based-geolocation-in-clickhouse-e3f8e96a374b57a6"
canonical: "https://clickhouse.com/blog/geolocating-ips-in-clickhouse-and-grafana"
source: "ClickHouse"
published: "2023-05-23"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["analytics","geospatial","sql","networking"]
---

# IP-based Geolocation in ClickHouse

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2023-05-23, 9 min read, How-to. Original: [https://clickhouse.com/blog/geolocating-ips-in-clickhouse-and-grafana](https://clickhouse.com/blog/geolocating-ips-in-clickhouse-and-grafana)

## Summary

ClickHouse's ip\_trie dictionary layout enables low-latency IP-to-location lookups once IP ranges are expressed as CIDR blocks. The walkthrough loads the DB-IP city dataset via the URL table engine and converts start/end ranges to CIDR using bitXor, log2/ceil, bitNot and bitAnd. It then builds an in-memory ip\_trie dictionary queried with dictGet against SEC EDGAR access logs. For mapping, geohashEncode at low precision with log10-normalized counts rolls 20M+ rows into about 6,300 cells for a Grafana Geo Map panel.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [Maps & geospatial](https://swe.quest/tags/geospatial.md) (Domains)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Networking](https://swe.quest/tags/networking.md) (Infrastructure)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- CIDR notation
- bitwise operations

## Keywords

ClickHouse, ip\_trie, dictGet, DB-IP, geohash, Grafana, CIDR

## Related posts

- [Exploring Global Internet Speeds using Apache Iceberg and ClickHouse](https://swe.quest/articles/exploring-global-internet-speeds-using-apache-iceberg-and-clickhouse-2b3375957fd7714d.md): ClickHouse · 2024-02-08 · Medium · 25 min read · Analytics & OLAP, Maps & geospatial, SQL, Data pipelines
  ClickHouse's iceberg table function can query the Ookla global internet-speed dataset (128M rows) directly from S3.
- [Using Dictionaries to Accelerate Queries](https://swe.quest/articles/using-dictionaries-to-accelerate-queries-fcf5f4fb14453a0d.md): ClickHouse · 2022-12-07 · Medium · 17 min read · Query engines, Analytics & OLAP, SQL, Performance engineering
  ClickHouse dictionaries, in-memory key-value structures, can replace JOINs against small lookup tables.
- [Join me if you can: ClickHouse vs. Databricks vs. Snowflake - Part 2](https://swe.quest/articles/join-me-if-you-can-clickhouse-vs-databricks-vs-snowflake-part-2-66aede87450dc5eb.md): ClickHouse · 2025-06-25 · Medium · 8 min read · Analytics & OLAP, Performance engineering, Query engines, SQL
  Replacing fact-to-dimension joins with in-memory dictionaries in ClickHouse Cloud sped up a 17-query join benchmark over 1.4 billion rows by 1.9–3.5× in total runtime, up to 6.6× on individual queries, and cut cost by up to 60%.
- [Visualizing Foursquare places with ClickHouse](https://swe.quest/articles/visualizing-foursquare-places-with-clickhouse-7a2e9fe18ad158f7.md): ClickHouse · 2025-05-08 · Medium · 5 min read · Maps & geospatial, Analytics & OLAP, SQL
  The Foursquare open places dataset (about 100 million records) loads straight from S3 Parquet into ClickHouse in 42 seconds and 11 GB.
- [Hosting Postgres with GeoLite2: a practical guide to IP geolocation, data loading, and updates](https://swe.quest/articles/hosting-postgres-with-geolite2-a-practical-guide-to-ip-geolocation-data-loading-f98c61ed175d32d1.md): Railway · 2025-12-16 · Medium · 8 min read · PostgreSQL, Maps & geospatial, Data pipelines
  Loading MaxMind's GeoLite2 CSVs into Postgres pays off over the MMDB binary format when you need SQL joins, batch geolocation of many IPs, queries over the ranges themselves, or one copy shared across services; for simple single-IP lookups MMDB is easier.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
