0

我在 tarantool 空间中有我不再需要的字段。

local space = box.schema.space.create('my_space', {if_not_exists = true})
space:format({
        {'field_1', 'unsigned'},
        {'field_2', 'unsigned'},
        {'field_3', 'string'},
})

field_2如果已编入索引和未编入索引,如何删除?

4

1 回答 1

0

没有任何方便的方法可以做到这一点。

第一种方法,只需将此字段声明为可为空并将NULL值插入此字段。是的,它将以物理方式存储,但您可以对用户隐藏它们。它很简单而且不贵。

第二种方式,就地写入迁移。如果您在要删除的字段之后有索引字段(在您的示例中为field_3),则这是不可能的。如果你在这个空间中有大量数据,那就很危险了。

local space = box.schema.space.create('my_space', {if_not_exists = true})
space:create_index('id', {parts = {{field = 1, type = 'unsigned'}}})
space:format({
    {'field_1', 'unsigned'},
    {'field_2', 'unsigned'},
    {'field_3', 'string'},
})

-- Create key_def instance to simplify primary key extraction
local key_def = require('key_def').new(space.index[0].parts)

-- drop previous format
space:format({})

-- Migrate your data
for _, tuple in space:pairs() do 
    space:depete(key_def:extract_key(tuple))
    space:replace({tuple[1], tuple[3]})
end

-- Setup new format
space:format({
    {'field_1', 'unsigned'},
    {'field_3', 'string'},
})

第三种方法是创建新空间,将数据迁移到其中并删除以前的空间。仍然是相当危险的。

local space = box.schema.space.create('new_my_space', {if_not_exists = true})
space:create_index('id', {parts = {{field = 1, type = 'unsigned'}}})
space:format({
    {'field_1', 'unsigned'},
    {'field_3', 'string'},
})

-- Migrate your data
for _, tuple in box.space['my_space']:pairs() do 
    space:replace({tuple[1], tuple[3]})
end

-- Drop the old space
box.space['my_space']:drop()

-- Rename new space
local space_id = box.space._space.index.name:get({'my_new_space'}).id

-- In newer version of Tarantool (2.6+) space.alter method available
-- But in older versions you could update name via system "_space" space 
box.space._space:update({space_id}, {{'=', 'name', 'my_space'}})
于 2020-10-02T17:39:03.177 回答