{"id":330,"date":"2020-10-01T06:00:40","date_gmt":"2020-09-30T22:00:40","guid":{"rendered":"https:\/\/www.mengqingzhong.com\/?p=330"},"modified":"2024-01-14T11:38:31","modified_gmt":"2024-01-14T03:38:31","slug":"postgresql-index-spgist-6","status":"publish","type":"post","link":"https:\/\/www.mengqingzhong.com\/2020\/10\/01\/postgresql-index-spgist-6\/","title":{"rendered":"PostgreSQL\u7d22\u5f15(6) \u2013 SP-GiST"},"content":{"rendered":"

\n\u672c\u6587\u7ffb\u8bd1\u81eaEgor Rogov\u7684\u82f1\u6587\u535a\u5ba2<\/a>\uff0c\u4e14\u5df2\u5f81\u5f97Egor Rogov\u7684\u540c\u610f\u3002\u8bd1\u8005\u505a\u4e86\u5c11\u91cf\u4fee\u6539\u3002\n<\/p>\n

\n\u5ba1\u6821\uff1a\u7eaa\u6600\u7ea2\uff0c\u4e2d\u56fd\u4eba\u6c11\u5927\u5b66\u4fe1\u606f\u5b66\u9662\u5728\u8bfb\u535a\u58eb\u751f\u3002\n<\/p>\n

\u5728\u524d\u9762\u51e0\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u901a\u7528\u7d22\u5f15\u5f15\u64ce\u3001AM\u7684\u63a5\u53e3\u548c\u4e09\u79cdAM\uff08Hash\u7d22\u5f15\u3001B-tree\u7d22\u5f15\u3001GiST\u7d22\u5f15\uff09\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4ecb\u7ecdSP-GiST\u7d22\u5f15\u3002<\/p>\n

\u9996\u5148\uff0c\u4ecb\u7ecd\u4e00\u4e2a\u8fd9\u4e2a\u540d\u5b57\u3002GiST\u6697\u793a\u7740\u5b83\u548cGiST\u7d22\u5f15\u6709\u4e00\u4e9b\u7c7b\u4f3c\u3002\u5b83\u4eec\u90fd\u662f\u901a\u7528\u641c\u7d22\u6811\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6846\u67b6\u7528\u6765\u521b\u5efa\u4e0d\u540c\u7684\u7d22\u5f15\u7c7b\u578b\u3002<\/p>\n

SP\u8868\u793a\u7a7a\u95f4\u5206\u5272\uff08space partitioning\uff09\u3002\u8fd9\u91cc\u7684\u7a7a\u95f4\uff0c\u5c31\u662f\u6211\u4eec\u5e73\u65f6\u6240\u6210\u7684\u7a7a\u95f4\uff0c\u4f8b\u5982\uff0c\u5b83\u53ef\u4ee5\u662f\u4e00\u4e2a\u4e8c\u7ef4\u7684\u5e73\u9762\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5c06\u4f1a\u770b\u5230\u5b83\u53ef\u4ee5\u8868\u793a\u4efb\u610f\u641c\u7d22\u7a7a\u95f4\u3002<\/p>\n

SP-GiST\u9002\u5408\u53ef\u4ee5\u88ab\u9012\u5f52\u5206\u5272\u6210\u4e92\u4e0d\u76f8\u4ea4\u533a\u57df\u7684\u7a7a\u95f4\u3002\u8fd9\u7c7b\u7a7a\u95f4\u5305\u62ec\uff1a\u56db\u53c9\u6811\uff08quadtrees\uff09\u3001K-D tree\u3001\u57fa\u6570\u6811\uff08radix trees\uff09\u7b49\u3002<\/p>\n

1. \u7ed3\u6784<\/h1>\n

SP-GiST\u7684\u601d\u60f3\u5c31\u662f\u628a\u503c\u57df\u5212\u5206\u6210\u4e92\u4e0d\u76f8\u4ea4\u7684\u5b50\u57df\uff0c\u5e76\u4e14\u8fd9\u4e9b\u5b50\u57df\u53ef\u4ee5\u7ee7\u7eed\u88ab\u5206\u5272\u3002\u4f7f\u7528\u8fd9\u6837\u7684\u5206\u5272\u65b9\u5f0f\uff0c\u4f1a\u5bfc\u81f4\u6811\u4e0d\u5e73\u8861\uff08\u4e0d\u50cfB-tree\u548c\u5e38\u89c4\u7684GiST\u7d22\u5f15\uff09\u3002<\/p>\n

\u4e0d\u76f8\u4ea4\u7684\u7279\u70b9\uff0c\u4f7f\u5f97\u7b97\u6cd5\u5728\u63d2\u5165\u548c\u641c\u7d22\u65f6\u7684\u51b3\u7b56\u8fc7\u7a0b\u53d8\u5f97\u7b80\u5355\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u8fd9\u4e5f\u5bfc\u81f4\u6811\u7684\u5e73\u8861\u5ea6\u964d\u4f4e\u3002\u4f8b\u5982\uff0c\u56db\u53c9\u6811\u7684\u4e00\u4e2a\u8282\u70b9\u901a\u5e38\u5305\u542b4\u4e2a\u5b50\u8282\u70b9\uff08\u4e0d\u50cfB-tree\u90a3\u6837\u53ef\u4ee5\u5305\u542b\u51e0\u767e\u4e2a\uff09\u5e76\u4e14\u6811\u6bd4\u8f83\u6df1\u3002\u8fd9\u79cd\u6811\u7684\u7ed3\u6784\u6bd4\u8f83\u9002\u5408\u5185\u5b58\uff0c\u4f46\u662f\u6811\u88ab\u5b58\u5728\u78c1\u76d8\u4e0a\uff0c\u56e0\u6b64\uff0c\u4e3a\u4e86\u51cf\u5c11IO\u64cd\u4f5c\uff0c\u8282\u70b9\u9700\u8981\u88ab\u6253\u5305\u5b58\u5728\u9875\u9762\u4e2d\u3002\u6253\u5305\u5176\u5b9e\u5f88\u96be\u9ad8\u6548\u7684\u5b8c\u6210\u3002\u800c\u4e14\uff0c\u56e0\u4e3a\u4e0d\u540c\u8282\u70b9\u7684\u6df1\u5ea6\u4e0d\u540c\uff0c\u6240\u4ee5\u5728\u6811\u4e2d\u67e5\u627e\u4e0d\u540c\u7684\u503c\uff0c\u4ee3\u4ef7\u5e76\u4e0d\u76f8\u540c\u3002<\/p>\n

\u548cGiST\u4e00\u6837\uff0cSP-GiST\u4e5f\u5904\u7406\u4e86\u5e95\u5c42\u7684\u4efb\u52a1\uff08\u5e76\u53d1\u63a7\u5236\u3001\u65e5\u5fd7\u3001\u641c\u7d22\u6846\u67b6\uff09\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\u4e3a\u6dfb\u52a0\u65b0\u7684\u6570\u636e\u7c7b\u578b\u548c\u65b0\u7684\u5206\u5272\u7b97\u6cd5\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n

SP-GiST\u7684\u975e\u53f6\u5b50\u8282\u70b9\u5b58\u50a8\u4e86\u6307\u5411\u5b69\u5b50\u8282\u70b9\u7684\u6307\u9488\uff1b\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6307\u9488\u6307\u5b9a\u4e00\u4e2a\u6807\u7b7e\u3002\u975e\u53f6\u5b50\u8282\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e00\u4e2a\u79f0\u4e3a\u524d\u7f00\u7684\u503c\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u503c\u4e0d\u4e00\u5b9a\u662f\u4e00\u4e2a\u524d\u7f00\uff1b\u5b83\u53ef\u4ee5\u662f\u4efb\u610f\u4e00\u4e2a\u6240\u6709\u5b69\u5b50\u8282\u70b9\u90fd\u6ee1\u8db3\u7684\u8c13\u8bcd\u3002<\/p>\n

SP-GiST\u7684\u53f6\u5b50\u8282\u70b9\u5b58\u50a8\u4e86\u7d22\u5f15\u7c7b\u578b\u7684\u4e00\u4e2a\u503c\uff0c\u548c\u4e00\u4e2a\u6307\u5411\u57fa\u8868\u7684TID\u3002\u7d22\u5f15\u6570\u636e\u672c\u8eab\uff08\u641c\u7d22key\uff09\u53ef\u4ee5\u88ab\u7528\u4f5c\u8fd9\u4e2a\u503c\uff0c\u4f46\u8fd9\u4e0d\u662f\u5fc5\u987b\u7684\uff1a\u53ef\u4ee5\u7528\u4e00\u4e2a\u66f4\u77ed\u7684\u503c\u3002<\/p>\n

\u800c\u4e14\uff0c\u53f6\u5b50\u8282\u70b9\u53ef\u4ee5\u88ab\u5206\u7ec4\u6210list\u3002\u6240\u4ee5\uff0c\u975e\u53f6\u5b50\u8282\u70b9\u4e0d\u4f46\u53ef\u4ee5\u6307\u5411\u4e00\u4e2a\u503c\uff0c\u800c\u4e14\u53ef\u4ee5\u6307\u5411\u4e00\u4e2a\u94fe\u8868\u3002<\/p>\n

\u6ce8\u610f\uff0c\u53f6\u5b50\u8282\u70b9\u4e2d\u7684\u524d\u7f00\u3001\u6807\u7b7e\u3001\u503c\u90fd\u6709\u4ed6\u4eec\u4e92\u76f8\u72ec\u7acb\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n

\u548cGiST\u7d22\u5f15\u4e00\u6837\uff0c\u5b9a\u4e49\u67e5\u8be2\u7684\u4e3b\u8981\u51fd\u6570\u5c31\u662f\u4e00\u81f4\u6027\u51fd\u6570\u3002\u5904\u7406\u6bcf\u4e2a\u8282\u70b9\u65f6\u90fd\u4f1a\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e0e\u641c\u7d22\u8c13\u8bcd\uff08indexed-field operator expression\uff09\u4e00\u81f4\u7684\u5b69\u5b50\u8282\u70b9\u7684\u96c6\u5408\u3002\u5bf9\u53f6\u5b50\u8282\u70b9\u6765\u8bf4\uff0c\u8fd9\u4e2a\u4e00\u81f4\u6027\u51fd\u6570\u51b3\u5b9a\u8282\u70b9\u4e2d\u7684\u7d22\u5f15\u503c\u662f\u5426\u6ee1\u8db3\u641c\u7d22\u6761\u4ef6\u3002<\/p>\n

\u67e5\u8be2\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u3002\u4e00\u81f4\u6027\u51fd\u6570\u53ef\u4ee5\u627e\u51fa\u9700\u8981\u8bbf\u95ee\u7684\u5b69\u5b50\u8282\u70b9\u3002\u5bf9\u627e\u5230\u7684\u8282\u70b9\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u3002\u4f7f\u7528\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002<\/p>\n

\u5728\u7269\u7406\u5c42\u9762\uff0c\u7d22\u5f15\u8282\u70b9\u88ab\u6253\u5305\u5230\u9875\u9762\u4e2d\uff0c\u4f7f\u5f97IO\u66f4\u9ad8\u6548\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e2a\u9875\u9762\u8981\u4e48\u5b58\u653e\u975e\u53f6\u5b50\u8282\u70b9\uff0c\u8981\u4e48\u5b58\u50a8\u53f6\u5b50\u8282\u70b9\uff0c\u4e0d\u80fd\u540c\u65f6\u5b58\u50a8\u4e8c\u8005\u3002<\/p>\n

2. \u793a\u4f8b\uff1a\u56db\u53c9\u6811<\/h1>\n

\u56db\u53c9\u6811\u7528\u6765\u7d22\u5f15\u5e73\u9762\u4e0a\u7684\u70b9\u3002\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u5c31\u662f\uff1a\u4f7f\u7528\u4e00\u4e2a\u4e2d\u5fc3\u70b9\u628a\u533a\u57df\u5212\u5206\u62104\u4e2a\u5b50\u533a\u57df\uff08\u8c61\u9650\uff09\u3002\u6811\u4e2d\u5206\u652f\u7684\u6df1\u5ea6\u4e0e\u76f8\u5e94\u8c61\u9650\u4e2d\u70b9\u7684\u5bc6\u5ea6\u76f8\u5173\u3002<\/p>\n

Demo\u6570\u636e\u5e93\u52a0\u4e0aopenflights.org\u7684\u6570\u636e\u540e\uff0c\u753b\u51fa\u7684\u56fe\u7247\u5982\u4e0b\u3002\u540e\u6587\u4e2d\uff0c\u6211\u4eec\u628a\u7ecf\u5ea6\u548c\u7ef4\u5ea6\u6362\u6210point\u3002<\/p>\n

\"\"<\/a>
\n\u9996\u5148\uff0c\u628a\u5e73\u9762\u5212\u5206\u62104\u4e2a\u8c61\u9650<\/p>\n

\"\"<\/a>
\n\u7136\u540e\uff0c\u5bf9\u6bcf\u4e2a\u8c61\u9650\u7ee7\u7eed\u5212\u5206<\/p>\n

\"\"<\/a>
\n\u76f4\u5230\u5f97\u5230\u6700\u540e\u7684\u5212\u5206<\/p>\n

\u6211\u4eec\u7ee7\u7eed\u8ba8\u8bba\u4e0a\u7bc7\u6587\u7ae0\u4e2d\u90a3\u4e2a\u7b80\u5355\u793a\u4f8b\u7684\u4e00\u4e9b\u7ec6\u8282\u3002\u5212\u5206\u60c5\u51b5\u53ef\u80fd\u662f\u8fd9\u6837\uff1a<\/p>\n

\"\"<\/a><\/p>\n

\u8c61\u9650\u7684\u7f16\u53f7\u5728\u5de6\u8fb9\u7684\u56fe\u7247\u4e2d\u3002\u4e3a\u4e86\u76f4\u89c2\uff0c\u6211\u4eec\u6309\u7167\u76f8\u540c\u7684\u987a\u5e8f\u4ece\u5de6\u5230\u53f3\u4f9d\u6b21\u5b58\u653e\u5b69\u5b50\u8282\u70b9\u3002\u7d22\u5f15\u53ef\u80fd\u50cf\u4e0b\u56fe\u4e00\u6837\u5b58\u653e\u3002\u6bcf\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u6700\u591a\u5b58\u653e4\u4e2a\u5b69\u5b50\u8282\u70b9\u3002\u6bcf\u4e2a\u6307\u9488\u53ef\u4ee5\u7528\u8c61\u9650\u7684\u7f16\u53f7\u8868\u793a\u3002\u5728\u771f\u6b63\u7684\u5b9e\u73b0\u4e2d\u5e76\u4e0d\u9700\u8981\u6807\u7b7e\uff0c\u76f4\u63a5\u4f7f\u7528\u6570\u7ec4\u7684\u4e0b\u6807\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n

\"\"<\/a><\/p>\n

\u5728\u8c61\u9650\u8fb9\u754c\u4e0a\u7684\u70b9\u5c5e\u4e8e\u7f16\u53f7\u8f83\u5c0f\u7684\u8c61\u9650\u3002<\/p>\n

create table points(p point);\ninsert into points(p) values\n  (point '(1,1)'), (point '(3,2)'), (point '(6,3)'),\n  (point '(5,5)'), (point '(7,8)'), (point '(8,6)');\ncreate index points_quad_idx on points using spgist(p);\n<\/code><\/pre>\n

\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528quad_point_ops\u64cd\u4f5c\u7b26\u7c7b\uff0c\u5b83\u5305\u542b\u4e0b\u5217\u64cd\u4f5c\u7b26\uff1a<\/p>\n

select amop.amopopr::regoperator, amop.amopstrategy\nfrom pg_opclass opc, pg_opfamily opf, pg_am am, pg_amop amop\nwhere opc.opcname = 'quad_point_ops'\nand opf.oid = opc.opcfamily\nand am.oid = opf.opfmethod\nand amop.amopfamily = opc.opcfamily\nand am.amname = 'spgist'\nand amop.amoplefttype = opc.opcintype;\n<\/code><\/pre>\n
     amopopr     | amopstrategy\n-----------------+--------------\n <<(point,point) |            1  strictly left\n >>(point,point) |            5  strictly right\n ~=(point,point) |            6  coincides\n <^(point,point) |           10  strictly below\n >^(point,point) |           11  strictly above\n <@(point,box)   |            8  contained in rectangle\n(6 rows)\n<\/code><\/pre>\n

\u4f8b\u5982\uff0c\u6211\u4eec\u7814\u7a76select * from points where p >^ point ‘(2,7)’\u7684\u6267\u884c\u8fc7\u7a0b\uff08\u627e\u51fa\u5728(2,7)\u4e0a\u9762\u7684\u70b9\uff09\u3002<\/p>\n

\"\"<\/a><\/p>\n

\u6211\u4eec\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4f7f\u7528\u4e00\u81f4\u6027\u51fd\u6570\u786e\u5b9a\u9700\u8981\u641c\u7d22\u54ea\u4e9b\u5b69\u5b50\u8282\u70b9\u3002\u5bf9\u64cd\u4f5c\u7b26>^\u6765\u8bf4\uff0c\u8fd9\u4e2a\u51fd\u6570\u628a(2,7)\u548c\u4e2d\u5fc3\u70b9(4,4)\u6bd4\u8f83\uff0c\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u7b2c\u4e00\u3001\u56db\u8c61\u9650\u3002<\/p>\n

\u5bf9\u4e0e\u7b2c\u4e00\u8c61\u9650\u5173\u8054\u7684\u8282\u70b9\uff0c\u6211\u4eec\u7ee7\u7eed\u4f7f\u7528\u4e00\u81f4\u6027\u51fd\u6570\u3002\u4e2d\u5fc3\u70b9\u662f(6,6)\uff0c\u63a5\u7740\u518d\u53bb\u904d\u5386\u7b2c\u4e00\u3001\u56db\u8c61\u9650\u3002<\/p>\n

\"\"<\/a><\/p>\n

\u53f6\u5b50\u8282\u70b9\u4e2d\u7684(8,6) and (7,8)\u5c5e\u4e8e\u7b2c\u4e00\u8c61\u9650\uff0c\u53ea\u6709(7,8)\u6ee1\u8db3\u6761\u4ef6\u3002\u7b2c\u56db\u8c61\u9650\u4ee3\u8868\u7684\u5185\u5bb9\u4e3a\u7a7a\u3002<\/p>\n

\u975e\u53f6\u5b50\u8282\u70b9(4,4)\u6307\u5411\u7684\u7b2c\u56db\u8c61\u9650\u4e5f\u4e3a\u7a7a\uff0c\u56e0\u6b64\u641c\u7d22\u8fc7\u7a0b\u7ed3\u675f\u3002<\/p>\n

set enable_seqscan = off;\nexplain (costs off) select * from points where p >^ point '(2,7)';\n<\/code><\/pre>\n
                   QUERY PLAN                  \n------------------------------------------------\n Index Only Scan using points_quad_idx on points\n   Index Cond: (p >^ '(2,7)'::point)\n(2 rows)\n<\/code><\/pre>\n

\u5185\u90e8\u7ed3\u6784<\/h2>\n

\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528gevel\u63d2\u4ef6\u63a2\u7d22SP-GiST\u7684\u5185\u90e8\u7ed3\u6784\u3002\u4e0d\u5e78\u7684\u662fgevel\u63d2\u4ef6\u4e0e\u6700\u65b0\u7684PG\u4e0d\u517c\u5bb9\u3002<\/p>\n

\u4f8b\u5982\uff0cdemo\u6570\u636e\u5e93\u4e2d\u5c55\u793a\u4e16\u754c\u5730\u56fe\u7684\u4f8b\u5b50\u3002<\/p>\n

create index airports_coordinates_quad_idx on airports_ml using spgist(coordinates);\n<\/code><\/pre>\n

\u7d22\u5f15\u7684\u4e00\u4e9b\u7edf\u8ba1\u4fe1\u606f\uff1a<\/p>\n

select * from spgist_stats('airports_coordinates_quad_idx');\n<\/code><\/pre>\n
           spgist_stats           \n----------------------------------\n totalPages:        33           +\n deletedPages:      0            +\n innerPages:        3            +\n leafPages:         30           +\n emptyPages:        2            +\n usedSpace:         201.53 kbytes+\n usedInnerSpace:    2.17 kbytes  +\n usedLeafSpace:     199.36 kbytes+\n freeSpace:         61.44 kbytes +\n fillRatio:         76.64%       +\n leafTuples:        5993         +\n innerTuples:       37           +\n innerAllTheSame:   0            +\n leafPlaceholders:  725          +\n innerPlaceholders: 0            +\n leafRedirects:     0            +\n innerRedirects:    0\n(1 row)\n<\/code><\/pre>\n

\u7136\u540e\uff0c\u8f93\u51fa\u6811\u672c\u8eab\uff1a<\/p>\n

select tid, n, level, tid_ptr, prefix, leaf_value\nfrom spgist_print('airports_coordinates_quad_idx') as t(\n  tid tid,\n  allthesame bool,\n  n int,\n  level int,\n  tid_ptr tid,\n  prefix point,    -- prefix type\n  node_label int,  -- label type (unused here)\n  leaf_value point -- list value type\n)\norder by tid, n;\n<\/code><\/pre>\n
   tid   | n | level | tid_ptr |      prefix      |    leaf_value\n---------+---+-------+---------+------------------+------------------\n (1,1)   | 0 |     1 | (5,3)   | (-10.220,53.588) |\n (1,1)   | 1 |     1 | (5,2)   | (-10.220,53.588) |\n (1,1)   | 2 |     1 | (5,1)   | (-10.220,53.588) |\n (1,1)   | 3 |     1 | (5,14)  | (-10.220,53.588) |\n (3,68)  |   |     3 |         |                  | (86.107,55.270)\n (3,70)  |   |     3 |         |                  | (129.771,62.093)\n (3,85)  |   |     4 |         |                  | (57.684,-20.430)\n (3,122) |   |     4 |         |                  | (107.438,51.808)\n (3,154) |   |     3 |         |                  | (-51.678,64.191)\n (5,1)   | 0 |     2 | (24,27) | (-88.680,48.638) |\n (5,1)   | 1 |     2 | (5,7)   | (-88.680,48.638) |\n ...\n<\/code><\/pre>\n

spgist_print\u6ca1\u6709\u8f93\u51fa\u6240\u6709\u53f6\u5b50\u8282\u70b9\u7684\u503c\uff0c\u53ea\u8f93\u51fa\u4e86\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u3002\u56e0\u6b64\uff0c\u53ea\u8f93\u51fa\u4e86\u6811\u7684\u7ed3\u6784\uff0c\u800c\u4e0d\u662f\u6240\u6709\u7684\u5185\u5bb9\u3002<\/p>\n

3. \u793a\u4f8b\uff1ak-dimensional trees<\/h1>\n

\u4ecd\u7136\u4f7f\u7528\u4e0a\u9762\u7684\u793a\u4f8b\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u5206\u5272\u7a7a\u95f4\u3002<\/p>\n

\u8ba9\u6211\u4eec\u4f7f\u7528\u7b2c\u4e00\u4e2a\u88ab\u7d22\u5f15\u7684\u70b9\u6c34\u5e73\u753b\u4e00\u6761\u76f4\u7ebf\u3002\u5b83\u5c06\u5e73\u9762\u5206\u6210\u4e0a\u4e0b\u4e24\u4e2a\u90e8\u5206\u3002\u7b2c\u4e8c\u70b9\u5c5e\u4e8e\u5176\u4e2d\u4e00\u4e2a\u90e8\u5206\uff0c\u4f7f\u7528\u8fd9\u4e2a\u70b9\u5782\u76f4\u753b\u4e00\u6761\u76f4\u7ebf\uff0c\u53c8\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u90e8\u5206\u5212\u5206\u6210\u5de6\u53f3\u4e24\u90e8\u5206\u3002\u4f9d\u6b64\u7c7b\u63a8\uff0c\u7ee7\u7eed\u4f7f\u7528\u540e\u9762\u7684\u70b9\u6a2a\u7ad6\u753b\u7ebf\u3002<\/p>\n

\u6240\u6709\u5185\u90e8\u8282\u70b9\u90fd\u5305\u542b2\u4e2a\u5b69\u5b50\u8282\u70b9\u3002\u4e24\u4e2a\u6307\u9488\u8981\u4e48\u90fd\u6307\u5411\u4e0b\u4e00\u5c42\u7684\u53f6\u5b50\u8282\u70b9\uff0c\u8981\u4e48\u90fd\u6307\u5411\u5b69\u5b50\u8282\u70b9\u3002<\/p>\n

\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5212\u5206k\u7ef4\u7a7a\u95f4\uff0c\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3ak-D tree\u3002<\/p>\n

\u5c55\u793a\u673a\u573a\u793a\u4f8b\u7684\u5212\u5206\u8fc7\u7a0b\uff1a<\/p>\n

\"\"<\/a>
\n\u9996\u5148\u5c06\u5e73\u9762\u5212\u5206\u6210\u4e0a\u4e0b\u4e24\u90e8\u5206<\/p>\n

\"\"<\/a>
\n\u628a\u6bcf\u4e2a\u90e8\u5206\u7ee7\u7eed\u5212\u5206\u6210\u5de6\u53f3\u4e24\u90e8\u5206<\/p>\n

\"\"<\/a>
\n\u76f4\u5230\u5f97\u5230\u6700\u7ec8\u7684\u5212\u5206<\/p>\n

\u4e3a\u4e86\u4f7f\u7528\u8fd9\u6837\u7684\u5212\u5206\u65b9\u5f0f\uff0c\u6211\u4eec\u9700\u8981\u5728\u521b\u5efa\u7d22\u5f15\u65f6\u663e\u5f0f\u7684\u6307\u5b9akd_point_ops\u64cd\u4f5c\u7b26\u7c7b\u3002<\/p>\n

create index points_kd_idx on points using spgist(p kd_point_ops);\n<\/code><\/pre>\n

\u8fd9\u4e2a\u64cd\u4f5c\u7b26\u7c7b\u4e0e\u9ed8\u8ba4\u7684quad_point_ops\u64cd\u4f5c\u7b26\u7c7b\u5305\u542b\u76f8\u540c\u7684\u64cd\u4f5c\u7b26\u3002<\/p>\n

\u5185\u90e8\u7ed3\u6784<\/h2>\n

\u5f53\u6211\u4eec\u67e5\u770b\u8fd9\u68f5\u6811\u7684\u5185\u90e8\u7ed3\u6784\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u5230\uff0c\u8fd9\u4e2a\u573a\u666f\u4e0b\u7684\u524d\u7f00\u662f\u4e00\u7ef4\u5750\u6807\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u70b9\u3002<\/p>\n

select tid, n, level, tid_ptr, prefix, leaf_value\nfrom spgist_print('airports_coordinates_kd_idx') as t(\n  tid tid,\n  allthesame bool,\n  n int,\n  level int,\n  tid_ptr tid,\n  prefix float,    -- prefix type\n  node_label int,  -- label type (unused here)\n  leaf_value point -- list node type\n)\norder by tid, n;\n<\/code><\/pre>\n
   tid   | n | level | tid_ptr |   prefix   |    leaf_value\n---------+---+-------+---------+------------+------------------\n (1,1)   | 0 |     1 | (5,1)   |     53.740 |\n (1,1)   | 1 |     1 | (5,4)   |     53.740 |\n (3,113) |   |     6 |         |            | (-7.277,62.064)\n (3,114) |   |     6 |         |            | (-85.033,73.006)\n (5,1)   | 0 |     2 | (5,12)  |    -65.449 |\n (5,1)   | 1 |     2 | (5,2)   |    -65.449 |\n (5,2)   | 0 |     3 | (5,6)   |     35.624 |\n (5,2)   | 1 |     3 | (5,3)   |     35.624 |\n ...\n<\/code><\/pre>\n

4. \u793a\u4f8b\uff1aradix tree<\/h1>\n

\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528SP-GiST\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u57fa\u6570\u6811\u3002\u57fa\u6570\u6811\u7684\u601d\u60f3\u662f\uff0c\u5b57\u7b26\u4e32\u4e0d\u662f\u5168\u90e8\u5b58\u50a8\u5728\u53f6\u5b50\u8282\u70b9\uff0c\u800c\u662f\u628a\u4ece\u6839\u5230\u6b64\u53f6\u5b50\u8282\u70b9\u7684\u503c\u62fc\u63a5\u8d77\u6765\u3002<\/p>\n

\u5047\u5982\uff0c\u6211\u4eec\u8981\u5b58\u50a8\u8fd9\u51e0\u4e2aURL\uff1apostgrespro.ru\u3001postgrespro.com\u3001postgresql.org\u548cplanet.postgresql.org\u3002<\/p>\n

create table sites(url text);\ninsert into sites values ('postgrespro.ru'),('postgrespro.com'),('postgresql.org'),('planet.postgresql.org');\ncreate index on sites using spgist(url);\n<\/code><\/pre>\n

\u6811\u7684\u6837\u5b50\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\"\"<\/a><\/p>\n

\u975e\u53f6\u5b50\u8282\u70b9\u5b58\u50a8\u4e86\u5b83\u5b69\u5b50\u8282\u70b9\u7684\u516c\u5171\u524d\u7f00\u3002\u4f8b\u5982\u8282\u70b9stgres\u7684\u6240\u6709\u5b69\u5b50\u8282\u70b9\u90fd\u4ee5p + o + stgres\u5f00\u5934\u3002<\/p>\n

\u4e0e\u56db\u53c9\u6811\u4e0d\u540c\u7684\u662f\uff0c\u6307\u5411\u5b69\u5b50\u8282\u70b9\u7684\u6307\u9488\u5305\u542b\u4e00\u4e2a\u989d\u5916\u7684\u6807\u7b7e\uff08\u7cbe\u786e\u7684\u8bf4\u662f\u4e24\u4e2a\u5b57\u8282\uff0c\u8fd9\u4e0d\u91cd\u8981\uff09\u3002<\/p>\n

text_ops\u64cd\u4f5c\u7b26\u7c7b\u652f\u6301B-tree\u7c7b\u578b\u7684\u64cd\u4f5c\u7b26\uff1a\u76f8\u7b49\u3001\u5927\u4e8e\u548c\u5c0f\u4e8e\u3002<\/p>\n

select amop.amopopr::regoperator, amop.amopstrategy\nfrom pg_opclass opc, pg_opfamily opf, pg_am am, pg_amop amop\nwhere opc.opcname = 'text_ops'\nand opf.oid = opc.opcfamily\nand am.oid = opf.opfmethod\nand amop.amopfamily = opc.opcfamily\nand am.amname = 'spgist'\nand amop.amoplefttype = opc.opcintype;\n<\/code><\/pre>\n
     amopopr     | amopstrategy\n-----------------+--------------\n ~<~(text,text)  |            1\n ~<=~(text,text) |            2\n =(text,text)    |            3\n ~>=~(text,text) |            4\n ~>~(text,text)  |            5\n <(text,text)    |           11\n <=(text,text)   |           12\n >=(text,text)   |           14\n >(text,text)    |           15\n(9 rows)\n<\/code><\/pre>\n

\u5305\u542b\u6ce2\u6d6a\u53f7\u7684\u64cd\u4f5c\u7b26\u5904\u7406\u7684\u662f\u5b57\u8282\u800c\u4e0d\u662f\u5b57\u7b26\u3002<\/p>\n

\u6709\u65f6\u5019\uff0c\u4f7f\u7528\u57fa\u6570\u6811\u7684\u8868\u793a\u65b9\u5f0f\u4f1a\u6bd4B-tree\u66f4\u7d27\u51d1\uff0c\u56e0\u4e3a\u6ca1\u6709\u5b58\u50a8\u6240\u6709\u7684\u503c\uff0c\u800c\u662f\u5728\u641c\u7d22\u7684\u65f6\u5019\u6784\u9020\u51fa\u6765\u3002<\/p>\n

\u8003\u8651\u8fd9\u4e2a\u67e5\u8be2<\/p>\n

select * from sites where url like 'postgresp%ru'\n<\/code><\/pre>\n

\u5b83\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\uff1a<\/p>\n

explain (costs off) select * from sites where url like 'postgresp%ru';\n<\/code><\/pre>\n
                                  QUERY PLAN                                  \n------------------------------------------------------------------------------\n Index Only Scan using sites_url_idx on sites\n   Index Cond: ((url ~>=~ 'postgresp'::text) AND (url ~<~ 'postgresq'::text))\n   Filter: (url ~~ 'postgresp%ru'::text)\n(3 rows)\n<\/code><\/pre>\n

\u5b9e\u9645\u4e0a\u4f7f\u7528\u7d22\u5f15\u627e\u51fapostgresp\u548cpostgresq\u4e4b\u95f4\u7684\u503c (Index Cond)\uff0c\u518d\u8fc7\u6ee4\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u503c(Filter)\u3002<\/p>\n

\u9996\u5148\uff0c\u4e00\u81f4\u6027\u51fd\u6570\u5fc5\u987b\u51b3\u5b9a\u51fa\u4ece\u6839\u8282\u70b9p\u4e0b\u964d\u5230\u54ea\u4e2a\u5b69\u5b50\u8282\u70b9\u3002\u65e0\u9700\u4e0b\u964d\u5230p+l\uff0c\u53ea\u9700\u8981\u4e0b\u964d\u5230 p + o + stgres \u3002<\/p>\n

\u4ee5\u6b64\u7c7b\u63a8\uff0c\u4e0b\u964d\u5230\u53f6\u5b50\u8282\u70b9\u3002<\/p>\n

\u5185\u90e8\u7ed3\u6784<\/h2>\n

\u89c2\u5bdf\u5185\u90e8\u7ed3\u6784\u65f6\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n

select * from spgist_print('sites_url_idx') as t(\n  tid tid,\n  allthesame bool,\n  n int,\n  level int,\n  tid_ptr tid,\n  prefix text,         -- prefix type\n  node_label smallint, -- label type\n  leaf_value text      -- leaf node type\n)\norder by tid, n;\n<\/code><\/pre>\n

5. Properties<\/h1>\n

\u9996\u5148\u770bAM\u5c42\u9762\u7684\u5c5e\u6027\uff08\u67e5\u8be2\u89c1\u672c\u7cfb\u5217\u6587\u7ae0\u7684\u7b2c\u4e8c\u7bc7\uff09<\/p>\n

 amname |     name      | pg_indexam_has_property\n--------+---------------+-------------------------\n spgist | can_order     | f\n spgist | can_unique    | f\n spgist | can_multi_col | f\n spgist | can_exclude   | t\n<\/code><\/pre>\n

SP-GiST\u4e0d\u652f\u6301\u6392\u5e8f\u3001\u552f\u4e00\u7d22\u5f15\u548c\u591a\u5217\u3002\u4f46\u662f\u652f\u6301\u6392\u5b83\u7ea6\u675f\u3002<\/p>\n

\u5177\u4f53\u7d22\u5f15\u5c42\u9762\u7684\u5c5e\u6027\uff1a<\/p>\n

     name      | pg_index_has_property\n---------------+-----------------------\n clusterable   | f\n index_scan    | t\n bitmap_scan   | t\n backward_scan | f\n<\/code><\/pre>\n

\u548cGiST\u7684\u4e0d\u540c\u7684\u662f\uff0c\u5b83\u4e0d\u652f\u6301clustering\u3002<\/p>\n

\u6700\u540e\u770b\u5217\u5c42\u9762\u7684\u5c5e\u6027\uff1a<\/p>\n

        name        | pg_index_column_has_property \n--------------------+------------------------------\n asc                | f\n desc               | f\n nulls_first        | f\n nulls_last         | f\n orderable          | f\n distance_orderable | f\n returnable         | t\n search_array       | f\n search_nulls       | t\n<\/code><\/pre>\n

\u4e0d\u652f\u6301\u6392\u5e8f\uff0c\u672c\u6587\u4e2dPG\u7684\u7248\u672c\u4e0d\u652f\u6301k-NN\u67e5\u8be2\uff0c\u4f46\u662f\u6700\u65b0\u7248\u7684PG\u5df2\u7ecf\u652f\u6301\u3002<\/p>\n

SP-GiST can be used for index-only scan, at least for the discussed operator classes. As we have seen, in some instances, indexed values are explicitly stored in leaf nodes, while in the other ones, the values are reconstructed part by part during the tree descent.
\nSP-GiST\u53ef\u4ee5\u88ab\u7528\u4f5cIndex Only Scan\uff0c\u81f3\u5c11\u5bf9\u5df2\u7ecf\u8ba8\u8bba\u8fc7\u7684\u64cd\u4f5c\u7b26\u5982\u6b64\u3002\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7d22\u5f15\u7684\u503c\u88ab\u5b58\u50a8\u5728\u53f6\u5b50\u8282\u70b9\u4e2d\uff0c\u5728\u6709\u4e9b\u573a\u666f\u4e0b\uff0c\u8fd9\u4e9b\u503c\u662f\u5728\u4e0b\u964d\u8fc7\u7a0b\u4e2d\u88ab\u6784\u9020\u51fa\u6765\u7684\u3002<\/p>\n

6. NULL\u503c<\/h1>\n

\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u8fd8\u6ca1\u6709\u8ba8\u8bbaNULL\u503c\u3002\u5f88\u660e\u663e\uff0c\u5b83\u652f\u6301NULL\uff1a<\/p>\n

explain (costs off)\nselect * from sites where url is null;\n<\/code><\/pre>\n
                  QUERY PLAN                  \n----------------------------------------------\n Index Only Scan using sites_url_idx on sites\n   Index Cond: (url IS NULL)\n(2 rows)\n<\/code><\/pre>\n

\u4f46\u662f\uff0cNULL\u503c\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u533a\u57df\u3002spgist\u64cd\u4f5c\u7b26\u7c7b\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u7b26\u90fd\u5fc5\u987b\u662f\u4e25\u683c\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u4efb\u4f55\u53c2\u6570\u4e3aNULL\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u5fc5\u987b\u8fd4\u56deNULL\u3002GiST\u7d22\u5f15\u4fdd\u8bc1\u4e0d\u628aNULL\u503c\u4f20\u7ed9\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u3002<\/p>\n

7. \u5176\u5b83\u6570\u636e\u7c7b\u578b<\/h1>\n

box_ops\u64cd\u4f5c\u7b26\u7c7b\u4e3a\u77e9\u5f62\u63d0\u4f9b\u4e86\u56db\u53c9\u6811\u7684\u529f\u80fd\u3002\u6bcf\u4e2a\u88ab\u8868\u793a\u6210\u56db\u7ef4\u7a7a\u95f4\u4e2d\u7684\u4e00\u4e2a\u70b9\uff0c\u7a7a\u95f4\u5171\u670916\u4e2a\u8c61\u9650\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u7d22\u5f15\u5728\u6027\u80fd\u4e0a\u5927\u5927\u597d\u4e8eGiST\u3002\u56e0\u4e3a\u5728GiST\u7d22\u5f15\u4e2d\u4e0d\u53ef\u80fd\u5728\u76f8\u4ea4\u7684\u7269\u4f53\u4e4b\u95f4\u5212\u6e05\u8fb9\u754c\uff0c\u4f46\u662f\u5bf94\u7ef4\u7a7a\u95f4\u4e2d\u7684\u70b9\u6765\u8bf4\uff0c\u5c31\u4e0d\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n

range_ops\u4e3a\u533a\u95f4\u63d0\u4f9b\u4e86\u56db\u53c9\u6811\u7684\u529f\u80fd\u3002\u4e00\u4e2a\u533a\u95f4\u88ab\u8868\u793a\u6210\u4e00\u4e2a\u4e8c\u7ef4\u7684\u70b9\u3002\u4e0b\u8fb9\u754c\u8868\u793a\u6a2a\u5750\u6807\uff0c\u4e0a\u8fb9\u754c\u8868\u793a\u7eb5\u5750\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

SP-GiST\u7d22\u5f15\u4e3b\u8981\u7a81\u51faSP\uff0c\u5373\u7a7a\u95f4\u5212\u5206\u3002\u5b83\u9002\u7528\u4e8e\u4e00\u4e2a\u7a7a\u95f4\u53ef\u4ee5\u88ab\u9012\u5f52\u5212\u5206\u6210\u5b50\u7a7a\u95f4\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/posts\/330"}],"collection":[{"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":13,"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":468,"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/posts\/330\/revisions\/468"}],"wp:attachment":[{"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mengqingzhong.com\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}