// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

Performance of custom field schemas

How do KVP tables compare to PostgreSQL hstore for custom field search performance? A look at benchmark results from Michel Ott's paper on SELECT queries.

Art+Logic Oct 29, 20122 min read

From the Art+Logic archive · written in 2012. The tools may have moved on; the thinking still holds.

Overview of SELECT performance on indexed KVP vs. hstore. Figure 17 from “Key/Value Pair versus hstore” by Michel Ott.
Overview of SELECT performance on indexed KVP vs. hstore. Figure 17 from "Key/Value Pair versus hstore" by Michel Ott.

In a previous post comparing options for storing custom fields in a database app, I considered the implications of each option on code design and clarity.  Of course, another important consideration is the relative performance of the different approaches.  How does searching for values in KVP custom fields (a table with key and value columns) compare to PostgreSQL hstore custom fields (a column containing a special NoSQL-like key/value hash)?

Performance Comparison

A paper titled “Key/Value Pair versus hstore: Benchmarking Entity-Attribute-Value Structures in PostgreSQL” provides some insight.  It was published May 26, 2011, by Michel Ott of the HSR Hochschule fur Technik Rapperswil.

Ott’s benchmarking, which focused solely on SELECT statements using keys and values, finds hstore is a clear winner for large datasets of more than 500 records.  In these tests, using an index on the key column for both approaches, hstore is up to 4 times faster than KVP.  The speed ratio grows alongside the number of rows searched.

Adding an index to the KVP approach that combines the key and value columns increases KVP’s performance, though it still lags behind that of hstore.  However, the two KVP indexes both require quite a bit more disk space than the index on the hstore column.  For 250k records, each of the KVP indexes requires 4 - 6 times more disk space.

Comparison of index sizes for KVP and hstore fields. Figure 19 from “Key/Value Pair versus hstore” by Michel Ott
Comparison of index sizes for KVP and hstore fields. Figure 19 from "Key/Value Pair versus hstore" by Michel Ott.

A full performance comparison should also consider write speed, and preferably more complex searches (e.g. partial text matching).  I haven’t yet found such benchmarks or performed them myself, but I’ll post back here if I do.

Benchmarking Notes

Ott used a typical KVP schema, where a custom field table includes key and value columns and a reference to another table that stores the non-custom field data.

CREATE TABLE bench_kvp_id (bench_id BIGINT PRIMARY KEY);
CREATE TABLE bench_kvp (
 bench_id BIGINT REFERENCES bench_kvp_id(bench_id),
 key TEXT NOT NULL,
 value TEXT
);

For different tests, Ott used indexes on just the key column, and the key and value columns combined.

For the hstore tests, Ott used a single table with two columns: primary key (for the non-custom field record) and hstore.

CREATE TABLE bench_hstore (bench_id BIGINT PRIMARY KEY, bench_hstore HSTORE NOT NULL);

Ott indexed the hstore column using a GiST index (for full text searching).

Ott tested with PostgreSQL 9.0.4 on Ubuntu 10.04.12 LTS, using preconfigured PostgreSQL databases without any specialized performance tuning.  It’s possible that the performance of one approach or another could be impacted by certain configuration settings.

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…