{"id":287,"date":"2025-11-27T15:19:28","date_gmt":"2025-11-27T07:19:28","guid":{"rendered":"https:\/\/yuzikimikage.top\/?p=287"},"modified":"2025-11-27T15:29:06","modified_gmt":"2025-11-27T07:29:06","slug":"postgresql_index","status":"publish","type":"post","link":"https:\/\/yuzikimikage.top\/index.php\/2025\/11\/27\/postgresql_index\/","title":{"rendered":"PG\u7d22\u5f15\u4ecb\u7ecd"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">POSTGRESQL\u7684\u5e38\u7528\u7d22\u5f15<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u7d22\u5f15\u7c7b\u578b<\/td><td>\u7d22\u5f15\u529f\u80fd<\/td><\/tr><tr><td>btree<\/td><td>\u5feb\u901f\u7684\u6570\u636e\u67e5\u627e\u529f\u80fd,\u5bf9\u4e8e\u5927\u4e8e,\u5c0f\u4e8e,\u7b49\u4e8e, \u534a\u6a21\u7cca, \u7b49\u67e5\u627e\u63d0\u4f9b\u641c\u7d22\u529f\u80fd<\/td><\/tr><tr><td>hash<\/td><td>\u5feb\u901f\u7684\u6570\u636e\u67e5\u627e\u529f\u80fd,\u5bf9\u4e8e\u7b49\u503c,\u5e76\u4e14\u5b57\u6bb5\u8f83\u957f\u7684\u5b57\u6bb5\u67e5\u627e\u6709\u7279\u6b8a\u7684\u4f18\u5316\u529f\u80fd<\/td><\/tr><tr><td>gin<\/td><td>\u4e00\u79cd\u5012\u6392\u7d22\u5f15,\u5bf9\u4e8e\u6570\u7ec4,\u5168\u6587\u68c0\u7d22,\u4e2d\u7684\u76f8\u4ea4,\u5305\u542b,\u5927\u4e8e,\u5de6\u53f3\u8fb9\u7684\u67e5\u8be2\u6709\u7279\u6b8a\u7684\u4f18\u5316<\/td><\/tr><tr><td>gist<\/td><td>\u4e00\u79cd\u901a\u7528\u7684\u7d22\u5f15\u5165\u53e3,\u5bf9\u4e8e\u8303\u56f4,\u7a7a\u95f4,\u51e0\u4f55,\u7a7a\u95f4\u7b49\u6570\u636e\u6709\u7279\u6b8a\u7684\u67e5\u8be2\u4f18\u5316<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>POSTGRESQL\u7684\u7d22\u5f15\u7c7b\u578b\u672c\u8eab\u662f\u6bd4\u8f83\u4e30\u5bcc\u7684,\u5bf9\u4e8e\u4e0d\u540c\u7684\u67e5\u8be2\u7684\u65b9\u5f0f\u6709\u4e0d\u540c\u7684\u5bf9\u5e94\u7684\u7d22\u5f15\u6765\u8fdb\u884c\u5efa\u7acb<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">BTREE\u7d22\u5f15<\/h1>\n\n\n\n<p>BTREE \u662f\u5404\u79cd\u6570\u636e\u5e93\u5fc5\u5907\u7684\u7d22\u5f15\u7c7b\u578b,\u63d0\u4f9b\u7b49\u503c,\u4e0d\u7b49\u503c,\u5927\u4e8e\u5c0f\u4e8e\u4ee5\u53ca\u534a\u6a21\u7cca\u67e5\u8be2\u7b49\u65b9\u5f0f\u63d0\u4f9b\u6570\u636e\u7684\u641c\u7d22<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>create table t_btree(id int, info text);\ninsert into t_btree select generate_series(1,10000), md5(random()::text);\nexplain (analyze,verbose,timing,costs,buffers) select * from t_btree where id=1;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"222\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226508-image-1024x222.gif\" alt=\"\" class=\"wp-image-289\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226508-image-1024x222.gif 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226508-image-300x65.gif 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226508-image-768x166.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>create index idx_t_btree_1 on t_btree using btree (id);\nexplain (analyze,verbose,timing,costs,buffers) select * from t_btree where id=1;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"598\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-1024x598.gif\" alt=\"\" class=\"wp-image-299\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-1024x598.gif 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-300x175.gif 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-768x448.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\nrelname AS \"\u8868\u540d\",\n&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_total_relation_size(relname::regclass)) AS \"\u8868\u603b\u5927\u5c0f\",\n&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_relation_size(relname::regclass)) AS \"\u8868\u5927\u5c0f\",\n&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_indexes_size(relname::regclass)) AS \"\u7d22\u5f15\u5927\u5c0f\"\nFROM pg_stat_all_tables\nWHERE relname = 't_btree';<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"885\" height=\"308\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-1.gif\" alt=\"\" class=\"wp-image-302\"\/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">HASH\u7d22\u5f15<\/h1>\n\n\n\n<p>HASH \u7d22\u5f15\u662f\u9488\u5bf9\u5927\u5b57\u6bb5\u7684\u67e5\u8be2,\u7531\u4e8eBTREE \u5b57\u6bb5\u6bcf\u884c\u5927\u5c0f\u6700\u5927\u4e3a 2704 ,\u6240\u4ee5\u8d85\u51fa2704 BYTE\u4e00\u884c\u7684INDEX \u5e76\u4e14\u662f\u7b49\u503c\u8fd0\u7b97\u7684,\u5f88\u9002\u5408HASH \u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\u3001\u5efa\u8868\uff0c\u63d2\u5165\u6570\u636e\ncreate table t_hash (id int,info text);\ninsert into t_hash select generate_series(1,100),repeat(md5(random()::text),10000);\n\n2\u3001btree\u7d22\u5f15\ncreate index idx_t_hash_1 on t_hash using btree (info);\n\n3\u3001hash\u7d22\u5f15\ncreate index idx_t_hash_1 on t_hash using hash (info);\nSELECT pg_size_pretty(pg_relation_size('idx_t_hash_1'));\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"484\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-5-1024x484.gif\" alt=\"\" class=\"wp-image-304\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-5-1024x484.gif 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-5-300x142.gif 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-5-768x363.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><br><a><\/a>\u4f8b\uff1a\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u7ba1\u7406\u7cfb\u7edf\uff0c\u5176\u4e2d\u7528\u6237\u8868 users \u5305\u542b\u7528\u6237\u7684 id\uff08\u552f\u4e00\u6807\u8bc6\u7b26\uff09\u3001username\uff08\u7528\u6237\u540d\uff09\u548c email\uff08\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff09\u3002\u6211\u4eec\u9884\u8ba1\u4f1a\u6709\u5927\u91cf\u7684\u67e5\u8be2\u64cd\u4f5c\u662f\u57fa\u4e8e email \u5b57\u6bb5\u8fdb\u884c\u7684\uff0c\u5e76\u4e14 email \u5b57\u6bb5\u5177\u6709\u552f\u4e00\u6027\u7ea6\u675f\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u5728 email \u5b57\u6bb5\u4e0a\u4f7f\u7528\u54c8\u5e0c\u7d22\u5f15\u6765\u4f18\u5316\u8fd9\u4e9b\u67e5\u8be2\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1.\u5efa\u8868\nCREATE TABLE users (\n    id SERIAL PRIMARY KEY,\n    username VARCHAR(255) NOT NULL,\n    email VARCHAR(255) NOT NULL UNIQUE\n);\n\n2.\u9020\u6570\nDO $$\nDECLARE\n    i INT := 1;\nBEGIN\n    WHILE i &lt;= 30000 LOOP\n        INSERT INTO users1 (username, email) VALUES (\n            'user' || i,\n            'user' || i || '@example123456789example.com'\n        );\n        i := i + 1;\n    END LOOP;\nEND $$;\n\n3.\u521b\u5efabtree\u7d22\u5f15\nCREATE INDEX idx_email ON users using btree(email);\nSELECT pg_size_pretty(pg_relation_size('idx_email'));\nexplain (analyze,verbose,timing,costs,buffers) SELECT * FROM users WHERE email = 'user999@example123456789example.com';\n\n4.\u5220\u9664btree\u7d22\u5f15\ndrop index idx_email;\n\n5.\u521b\u5efahash\u7d22\u5f15\nCREATE INDEX idx_email ON users using hash(email);\nSELECT pg_size_pretty(pg_relation_size('idx_email'));\n\nexplain (analyze,verbose,timing,costs,buffers) SELECT * FROM users WHERE email = 'user999@example123456789example.com';\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"613\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-1024x613.jpg\" alt=\"\" class=\"wp-image-294\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-1024x613.jpg 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-300x179.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-768x459.jpg 768w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image.jpg 1063w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"745\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-4-1024x745.jpg\" alt=\"\" class=\"wp-image-295\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-4-1024x745.jpg 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-4-300x218.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-4-768x559.jpg 768w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-4.jpg 1062w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"930\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-1024x930.jpg\" alt=\"\" class=\"wp-image-303\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-1024x930.jpg 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-300x272.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-768x697.jpg 768w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image.jpg 1062w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5927\u5b57\u6bb5\u8fdb\u884c\u7b49\u503c\u67e5\u8be2\u65f6\uff0c\u4f7f\u7528HASH \u7d22\u5f15\u901f\u5ea6\u66f4\u5feb<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>hash \u7d22\u5f15\u7684\u7f3a\u70b9\uff0c\u521b\u5efa\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\uff0c\u662f\u9700\u8981CPU \u8fdb\u884c\u8ba1\u7b97\u7684\uff0c\u8017\u8d39CPU\u5927\u91cf\u8d44\u6e90<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>hash \u7d22\u5f15\u4e3b\u8981\u7684\u67e5\u8be2\u65b9\u5f0f\u548c\u5bf9\u8c61\u662f \u7b49\u503c\u8ba1\u7b97 select * from table where id = \u2018223\u2019; \u7528hash\u7d22\u5f15<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">GIN\u7d22\u5f15<\/h1>\n\n\n\n<p>GIN \u7d22\u5f15\u5bf9\u4e8e\u591a\u5217\u503c\u7684\u67e5\u627e\u6709\u7279\u6b8a\u7684\u529f\u80fd<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>create table t_gin3 (id int, c1 int, c2 int, c3 int, c4 int, c5 int, c6 int, c7 int, c8 int, c9 int);\ninsert into t_gin3 select generate_series(1,10000), random()*10, random()*20, random()*30, random()*40, random()*50, random()*60, random()*70, random()*80, random()*90;\nexplain (analyze,verbose,timing,costs,buffers) select * from t_gin3 where c5 = 5 or c6 =9 or c7=19 or c4 = 3;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"218\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-4-1024x218.gif\" alt=\"\" class=\"wp-image-301\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-4-1024x218.gif 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-4-300x64.gif 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-4-768x163.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"452\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-6-1024x452.jpg\" alt=\"\" class=\"wp-image-296\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-6-1024x452.jpg 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-6-300x132.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-6-768x339.jpg 768w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-6.jpg 1062w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE EXTENSION btree_gin;\ncreate index idx_t_gin3_1 on t_gin3 using gin (c1,c2,c3,c4,c5,c6,c7,c8,c9);<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"925\" height=\"95\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226508-image.jpg\" alt=\"\" class=\"wp-image-290\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226508-image.jpg 925w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226508-image-300x31.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226508-image-768x79.jpg 768w\" sizes=\"auto, (max-width: 925px) 100vw, 925px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\nrelname AS \"\u8868\u540d\",\n&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_total_relation_size(relname::regclass)) AS \"\u8868\u603b\u5927\u5c0f\",\n&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_relation_size(relname::regclass)) AS \"\u8868\u5927\u5c0f\",\n&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_indexes_size(relname::regclass)) AS \"\u7d22\u5f15\u5927\u5c0f\"\nFROM pg_stat_all_tables\nWHERE relname = 't_gin3';<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1014\" height=\"296\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-3.gif\" alt=\"\" class=\"wp-image-298\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>explain (analyze,verbose,timing,costs,buffers) select * from t_gin3 where c5 = 5 or c6 =9 or c7=19 or c4 = 3;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"993\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-5-1024x993.jpg\" alt=\"\" class=\"wp-image-297\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-5-1024x993.jpg 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-5-300x291.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-5-768x745.jpg 768w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-5.jpg 1062w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">\u8054\u5408\u7d22\u5f15<\/h1>\n\n\n\n<p>\u6761\u4ef6\u4e2d\u7ecf\u5e38<strong>\u7ec4\u5408\u51fa\u73b0\u7684\u5217<\/strong>\u53ef\u4ee5\u8003\u8651\u5efa\u7acb\u8054\u5408\u7d22\u5f15\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u8ba2\u5355\u8868 orders\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<p>order_id\uff1a\u8ba2\u5355\u7f16\u53f7<\/p>\n\n\n\n<p>customer_id\uff1a\u5ba2\u6237\u7f16\u53f7<\/p>\n\n\n\n<p>order_date\uff1a\u8ba2\u5355\u65e5\u671f<\/p>\n\n\n\n<p>amount\uff1a\u8ba2\u5355\u91d1\u989d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\u3001\u5efa\u8868\n\nCREATE TABLE orders (\n    order_id SERIAL PRIMARY KEY,\n    customer_id INT NOT NULL,\n    order_date DATE NOT NULL,\n    amount DECIMAL(10, 2) NOT NULL\n);\n\n2\u3001\u63d2\u516530w\u6761\u6570\u636e\n\nINSERT INTO orders (customer_id, order_date, amount)\nSELECT\n    (ABS((RANDOM() * 1000000000000000000)::BIGINT) % 10000) + 1 AS customer_id,\n    CURRENT_DATE - INTERVAL '1 year' * (ABS((RANDOM() * 1000000000000000000)::BIGINT) % 10) AS order_date,\n    (ABS((RANDOM() * 1000000000000000000)::BIGINT) % 10000) \/ 100.0 AS amount\nFROM generate_series(1, 300000);\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"505\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-6-1024x505.gif\" alt=\"\" class=\"wp-image-305\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-6-1024x505.gif 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-6-300x148.gif 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-6-768x379.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>3\u3001\u521b\u5efa\u5355\u4e2a\u7d22\u5f15\n\nCREATE INDEX idx_customer_id ON orders (customer_id);\nCREATE INDEX idx_order_date ON orders (order_date);\n\n4\u3001\u67e5\u8be2\u8bed\u53e5\n\nEXPLAIN ANALYZE\nSELECT * FROM orders\nWHERE customer_id = 1234 AND order_date BETWEEN '2024-01-01' AND '2024-12-31';\n\n5\u3001\u7d22\u5f15\u5927\u5c0f\nSELECT pg_size_pretty(pg_relation_size('idx_customer_id'));\nSELECT pg_size_pretty(pg_relation_size('idx_order_date'));\n\n6\u3001\u5220\u9664\u5355\u4e2a\u7d22\u5f15\ndrop index idx_customer_id;\ndrop index idx_order_date;\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"832\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226511-image-1-1024x832.gif\" alt=\"\" class=\"wp-image-307\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226511-image-1-1024x832.gif 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226511-image-1-300x244.gif 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226511-image-1-768x624.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>7\u3001\u521b\u5efa\u8054\u5408\u7d22\u5f15\n\nCREATE INDEX idx_customer_id_order_date ON orders (customer_id, order_date);\n\n8\u3001\u67e5\u8be2\u8bed\u53e5\nEXPLAIN ANALYZE\nSELECT * FROM orders\nWHERE customer_id = 1234 AND order_date BETWEEN '2024-01-01' AND '2024-12-31';\n\n9\u3001\u7d22\u5f15\u5927\u5c0f\n\nSELECT pg_size_pretty(pg_relation_size('idx_customer_id_order_date'));\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"910\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226511-image-1024x910.gif\" alt=\"\" class=\"wp-image-306\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226511-image-1024x910.gif 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226511-image-300x267.gif 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226511-image-768x683.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u6027\u80fd\u8bb2\u89e3<\/p>\n\n\n\n<p>\u5355\u4e2a\u7d22\u5f15\uff1a\u867d\u7136\u53ef\u4ee5\u52a0\u901f\u5bf9 customer_id \u7684\u8fc7\u6ee4\uff0c\u4f46\u5bf9 order_date \u7684\u8fc7\u6ee4\u4ecd\u9700\u8981\u989d\u5916\u7684\u626b\u63cf\u548c\u8fc7\u6ee4\u64cd\u4f5c\uff0c\u5bfc\u81f4\u67e5\u8be2\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n\n\n\n<p>\u8054\u5408\u7d22\u5f15\uff1a\u80fd\u591f\u540c\u65f6\u5229\u7528 customer_id \u548c order_date \u7684\u8fc7\u6ee4\u6761\u4ef6\uff0c\u76f4\u63a5\u5b9a\u4f4d\u5230\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\uff0c\u51cf\u5c11\u4e86\u626b\u63cf\u8303\u56f4\uff0c\u663e\u8457\u63d0\u9ad8\u4e86\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u540c\u65f6\uff0c\u8054\u5408\u7d22\u5f15\u5360\u7528\u66f4\u5c11\u7684\u7a7a\u95f4<\/p>\n\n\n\n<p>\u6ce8\u610f\u4e8b\u9879<\/p>\n\n\n\n<p>\u8054\u5408\u7d22\u5f15\u7684\u6548\u7387\u53d6\u51b3\u4e8e\u67e5\u8be2\u6761\u4ef6\u662f\u5426\u8986\u76d6\u7d22\u5f15\u7684\u6240\u6709\u5217\u3002\u5982\u679c\u67e5\u8be2\u6761\u4ef6\u4e2d\u7f3a\u5c11\u8054\u5408\u7d22\u5f15\u7684\u5148\u5bfc\u5217\uff08\u5982 customer_id\uff09\uff0c\u5219\u8054\u5408\u7d22\u5f15\u53ef\u80fd\u65e0\u6cd5\u88ab\u5145\u5206\u5229\u7528\u3002<\/p>\n\n\n\n<p>\u521b\u5efa\u8054\u5408\u7d22\u5f15\u65f6\uff0c\u5e94\u6839\u636e\u67e5\u8be2\u6a21\u5f0f\u548c\u6570\u636e\u5206\u5e03\u5408\u7406\u9009\u62e9\u5217\u7684\u987a\u5e8f\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7d22\u5f15\u63a8\u8350\uff08openGauss\uff09<\/h1>\n\n\n\n<p>\u5355query\u7d22\u5f15\u63a8\u8350\u529f\u80fd\u57fa\u4e8e\u67e5\u8be2\u8bed\u53e5\u7684\u8bed\u4e49\u4fe1\u606f\u548c\u6570\u636e\u5e93\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u5bf9\u7528\u6237\u8f93\u5165\u7684\u5355\u6761\u67e5\u8be2\u8bed\u53e5\u751f\u6210\u63a8\u8350\u7684\u7d22\u5f15\u3002\u5355query\u7d22\u5f15\u63a8\u8350\u529f\u80fd\u652f\u6301\u7528\u6237\u5728\u6570\u636e\u5e93\u4e2d\u76f4\u63a5\u8fdb\u884c\u64cd\u4f5c\uff0c\u672c\u529f\u80fd\u6d89\u53ca\u7684\u51fd\u6570\u63a5\u53e3\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>gs_index_advise<\/p>\n\n\n\n<p>\u529f\u80fd\u63cf\u8ff0\uff1a\u9488\u5bf9\u5355\u6761\u67e5\u8be2\u8bed\u53e5\u751f\u6210\u63a8\u8350\u7d22\u5f15\u3002<\/p>\n\n\n\n<p>\u53c2\u6570\u8bf4\u660e\uff1aSQL\u8bed\u53e5\u5b57\u7b26\u4e32\u3002 \u53c2\u6570\u7c7b\u578b\u662f\u6587\u672c\u578b\uff0c\u5982\u679c\u53c2\u6570\u4e2d\u5b58\u5728\u5982\u5355\u5f15\u53f7\uff08'\uff09\u7b49\u7279\u6b8a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\uff08'\uff09\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b1\uff1a\u666e\u901a\u8868\u63a8\u8350\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\u3001\u521b\u5efa\u6d4b\u8bd5\u8868\u5e76\u63d2\u5165\u6570\u636e\ncreate table tb_ai_01 (c_w_id integer not null,c_d_id integer not null,c_id integer not null);\ninsert into tb_ai_01 values(generate_series(1, 200000),generate_series(1, 200000),generate_series(1, 200000));\n2\u3001\u5206\u6790\u8868\nanalyze tb_ai_01;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"167\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-3-1024x167.jpg\" alt=\"\" class=\"wp-image-292\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-3-1024x167.jpg 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-3-300x49.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-3-768x125.jpg 768w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-3.jpg 1063w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>3\u3001\u6267\u884c\u5355\u7d22\u5f15\u63a8\u8350\nselect \"table\", \"column\" from gs_index_advise('SELECT c_d_id from tb_ai_01 where c_w_id &gt; 10 and c_w_id &lt; 20');\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"115\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-1-1024x115.jpg\" alt=\"\" class=\"wp-image-293\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-1-1024x115.jpg 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-1-300x34.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-1-768x86.jpg 768w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-1.jpg 1062w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>4\u3001\u6309\u63a8\u8350\u521b\u5efa\u7d22\u5f15\uff0c\u518d\u6b21\u6267\u884c\u7d22\u5f15\u63a8\u8350\u8bed\u53e5\nCREATE INDEX idx on tb_ai_01(c_w_id);\nselect \"table\", \"column\" from gs_index_advise('SELECT c_d_id from tb_ai_01 where c_w_id &gt; 10 and c_w_id &lt; 20');<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"153\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-2-1024x153.jpg\" alt=\"\" class=\"wp-image-291\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-2-1024x153.jpg 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-2-300x45.jpg 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-2-768x115.jpg 768w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226509-image-2.jpg 1063w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u793a\u4f8b2\uff1b\u5206\u533a\u8868\u7d22\u5f15\u63a8\u8350<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\u3001\u521b\u5efa\u5206\u533a\u8868\u5e76\u63d2\u5165\u6570\u636e\ncreate table tb_ai_par_01(id int, val1 int, val2 text)\npartition by range(id) (\npartition tb_par_range_p1 values less than (200),\npartition tb_par_range_p2 values less than (400),\npartition tb_par_range_p3 values less than (600),\npartition tb_par_range_p4 values less than (800),\npartition tb_par_range_p5 values less than (1000),\npartition tb_par_range_p6 values less than (1200),\npartition tb_par_range_p7 values less than (1400),\npartition tb_par_range_p8 values less than (1600),\npartition tb_par_range_p9 values less than (MAXVALUE)\n)enable row movement;\n\ninsert into tb_ai_par_01 values(generate_series(1, 200000), generate_series(1, 200000), generate_series(1, 200000));\n\n2\u3001\u5206\u6790\u5206\u533a\u8868\nanalyze tb_ai_par_01;\n\n3\u3001\u751f\u6210\u63a8\u8350\u7d22\u5f15\nselect \"table\", \"column\", \"indextype\" from gs_index_advise('select id,val1 from tb_ai_par_01 where val1 = 2000;');\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"451\" src=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-2-1024x451.gif\" alt=\"\" class=\"wp-image-300\" srcset=\"https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-2-1024x451.gif 1024w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-2-300x132.gif 300w, https:\/\/yuzikimikage.top\/wp-content\/uploads\/2025\/11\/1764226510-image-2-768x338.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u603b\u7ed3<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u67e5\u8be2\u7ecf\u5e38\u7528\u5230\u7684\u5b57\u6bb5\u5efa\u7d22\u5f15<\/li>\n\n\n\n<li>\u8d85\u957f\u5b57\u6bb5\u4e0d\u52a0\u7d22\u5f15<\/li>\n\n\n\n<li>\u91cd\u590d\u503c\u591a\u7684\u5b57\u6bb5\u4e0d\u52a0\u7d22\u5f15<\/li>\n\n\n\n<li>\u7ecf\u5e38\u7ec4\u5408\u51fa\u73b0\u4f5c\u4e3a\u6761\u4ef6\u7684\u5217\u5efa\u8054\u5408\u7d22\u5f15<\/li>\n\n\n\n<li>\u6839\u636e\u4e1a\u52a1\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u7d22\u5f15\u7c7b\u578b<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>POSTGRESQL\u7684\u5e38\u7528\u7d22\u5f15 \u7d22\u5f15\u7c7b\u578b \u7d22\u5f15\u529f\u80fd btree \u5feb\u901f\u7684\u6570\u636e\u67e5\u627e\u529f\u80fd,\u5bf9\u4e8e\u5927\u4e8e,\u5c0f\u4e8e,\u7b49\u4e8e, \u534a\u6a21\u7cca, \u7b49\u67e5\u627e\u63d0\u4f9b\u641c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[22],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-22"],"_links":{"self":[{"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":7,"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/posts\/287\/revisions\/313"}],"wp:attachment":[{"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yuzikimikage.top\/index.php\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}