[Original] PostgreSQLhstore Column Performance Improvement example

Source: Internet
Author: User
PostgreSQL supports hstore to store KEY-VALUE data, which is similar to ARRAY or JSON. To use this type of data efficiently, you must use efficient indexes. Let's take a look at two different types of indexes today.

PostgreSQL supports hstore to store KEY-VALUE data, which is similar to ARRAY or JSON. To use this type of data efficiently, you must use efficient indexes. Let's take a look at two different types of indexes today.

PostgreSQL supports hstore to store data such as KEY-> VALUE, which is also similar to ARRAY or JSON. To use this type of data efficiently, you must use efficient indexes. Let's take a look at the performance of two different types of indexes for the same retrieval request.


Suppose we have an original table with a BTREE index based on the str1 field.

T_girl = # \ d status_check; Table "ytt. status_check "Column | Type | Modifiers -------- + keys + ----------- is_yes | boolean | not null str1 | character varying (20) | not null str2 | character varying (20) | not nullIndexes: "index_status_check_str1" btree (str1)



There are 10 million records. The data is roughly as follows,

T_girl = # select * from status_check limit 2; is_yes | str1 | str2 -------- + ------ + ---------------------- f | 0 | counter t | 1 | average (2 rows) Time: 0.617 mst_girl = #


Stores the status_check_hstore table structure of the hstore type. There is a GIST index based on the str1_str2 field.

Table "ytt. pipeline "Column | Type | Modifiers ----------- + --------- + ----------- is_yes | boolean | str1_str2 | hstore | Indexes:" Pipeline "gist (str1_str2) t_girl = # select * from status_check_hstore limit 2; is_yes | str1_str2 -------- + effecf | "0" => "cfcd208495d565ef66e7" t | "1" => "c4ca4238a0b923820dcc" (2 rows) Time: 39.874 MS


Next we will get the same results as the original table query, of course, the original table query is very efficient. The table statements and results are as follows,

T_girl = # select * from status_check where str1 in ('10', '23', '33 '); is_yes | str1 | str2 -------- + ------ + -------------------- t | 10 | d3d9446802a44259755d t | 23 | RJF | 33 | 182be0c5cd5072bb18 (3 rows) Time: 0.690 MS


The preceding statement takes less than 1 ms.


Next we will query the hstore table,

T_girl = # select is_yes, skeys (strpolicstr2), svals (strpolicstr2) from status_check_hstore where str1_str2? | Array ['10', '23', '33']; is_yes | skeys | svals -------- + ------- + -------------------- t | 10 | d3d9446802a44259755d t | 23 | RJF | 33 | 182be0c5cd5072bb18 (3 rows) Time: 40.256 MS

My days are dozens of times slower than the query of the original table.


Check the query plan and scan all rows.

Query plan into Bitmap Heap Scan on status_check_hstore (cost = 5. 06 .. 790.12 rows = 100000 width = 38) Recheck Cond: (str1_str2? | '{10, 23, 33}': text [])-> Bitmap Index Scan on idx_str_str2_gist (cost = 0. 00 .. 5.03 rows = 100 width = 0) Index Cond: (str1_str2? | '{0.688, 33}': text []) (4 rows) Time: MS


We want to optimize this statement. If we convert this statement into the same as the original statement, can we use the BTREE index?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.