加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

PostgreSQL何以支持丰富的NoSQL特性?

发布时间:2018-11-08 07:44:24 所属栏目:MySql教程 来源:DBAplus社群
导读:副标题#e# 【新品产上线啦】51CTO播客,随时随地,碎片化学习 作者介绍 谭峰,网名francs,中国开源软件推进联盟PostgreSQL分会特聘专家,《PostgreSQL实战》作者之一,《PostgreSQL 9 Administration Cookbook》译者之一。现就职于浙江移动负责应用上云架

接着创建random_text_simple(length int4)函数,此函数会调用random_range(int4, int4)函数。

  1. CREATE OR REPLACE FUNCTION random_text_simple(length int4)  
  2. RETURNS text  
  3. LANGUAGE PLPGSQL  
  4. AS $$  
  5. DECLARE  
  6. possible_chars text := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';  
  7. output text := '';  
  8. i int4;  
  9. pos int4;  
  10. BEGIN  
  11. FOR i IN 1..length LOOP  
  12. pos := random_range(1, length(possible_chars));  
  13. output := output || substr(possible_chars, pos, 1);  
  14. END LOOP;  
  15. RETURN output;  
  16. END; 
  17. $$; 

random_text_simple(length int4)函数可以随机生成指定长度字符串,如下随机生成含三位字符的字符串:

  1. mydb=> SELECT random_text_simple(3);  
  2. random_text_simple  
  3. --------------------  
  4. LL9  
  5. (1 row) 

随机生成含六位字符的字符串,如下所示:

  1. mydb=> SELECT random_text_simple(6);  
  2. B81BPW  
  3. (1 row) 

后面会用到这个函数生成测试数据。

创建JSON测试表

创建user_ini测试表,并通过random_text_simple(length int4)函数插入100万随机生成六位字符的字符串测试数据,如下所示:

  1. mydb=> CREATE TABLE user_ini(id int4 ,user_id int8,  
  2. user_name character varying(64),  
  3. create_time timestamp(6) with time zone default clock_timestamp);  
  4. SELECT r,round(random*1000000), random_text_simple(6)  
  5. FROM generate_series(1,1000000) as r;  
  6. INSERT 0 1000000 

创建tbl_user_search_json表,并通过row_to_json函数将表user_ini行数据转换成json数据,如下所示:

  1. mydb=> CREATE TABLE tbl_user_search_json(id serial, user_info json);  
  2. CREATE TABLE  
  3. mydb=> INSERT INTO tbl_user_search_json(user_info)  
  4. SELECT row_to_json(user_ini) FROM user_ini;  
  5. INSERT 0 1000000 

生成的数据如下:

  1. mydb=> SELECT * FROM tbl_user_search_json LIMIT 1;  
  2. id | user_info  
  3. ----+-----------------------------------------------------------------------------------------------  
  4. 1 | {"id":1,"user_id":186536,"user_name":"KTU89H","create_time":"2017-08-05T15:59:25.359148+08:00"}  
  5. (1 row) 

JSON数据全文检索测试

使用全文检索查询表tbl_user_search_json的user_info字段中包含KTU89H字符的记录,如下所示:

  1. mydb=> SELECT * FROM tbl_user_search_json  
  2. WHERE to_tsvector('english',user_info) @@ to_tsquery('ENGLISH','KTU89H');  
  3. id | user_info  
  4. ----+---------------------------------------------------------------------------------------- 

以上SQL能正常执行说明全文检索支持json数据类型,只是上述SQL走了全表扫描性能低,执行时间为8061毫秒,如下所示:

  1. mydb=> EXPLAIN ANALYZE SELECT * FROM tbl_user_search_json  
  2. -----------------------------------------------------------------------------------  
  3. Seq Scan on tbl_user_search_json (cost=0.00..279513.00 rows=5000 width=104) (actual time=0.046..8061.858 rows=1 loops=1)  
  4. Filter: (to_tsvector('english'::regconfig, user_info) @@ '''ktu89h'''::tsquery)  
  5. Rows Removed by Filter: 999999  
  6. Planning time: 0.091 ms  
  7. Execution time: 8061.880 ms  
  8. (5 rows) 

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读