如何使用 Knex.js 添加 GIN 索引

zer*_*d1s 4 javascript postgresql node.js knex.js

我一直在尝试使用 Knex.js 在我的表中的一列上添加 GIN 索引,但我无法实现这一点。我正在尝试做这样的事情-

exports.up = function(knex, Promise) {
    return knex.schema.createTable('prediction_models', function(table){
            table.increments('id');
            table.string('model_name').index();
            table.jsonb('model_params');
            table.timestamp('createdAt').defaultTo(knex.fn.now());
        })
        .then(createGinIndex());

    function createGinIndex() {
        return knex.raw('CREATE INDEX modelgin ON public.prediction_models USING GIN (model_params);');
    }
};

exports.down = function(knex, Promise) {
    return knex.schema.dropTable('prediction_models');
};
Run Code Online (Sandbox Code Playgroud)

我正在使用 PostgreSQL 数据库。谁能告诉我这是否是实现这一点的正确方法?如果是,我的代码有什么问题,如果没有,如何实现?谢谢!

Tim*_*Tim 6

那些为这个问题寻找更“灵活”的解决方案的人:没有必要.raw像 Mikael Lepistö 的解决方案那样使用 。该.index可链接接受称为第二个参数indexType。它可以像这样使用:

knex.schema.createTable('table_name', table => {
  table.jsonb('column_name').index(null, 'GIN')
})
Run Code Online (Sandbox Code Playgroud)

null第一个参数是我说“为我命名这个索引”)

这段代码将产生以下查询:

create table "table_name" ("column_name" jsonb);
create index "table_name_column_name_index" on "table_name" using GIN ("column_name")
Run Code Online (Sandbox Code Playgroud)

目前我正在创建这样一个索引,但出于性能原因,我希望它只支持jsonb_path_ops操作符类(根据文档的第 8.14.4 节)。这需要一个create indexusing GIN ("column_name" jsonb_path_ops). 因此,我需要使用原始数据,但有很多用例.index就足够了。


Mik*_*stö 1

您应该能够像这样创建完整的 GIN 索引:

CREATE INDEX on prediction_models USING GIN (model_params)
Run Code Online (Sandbox Code Playgroud)

有关为 jsonb 列创建索引的更多信息可以在https://www.vincit.fi/en/blog/objection-js-postgresql-power-json-queries/的末尾找到

您可以像这样打印出迁移运行的查询(http://runkit.com/embed/8fm3z9xzjz9b):

var knex = require("knex")({ client: 'pg' });
const queries = knex.schema.createTable('prediction_models', function (table){
    table.increments('id');
    table.string('model_name').index();
    table.jsonb('model_params');
    table.timestamp('createdAt').defaultTo(knex.fn.now());
}).raw('CREATE INDEX on prediction_models USING GIN (model_params)')
.toSQL();

queries.forEach(toSql => console.log(toSql.sql));
Run Code Online (Sandbox Code Playgroud)

并将它们复制粘贴到 psql:

mikaelle=# begin;
BEGIN
mikaelle=# create table "prediction_models" ("id" serial primary key, "model_name" varchar(255), "model_params" jsonb, "createdAt" timestamptz default CURRENT_TIMESTAMP);
CREATE TABLE
mikaelle=# create index "prediction_models_model_name_index" on "prediction_models" ("model_name");
CREATE INDEX
mikaelle=# CREATE INDEX on prediction_models USING GIN (model_params);
CREATE INDEX
mikaelle=# commit;
COMMIT
mikaelle=#
mikaelle=# \d prediction_models
                                     Table "public.prediction_models"
    Column    |           Type           |                           Modifiers                            
--------------+--------------------------+----------------------------------------------------------------
 id           | integer                  | not null default nextval('prediction_models_id_seq'::regclass)
 model_name   | character varying(255)   | 
 model_params | jsonb                    | 
 createdAt    | timestamp with time zone | default now()
Indexes:
    "prediction_models_pkey" PRIMARY KEY, btree (id)
    "prediction_models_model_name_index" btree (model_name)
    "prediction_models_model_params_idx" gin (model_params)

mikaelle=# 
Run Code Online (Sandbox Code Playgroud)