1

我有store表:

CREATE TABLE store(
  id SERIAL,
  product_ids INT[],
  product_prices NUMERIC[]
)

例如:

id | product_ids | product_prices 
----------------------------------
1  | {12, 4}     | {100, 202.5}

也就是说,ID为商品的价格为,商品ID12100的商品价格4202.5product_ids数组中的ID是唯一的。

例如,如果产品id=4有新价格,199我会这样更新:

UPDATE store SET 
product_prices [idx(product_ids , 4)] = 199
WHERE id = ?

这些数组中元素的顺序非常重要。我从不对这些数组进行排序,也许只是添加一个新的 product_id 和适当的价格,使用array_append().

所以问题是:Postgres 数组中的排序有多可靠?Postgres 是否有可能有一天会对数组或交换元素位置进行排序?

PS我需要用数组来做这个,我不想使用其他带有列的表store_idproduct_id等等product_price

4

1 回答 1

2

不,Postgres 不会自行更改数组的顺序。如果数据库这样做,我们都会遇到很多麻烦。

于 2015-12-19T16:21:21.887 回答