{"id":282,"date":"2020-10-01T05:00:17","date_gmt":"2020-09-30T21:00:17","guid":{"rendered":"https:\/\/www.mengqingzhong.com\/?p=282"},"modified":"2024-01-14T11:38:31","modified_gmt":"2024-01-14T03:38:31","slug":"postgresql-index-gist-5","status":"publish","type":"post","link":"https:\/\/www.mengqingzhong.com\/2020\/10\/01\/postgresql-index-gist-5\/","title":{"rendered":"PostgreSQL\u7d22\u5f15(5) \u2013 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\u4e24\u79cdAM\uff08Hash\u7d22\u5f15\u3001B-tree\u7d22\u5f15\uff09\u3002\u672c\u6587\u5c06\u4ecb\u7ecdGiST\u7d22\u5f15\u3002<\/p>\n GiST\u662fgeneralized search tree\u7684\u7b80\u79f0\uff0c\u5373\u901a\u7528\u641c\u7d22\u6811\u3002\u50cfB-tree\u4e00\u6837\uff0c\u5b83\u4e5f\u662f\u4e00\u9897\u5e73\u8861\u6811\u3002<\/p>\n \u5b83\u4eec\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1fB-tree\u548c\u6bd4\u8f83<\/strong>\u7684\u8bed\u4e49\u5f3a\u5173\u8054\u5728\u4e00\u8d77\uff0c\u5373\u5b83\u53ea\u6709\u4f7f\u7528>\u3001>=\u3001=\u3001<=\u3001<\u8fd9\u4e94\u4e2a\u64cd\u4f5c\u7b26\u8fdb\u884c\u6bd4\u8f83\u7684\u80fd\u529b\uff0c\u5f53\u7136\u8fd9\u4e2a\u80fd\u529b\u5df2\u7ecf\u975e\u5e38\u5f3a\u5927\u3002\u4f46\u662f\uff0c\u73b0\u4ee3\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u7ecf\u5e38\u5b58\u50a8\u4e00\u4e9b\u5728\u8fd95\u4e2a\u64cd\u4f5c\u7b26\u4e0a\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u5730\u7406\u6570\u636e\u3001\u56fe\u50cf\u6570\u636e\u3001\u6587\u672c\u6570\u636e\u3002<\/p>\n GiST\u7d22\u5f15\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u3002\u5b83\u5141\u8bb8\u7528\u6237\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a\u89c4\u5219\uff0c\u8fd9\u4e2a\u89c4\u5219\u53ef\u4ee5\u628a\u4efb\u610f\u6570\u636e\u7c7b\u578b\u7684\u6570\u636e\u5206\u5e03\u5728\u4e00\u9897\u5e73\u8861\u6811\u4e0a\u3002\u5e76\u4e14\u5b83\u63d0\u4f9b\u4e00\u79cd\u65b9\u6cd5\uff0c\u4e00\u4e9b\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5229\u7528\u8fd9\u79cd\u65b9\u6cd5\u68c0\u7d22\u8fd9\u68f5\u6811\u3002\u4f8b\u5982\uff0c\u501f\u52a9\u4e8e\u76f8\u5bf9\u4f4d\u7f6e\uff08\u5728\u5de6\u8fb9\u3001\u5728\u53f3\u8fb9\u3001\u5305\u542b\u7b49\uff09\u64cd\u4f5c\u7b26\uff0cR-tree\u5c31\u53ef\u4ee5\u88ab\u5b58\u50a8\u5728GiST\u7d22\u5f15\u4e2d\uff0c\u7528\u6765\u5904\u7406\u7a7a\u95f4\u6570\u636e\uff1b\u501f\u52a9\u4e8e\u96c6\u5408\u7684\u4ea4\u3001\u5305\u542b\u64cd\u4f5c\u7b26\uff0cRD-tree\u4e5f\u53ef\u4ee5\u88ab\u5b58\u50a8\u5728GiST\u7d22\u5f15\u4e2d\u3002<\/p>\n PG\u5177\u6709\u5f88\u597d\u7684\u6269\u5c55\u6027\uff0c\u901a\u8fc7\u524d\u51e0\u7bc7\u6587\u7ae0\u53ef\u77e5\uff0c\u6211\u4eec\u5b8c\u5168\u53ef\u4ee5\u4ece\u5934\u5b9e\u73b0\u4e00\u79cd\u65b0\u7684\u7d22\u5f15\u7c7b\u578b\u3002\u4e3a\u4e86\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff0c\u6211\u4eec\u5fc5\u987b\u5b9e\u73b0\u4e00\u4e2a\u901a\u7528\u7d22\u5f15\u5f15\u64ce\u7684\u63a5\u53e3\u3002\u4f46\u662f\uff0c\u8fd9\u4ecd\u7136\u9700\u8981\u5f88\u5f3a\u7684\u4e13\u4e1a\u77e5\u8bc6\uff0c\u9700\u8981\u82b1\u8d39\u5927\u91cf\u7684\u4eba\u529b\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u4f46\u9700\u8981\u4e86\u89e3\u65b0\u7d22\u5f15\u672c\u8eab\u7684\u903b\u8f91\uff0c\u8fd8\u8981\u628a\u7d22\u5f15\u7684\u6570\u636e\u6620\u5c04\u5230PG\u7684\u9875\u9762\u4e2d\uff0c\u9700\u8981\u9ad8\u6548\u5730\u4f7f\u7528\u9501\uff0c\u9700\u8981\u8003\u8651redo\u65e5\u5fd7\u3002GiST\u7b80\u5316\u4e86\u6211\u4eec\u7684\u4efb\u52a1\uff0c\u5b83\u4e3a\u6211\u4eec\u5904\u7406\u4e86\u4e00\u4e9b\u5e95\u5c42\u7684\u95ee\u9898\uff0c\u5e76\u4e14\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5b83\u81ea\u5df1\u7684\u63a5\u53e3\uff1a\u4e0e\u5e94\u7528\u76f8\u5173\uff0c\u4f46\u662f\u4e0ePG\u5185\u90e8\u6280\u672f\u65e0\u5173\u7684\u63a5\u53e3\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u628aGiST\u770b\u4f5c\u4e00\u4e2a\u5b9e\u73b0\u65b0\u7d22\u5f15\u7c7b\u578b\u7684\u6846\u67b6\u3002<\/p>\n GiST\u662f\u4e00\u9897\u5e73\u8861\u6811\uff0c\u5373\u4ece\u6839\u8282\u70b9\u5230\u53f6\u5b50\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8282\u70b9\u6570\u90fd\u76f8\u540c\u3002\u8282\u70b9\u4e2d\u5305\u542b\u4e86\u7d22\u5f15\u7684\u6570\u636e\u884c\u3002<\/p>\n \u901a\u5e38\u6765\u8bb2\uff0c\u53f6\u5b50\u8282\u70b9\u4e2d\u7684\u6bcf\u4e00\u884c\u90fd\u5305\u542b\u4e00\u4e2a\u8c13\u8bcd\uff08bool\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\uff09\u548c\u4e00\u4e2a\u6307\u5411\u57fa\u8868\u7684TID\u3002\u7d22\u5f15\u6570\u636e\uff08key\uff09\u5fc5\u987b\u6ee1\u8db3\u8fd9\u4e2a\u8c13\u8bcd\u3002<\/p>\n \u975e\u53f6\u5b50\u8282\u70b9\u7684\u6bcf\u4e00\u884c\u4e5f\u5305\u542b\u4e00\u4e2a\u8c13\u8bcd\u548c\u4e00\u4e2a\u6307\u5411\u5b69\u5b50\u8282\u70b9\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u5b69\u5b50\u8282\u70b9\u6240\u8868\u793a\u7684\u5b50\u6811\u4e2d\u7684\u6240\u6709\u6570\u636e\u90fd\u5fc5\u987b\u6ee1\u8db3\u8fd9\u4e2a\u8c13\u8bcd\u3002GiST\u8fd9\u4e2a\u91cd\u8981\u7684\u7279\u6027\u66ff\u6362\u4e86B-tree\u4e2d\u7b80\u5355\u7684\u6709\u5e8f\u6027\u8d28\u3002<\/p>\n \u67e5\u8be2GiST\u6811\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u7279\u6b8a\u7684\u4e00\u81f4\u6027(consistent) \u51fd\u6570\uff0c\u8fd9\u662fGiST\u7684\u4e00\u4e2a\u63a5\u53e3\u51fd\u6570\uff0c\u53ef\u4ee5\u88ab\u5404\u79cd\u64cd\u4f5c\u7b26\u65cf\u81ea\u7531\u7684\u5b9e\u73b0\u3002<\/p>\n GiST\u5728\u5904\u7406\u6bcf\u884c\u6570\u636e\u65f6\uff0c\u90fd\u4f1a\u8c03\u7528\u8fd9\u4e2a\u4e00\u81f4\u6027\u51fd\u6570\uff0c\u7528\u6765\u786e\u5b9a\u8fd9\u884c\u6570\u636e\u662f\u5426\u6ee1\u8db3\u8c13\u8bcd\u6761\u4ef6\uff08\u7531indexed-field operator expression\u6307\u5b9a\uff09\u3002\u5bf9\u975e\u53f6\u5b50\u8282\u70b9\u6765\u8bf4\uff0c\u8fd9\u4e2a\u51fd\u6570\u51b3\u5b9a\u662f\u5426\u6709\u5fc5\u8981\u5411\u4e0b\u641c\u7d22\u76f8\u5e94\u7684\u5b50\u6811\uff1b\u5bf9\u53f6\u5b50\u8282\u70b9\u6765\u8bf4\uff0c\u8fd9\u4e2a\u51fd\u6570\u51b3\u5b9a\u67d0\u884c\u7d22\u5f15\u6570\u636e\u662f\u5426\u6ee1\u8db3\u8c13\u8bcd\u6761\u4ef6\u3002<\/p>\n \u4e0e\u4e00\u822c\u6811\u7684\u641c\u7d22\u7b97\u6cd5\u76f8\u540c\uff0c\u67e5\u8be2\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u3002\u901a\u8fc7\u8c03\u7528\u4e00\u81f4\u6027\u51fd\u6570\uff0c\u53ef\u4ee5\u77e5\u9053\u7ee7\u7eed\u641c\u7d22\u54ea\u4e9b\u5b50\u6811\uff08\u53ef\u80fd\u6709\u591a\u4e2a\u5b50\u6811\u6ee1\u8db3\u6761\u4ef6\uff09\uff0c\u53ef\u4ee5\u6392\u9664\u54ea\u4e9b\u5b50\u6811\u3002\u5bf9\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5b50\u8282\u70b9\uff0c\u91cd\u590d\u8fd9\u4e2a\u641c\u7d22\u8fc7\u7a0b\u3002\u5bf9\u53f6\u5b50\u8282\u70b9\uff0c\u5c06\u6ee1\u8db3\u4e00\u81f4\u6027\u51fd\u6570\u7684\u884c\u4f5c\u4e3a\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n \u641c\u7d22\u8fc7\u7a0b\u4f7f\u7528\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002\u5c3d\u53ef\u80fd\u5feb\u7684\u5230\u8fbe\u53f6\u5b50\u8282\u70b9\uff0c\u4f7f\u5f97\u67e5\u8be2\u5c3d\u5feb\u8fd4\u56de\u7ed3\u679c\u3002\u8fd9\u5728\u7528\u6237\u4ec5\u4ec5\u9700\u8981\u90e8\u5206\u7ed3\u679c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n \u518d\u6b21\u5f3a\u8c03\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u4e00\u81f4\u6027\u51fd\u6570\u4e0e>\u3001>=\u3001=\u3001<=\u3001<\u5b8c\u5168\u6ca1\u6709\u5173\u7cfb\u3002\u5404\u79cd\u4e0d\u540c\u7d22\u5f15\u4e4b\u95f4\u7684\u4e00\u81f4\u6027\u51fd\u6570\u7684\u542b\u4e49\u53ef\u80fd\u5b8c\u5168\u4e0d\u540c\uff0c\u56e0\u6b64\uff0c\u7d22\u5f15\u4e5f\u4e0d\u4fdd\u8bc1\u6309\u67d0\u79cd\u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n \u672c\u6587\u4e0d\u4f1a\u4ecb\u7ecdGiST\u7d22\u5f15\u7684\u63d2\u5165\u548c\u5220\u9664\u7b97\u6cd5\uff1a\u5904\u7406\u8fd9\u4e9b\u64cd\u4f5c\u7684\u662f\u5176\u5b83\u51e0\u4e2a\u63a5\u53e3\u3002\u7136\u800c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u5411\u7d22\u5f15\u4e2d\u63d2\u5165\u4e00\u4e2a\u65b0\u503c\u65f6\uff0c\u63d2\u5165\u7684\u4f4d\u7f6e\u4f1a\u57fa\u4e8e\u8fd9\u6837\u7684\u539f\u5219\u8fdb\u884c\u9009\u62e9\uff1a\u63d2\u5165\u8fd9\u4e2a\u70b9\u540e\uff0c\u53ef\u4ee5\u8ba9\u7236\u8282\u70b9\u7684\u8c13\u8bcd\u6269\u5c55\u7684\u5c3d\u53ef\u80fd\u5c0f\u3002\u4f46\u662f\u5f53\u5220\u9664\u4e00\u4e2a\u503c\u65f6\uff0c\u7236\u8282\u70b9\u7684\u8c13\u8bcd\u6761\u4ef6\u7684\u8303\u56f4\u4e0d\u4f1a\u6536\u7f29\u3002\u6536\u7f29\u4ec5\u4ec5\u53d1\u751f\u5728\u8fd9\u4e9b\u573a\u666f\uff1a\u4e00\u4e2a\u9875\u9762\u88ab\u5206\u88c2\u6210\u4e24\u4e2a\uff08\u5f53\u4e00\u4e2a\u9875\u9762\u6ca1\u6709\u7a7a\u95f4\u5b58\u653e\u65b0\u503c\u65f6\uff09\u3001\u7d22\u5f15\u88ab\u91cd\u5efa\uff08reindex\u3001vacuum full\uff09\u65f6\u3002\u56e0\u6b64\uff0c\u5728\u9891\u7e41\u66f4\u65b0\u7684\u573a\u666f\u4e0b\uff0cGiST\u7684\u6548\u7387\u4f1a\u968f\u7740\u65f6\u95f4\u9010\u6e10\u4e0b\u964d\u3002<\/p>\n \u4e0b\u6587\u901a\u8fc7\u5177\u4f53\u7684\u793a\u4f8b\u4ecb\u7ecd\u51e0\u79cd\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u7d22\u5f15\uff0c\u548c\u5b83\u4eec\u4e00\u4e9b\u6709\u7528\u7684\u6027\u8d28\uff1a<\/p>\n \u6211\u4eec\u5c06\u4f7f\u7528\u5e73\u9762\u4e0a\u7684\u70b9\u5c55\u793a\u8fd9\u4e2a\u793a\u4f8b\uff08\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u4e3a\u5176\u5b83\u5730\u7406\u6570\u636e\u7c7b\u578b\u521b\u5efa\u7c7b\u4f3c\u7684\u7d22\u5f15\uff09\u3002\u5e38\u89c4\u7684B-tree\u5e76\u4e0d\u9002\u5408\u8fd9\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u56e0\u4e3a\u6bd4\u8f83\u4e24\u4e2a\u70b9\u7684\u5927\u5c0f\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\u3002<\/p>\n R-tree\u7684\u601d\u60f3\u662f\u5c06\u5e73\u9762\u5212\u5206\u6210\u591a\u4e2a\u77e9\u5f62\uff0c\u8fd9\u4e9b\u77e9\u5f62\u53ef\u4ee5\u8986\u76d6\u6240\u6709\u88ab\u7d22\u5f15\u7684\u70b9\u3002\u4e00\u4e2a\u7d22\u5f15\u884c\u5b58\u653e\u4e00\u4e2a\u77e9\u5f62\uff0c\u8c13\u8bcd\u53ef\u4ee5\u88ab\u5b9a\u4e49\u6210\uff1a\u88ab\u641c\u7d22\u7684\u70b9\u662f\u5426\u88ab\u7ed9\u5b9a\u7684\u77e9\u5f62\u5305\u542b\u3002<\/p>\n R-tree\u7684\u6839\u8282\u70b9\u5b58\u50a8\u51e0\u4e2a\u6700\u5927\u7684\u77e9\u5f62\uff0c\u5b83\u4eec\u53ef\u80fd\u76f8\u4e92\u91cd\u53e0\u3002\u5b69\u5b50\u8282\u70b9\u5b58\u50a8\u5c0f\u4e00\u4e9b\u7684\u77e9\u5f62\uff1b\u5b69\u5b50\u8282\u70b9\u5b58\u50a8\u7684\u77e9\u5f62\u88ab\u7236\u8282\u70b9\u5305\u542b\uff1b\u5b69\u5b50\u8282\u70b9\u5b58\u50a8\u7684\u77e9\u5f62\u53c8\u5305\u542b\u5b59\u5b50\u8282\u70b9\u4e2d\u6240\u6709\u7684\u77e9\u5f62\uff08\u6216\u70b9\uff09\u3002<\/p>\n \u7406\u8bba\u4e0a\u6765\u8bf4\uff0c\u53f6\u5b50\u8282\u70b9\u5e94\u8be5\u5b58\u50a8\u70b9\uff0c\u4f46\u662f\u53f6\u5b50\u8282\u70b9\u548c\u975e\u53f6\u5b50\u8282\u70b9\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\uff0c\u56e0\u6b64\uff0c\u53f6\u5b50\u8282\u70b9\u4e5f\u5b58\u653e\u77e9\u5f62\uff0c\u4f46\u662f\u8fd9\u4e9b\u77e9\u5f62\u9000\u5316\u6210\u70b9\u3002<\/p>\n \u6211\u4eec\u4f7f\u7528\u53ef\u89c6\u5316\u7684\u65b9\u5f0f\u5c55\u793a\u4e00\u4e2a\u4e09\u5c42\u7684R-tree\u3002\u8fd9\u4e9b\u70b9\u662f\u98de\u673a\u573a\u7684\u5750\u6807\uff08\u548cdemo\u6570\u636e\u5e93\u4e2d<\/a>\u7684airports\u8868\u7c7b\u4f3c\uff0c\u4f46\u662f\u5c55\u793a\u4e86\u66f4\u591aopenflights.org\u4e0a\u7684\u6570\u636e\uff09\u3002<\/p>\n \u73b0\u5728\u8003\u8651\u4e00\u4e2a\u66f4\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b83\u53ea\u6709\u4e00\u5c42\u3002<\/p>\n <\/a><\/p>\n \u5982\u679c\u6309\u7167\u4e0a\u56fe\u5206\u88c2\uff0c\u7d22\u5f15\u8282\u70b9\u7684\u5173\u7cfb\u5982\u4e0b\uff1a<\/p>\n <\/a><\/p>\n \u64cd\u4f5c\u7b26\uff08indexed-field <@ expression\uff0cindexed-field\u662f\u4e00\u4e2a\u70b9\uff0cexpression\u662f\u4e00\u4e2a\u77e9\u5f62\uff09\u7684\u4e00\u81f4\u6027\u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a\u5bf9\u4e00\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u884c\uff0c\u5982\u679c\u5b83\u8868\u793a\u7684\u77e9\u5f62\u4e0e\u8868\u8fbe\u5f0f\u4e2d\u7684\u77e9\u5f62\u76f8\u4ea4\uff0c\u5219\u8fd4\u56deyes\uff1b\u5bf9\u4e00\u4e2a\u53f6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u884c\uff0c\u5982\u679c\u5b83\u8868\u793a\u7684\u70b9\uff08\u9000\u5316\u7684\u77e9\u5f62\uff09\u88ab\u8868\u8fbe\u5f0f\u4e2d\u7684\u77e9\u5f62\u5305\u542b\uff0c\u5219\u8fd4\u56deyes\u3002<\/p>\n <\/a><\/p>\n \u67e5\u8be2\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u3002\u77e9\u5f62{(2,1)-(7,4)}\u4e0e{(1,1)-(6,3)}\u76f8\u4ea4\uff0c\u4f46\u662f\u4e0e{(5,5)-(8,8)}\u4e0d\u76f8\u4ea4\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u641c\u7d22\u7b2c\u4e8c\u9897\u5b50\u6811\u3002<\/p>\n <\/a><\/p>\n \u5230\u8fbe\u7b2c\u4e00\u4e2a\u53f6\u5b50\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u904d\u5386\u5b83\u5305\u542b\u76843\u4e2a\u70b9\uff0c\u8fd4\u56de\u5176\u4e2d\u4e24\u4e2a\u4f5c\u4e3a\u7ed3\u679c\uff1a(3,2)\u548c(6,3)\u3002<\/p>\n \u672c\u5c0f\u8282\u4f7f\u7528\u4e0a\u6587\u98de\u673a\u573a\u7684\u793a\u4f8b\u4ecb\u7ecd\u7d22\u5f15\u7684\u5185\u90e8\u7ed3\u6784\u3002<\/p>\n Pageinspect\u63d2\u4ef6\u4e0d\u80fd\u67e5\u770bGiST\u7d22\u5f15\u7684\u5185\u90e8\u7ed3\u6784\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53e6\u5916\u4e00\u4e2a\u63d2\u4ef6\uff1agevel\u3002\u5b83\u6ca1\u6709\u88ab\u5305\u542b\u5728\u6807\u51c6\u7684\u53d1\u884c\u7248\u4e2d\uff0c\u70b9\u51fb\u8fd9\u91cc<\/a>\u67e5\u770b\u5b89\u88c5\u6307\u5357\u3002<\/p>\n \u5982\u679c\u4e00\u5207\u987a\u5229\uff0c\u5b83\u5c06\u63d0\u4f9b3\u4e2a\u51fd\u6570\u3002\u9996\u5148\uff0c\u770b\u4e00\u4e0b\u7d22\u5f15\u7684\u7edf\u8ba1\u6570\u636e\uff1a<\/p>\n \u53ef\u4ee5\u770b\u51fa\uff0c\u7d22\u5f15\u5305\u542b4\u5c42\uff0c\u5171\u6709690\u4e2a\u9875\u9762\uff1a\u6839\u8282\u70b9\u548c\u4e2d\u95f4\u4e24\u5c42\u5df2\u5728\u4e0a\u6587\u7684\u56fe\u4e2d\u88ab\u5c55\u793a\uff0c\u7b2c4\u5c42\u662f\u53f6\u5b50\u8282\u70b9\u3002\u5b9e\u9645\u4e0a\uff0c\u5305\u542b8000\u4e2a\u70b9\u7684\u7d22\u5f15\u7684\u4f53\u79ef\u4e0d\u5e94\u8be5\u8fd9\u4e48\u5927\uff1a\u4e3a\u4e86\u6e05\u6670\u5c55\u793a\uff0c\u6211\u4eec\u53ea\u4f7f\u7528\u4e8610%\u7684\u586b\u5145\u7387\uff0c\u5bfc\u81f4\u7d22\u5f15\u7684\u4f53\u79ef\u5927\u4e00\u4e9b\u3002<\/p>\n \u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8f93\u51fa\u8fd9\u68f5\u6811\uff1a<\/p>\n \u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8f93\u51fa\u7d22\u5f15\u884c\u7684\u6570\u636e\u3002\u6ce8\u610f\uff1a\u7ed3\u679c\u5fc5\u987b\u88ab\u5f3a\u5236\u8f6c\u6362\u6210\u6240\u9700\u7684\u6570\u636e\u7c7b\u578b\u3002\u5728\u6211\u4eec\u7684\u573a\u666f\u4e2d\uff0c\u8fd9\u4e2a\u7c7b\u578b\u662fbox\uff08\u77e9\u5f62\u7684\u8fb9\u6846\uff09\u3002\u9876\u5c42\u8282\u70b9\u4e2d\u76845\u884c\u5982\u4e0b\uff1a<\/p>\n \u5b9e\u9645\u4e0a\uff0c\u4e0a\u9762\u7684\u56fe\u7247\uff0c\u5c31\u662f\u7528\u8fd9\u4e9b\u6570\u636e\u751f\u6210\u7684\u3002<\/p>\n \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6240\u8ba8\u8bba\u7684\u64cd\u4f5c\u7b26\u90fd\u88ab\u79f0\u4f5c\u641c\u7d22\u64cd\u4f5c\u7b26\uff08\u4f8b\u5982p <@ box ‘(2,1),(7,4)’\u4e2d\u7684 <@\uff09\uff0c\u56e0\u4e3a\u4ed6\u4eec\u5728\u67e5\u8be2\u4e2d\u8868\u793a\u641c\u7d22\u6761\u4ef6\u3002<\/p>\n \u9664\u4e86\u641c\u7d22\u64cd\u4f5c\u7b26\uff0c\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u64cd\u4f5c\u7b26\uff1a\u6392\u5e8f\u64cd\u4f5c\u7b26\u3002\u5b83\u4eec\u53ef\u4ee5\u51fa\u73b0\u5728order by\u5b50\u53e5\u4e2d\u7528\u6765\u6307\u5b9a\u6392\u5e8f\u6761\u4ef6\uff0c\u4ee3\u66ff\u901a\u5e38\u7684\u5217\u4f5c\u4e3a\u6392\u5e8f\u6761\u4ef6\u3002\u8bf7\u770b\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n p <-> point ‘(4,7)’\u6761\u4ef6\u7528\u4e86\u4e00\u4e2a\u6392\u5e8f\u64cd\u4f5c\u7b26\uff1a<->\u3002\u5b83\u8868\u793a\uff1a\u628a\u7ed3\u679c\u6309\u7167\u4e0e\u70b9(4,7)\u7684\u8ddd\u79bb\u8fdb\u884c\u6392\u5e8f\u3002limit 2\u8868\u793a\u8fd4\u56de\u4e24\u4e2a\u3002\u8fd9\u79cd\u67e5\u8be2\u4e5f\u88ab\u79f0\u4f5ck-NN\u67e5\u8be2\uff08K\u8fd1\u90bb\u67e5\u8be2\uff09\u3002<\/p>\n \u4e00\u4e2aAM\u4e3a\u4e86\u652f\u6301\u8fd9\u79cd\u67e5\u8be2\uff0c\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u989d\u5916\u7684\u8ddd\u79bb\u51fd\u6570\uff0c\u800c\u4e14\u6392\u5e8f\u64cd\u4f5c\u7b26\u5fc5\u987b\u88ab\u9002\u5f53\u7684\u64cd\u4f5c\u7b26\u7c7b\u5305\u542b\uff0c\u4f8b\u5982\u70b9\u7684points_ops\u7c7b\u3002\u4e0b\u9762\u5c55\u793a\u4e86\u64cd\u4f5c\u7b26\u548c\u5b83\u4eec\u7684\u7c7b\u578b\uff08s\u8868\u793a\u641c\u7d22\u64cd\u4f5c\u7b26\uff0co\u8868\u793a\u6392\u5e8f\u64cd\u4f5c\u7b26\uff09\uff1a<\/p>\n \u7b56\u7565\uff08B-tree\u4e00\u6587\u4e2d\u63d0\u5230\u8fc7\u7b56\u7565\uff0cB-tree\u652f\u63015\u79cd\u6bd4\u8f83\u7b56\u7565\uff09\u7684\u5e8f\u53f7\u4e5f\u88ab\u5c55\u793a\u51fa\u6765\uff0c\u5e76\u4e14\u9644\u4e0a\u4e86\u5b83\u4eec\u7684\u542b\u4e49\u3002\u660e\u663e\u770b\u51fa\uff0c\u5b83\u652f\u6301\u7684\u7b56\u7565\u6bd4B-tree\u591a\uff0c\u6709\u4e00\u4e9b\u7b56\u7565\u53ea\u652f\u6301\u70b9\u3002\u8fd8\u53ef\u4ee5\u4e3a\u5176\u5b83\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u4e0d\u540c\u7684\u7b56\u7565\u3002<\/p>\n \u6bcf\u5904\u7406\u4e00\u4e2a\u7d22\u5f15\u884c\uff0c\u90fd\u4f1a\u8c03\u7528\u8ddd\u79bb\u51fd\u6570\uff0c\u5b83\u5fc5\u987b\u53ef\u4ee5\u8ba1\u7b97\u51fa\u8868\u8fbe\u5f0f\uff08indexed-field ordering-operator expression\uff09\u4e2d\u7684\u503c\uff08expression\uff09\u4e0e\u7d22\u5f15\u884c\u7684\u8ddd\u79bb\u3002\u5bf9\u53f6\u5b50\u8282\u70b9\u4e2d\u7684\u7d22\u5f15\u884c\u6765\u8bf4\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f\u51c6\u786e\u7684\u8ddd\u79bb\u3002\u5bf9\u975e\u53f6\u5b50\u8282\u70b9\u6765\u8bf4\uff0c\u8fd9\u4e2a\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u6b64\u8282\u70b9\u6240\u5305\u542b\u7684\u6240\u6709\u53f6\u5b50\u8282\u70b9\u4e2d\u7684\u6570\u636e\u884c\u4e0eexpression\u7684\u6700\u77ed\u8ddd\u79bb\u3002\u56e0\u4e3a\u904d\u5386\u6240\u6709\u7684\u53f6\u5b50\u8282\u70b9\u4ee3\u4ef7\u5f88\u5927\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u4f4e\u4f30\u8ddd\u79bb\uff0c\u4ee5\u635f\u5931\u6548\u7387\u4e3a\u4ee3\u4ef7\u3002\u4f46\u662f\u4e0d\u53ef\u4ee5\u9ad8\u4f30\u8fd9\u4e2a\u8ddd\u79bb\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u7834\u574f\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\u3002<\/p>\n \u8ddd\u79bb\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4efb\u610f\u53ef\u4ee5\u6392\u5e8f\u7684\u7c7b\u578b\u3002\u5c31\u50cf\u4e4b\u524d\u4ecb\u7ecd\u7684\u4e00\u6837\uff0c\u4e3a\u4e86\u5bf9\u503c\u6392\u5e8f\uff0cPG\u4f1a\u4eceb-tree\u7684\u64cd\u4f5c\u7b26\u65cf\u4e2d\u9009\u62e9\u9002\u5f53\u7684\u64cd\u4f5c\u7b26\u3002<\/p>\n \u5bf9\u5e73\u9762\u4e0a\u7684\u70b9\u6765\u8bf4\uff0c\u8fd9\u4e2a\u8ddd\u79bb\u53ef\u4ee5\u88ab\u7b80\u5355\u7684\u89e3\u91ca\u4e3a\u4e24\u4e2a\u70b9\u7684\u6b27\u5f0f\u8ddd\u79bb\u3002\u70b9\u4e0e\u77e9\u5f62\u4e4b\u95f4\u7684\u8ddd\u79bb\u662f\u4ece\u70b9\u5230\u8fd9\u4e2a\u77e9\u5f62\u7684\u6700\u5c0f\u8ddd\u79bb\uff0c\u5982\u679c\u77e9\u5f62\u5305\u542b\u8fd9\u4e2a\u70b9\uff0c\u5219\u8ddd\u79bb\u4e3a0\u3002\u65e0\u9700\u904d\u5386\u5b69\u5b50\u8282\u70b9\u5c31\u53ef\u4ee5\u7b80\u5355\u8ba1\u7b97\u51fa\u8fd9\u4e2a\u8ddd\u79bb\uff0c\u8fd9\u4e2a\u503c\u80af\u5b9a\u4e0d\u4f1a\u5927\u4e8e\u70b9\u5230\u77e9\u5f62\u4e2d\u4efb\u610f\u70b9\u7684\u8ddd\u79bb\u3002<\/p>\n \u6211\u4eec\u5206\u6790\u4e0a\u9762\u67e5\u8be2\u7684\u641c\u7d22\u7b97\u6cd5\u3002<\/p>\n <\/a><\/p>\n \u67e5\u8be2\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u3002\u6839\u8282\u70b9\u5305\u542b\u4e24\u4e2a\u77e9\u5f62\uff0c(4,7)\u5230\u77e9\u5f62{(1,1)-(6,3)}\u7684\u8ddd\u79bb\u4e3a4.0\uff0c\u5230\u77e9\u5f62{(5,5)-(8,8)}\u7684\u8ddd\u79bb\u4e3a1.0\u3002<\/p>\n \u5b69\u5b50\u8282\u70b9\u6309\u7167\u8ddd\u79bb\u9012\u589e\u7684\u987a\u5e8f\u88ab\u641c\u7d22\u3002\u5728\u8fd9\u79cd\u65b9\u5f0f\u4e0b\uff0c\u6211\u4eec\u9996\u5148\u4e0b\u964d\u5230\u6700\u8fd1\u7684\u5b69\u5b50\u8282\u70b9\u4e2d\uff0c\u5e76\u4e14\u8ba1\u7b97\u5b69\u5b50\u8282\u70b9\u4e2d\u7684\u70b9\u5230(4,7)\u7684\u8ddd\u79bb\uff1a<\/p>\n <\/a><\/p>\n \u8fd9\u4e9b\u4fe1\u606f\u5df2\u7ecf\u8db3\u591f\u8fd4\u56de\u8ddd\u79bb(4,7)\u6700\u8fd1\u7684\u4e24\u4e2a\u70b9\u4f5c\u4e3a\u7ed3\u679c\uff1a(5,5) and (7,8)\u3002\u56e0\u4e3a\u6211\u4eec\u77e5\u9053\u5230\u77e9\u5f62{(1,1)-(6,3)}\u4e2d\u7684\u70b9\u5230(4,7)\u7684\u8ddd\u79bb\u6700\u5c11\u662f4.0\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u641c\u7d22\u7b2c\u4e00\u4e2a\u5b69\u5b50\u8282\u70b9\u3002<\/p>\n \u4f46\u662f\uff0c\u5982\u679c\u9700\u8981\u8fd4\u56de3\u4e2a\u70b9\u5462\uff1f<\/p>\n \u867d\u7136\u7b2c\u4e8c\u4e2a\u5b69\u5b50\u8282\u70b9\u5305\u542b3\u4e2a\u70b9\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u80fd\u76f4\u63a5\u8fd4\u56de(8,6)\u3002\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u5b69\u5b50\u8282\u70b9\u4e2d\u7684\u70b9\u53ef\u80fd\u8ddd\u79bb(4,7)\u66f4\u8fd1\uff084.0 < 4.1\uff09\u3002<\/p>\n <\/a><\/p>\n \u8fd9\u4e2a\u793a\u4f8b\u8bf4\u660e\u4e86\u975e\u53f6\u5b50\u8282\u70b9\u5bf9\u8ddd\u79bb\u51fd\u6570\u7684\u8981\u6c42\u3002\u901a\u8fc7\u4e3a\u7b2c\u4e8c\u4e2a\u5b69\u5b50\u8282\u70b9\u9009\u53d6\u4e00\u4e2a\u66f4\u5c0f\u7684\u8ddd\u79bb\uff08\u9009\u53d64.0\uff0c\u800c\u4e0d\u662f\u9009\u53d6\u771f\u5b9e\u503c4.5\uff09\uff0c\u964d\u4f4e\u4e86\u6548\u7387\uff08\u641c\u7d22\u7b97\u6cd5\u9700\u8981\u989d\u5916\u641c\u7d22\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u4f46\u662f\u6ca1\u6709\u7834\u574f\u7b97\u6cd5\u7684\u6b63\u786e\u6027\u3002<\/p>\n \u76f4\u5230\u6700\u8fd1\uff0cGiST\u662f\u552f\u4e00\u652f\u6301\u6392\u5e8f\u64cd\u4f5c\u7b26\u7684AM\u3002\u4f46\u662f\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u53d8\u5316\u4e86\uff1aRUM \u7d22\u5f15\uff08\u4ee5\u540e\u8ba8\u8bba\uff09\u52a0\u5165\u4e86\u8fd9\u4e2a\u884c\u5217\uff0cB-tree\u4e5f\u4e0d\u662f\u4e0d\u53ef\u80fd\u52a0\u5165\u8fd9\u4e2a\u884c\u5217\uff1aNikita Glukhov\u7684\u4e00\u4e2apatch\uff0c\u6b63\u5728\u793e\u533a\u4e2d\u88ab\u8ba8\u8bba\u3002<\/p>\n \u622a\u6b622019\u5e743\u6708\uff0cSP-GiST\u5373\u5c06\u5728PG 12\u4e2d\u652f\u6301K-NN\uff0c\u4e5f\u662f\u7531Nikita Glukhov\u5f00\u53d1\u3002B-tree\u7684patch\u8fd8\u5728\u5f00\u53d1\u4e2d\u3002(\u8bd1\u8005\u6ce8\uff1a\u6700\u65b0\u7248PG\u7684sp-GiST\u5df2\u7ecf\u652f\u6301K-NN\u67e5\u8be2)<\/p>\n \u8fd9\u4e00\u5c0f\u8282\u901a\u8fc7\u793a\u4f8b\u5c55\u793aGiST\u7d22\u5f15\u5728\u533a\u95f4\u6570\u636e\u4e0a\u7684\u5e94\u7528\uff0c\u5e76\u4e14\u4ecb\u7ecd\u6392\u5b83\u7ea6\u675f\u3002<\/p>\n \u5bf9\u533a\u95f4\u6570\u636e\uff0c\u6bd4\u5982\u65f6\u95f4\u533a\u95f4\uff08tsrange\u7c7b\u578b\uff09\uff0c\u5efa\u7acb\u7684GiST\u7d22\u5f15\uff0c\u4e0e\u4e0a\u4e00\u5c0f\u8282\u793a\u4f8b\u7684\u552f\u4e00\u4e0d\u540c\u5c31\u662f\uff0c\u8282\u70b9\u4e2d\u7684\u6570\u636e\u8868\u793a\u4e00\u4e2a\u533a\u95f4\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u77e9\u5f62\u3002<\/p>\n \u4e0b\u9762\u4e3e\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a\u6211\u4eec\u60f3\u8981\u51fa\u79df\u4e00\u5957\u623f\u5b50\uff0c\u5e76\u4e14\u7528\u4e00\u5f20\u8868\u8868\u793a\u6bcf\u6b21\u9884\u5b9a\u7684\u65f6\u95f4\u533a\u95f4\u3002<\/p>\n \u8fd9\u4e2a\u7d22\u5f15\u53ef\u4ee5\u52a0\u901f\u4e0b\u9762\u7684\u67e5\u8be2\uff1a<\/p>\n &&\u64cd\u4f5c\u7b26\u8868\u793a\u533a\u95f4\u76f8\u4ea4\uff1b\u67e5\u8be2\u5fc5\u987b\u8fd4\u56de\u6240\u6709\u4e0e\u7ed9\u5b9a\u533a\u95f4\u76f8\u4ea4\u7684\u533a\u95f4\u3002\u5bf9\u8fd9\u79cd\u64cd\u4f5c\u7b26\uff0c\u4e00\u81f4\u6027\u51fd\u6570\u7684\u5b9a\u4e49\u51b3\u5b9a\u7ed9\u5b9a\u7684\u533a\u95f4\u662f\u5426\u4e0e\u5185\u90e8\u8282\u70b9\u6216\u53f6\u5b50\u8282\u70b9\u4e2d\u7684\u884c\u76f8\u4ea4\u3002<\/p>\n \u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u8868\u793a\u4ee5\u67d0\u79cd\u987a\u5e8f\u67e5\u8be2\u5230\u533a\u95f4\uff0c\u5c3d\u7ba1\u533a\u95f4\u7c7b\u578b\u6709\u6bd4\u8f83\u64cd\u4f5c\u7b26\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u533a\u95f4\u5efa\u7acbbtree\u7d22\u5f15\uff0c\u4f46\u662fB-tree\u7f3a\u5c11\u4e0b\u9762\u8fd9\u4e9b\u64cd\u4f5c\u7b26\uff0c\u4f1a\u4f7f\u5f97\u6211\u4eec\u5f88\u96be\u5904\u7406\u533a\u95f4\u6570\u636e:<\/p>\n (\u9664\u4e86\u7b49\u4e8e\u64cd\u4f5c\u7b26\u88abB-tree\u5305\u542b\u4e4b\u5916\uff0c\u5176\u5b83\u64cd\u4f5c\u7b26\u5728B-tree\u4e2d\u90fd\u4e0d\u5b58\u5728)<\/p>\n \u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528gevel\u63d2\u4ef6\u67e5\u770b\u5b83\u7684\u5185\u90e8\u7ed3\u6784\uff0c\u53ea\u9700\u8981\u5728\u8c03\u7528gist_print\u65f6\u5bf9\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n \u4f7f\u7528GiST\u7d22\u5f15\u53ef\u4ee5\u5b9e\u73b0\u6392\u5b83\u7ea6\u675f\uff08EXCLUDE\uff09\u3002<\/p>\n \u6392\u5b83\u7ea6\u675f\u4fdd\u8bc1\u8868\u4e2d\u7684\u4efb\u610f\u4e24\u884c\u6570\u636e\u5728\u67d0\u4e9b\u5217\u4e0a\u90fd\u4e0d\u80fd\u6709\u67d0\u79cd\u76f8\u5173\u6027\uff08\u7531\u7528\u6237\u6307\u5b9a\u76f8\u5173\u6027\u7684\u64cd\u4f5c\u7b26\uff09\u3002\u5982\u679c\u64cd\u4f5c\u7b26\u662f\u76f8\u7b49\u64cd\u4f5c\u7b26\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u552f\u4e00\u7ea6\u675f\uff1a\u4efb\u610f\u4e24\u884c\u6570\u636e\u5728\u67d0\u4e9b\u5217\u4e0a\u4e92\u4e0d\u76f8\u7b49\u3002<\/p>\n \u6392\u5b83\u7ea6\u675f\u7531\u7d22\u5f15\u63d0\u4f9b\u652f\u6301\uff0c\u5c31\u50cf\u552f\u4e00\u7ea6\u675f\u4e00\u6837\u3002\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u6ee1\u8db3\u4e0b\u5217\u6761\u4ef6\u7684\u4efb\u610f\u64cd\u4f5c\u7b26\uff1a<\/p>\n \u4e0b\u9762\u5217\u51fa\u4e86\u4e00\u4e9b\u53ef\u88ab\u4f7f\u7528\u7684\u7b56\u7565\u548c\u64cd\u4f5c\u7b26\u7684\u793a\u4f8b\uff08\u64cd\u4f5c\u7b26\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u540d\u79f0\uff0c\u4e5f\u53ef\u80fd\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u6570\u636e\u7c7b\u578b\uff09\uff1a<\/p>\n \u6211\u4eec\u53ef\u4ee5\u5728\u6392\u5b83\u7ea6\u675f\u4e2d\u4f7f\u7528\u7b49\u4e8e\u64cd\u4f5c\u7b26\uff0c\u4f46\u662f\u8fd9\u5728\u5b9e\u9645\u4e2d\u5e76\u6ca1\u6709\u610f\u4e49\uff0c\u56e0\u4e3a\u76f4\u63a5\u4f7f\u7528\u552f\u4e00\u7ea6\u675f\u4f1a\u66f4\u52a0\u9ad8\u6548\u3002\u8fd9\u5c31\u662f\u6211\u4eec\u5728\u4ecb\u7ecdB-tree\u65f6\u6ca1\u6709\u4ecb\u7ecd\u6392\u5b83\u7ea6\u675f\u7684\u539f\u56e0\u3002<\/p>\n \u4e0b\u9762\u4e3e\u4e00\u4e2a\u6392\u5b83\u7ea6\u675f\u7684\u4f8b\u5b50\uff0c\u5b83\u4e0d\u5141\u8bb8\u9884\u5b9a\u7684\u65f6\u95f4\u4e92\u76f8\u91cd\u53e0\u3002<\/p>\n \u5148\u521b\u5efa\u6392\u5b83\u7ea6\u675f\uff0c\u7136\u540e\u63d2\u5165\u4e00\u884c\u6570\u636e\uff1a<\/p>\n \u5f53\u5c1d\u8bd5\u63d2\u5165\u4e00\u884c\u4e0e[2017-06-10, 2017-06-13)\u6709\u91cd\u53e0\u7684\u533a\u95f4\u65f6\uff0c\u4f1a\u76f4\u63a5\u62a5\u9519\uff1a<\/p>\n \u4e0b\u9762\u8ba9\u793a\u4f8b\u53d8\u5f97\u66f4\u590d\u6742\u4e00\u70b9\uff1a\u6211\u4eec\u5c06\u8981\u51fa\u79df\u591a\u5957\u623f\u5b50\uff0c\u6bcf\u5957\u623f\u5b50\u7684\u591a\u4e2a\u9884\u5b9a\u65f6\u95f4\u4e0d\u91cd\u53e0\u5373\u53ef\uff0c\u4e0d\u540c\u623f\u5b50\u7684\u9884\u5b9a\u65f6\u95f4\u53ef\u4ee5\u91cd\u53e0\u3002<\/p>\n \u6211\u4eec\u9700\u8981\u66f4\u6539\u6392\u5b83\u7ea6\u675f\uff0c\u8ba9\u5b83\u540c\u65f6\u8003\u8651\u623f\u5b50\u7684\u7f16\u53f7\u3002\u4f46\u662f\uff0cGiST\u5e76\u4e0d\u652f\u6301\u6574\u6570\u7c7b\u578b\u7684\u76f8\u7b49\u64cd\u4f5c\u7b26\uff1a<\/p>\n \u5728\u8fd9\u79cd\u573a\u666f\u4e0b\uff0cbtree_gist\u63d2\u4ef6\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\uff0c\u5b83\u5bf9B-tree\u56fa\u6709\u7684\u64cd\u4f5c\u7b26\u589e\u52a0\u4e86GiST\u529f\u80fd\u3002GiST\u672c\u8eab\u5c31\u652f\u6301\u4efb\u610f\u64cd\u4f5c\u7b26\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u8ba9\u5b83\u652f\u6301>\u3001>=\u3001=\u3001<=\u3001<\u5462\uff1f<\/p>\n \u73b0\u5728\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u540c\u4e00\u4e2a\u65f6\u95f4\u6bb5\u5185\u9884\u5b9a\u7b2c\u4e00\u5957\u623f\uff1a<\/p>\n \u4f46\u662f\u53ef\u4ee5\u5728\u76f8\u4ea4\u7684\u65f6\u95f4\u6bb5\u5185\u9884\u5b9a\u7b2c\u4e8c\u95f4\u623f<\/p>\n \u6ce8\u610f\uff0c\u867d\u7136GiST\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u53ef\u4ee5\u652f\u6301>\u3001>=\u3001=\u3001<=\u3001<\uff0c\u4f46\u662fB-tree\u7684\u6548\u7387\u66f4\u9ad8\u3002\u6240\u4ee5\uff0c\u4ec5\u4ec5\u5728\u5fc5\u987b\u7684\u65f6\u5019\u624d\u5e94\u8be5\u8fd9\u6837\u505a\uff0c\u5c31\u50cf\u672c\u4f8b\u4e00\u6837\u3002<\/p>\n \u9996\u5148\u7b80\u8981\u4ecb\u7ecd\u4e00\u4e0bPG\u4e2d\u7684\u5168\u6587\u68c0\u7d22\u3002<\/p>\n \u5168\u6587\u68c0\u7d22\u7684\u4efb\u52a1\u5c31\u662f\u4ece\u4e00\u5806\u6587\u6863\u4e2d\u627e\u51fa\u7b26\u5408\u67e5\u8be2\u7684\u6587\u6863\u3002\u5982\u679c\u6709\u5f88\u591a\u6587\u6863\u7b26\u5408\u6761\u4ef6\uff0c\u9700\u8981\u627e\u51fa\u6700\u4f73\u5339\u914d\u7684\u6587\u6863\uff0c\u672c\u6587\u4e0d\u8ba8\u8bba\u8fd9\u4e2a\u8bdd\u9898\u3002<\/p>\n \u4e3a\u4e86\u652f\u6301\u67e5\u8be2\uff0c\u9996\u5148\u628a\u4e00\u4e2a\u6587\u6863\u8f6c\u6362\u6210tsvector\u7c7b\u578b\uff0c\u5b83\u5305\u542b\u4e00\u7cfb\u5217\u8bcd\u7d20\u548c\u5b83\u4eec\u5728\u6587\u6863\u4e2d\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002\u8bcd\u7d20\u5c31\u662f\u4e00\u7cfb\u5217\u5355\u8bcd\uff0c\u8fd9\u4e9b\u5355\u8bcd\u5df2\u7ecf\u88ab\u5904\u7406\u8fc7\uff0c\u5177\u6709\u9002\u5408\u67e5\u8be2\u7684\u5f62\u5f0f\u3002\u4f8b\u5982\uff0c\u7edf\u4e00\u8f6c\u6362\u6210\u5c0f\u5199\uff0c\u53bb\u6389\u53ef\u53d8\u540e\u7f00\u7b49\u3002<\/p>\n \u53ef\u4ee5\u770b\u5230\u6709\u4e9b\u8bcd\u88ab\u76f4\u63a5\u4e22\u5f03\u4e86\uff08there, was, a, and, he\uff09\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u6587\u6863\u4e2d\u51fa\u73b0\u7684\u9891\u7387\u5f88\u9ad8\uff0c\u641c\u7d22\u8fd9\u4e9b\u8bcd\u901a\u5e38\u6ca1\u6709\u610f\u4e49\u3002\u6240\u6709\u8fd9\u4e9b\u8f6c\u6362\u89c4\u5219\u90fd\u53ef\u4ee5\u88ab\u5b9a\u5236\uff0c\u8fd9\u662f\u53e6\u5916\u4e00\u8bdd\u9898\uff0c\u672c\u6587\u4e0d\u518d\u8ba8\u8bba\u3002<\/p>\n \u67e5\u8be2\u4f7f\u7528\u53e6\u5916\u4e00\u4e2a\u7c7b\u578b\u8868\u793a\uff1atsquery\u3002\u7b80\u8981\u6765\u8bf4\uff0c\u67e5\u8be2\u5305\u542b\u4e00\u4e2a\u6216\u7531\u51e0\u4e2a\u6761\u4ef6\u8fde\u63a5\u8d77\u6765\u7684\u8bcd\u7d20\uff1a\u4e0e &, \u6216 |, \u975e !\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5c0f\u62ec\u53f7\u8868\u793a\u4f18\u5148\u7ea7\u3002<\/p>\n \u5168\u6587\u68c0\u7d22\u4e2d\uff0c\u53ea\u7528\u4e86\u4e00\u4e2a\u64cd\u4f5c\u7b26\uff08@@\uff0c\u5339\u914d\u64cd\u4f5c\u7b26\uff09\u3002<\/p>\n \u73b0\u5728\u6709\u8fd9\u4e9b\u77e5\u8bc6\u5df2\u7ecf\u591f\u7528\u4e86\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7bc7\u8ba8\u8bbaGIN\u7d22\u5f15\u65f6\uff0c\u518d\u6df1\u5165\u8ba8\u8bba\u5168\u6587\u68c0\u7d22\u3002<\/p>\n \u4e3a\u4e86\u66f4\u5feb\u5730\u8fdb\u884c\u5168\u6587\u68c0\u7d22\uff0c\u9996\u5148\uff0c\u8868\u4e2d\u5e94\u8be5\u76f4\u63a5\u5b58\u50a8tsvector\u7c7b\u578b\u7684\u5217\uff08\u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u641c\u7d22\u65f6\uff0c\u90fd\u505a\u4e00\u4e9b\u590d\u6742\u7684\u8f6c\u6362\uff09\u3002\u5176\u6b21\uff0c\u5fc5\u987b\u5728\u8fd9\u4e00\u5217\u4e0a\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\uff0c\u5176\u4e2d\u4e00\u4e2a\u9009\u62e9\u5c31\u662fGiST\u3002<\/p>\n \u5f53\u7136\uff0c\u4f7f\u7528trigger\u5c06\u6587\u6863\u8f6c\u6362\u6210tsvector\u66f4\u597d\u4e00\u4e9b\u3002<\/p>\n \u7d22\u5f15\u7684\u7ed3\u6784\u5e94\u8be5\u662f\u4ec0\u4e48\u6837\u5462\uff1f\u76f4\u63a5\u4f7f\u7528R-tree\u80af\u5b9a\u4e0d\u884c\uff0c\u56e0\u4e3a\u6ca1\u529e\u6cd5\u4e3a\u6587\u6863\u5b9a\u4e49\u4e00\u4e2a\u77e9\u5f62\u6846\u3002\u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u628a\u8fd9\u79cd\u65b9\u6cd5\u5728\u96c6\u5408\u7c7b\u578b\u4e0a\u7a0d\u4f5c\u6539\u52a8\uff0c\u79f0\u4f5cRD-tree\uff08RD\u662fRussian Doll\u7684\u610f\u601d\uff09\u3002\u8fd9\u79cd\u573a\u666f\u4e0b\uff0c\u96c6\u5408\u7684\u5143\u7d20\u662f\u8bcd\u7d20\u3002<\/p>\n RD-tree\u7684\u4e00\u4e2a\u601d\u60f3\u5c31\u662f\u628a\u77e9\u5f62\u6846\u7528\u96c6\u5408\u66ff\u4ee3\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2a\u96c6\u5408\u53ef\u4ee5\u5305\u542b\u5176\u5b83\u5b50\u96c6\u3002<\/p>\n \u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\u662f\u600e\u6837\u628a\u96c6\u5408\u8868\u793a\u6210\u7d22\u5f15\u7684\u884c\u3002\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u679a\u4e3e\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n <\/a><\/p>\n \u5047\u5982\u4f7f\u7528doc_tsv @@ to_tsquery(‘sit’)\u641c\u7d22\uff0c\u6211\u4eec\u53ef\u4ee5\u53ea\u641c\u7d22\u5305\u542bsit\u8bcd\u7d20\u7684\u8282\u70b9\uff1a<\/p>\n <\/a><\/p>\n \u8fd9\u79cd\u8868\u793a\u6709\u5f88\u5927\u7684\u95ee\u9898\u3002\u6587\u6863\u4e2d\u8bcd\u7d20\u7684\u6570\u91cf\u53ef\u80fd\u4f1a\u5f88\u5927\uff0c\u6240\u4ee5\u7d22\u5f15\u884c\u5c31\u4f1a\u5f88\u5927\uff0c\u5bfc\u81f4\u7d22\u5f15\u884c\u88ab\u5b58\u50a8\u5728toast\u8868\u4e2d\uff0c\u4f7f\u5f97\u7d22\u5f15\u975e\u5e38\u4f4e\u6548\u3002\u5373\u4f7f\u6587\u6863\u4e2d\u5305\u542b\u5f88\u5c11\u7684\u8bcd\u7d20\uff0c\u96c6\u5408\u7684\u5e76\u96c6\u4e5f\u4f1a\u5f88\u5927\uff1a\u8d8a\u9760\u8fd1root\u7684\u8282\u70b9\uff0c\u7d22\u5f15\u884c\u8d8a\u5927\u3002<\/p>\n \u8fd9\u79cd\u8868\u793a\u5f62\u5f0f\u6709\u65f6\u4e5f\u4f1a\u88ab\u4f7f\u7528\uff0c\u4f46\u662f\u88ab\u7528\u5728\u5176\u5b83\u7c7b\u578b\u4e0a\u3002\u5bf9\u5168\u6587\u68c0\u7d22\u6765\u8bf4\uff0c\u4f7f\u7528\u4e00\u79cd\u66f4\u7d27\u51d1\u7684\u7ed3\u6784 \u2014\u2014 \u88ab\u79f0\u4f5c\u7b7e\u540d\u6811\u3002\u5b83\u7684\u601d\u60f3\u4e0eBloom filer\u975e\u5e38\u76f8\u4f3c\u3002<\/p>\n \u6bcf\u4e2a\u8bcd\u7d20\u53ef\u4ee5\u88ab\u8868\u793a\u6210\u4e00\u4e2a\u7b7e\u540d\uff1a\u4e00\u4e2a\u6bd4\u7279\u4e32\u4e2d\u53ea\u6709\u4e00\u4e2a1\uff0c\u5176\u5b83\u4f4d\u5168\u662f0\u30021\u7684\u4f4d\u7f6e\u7531\u8bcd\u7d20\u7684hash\u503c\u51b3\u5b9a\u3002<\/p>\n \u5bf9\u6240\u6709\u8bcd\u7d20\u7684\u7b7e\u540d\u505aOR\u64cd\u4f5c\u7684\u7ed3\u679c\uff0c\u5c31\u662f\u6587\u6863\u7684\u7b7e\u540d\u3002<\/p>\n \u5047\u8bbe\u6709\u4e0b\u5217\u8bcd\u7d20\u53ca\u5176\u7b7e\u540d\uff1a<\/p>\n \u6bcf\u4e2a\u6587\u6863\u7684\u7b7e\u540d\u5982\u4e0b\uff1a<\/p>\n \u7d22\u5f15\u6811\u53ef\u4ee5\u8868\u793a\u6210\u8fd9\u6837\uff1a<\/p>\n1. \u7ed3\u6784<\/h1>\n
\n
2. \u7528R-tree\u7d22\u5f15\u70b9<\/h1>\n
2.1 \u4e24\u4e2a\u793a\u4f8b<\/h2>\n
\n\u7b2c\u4e00\u5c42\uff1a\u4e24\u4e2a\u76f8\u4ea4\u7684\u77e9\u5f62\n<\/div>\n
\n\u7b2c\u4e8c\u5c42\uff1a\u5927\u77e9\u5f62\u88ab\u5212\u5206\u6210\u66f4\u5c0f\u7684\u77e9\u5f62\n<\/div>\n
\n\u7b2c\u4e09\u5c42\uff1a\u6bcf\u4e2a\u77e9\u5f62\u5b58\u653e\u5c3d\u53ef\u80fd\u591a\u7684\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u88ab\u5bb9\u7eb3\u5230\u4e00\u4e2a\u7d22\u5f15\u9875\u9762\u4e2d\n<\/div>\ncreate 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 on points using gist(p);\n<\/code><\/pre>\n
set enable_seqscan = off;\nexplain(costs off) select * from points where p <@ box '(2,1),(7,4)';\n<\/code><\/pre>\n
QUERY PLAN \n----------------------------------------------\n Index Only Scan using points_p_idx on points\n Index Cond: (p <@ '(7,4),(2,1)'::box)\n(2 rows)\n<\/code><\/pre>\n
select * from points where p <@ box '(2,1),(7,4)';\n<\/code><\/pre>\n
p \n-------\n (3,2)\n (6,3)\n(2 rows)\n<\/code><\/pre>\n
2.2 \u5185\u90e8\u7ed3\u6784<\/h1>\n
select * from gist_stat('airports_coordinates_idx');\n<\/code><\/pre>\n
gist_stat \n------------------------------------------\n Number of levels: 4 +\n Number of pages: 690 +\n Number of leaf pages: 625 +\n Number of tuples: 7873 +\n Number of invalid tuples: 0 +\n Number of leaf tuples: 7184 +\n Total size of tuples: 354692 bytes +\n Total size of leaf tuples: 323596 bytes +\n Total size of index: 5652480 bytes+\n\n(1 row)\n<\/code><\/pre>\n
select * from gist_tree('airports_coordinates_idx');\n<\/code><\/pre>\n
gist_tree \n-----------------------------------------------------------------------------------------\n 0(l:0) blk: 0 numTuple: 5 free: 7928b(2.84%) rightlink:4294967295 (InvalidBlockNumber) +\n 1(l:1) blk: 335 numTuple: 15 free: 7488b(8.24%) rightlink:220 (OK) +\n 1(l:2) blk: 128 numTuple: 9 free: 7752b(5.00%) rightlink:49 (OK) +\n 1(l:3) blk: 57 numTuple: 12 free: 7620b(6.62%) rightlink:35 (OK) +\n 2(l:3) blk: 62 numTuple: 9 free: 7752b(5.00%) rightlink:57 (OK) +\n 3(l:3) blk: 72 numTuple: 7 free: 7840b(3.92%) rightlink:23 (OK) +\n 4(l:3) blk: 115 numTuple: 17 free: 7400b(9.31%) rightlink:33 (OK) +\n ...\n<\/code><\/pre>\n
select level, a from gist_print('airports_coordinates_idx')\n as t(level int, valid bool, a box) where level = 1;\n<\/code><\/pre>\n
level | a \n-------+-----------------------------------------------------------------------\n 1 | (47.663586,80.803207),(-39.2938003540039,-90)\n 1 | (179.951004028,15.6700000762939),(15.2428998947144,-77.9634017944336)\n 1 | (177.740997314453,73.5178070068359),(15.0664,10.57970047)\n 1 | (-77.3191986083984,79.9946975708),(-179.876998901,-43.810001373291)\n 1 | (-39.864200592041,82.5177993774),(-81.254096984863,-64.2382965088)\n(5 rows)\n<\/code><\/pre>\n
2.3 \u641c\u7d22\u64cd\u4f5c\u7b26\u548c\u6392\u5e8f\u64cd\u4f5c\u7b26<\/h2>\n
select * from points order by p <-> point '(4,7)' limit 2;\n<\/code><\/pre>\n
p \n-------\n (5,5)\n (7,8)\n(2 rows)\n<\/code><\/pre>\n
select amop.amopopr::regoperator, amop.amoppurpose, amop.amopstrategy\nfrom pg_opclass opc, pg_opfamily opf, pg_am am, pg_amop amop\nwhere opc.opcname = 'point_ops'\nand opf.oid = opc.opcfamily\nand am.oid = opf.opfmethod\nand amop.amopfamily = opc.opcfamily\nand am.amname = 'gist'\nand amop.amoplefttype = opc.opcintype;\n<\/code><\/pre>\n
amopopr | amoppurpose | amopstrategy\n-------------------+-------------+--------------\n <<(point,point) | s | 1 strictly left\n >>(point,point) | s | 5 strictly right\n ~=(point,point) | s | 6 coincides\n <^(point,point) | s | 10 strictly below\n >^(point,point) | s | 11 strictly above\n <->(point,point) | o | 15 distance\n <@(point,box) | s | 28 contained in rectangle\n <@(point,polygon) | s | 48 contained in polygon\n <@(point,circle) | s | 68 contained in circle\n(9 rows)\n<\/code><\/pre>\n
select * from points order by p <-> point '(4,7)' limit 3;\n<\/code><\/pre>\n
p \n-------\n (5,5)\n (7,8)\n (8,6)\n(3 rows)\n<\/code><\/pre>\n
3. \u7528R-tree\u7d22\u5f15\u533a\u95f4<\/h1>\n
3.1 \u793a\u4f8b<\/h2>\n
create table reservations(during tsrange);\ninsert into reservations(during) values\n('[2016-12-30, 2017-01-09)'),\n('[2017-02-23, 2017-02-27)'),\n('[2017-04-29, 2017-05-02)');\ncreate index on reservations using gist(during);\n<\/code><\/pre>\n
select * from reservations where during && '[2017-01-01, 2017-04-01)';\n<\/code><\/pre>\n
during \n-----------------------------------------------\n [\"2016-12-30 00:00:00\",\"2017-01-08 00:00:00\")\n [\"2017-02-23 00:00:00\",\"2017-02-26 00:00:00\")\n(2 rows)\n<\/code><\/pre>\n
explain (costs off) select * from reservations where during && '[2017-01-01, 2017-04-01)';\n<\/code><\/pre>\n
QUERY PLAN \n------------------------------------------------------------------------------------\n Index Only Scan using reservations_during_idx on reservations\n Index Cond: (during && '[\"2017-01-01 00:00:00\",\"2017-04-01 00:00:00\")'::tsrange)\n(2 rows)\n<\/code><\/pre>\n
select amop.amopopr::regoperator, amop.amoppurpose, amop.amopstrategy\nfrom pg_opclass opc, pg_opfamily opf, pg_am am, pg_amop amop\nwhere opc.opcname = 'range_ops'\nand opf.oid = opc.opcfamily\nand am.oid = opf.opfmethod\nand amop.amopfamily = opc.opcfamily\nand am.amname = 'gist'\nand amop.amoplefttype = opc.opcintype;\n<\/code><\/pre>\n
amopopr | amoppurpose | amopstrategy\n-------------------------+-------------+-------------- \n @>(anyrange,anyelement) | s | 16 contains element\n <<(anyrange,anyrange) | s | 1 strictly left\n &<(anyrange,anyrange) | s | 2 not beyond right boundary\n &&(anyrange,anyrange) | s | 3 intersects\n &>(anyrange,anyrange) | s | 4 not beyond left boundary\n >>(anyrange,anyrange) | s | 5 strictly right\n -|-(anyrange,anyrange) | s | 6 adjacent\n @>(anyrange,anyrange) | s | 7 contains interval\n <@(anyrange,anyrange) | s | 8 contained in interval\n =(anyrange,anyrange) | s | 18 equals\n(10 rows)\n<\/code><\/pre>\n
3.2 \u5185\u90e8\u7ed3\u6784<\/h2>\n
select level, a from gist_print('reservations_during_idx')\nas t(level int, valid bool, a tsrange);\n<\/code><\/pre>\n
level | a \n-------+-----------------------------------------------\n 1 | [\"2016-12-30 00:00:00\",\"2017-01-09 00:00:00\")\n 1 | [\"2017-02-23 00:00:00\",\"2017-02-27 00:00:00\")\n 1 | [\"2017-04-29 00:00:00\",\"2017-05-02 00:00:00\")\n(3 rows)\n<\/code><\/pre>\n
3.3 \u6392\u5b83\u7ea6\u675f<\/h2>\n
\n
\n
\n
\n
3.3.1 \u793a\u4f8b<\/h3>\n
alter table reservations add exclude using gist(during with &&);\ninsert into reservations(during) values ('[2017-06-10, 2017-06-13)');\n<\/code><\/pre>\n
insert into reservations(during) values ('[2017-05-15, 2017-06-15)');\n<\/code><\/pre>\n
ERROR: conflicting key value violates exclusion constraint \"reservations_during_excl\"\nDETAIL: Key (during)=([\"2017-05-15 00:00:00\",\"2017-06-15 00:00:00\")) conflicts with existing key (during)=([\"2017-06-10 00:00:00\",\"2017-06-13 00:00:00\")).\n<\/code><\/pre>\n
3.3.2 btree_gist\u63d2\u4ef6<\/h3>\n
alter table reservations add house_no integer default 1;\n<\/code><\/pre>\n
alter table reservations drop constraint reservations_during_excl;\nalter table reservations add exclude using gist(during with &&, house_no with =);\n<\/code><\/pre>\n
ERROR: data type integer has no default operator class for access method \"gist\"\nHINT: You must specify an operator class for the index or define a default operator class for the data type.\n<\/code><\/pre>\n
create extension btree_gist;\nalter table reservations add exclude using gist(during with &&, house_no with =);\n<\/code><\/pre>\n
insert into reservations(during, house_no) values ('[2017-05-15, 2017-06-15)', 1);\n<\/code><\/pre>\n
ERROR: conflicting key value violates exclusion constraint \"reservations_during_house_no_excl\"\n<\/code><\/pre>\n
insert into reservations(during, house_no) values ('[2017-05-15, 2017-06-15)', 2);\n<\/code><\/pre>\n
4. \u4f7f\u7528RD-tree\u8fdb\u884c\u5168\u6587\u68c0\u7d22<\/h1>\n
4.1 \u5168\u6587\u68c0\u7d22\u7b80\u4ecb<\/h2>\n
select to_tsvector('There was a crooked man, and he walked a crooked mile');\n<\/code><\/pre>\n
to_tsvector \n-----------------------------------------\n 'crook':4,10 'man':5 'mile':11 'walk':8\n(1 row)\n<\/code><\/pre>\n
select to_tsquery('man & (walking | running)');\n<\/code><\/pre>\n
to_tsquery \n----------------------------\n 'man' & ( 'walk' | 'run' )\n(1 row)\n<\/code><\/pre>\n
select to_tsvector('There was a crooked man, and he walked a crooked mile') @@ to_tsquery('man & (walking | running)');\n<\/code><\/pre>\n
?column?\n----------\n t\n(1 row)\n<\/code><\/pre>\n
select to_tsvector('There was a crooked man, and he walked a crooked mile') @@ to_tsquery('man & (going | running)');\n<\/code><\/pre>\n
?column?\n----------\n f\n(1 row)\n<\/code><\/pre>\n
4.2 RD-trees<\/h2>\n
create table ts(doc text, doc_tsv tsvector);\ncreate index on ts using gist(doc_tsv);\ninsert into ts(doc) values\n ('Can a sheet slitter slit sheets?'), \n ('How many sheets could a sheet slitter slit?'),\n ('I slit a sheet, a sheet I slit.'),\n ('Upon a slitted sheet I sit.'), \n ('Whoever slit the sheets is a good sheet slitter.'), \n ('I am a sheet slitter.'),\n ('I slit sheets.'),\n ('I am the sleekest sheet slitter that ever slit sheets.'),\n ('She slits the sheet she sits on.');\nupdate ts set doc_tsv = to_tsvector(doc);\n<\/code><\/pre>\n
select * from ts;\n<\/code><\/pre>\n
-[ RECORD 1 ]----------------------------------------------------\ndoc | Can a sheet slitter slit sheets?\ndoc_tsv | 'sheet':3,6 'slit':5 'slitter':4\n-[ RECORD 2 ]----------------------------------------------------\ndoc | How many sheets could a sheet slitter slit?\ndoc_tsv | 'could':4 'mani':2 'sheet':3,6 'slit':8 'slitter':7\n-[ RECORD 3 ]----------------------------------------------------\ndoc | I slit a sheet, a sheet I slit.\ndoc_tsv | 'sheet':4,6 'slit':2,8\n-[ RECORD 4 ]----------------------------------------------------\ndoc | Upon a slitted sheet I sit.\ndoc_tsv | 'sheet':4 'sit':6 'slit':3 'upon':1\n-[ RECORD 5 ]----------------------------------------------------\ndoc | Whoever slit the sheets is a good sheet slitter.\ndoc_tsv | 'good':7 'sheet':4,8 'slit':2 'slitter':9 'whoever':1\n-[ RECORD 6 ]----------------------------------------------------\ndoc | I am a sheet slitter.\ndoc_tsv | 'sheet':4 'slitter':5\n-[ RECORD 7 ]----------------------------------------------------\ndoc | I slit sheets.\ndoc_tsv | 'sheet':3 'slit':2\n-[ RECORD 8 ]----------------------------------------------------\ndoc | I am the sleekest sheet slitter that ever slit sheets.\ndoc_tsv | 'ever':8 'sheet':5,10 'sleekest':4 'slit':9 'slitter':6\n-[ RECORD 9 ]----------------------------------------------------\ndoc | She slits the sheet she sits on.\ndoc_tsv | 'sheet':4 'sit':6 'slit':2\n<\/code><\/pre>\n
could 1000000\never 0001000\ngood 0000010\nmani 0000100\nsheet 0000100\nsleekest 0100000\nsit 0010000\nslit 0001000\nslitter 0000001\nupon 0000010\nwhoever 0010000\n<\/code><\/pre>\n
Can a sheet slitter slit sheets? 0001101\nHow many sheets could a sheet slitter slit? 1001101\nI slit a sheet, a sheet I slit. 0001100\nUpon a slitted sheet I sit. 0011110\nWhoever slit the sheets is a good sheet slitter. 0011111\nI am a sheet slitter. 0000101\nI slit sheets. 0001100\nI am the sleekest sheet slitter that ever slit sheets. 0101101\nShe slits the sheet she sits on. 0011100\n<\/code><\/pre>\n