Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 9 min read How-to mediumAdept

IP-based Geolocation in ClickHouse

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.

Assumes: SQL basicsCIDR notationbitwise operations

Related posts

  • Exploring Global Internet Speeds using Apache Iceberg and ClickHouse

    ClickHouse mediumAdept

    ClickHouse's iceberg table function can query the Ookla global internet-speed dataset (128M rows) directly from S3. The schema comes from Iceberg metadata, and the walkthrough includes aggregations such as download-speed quantiles. It notes current gaps: no v2 row-level deletes or schema evolution, no partition pruning, S3-only support, and no use of sort order for file skipping. The data is then loaded into a MergeTree table, WKT tiles are parsed into Ring types, and polygon centroids are computed with shoelace-formula SQL UDFs built on arrayFold. The polygons are rendered as SVG maps using only SQL, with h3 indexes, polygon dictionaries, Mercator projection and color interpolation.

  • Using Dictionaries to Accelerate Queries

    ClickHouse mediumAdept

    ClickHouse dictionaries, in-memory key-value structures, can replace JOINs against small lookup tables. Using the NOAA weather dataset, a normalized INNER JOIN against a stations table forces a full scan of 1.08 billion rows because ClickHouse runs joins before WHERE filtering. Swapping in dictGet lookups alongside a primary-key-friendly subquery brings that query back to millisecond latency. The post walks through creating a dictionary, choosing a layout (flat, hashed, hashed_array, complex_key variants, sparse), setting LIFETIME and invalidate_query for refreshes, and loading data, recommending hashed_array or complex_key_hashed_array for most cases.

  • Join me if you can: ClickHouse vs. Databricks vs. Snowflake - Part 2

    ClickHouse mediumAdept

    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%. The Locations table becomes a complex_key_hashed dictionary for String-key lookups. The Products table becomes a range_hashed dictionary that resolves time-versioned rows with a hash map plus binary search over date ranges, provided the source has no overlapping ranges per key. Queries change only by swapping JOINs for dictGet calls, and the gains grew as the cluster scaled from 2 to 8 nodes.

  • Visualizing Foursquare places with ClickHouse

    ClickHouse mediumAdept

    The Foursquare open places dataset (about 100 million records) loads straight from S3 Parquet into ClickHouse in 42 seconds and 11 GB. The table stores Web Mercator coordinates as materialized UInt32 columns, is ordered by a Morton (Z-order) space-filling curve, and has minmax indices so map tiles can be queried fast. The existing adsb.exposed air-traffic visualizer was reused with a 48-line change to render pixel-level tiles in the browser. Compared with Foursquare Studio's H3 hexagons, Kepler.gl and DuckDB-based maps, it gives higher resolution and smoother interactivity, but does not correct point density for latitude.

  • Hosting Postgres with GeoLite2: a practical guide to IP geolocation, data loading, and updates

    Railway mediumAdept

    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. Three deployment options are covered: a docker-entrypoint-initdb.d script that downloads and COPYs the data, a custom image with the data baked in, and a scheduled loader service for updates. Lookups use the cidr `>>=` operator backed by a GiST inet_ops index, which keeps queries under 10ms. Recommended practice is weekly refreshes to match MaxMind's Tuesday releases, loading into new tables and swapping them via renames in a transaction, with roughly 500-600 MB of storage for all three databases and a ~200 MB City index that should fit in shared_buffers.

The quest for software engineering knowledge.

Look:
Layout: