News RSS feedSolutions
Contents
  1. What is a compound index in plain terms?
  2. How does field order affect whether a compound index is used?
  3. What does building and maintaining one actually involve?
  4. Where do compound indexes help most in geospatial work?
  5. What are the trade-offs and common mistakes?
  6. How should you decide whether to add one?
  7. Frequently asked questions

Guides

What The Compound Index Actually Involves

SolutionsPublished 6 min read

In data terms, a compound index is a single lookup structure built from two or more fields at once, rather than one index per field. It is the mechanism that lets a database answer a query covering several attributes without scanning every record or stitching together separate index results. In mapping and geospatial work, the same idea appears as a composite key over coordinates, identifiers and timestamps, and the principles are largely the same: order matters, the leftmost fields carry the most weight, and the structure only earns its keep when queries actually use it. This piece explains what building and maintaining a compound index involves, where it helps, and where it quietly costs more than it returns.

What is a compound index in plain terms?

A compound index stores entries sorted by a defined sequence of fields. If the index is ordered by region, then by category, then by date, the engine can jump straight to a region, walk the category order within it, and narrow by date without touching the wider table. That is different from three separate indexes, which the planner may have to intersect or choose between.

The analogy that holds up best in geospatial circles is a map series indexed by tile, layer and revision. A reader looking for a specific revision of a specific layer in a specific tile follows the same hierarchy the index enforces. Change the hierarchy and the same shelf of maps becomes much harder to search, even though nothing has been removed.

The practical consequence is that a compound index is a commitment to a query shape. It is not a general-purpose accelerator. It rewards teams who know how their data is filtered and penalises those who add fields speculatively.

How does field order affect whether a compound index is used?

Field order is the whole design. Most engines can use a compound index efficiently only when a query filters on a leading portion of the indexed fields. An index on region, category and date will serve queries on region alone, region plus category, or all three. It will not reliably serve a query that filters only on date.

This produces the classic mismatch: an index exists, the query is slow, and nobody can see why. The index is fine; the query simply enters the structure from the wrong side.

  • Equality first, range later. Fields tested for exact matches usually belong earlier in the order than fields used in ranges or inequalities.
  • Sort order follows the index. If results must come back in a particular order, aligning that order with the index sequence can remove a separate sorting step.
  • Selectivity is not the only test. A highly selective field is not automatically the best lead field if most queries filter on something else.

In spatial systems the same reasoning applies to composite keys. A key built from a geometry hash plus a timestamp behaves very differently from one built from a timestamp plus a geometry hash, and the right choice depends entirely on how tiles and time windows are requested.

What does building and maintaining one actually involve?

Creating a compound index is a schema change with operational weight. On a large table it can take time, consume disk, and compete with live traffic for input and output. The work is rarely finished at the moment the index is created.

  1. Profile the queries first. Collect the actual filter and sort patterns from logs or query plans rather than guessing from the schema.
  2. Model the candidate order. Test two or three plausible field sequences against representative data before committing.
  3. Build with headroom. Plan for the index build to run during a quieter window, and check storage before and after.
  4. Watch write paths. Every insert, update and delete must maintain the index, so write-heavy tables feel the cost most.
  5. Review periodically. Query patterns drift; an index that was well matched a year ago may now be dead weight.

That last point matters more than it is usually given credit for. Unused compound indexes are common in mature systems, and each one is a tax on every write plus a maintenance obligation nobody remembers signing up for.

Where do compound indexes help most in geospatial work?

Geospatial workloads tend to combine a spatial filter with a non-spatial one: features within an area, published after a date, belonging to a particular source. Compound indexes suit exactly this pattern when the access route is predictable.

Common cases include:

  • Tile and layer lookups where both are always supplied together.
  • Time-windowed feeds filtered by sensor or region.
  • Asset registers queried by site and category in the same request.
  • Change detection pipelines that walk a dataset by revision and extent.

Where the pattern breaks down is in exploratory analysis, where filters vary from query to query. There, a compound index may serve one analyst well and another not at all. It is worth being honest about this: some spatial workloads are better served by a spatial index plus a small number of well-chosen secondary indexes than by one ambitious composite.

What are the trade-offs and common mistakes?

The benefits are real: fewer scans, faster filtered reads, and sometimes the elimination of a sort step. The costs are equally real, and they compound, so to speak.

Storage and write amplification

Each compound index duplicates the indexed fields in a separate structure. On wide tables with frequent updates, that duplication shows up as slower writes and larger backups.

Over-indexing

Adding fields to an existing compound index to cover one more query is tempting and often wrong. It can invalidate the index for the queries it previously served well. A second, narrower index is frequently the better answer.

Assuming the planner will use it

Indexes are suggestions to the query planner, not commands. Statistics, data distribution and the shape of the query all influence the decision. Checking the actual execution plan is the only reliable way to know.

For teams working with reference datasets where lookups follow a stable pattern, the same reasoning applies to curated registries. If you are cross-referencing entities against a structured catalogue, the compound monograph index is a useful example of how a fixed lookup hierarchy is organised around the fields people actually search by, which is precisely the discipline a database index demands.

How should you decide whether to add one?

Start from evidence. If a recurring query filters on the same two or three fields, sorts on one of them, and shows up often enough to matter, a compound index is a reasonable candidate. If the query is ad hoc, or the fields change with each request, the case is much weaker.

Then ask three questions. Does the query filter on a leading portion of the proposed order? Is the table read-heavy enough that the write cost is acceptable? And is there an existing index that already covers most of the pattern? If the answers point one way, build it, measure, and keep measuring. An index is a hypothesis about how your data will be queried, and hypotheses deserve testing rather than assumption.

Frequently asked questions

What is a compound index and why does it matter?

It is a single index built across several fields in a defined order. It matters because it lets a database answer multi-field queries directly instead of scanning or combining separate indexes, but only when queries match the order it enforces.

Does field order really change whether a compound index gets used?

Yes. Most engines need a query to filter on the leading fields of the index. A query that filters only on a later field usually cannot use the index efficiently, even though the index exists and looks correct.

Can a compound index slow things down?

It can. Every write must update the index, so write-heavy tables pay a continuous cost. Unused or redundant compound indexes add storage and maintenance overhead without improving any query, which is why periodic review is worth the effort.