Arrays 如何改进或索引postgresql的jsonb数组字段?
我通常使用jsonb字段存储数组数据。 例如,我想存储客户的条形码信息,我将创建如下表:Arrays 如何改进或索引postgresql的jsonb数组字段?,arrays,postgresql,indexing,jsonb,Arrays,Postgresql,Indexing,Jsonb,我通常使用jsonb字段存储数组数据。 例如,我想存储客户的条形码信息,我将创建如下表: create table customers(fcustomerid bigint, fcodes jsonb); 一个客户有一行,所有条形码信息存储在其fcodes字段中,如下所示: [ { "barcode":"000000001", "codeid":1, "product":"Coca
create table customers(fcustomerid bigint, fcodes jsonb);
一个客户有一行,所有条形码信息存储在其fcodes字段中,如下所示:
[
{
"barcode":"000000001",
"codeid":1,
"product":"Coca Cola",
"createdate":"2021-01-19",
"lottorry":true,
"lottdate":"2021-01-20",
"bonus":50
},
{
"barcode":"000000002",
"codeid":2,
"product":"Coca Cola",
"createdate":"2021-01-19",
"lottorry":false,
"lottdate":"",
"bonus":0
}
...
{
"barcode":"000500000",
"codeid":500000,
"product":"Pepsi Cola",
"createdate":"2021-01-19",
"lottorry":false,
"lottdate":"",
"bonus":0
}
]
update customers
set fcodes=jsonb_set(fcodes,concat('{',(mycodeid-1)::text,',lottery}'),'true'::jsonb)
where fcustomerid=999;
jsonb阵列可能存储数百万个具有相同结构的条形码对象。也许这不是一个好主意,但您知道,当我有数千名客户时,我可以将所有数据存储在一个表中,一名客户在该表中有一行,其所有数据存储在一个字段中,看起来非常简洁且易于管理
对于这种应用场景,如何高效地插入、修改或查询数据
我可以使用jsonb_insert插入一个对象,就像:
update customers
set fcodes=jsonb_insert(fcodes,'{-1}','{...}'::jsonb)
where fcustomerid=999;
select jsonb_path_query_array(fcodes,'$ ? (product=="Pepsi Cola")'
from customer
where fcustomerid=999;
当我想修改一些对象时,我发现这有点困难,我应该先知道对象的索引,如果我使用增量键codeid作为数组索引,事情看起来很容易。我可以使用jsonb_modify,如下所示:
[
{
"barcode":"000000001",
"codeid":1,
"product":"Coca Cola",
"createdate":"2021-01-19",
"lottorry":true,
"lottdate":"2021-01-20",
"bonus":50
},
{
"barcode":"000000002",
"codeid":2,
"product":"Coca Cola",
"createdate":"2021-01-19",
"lottorry":false,
"lottdate":"",
"bonus":0
}
...
{
"barcode":"000500000",
"codeid":500000,
"product":"Pepsi Cola",
"createdate":"2021-01-19",
"lottorry":false,
"lottdate":"",
"bonus":0
}
]
update customers
set fcodes=jsonb_set(fcodes,concat('{',(mycodeid-1)::text,',lottery}'),'true'::jsonb)
where fcustomerid=999;
但是如果我想用createdate、bonus、lottorry或product查询jsonb数组中的对象,我应该使用jsonpath操作符。就像:
update customers
set fcodes=jsonb_insert(fcodes,'{-1}','{...}'::jsonb)
where fcustomerid=999;
select jsonb_path_query_array(fcodes,'$ ? (product=="Pepsi Cola")'
from customer
where fcustomerid=999;
或者说:
select jsonb_path_query_array(fcodes,'$ ? (lottdate.datetime()>="2021-01-01".datetime() && lottdate.datetime()<="2021-01-31".datetime())'
from customer
where fcustomerid=999;
这个jsonb索引看起来很有用,但它在不同的行之间看起来很有用,我的操作主要在一行的one jsonb字段中工作
我非常担心效率,对于存储在一行的一个jsonb字段中的数百万对象,这是一个好主意吗?在这种情况下,如何提高效率?特别是对于查询。您担心是对的。使用这样一个巨大的JSON,您将永远无法获得良好的性能 您的数据根本不需要JSON。创建一个表,该表存储单个条形码,并具有对客户的外键引用。然后一切都会变得简单高效
从本论坛的问题来看,在数据库中使用JSON几乎总是错误的选择。您的担心是对的。使用这样一个巨大的JSON,您将永远无法获得良好的性能 您的数据根本不需要JSON。创建一个表,该表存储单个条形码,并具有对客户的外键引用。然后一切都会变得简单高效
从本论坛的问题来看,在数据库中使用JSON几乎总是错误的选择。不要改进,重写为一对多关系中的两个表。不要改进,重写为一对多关系中的两个表。非常感谢!如果我使用一个表来存储,如果一个表中的行非常大(例如,十亿行),那么效率可以接受吗?给我一些建议,很多次的感恩节都没问题。大的JSON值是。谢谢你的建议,我将创建表格。非常感谢!如果我使用一个表来存储,如果一个表中的行非常大(例如,十亿行),那么效率可以接受吗?给我一些建议,很多次的感恩节都没问题。JSON值很大。谢谢您的建议,我将创建一个表