接着建设random_text_simple(length int4)函数,此函数会挪用random_range(int4, int4)函数。
- CREATE OR REPLACE FUNCTION random_text_simple(length int4)
- RETURNS text
- LANGUAGE PLPGSQL
- AS $$
- DECLARE
- possible_chars text := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- output text := '';
- i int4;
- pos int4;
- BEGIN
- FOR i IN 1..length LOOP
- pos := random_range(1, length(possible_chars));
- output := output || substr(possible_chars, pos, 1);
- END LOOP;
- RETURN output;
- END;
- $$;
random_text_simple(length int4)函数可以随机天生指定长度字符串,如下随机天生含三位字符的字符串:
- mydb=> SELECT random_text_simple(3);
- random_text_simple
- --------------------
- LL9
- (1 row)
随机天生含六位字符的字符串,如下所示:
- mydb=> SELECT random_text_simple(6);
- B81BPW
- (1 row)
后头会用到这个函数天生测试数据。
建设JSON测试表
建设user_ini测试表,并通过random_text_simple(length int4)函数插入100万随机天生六位字符的字符串测试数据,如下所示:
- mydb=> CREATE TABLE user_ini(id int4 ,user_id int8,
- user_name character varying(64),
- create_time timestamp(6) with time zone default clock_timestamp);
- SELECT r,round(random*1000000), random_text_simple(6)
- FROM generate_series(1,1000000) as r;
- INSERT 0 1000000
建设tbl_user_search_json表,并通过row_to_json函数将表user_ini行数据转换成json数据,如下所示:
- mydb=> CREATE TABLE tbl_user_search_json(id serial, user_info json);
- CREATE TABLE
- mydb=> INSERT INTO tbl_user_search_json(user_info)
- SELECT row_to_json(user_ini) FROM user_ini;
- INSERT 0 1000000
天生的数据如下:
- mydb=> SELECT * FROM tbl_user_search_json LIMIT 1;
- id | user_info
- ----+-----------------------------------------------------------------------------------------------
- 1 | {"id":1,"user_id":186536,"user_name":"KTU89H","create_time":"2017-08-05T15:59:25.359148+08:00"}
- (1 row)
JSON数据全文检索测试
行使全文检索查询表tbl_user_search_json的user_info字段中包括KTU89H字符的记录,如下所示:
- mydb=> SELECT * FROM tbl_user_search_json
- WHERE to_tsvector('english',user_info) @@ to_tsquery('ENGLISH','KTU89H');
- id | user_info
- ----+----------------------------------------------------------------------------------------
以上SQL能正常执行声名全文检索支持json数据范例,只是上述SQL走了全表扫描机能低,执行时刻为8061毫秒,如下所示:
- mydb=> EXPLAIN ANALYZE SELECT * FROM tbl_user_search_json
- -----------------------------------------------------------------------------------
- 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)
- Filter: (to_tsvector('english'::regconfig, user_info) @@ '''ktu89h'''::tsquery)
- Rows Removed by Filter: 999999
- Planning time: 0.091 ms
- Execution time: 8061.880 ms
- (5 rows)
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|