Coder Social home page Coder Social logo

Multi-column index? about pgvector HOT 2 CLOSED

pgvector avatar pgvector commented on May 21, 2024
Multi-column index?

from pgvector.

Comments (2)

ankane avatar ankane commented on May 21, 2024

Hey @zhihongx0, multi-column support would be pretty difficult and I think is outside the scope of IVFFlat indexes (and pgvector doesn't support GiST), but there are two strategies you can use to efficiently query on conditions:

  1. Use a partial index
CREATE INDEX ON table USING ivfflat (column) WHERE (other_column = 123);
  1. If you need to query on many different values of other_column, it should be more efficient to partition the table on other_column (so each partition will get a separate IVFFlat index)

from pgvector.

ankane avatar ankane commented on May 21, 2024

Added this info to the readme.

Here's a quick example for partitioning with Postgres 13 (should work with range, list, and hash).

CREATE EXTENSION IF NOT EXISTS vector;
DROP TABLE IF EXISTS items;
CREATE TABLE items (
  id bigserial,
  factors vector(3),
  category_id bigint
) PARTITION BY HASH(category_id);

CREATE TABLE items_part1 PARTITION OF items FOR VALUES WITH (modulus 3, remainder 0);
CREATE TABLE items_part2 PARTITION OF items FOR VALUES WITH (modulus 3, remainder 1);
CREATE TABLE items_part3 PARTITION OF items FOR VALUES WITH (modulus 3, remainder 2);

INSERT INTO items (factors, category_id) SELECT ARRAY[RANDOM(), RANDOM(), RANDOM()], n % 10 FROM generate_series(1, 100000) n;

CREATE INDEX ON items USING ivfflat (factors);

SELECT * FROM items WHERE category_id = 2 ORDER BY factors <-> '[0.5,0.5,0.5]' LIMIT 5;

EXPLAIN ANALYZE SELECT * FROM items WHERE category_id = 2 ORDER BY factors <-> '[0.5,0.5,0.5]' LIMIT 5;

from pgvector.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.