UPSERT VERTEX

UPSERT VERTEX语句结合UPDATEINSERT,如果点存在,会修改点的属性值;如果点不存在,会插入新的点。

UPSERT VERTEX性能远低于INSERT,因为UPSERT是一组分片级别的读取、修改、写入操作。

语法

  1. UPSERT VERTEX ON <tag> <vid>
  2. SET <update_prop>
  3. [WHEN <condition>]
  4. [YIELD <output>]
参数是否必须说明示例
ON <tag>指定点的Tag。要修改的属性必须在这个Tag内。ON player
<vid>指定要修改或插入的点ID。“player100”
SET <update_prop>指定如何修改属性值。SET age = age +1
WHEN <condition>指定过滤条件。WHEN name == “Tim”
YIELD <output>指定语句的输出格式。YIELD name AS Name

插入不存在的点

如果点不存在,无论WHEN子句的条件是否满足,都会插入点,同时执行SET子句,因此新插入的点的属性值取决于:

  • SET子句。

  • 属性是否有默认值。

例如:

  • 要插入的点包含基于Tagplayer的属性nameage

  • SET子句指定age=30

不同情况下的属性值如下表。

是否满足WHEN子句条件属性是否有默认值name属性值age属性值
默认值30
NULL30
默认值30
NULL30

示例如下:

  1. // 查看三个点是否存在,结果“Empty set”表示顶点不存在。
  2. nebula> FETCH PROP ON * "player666", "player667", "player668";
  3. Empty set
  4. nebula> UPSERT VERTEX ON player "player666" \
  5. SET age = 30 \
  6. WHEN name == "Joe" \
  7. YIELD name AS Name, age AS Age;
  8. +----------+----------+
  9. | Name | Age |
  10. +----------+----------+
  11. | __NULL__ | 30 |
  12. +----------+----------+
  13. nebula> UPSERT VERTEX ON player "player666" SET age = 31 WHEN name == "Joe" YIELD name AS Name, age AS Age;
  14. +----------+-----+
  15. | Name | Age |
  16. +----------+-----+
  17. | __NULL__ | 30 |
  18. +----------+-----+
  19. nebula> UPSERT VERTEX ON player "player667" \
  20. SET age = 31 \
  21. YIELD name AS Name, age AS Age;
  22. +----------+-----+
  23. | Name | Age |
  24. +----------+-----+
  25. | __NULL__ | 31 |
  26. +----------+-----+
  27. nebula> UPSERT VERTEX ON player "player668" \
  28. SET name = "Amber", age = age + 1 \
  29. YIELD name AS Name, age AS Age;
  30. +---------+----------+
  31. | Name | Age |
  32. +---------+----------+
  33. | "Amber" | __NULL__ |
  34. +---------+----------+

上面最后一个示例中,因为age没有默认值,插入点时,age默认值为NULL,执行age = age + 1后仍为NULL。如果age有默认值,则age = age + 1可以正常执行,例如:

  1. nebula> CREATE TAG player_with_default(name string, age int DEFAULT 20);
  2. Execution succeeded
  3. nebula> UPSERT VERTEX ON player_with_default "player101" \
  4. SET age = age + 1 \
  5. YIELD name AS Name, age AS Age;
  6. +----------+-----+
  7. | Name | Age |
  8. +----------+-----+
  9. | __NULL__ | 21 |
  10. +----------+-----+

修改存在的点

如果点存在,且满足WHEN子句的条件,就会修改点的属性值。

  1. nebula> FETCH PROP ON player "player101";
  2. +-----------------------------------------------------+
  3. | vertices_ |
  4. +-----------------------------------------------------+
  5. | ("player101" :player{age: 42, name: "Tony Parker"}) |
  6. +-----------------------------------------------------+
  7. nebula> UPSERT VERTEX ON player "player101" \
  8. SET age = age + 2 \
  9. WHEN name == "Tony Parker" \
  10. YIELD name AS Name, age AS Age;
  11. +---------------+-----+
  12. | Name | Age |
  13. +---------------+-----+
  14. | "Tony Parker" | 44 |
  15. +---------------+-----+

如果点存在,但是不满足WHEN子句的条件,修改不会生效。

  1. nebula> FETCH PROP ON player "player101";
  2. +-----------------------------------------------------+
  3. | vertices_ |
  4. +-----------------------------------------------------+
  5. | ("player101" :player{age: 44, name: "Tony Parker"}) |
  6. +-----------------------------------------------------+
  7. nebula> UPSERT VERTEX ON player "player101" \
  8. SET age = age + 2 \
  9. WHEN name == "Someone else" \
  10. YIELD name AS Name, age AS Age;
  11. +---------------+-----+
  12. | Name | Age |
  13. +---------------+-----+
  14. | "Tony Parker" | 44 |
  15. +---------------+-----+