自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PostgreSQL的數(shù)組

運維 數(shù)據(jù)庫運維 大數(shù)據(jù) PostgreSQL
PostgreSQL 有很多豐富的開箱即用的數(shù)據(jù)類型,從標準的數(shù)字數(shù)據(jù)類型、到幾何類型,甚至網(wǎng)絡數(shù)據(jù)類型等等。雖然很多人會忽略這些數(shù)據(jù)類型,但卻是我最喜歡的特性之一。

PostgreSQL 有很多豐富的開箱即用的數(shù)據(jù)類型,從標準的數(shù)字數(shù)據(jù)類型、到幾何類型,甚至網(wǎng)絡數(shù)據(jù)類型等等。雖然很多人會忽略這些數(shù)據(jù)類型,但卻是我最喜歡的特性之一。而數(shù)組數(shù)據(jù)類型正如你所期望的,可以在 PostgreSQL 存儲數(shù)組數(shù)據(jù),有了這個特性,你可以在單個表中實現(xiàn)以往需要多個表才能實現(xiàn)的存儲要求。

為什么要使用數(shù)組來存儲數(shù)據(jù),如果你是應用開發(fā)人員,那么在數(shù)據(jù)庫中使用同樣的模型來存儲程序中的數(shù)據(jù),何樂而不為呢。況且這樣的做法還能提升性能。下面我們將介紹如何使用 PostgreSQL 的數(shù)組類型。

假設你在一個網(wǎng)站上購買物品,那么你所購買的信息就可以用下面這個表來表示:

  1. CREATE TABLE purchases (  
  2.     id integer NOT NULL,  
  3.     user_id integer,  
  4.     items decimal(10,2) [100][1],  
  5.     occurred_at timestamp 
  6. ); 

在這個表中,擁有一個數(shù)組字段來保持多個商品記錄,包括:

  • 購買商品的編號
  • 數(shù)量
  • 價格

要往這個表里插入數(shù)據(jù)的 SQL 如下:

  1. INSERT INTO purchases VALUES (1, 37, '{{15.0, 1.0, 25.0}, {15.0, 1.0, 25.0}}', now());  
  2. INSERT INTO purchases VALUES (2, 2, '{{11.0, 1.0, 4.99}}', now()); 

一個更有實際意義的例子是標簽的使用,你可以用標簽來標識購買的物品:

  1. CREATE TABLE products (  
  2.     id integer NOT NULL,  
  3.     title character varying(255),  
  4.     description text,  
  5.     tags text[],  
  6.     price numeric(10,2)  
  7. ); 

你可使用基本的查詢語句來獲取數(shù)據(jù):

  1. SELECT title, unnest(tags) items FROM products 

你還可以使用 Postgres 的 Gin and Gist  索引來根據(jù)指定的標簽快速搜索產(chǎn)品:

  1. -- Search where product contains tag ids 1 AND 2  
  2. SELECT  *  
  3. FROM    products  
  4. WHERE   tags @> ARRAY[1, 2]  
  5.  
  6. -- Search where product contains tag ids 1 OR 2  
  7. SELECT  *  
  8. FROM    products  
  9. WHERE   tags && ARRAY[1, 2] 

英文原文:http://craigkerstiens.com/2012/08/20/arrays-in-postgres/

本文鏈接:http://www.oschina.net/question/12_65936

責任編輯:林師授 來源: OSCHINA
相關推薦

2014-03-03 10:10:37

PostgreSQL數(shù)組

2024-04-29 08:50:01

PostgreSQJSON數(shù)組

2022-10-20 23:15:10

PostgreSQL算法機制

2017-08-17 15:13:52

PostgreSQL MVCC機制

2014-01-05 17:51:03

2010-09-29 13:52:33

PostgreSQL

2023-03-27 09:57:00

PostgreSQL并發(fā)索引

2023-11-30 16:29:16

PostgreSQL數(shù)據(jù)庫Kubernetes

2022-07-05 10:06:55

PostgreSQLMySQL數(shù)據(jù)庫

2013-12-26 13:19:26

PostgreSQL優(yōu)化

2014-01-03 09:30:44

PostgreSQL數(shù)字函數(shù)

2014-01-03 13:27:33

PostgreSQL

2020-02-18 20:00:31

PostgreSQL數(shù)據(jù)庫

2014-09-16 10:24:49

PostgreSQLNoSQL

2023-03-10 08:37:33

預熱優(yōu)化PostgreSQL

2012-03-27 11:45:21

vmwareCloudFoundrPostgreSQL

2024-11-15 08:00:00

2014-01-02 13:22:01

PythonPostgreSQL

2014-01-02 14:04:39

PostgreSQLPerl

2014-01-02 15:41:24

PostgreSQLPHP
點贊
收藏

51CTO技術棧公眾號