在之前介绍的更新,原则上是kv形式的更新,但有些场景我们需要对一批数据进行更新操作,因此我们在之前kv更新的基础上,实现了传统SQL上的update更新

21.1. 使用限制

1.update set值之差update {table} set c1=x1 ,c2=x2 where ..表达,cN=XN 仅仅支持等值,如果需要进行计数更新,则必须借助upmode=xxx来实现
2.同累加更新一样,必须创建upmode字段
3.该表最好有unique主键,如若没有内部实际会通过uuid创建隐藏主键,内部加锁的粒度是按照主键进行更新加锁的,考虑到性能因素,并没有按照整个update语句加锁,因此update的过程中,可能会看到一部分记录更新过来,而另一部分记录还没有更新过来的情况.
4.更新模式非常消耗性能,目前只支持ssd硬盘,普通sata盘仅支持append,尽量不要更新.
5.每个列必须进行行存储,也即store中必须含有s标记

21.2. 建表例子

CREATE FOREIGN TABLE lxdb_update(
upkey text OPTIONS(key 'unique'),
upmode text,
str1 text, i1 bigint,
arr1 int[],arr2 text[]
) SERVER lxdb OPTIONS(store 'ids',realtime 'true');

select create_distributed_table('lxdb_update','upkey','hash','default',3);

21.3. 插入数据

INSERT INTO lxdb_update (upkey,upmode,str1,i1,arr1,arr2) VALUES 
('key1','replace','str1',1,ARRAY[0,1], ARRAY['111','222']);
INSERT INTO lxdb_update (upkey,upmode,str1,i1,arr1,arr2) VALUES 

('key2','replace','str1',1,ARRAY[0,1], ARRAY['333','444']);
INSERT INTO lxdb_update (upkey,upmode,str1,i1,arr1,arr2) VALUES 
('key3','replace','str1',1,ARRAY[0,1], ARRAY['555','666']);
INSERT INTO lxdb_update (upkey,upmode,str1,i1,arr1,arr2) VALUES 
('key4','replace','str1',1,ARRAY[0,1], ARRAY['777','888']);
--预览数据
select * from  lxdb_update ;

21.4. 批量修改值

update lxdb_update set i1=2, arr1=ARRAY[3,1] where str1='str1' ;
select * from  lxdb_update ;

数据会由原来的

upkey | upmode  | str1 | i1 | arr1  |  arr2   

-------+---------+------+----+-------+-----------

 key1  | replace | str1 |  1 | {0,1} | {111,222}

 key3  | replace | str1 |  1 | {0,1} | {555,666}

 key4  | replace | str1 |  1 | {0,1} | {777,888}

 key2  | replace | str1 |  1 | {0,1} | {333,444}

更新为

upkey | upmode  | str1 | i1 | arr1  |  arr2   

-------+--------+------+----+-------+-----------

 key4  | upset  | str1 |  2 | {3,1} | {777,888}

 key2  | upset  | str1 |  2 | {3,1} | {333,444}

 key1  | upset  | str1 |  2 | {3,1} | {111,222}

 key3  | upset  | str1 |  2 | {3,1} | {555,666}

21.5. 累加更新

​ upmode='incr'

update lxdb_update set upmode='incr' ,i1=2, arr1=ARRAY[3,1] where str1='str1' ;
select * from  lxdb_update ;

会将i1列在原有的基础上加2

会将arr1在原来数组的基础上追加3,1

数据会由原来的

upkey | upmode  | str1 | i1 | arr1  |  arr2   

-------+--------+------+----+-------+-----------

 key4  | upset  | str1 |  2 | {3,1} | {777,888}

 key2  | upset  | str1 |  2 | {3,1} | {333,444}

 key1  | upset  | str1 |  2 | {3,1} | {111,222}

 key3  | upset  | str1 |  2 | {3,1} | {555,666}

更新为

 upkey | upmode | str1 | i1 |  arr1   |  arr2   

-------+--------+------+----+-----------+-----------

 key4  | incr  | str1 |  4 | {3,1,3,1} | {777,888}

 key2  | incr  | str1 |  4 | {3,1,3,1} | {333,444}

 key1  | incr  | str1 |  4 | {3,1,3,1} | {111,222}

 key3  | incr  | str1 |  4 | {3,1,3,1} | {555,666}
Copyright © luxindb.com 2020 all right reserved修改时间: 2022-12-07 09:47:26

results matching ""

    No results matching ""