Sub-selects.md.json 14 KB

1234567891011121314151617
  1. {
  2. "d1f0a8bedc0db44ab6a61e3ca31f30c1e22872100783bbf19f4606ec5bcdf6c0": {
  3. "original": "# Sub-selects\n\nManticore supports SELECT subqueries via SQL in the following format:\n\nCODE_BLOCK_0\n\nThe outer select allows only `ORDER BY` and `LIMIT` clauses. Sub-select queries currently have two use cases:\n\n1. When you have a query with two ranking UDFs, one very fast and the other slow, and perform a full-text search with a large match result set. Without subselect, the query would look like:\n\nCODE_BLOCK_1\n\n With sub-selects, the query can be rewritten as:\n\nCODE_BLOCK_2\n\n In the initial query, the `slow_rank()` UDF is computed for the entire match result set. With SELECT sub-queries, only `fast_rank()` is computed for the entire match result set, while `slow_rank()` is computed for a limited set.\n\n2. The second case is useful for large result sets coming from a distributed table.\n\n For this query:\n\nCODE_BLOCK_3\n\n If you have 20 nodes, each node can send back to the master a maximum of 50K records, resulting in 20 x 50K = 1M records. However, since the master sends back only 50K (out of 1M), it might be good enough for the nodes to send only the top 10K records. With sub-select, you can rewrite the query as:\n\nCODE_BLOCK_4\n\n In this case, the nodes receive only the inner query and execute it. This means the master will receive only 20x10K=200K records. The master will take all the records received, reorder them by the OUTER clause, and return the best 50K records. The sub-select helps reduce the traffic between the master and the nodes, as well as reduce the master's computation time (since it processes only 200K instead of 1M records).\n<!-- proofread -->\n\n",
  4. "translations": {
  5. "chinese": "# \u5b50\u67e5\u8be2\n\nManticore \u901a\u8fc7\u4ee5\u4e0b\u683c\u5f0f\u652f\u6301 SQL \u4e2d\u7684 SELECT \u5b50\u67e5\u8be2\uff1a\n\nCODE_BLOCK_0\n\n\u5916\u5c42 select \u53ea\u5141\u8bb8\u5305\u542b `ORDER BY` \u548c `LIMIT` \u5b50\u53e5\u3002\u5b50\u67e5\u8be2\u76ee\u524d\u6709\u4e24\u4e2a\u4f7f\u7528\u573a\u666f\uff1a\n\n1. \u5f53\u4f60\u6709\u4e00\u4e2a\u67e5\u8be2\u5305\u542b\u4e24\u4e2a\u6392\u5e8f UDF\uff0c\u4e00\u4e2a\u975e\u5e38\u5feb\uff0c\u53e6\u4e00\u4e2a\u8f83\u6162\uff0c\u5e76\u4e14\u6267\u884c\u5168\u6587\u641c\u7d22\u65f6\u5339\u914d\u7ed3\u679c\u96c6\u5f88\u5927\u3002\u6ca1\u6709\u5b50\u67e5\u8be2\uff0c\u67e5\u8be2\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a\n\nCODE_BLOCK_1\n\n \u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u67e5\u8be2\u53ef\u4ee5\u91cd\u5199\u4e3a\uff1a\n\nCODE_BLOCK_2\n\n \u5728\u521d\u59cb\u67e5\u8be2\u4e2d\uff0c`slow_rank()` UDF \u4f1a\u5bf9\u6574\u4e2a\u5339\u914d\u7ed3\u679c\u96c6\u8ba1\u7b97\u3002\u4f7f\u7528 SELECT \u5b50\u67e5\u8be2\uff0c\u4ec5\u5bf9\u6574\u4e2a\u5339\u914d\u7ed3\u679c\u96c6\u8ba1\u7b97 `fast_rank()`\uff0c\u800c\u53ea\u5bf9\u6709\u9650\u96c6\u5408\u8ba1\u7b97 `slow_rank()`\u3002\n\n2. \u7b2c\u4e8c\u79cd\u60c5\u51b5\u9002\u7528\u4e8e\u6765\u81ea\u5206\u5e03\u5f0f\u8868\u7684\u5927\u578b\u7ed3\u679c\u96c6\u3002\n\n \u5bf9\u4e8e\u6b64\u67e5\u8be2\uff1a\n\nCODE_BLOCK_3\n\n \u5982\u679c\u4f60\u6709 20 \u4e2a\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u53ef\u4ee5\u8fd4\u56de 50K \u6761\u8bb0\u5f55\u7ed9\u4e3b\u8282\u70b9\uff0c\u7ed3\u679c\u662f 20 x 50K = 100\u4e07\u6761\u8bb0\u5f55\u3002\u7136\u800c\uff0c\u7531\u4e8e\u4e3b\u8282\u70b9\u53ea\u8fd4\u56de 50K \u6761\uff08\u4ece 100\u4e07\u6761\u4e2d\uff09\uff0c\u8282\u70b9\u53ea\u53d1\u9001\u524d 10K \u6761\u8bb0\u5f55\u53ef\u80fd\u5c31\u8db3\u591f\u4e86\u3002\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u4f60\u53ef\u4ee5\u5c06\u67e5\u8be2\u91cd\u5199\u4e3a\uff1a\n\nCODE_BLOCK_4\n\n \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8282\u70b9\u4ec5\u63a5\u6536\u5185\u90e8\u67e5\u8be2\u5e76\u6267\u884c\u5b83\u3002\u8fd9\u610f\u5473\u7740\u4e3b\u8282\u70b9\u5c06\u4ec5\u6536\u5230 20x10K=20\u4e07 \u6761\u8bb0\u5f55\u3002\u4e3b\u8282\u70b9\u4f1a\u5bf9\u63a5\u6536\u5230\u7684\u6240\u6709\u8bb0\u5f55\u6309\u5916\u5c42\u5b50\u53e5\u91cd\u65b0\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u6700\u4f73\u7684 50K \u6761\u8bb0\u5f55\u3002\u5b50\u67e5\u8be2\u6709\u52a9\u4e8e\u51cf\u5c11\u4e3b\u8282\u70b9\u548c\u8282\u70b9\u4e4b\u95f4\u7684\u6d41\u91cf\uff0c\u540c\u65f6\u51cf\u5c11\u4e3b\u8282\u70b9\u7684\u8ba1\u7b97\u65f6\u95f4\uff08\u56e0\u4e3a\u5b83\u53ea\u5904\u7406 20\u4e07 \u6761\u8bb0\u5f55\u800c\u975e 100\u4e07 \u6761\uff09\u3002 \n<!-- proofread -->",
  6. "russian": "# \u041f\u043e\u0434\u0432\u044b\u0431\u043e\u0440\u043a\u0438\n\nManticore \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u044b SELECT \u0432 SQL \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435:\n\nCODE_BLOCK_0\n\n\u0412\u043d\u0435\u0448\u043d\u0438\u0439 SELECT \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 `ORDER BY` \u0438 `LIMIT`. \u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u0434\u0432\u0443\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445:\n\n1. \u041a\u043e\u0433\u0434\u0430 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0434\u0432\u0443\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0440\u0430\u043d\u0436\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f UDF, \u043e\u0434\u043d\u0430 \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u0430\u044f, \u0434\u0440\u0443\u0433\u0430\u044f \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0430\u044f, \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439. \u0411\u0435\u0437 \u043f\u043e\u0434\u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0431\u044b \u0442\u0430\u043a:\n\nCODE_BLOCK_1\n\n \u0421 \u043f\u043e\u0434\u0432\u044b\u0431\u043e\u0440\u043a\u0430\u043c\u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:\n\nCODE_BLOCK_2\n\n \u0412 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 UDF `slow_rank()` \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 SELECT \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e `fast_rank()`, \u0430 `slow_rank()` \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430.\n\n2. \u0412\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043d\u0430\u0431\u043e\u0440\u043e\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432, \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0438\u0445 \u0438\u0437 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.\n\n \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430:\n\nCODE_BLOCK_3\n\n \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 20 \u0443\u0437\u043b\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043c\u0430\u0441\u0442\u0435\u0440\u0443 \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c 50K \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0447\u0442\u043e \u0432 \u0441\u0443\u043c\u043c\u0435 \u0434\u0430\u0435\u0442 20 x 50K = 1M \u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0430\u0441\u0442\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e 50K (\u0438\u0437 1M), \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0437\u043b\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u043f 10K \u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u0421 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0442\u0430\u043a:\n\nCODE_BLOCK_4\n\n \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0437\u043b\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u0435\u0433\u043e. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u0430\u0441\u0442\u0435\u0440 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e 20x10K=200K \u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u041c\u0430\u0441\u0442\u0435\u0440 \u0432\u043e\u0437\u044c\u043c\u0435\u0442 \u0432\u0441\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u043f\u0435\u0440\u0435\u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0442 \u0438\u0445 \u043f\u043e \u0412\u041d\u0415\u0428\u041d\u0415\u041c\u0423 \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0438 \u0432\u0435\u0440\u043d\u0435\u0442 \u043b\u0443\u0447\u0448\u0438\u0435 50K \u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u041f\u043e\u0434\u0432\u044b\u0431\u043e\u0440\u043a\u0430 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u0442\u0440\u0430\u0444\u0438\u043a \u043c\u0435\u0436\u0434\u0443 \u043c\u0430\u0441\u0442\u0435\u0440\u043e\u043c \u0438 \u0443\u0437\u043b\u0430\u043c\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u0430\u0441\u0442\u0435\u0440\u0430 (\u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e 200K \u0432\u043c\u0435\u0441\u0442\u043e 1M \u0437\u0430\u043f\u0438\u0441\u0435\u0439).\n<!-- proofread -->\n\n"
  7. },
  8. "is_code_or_comment": false,
  9. "model": "openai:gpt-4.1-mini",
  10. "updated_at": 1766339781
  11. },
  12. "__meta": {
  13. "source_text": "# Sub-selects\n\nManticore supports SELECT subqueries via SQL in the following format:\n\n```sql\nSELECT * FROM (SELECT ... ORDER BY cond1 LIMIT X) ORDER BY cond2 LIMIT Y\n```\n\nThe outer select allows only `ORDER BY` and `LIMIT` clauses. Sub-select queries currently have two use cases:\n\n1. When you have a query with two ranking UDFs, one very fast and the other slow, and perform a full-text search with a large match result set. Without subselect, the query would look like:\n\n ```sql\n SELECT id,slow_rank() as slow,fast_rank() as fast FROM index\n WHERE MATCH(\u2018some common query terms\u2019) ORDER BY fast DESC, slow DESC LIMIT 20\n OPTION max_matches=1000;\n ```\n\n With sub-selects, the query can be rewritten as:\n\n ```sql\n SELECT * FROM\n (SELECT id,slow_rank() as slow,fast_rank() as fast FROM index WHERE\n MATCH(\u2018some common query terms\u2019)\n ORDER BY fast DESC LIMIT 100 OPTION max_matches=1000)\n ORDER BY slow DESC LIMIT 20;\n ```\n\n In the initial query, the `slow_rank()` UDF is computed for the entire match result set. With SELECT sub-queries, only `fast_rank()` is computed for the entire match result set, while `slow_rank()` is computed for a limited set.\n\n2. The second case is useful for large result sets coming from a distributed table.\n\n For this query:\n\n ```sql\n SELECT * FROM my_dist_index WHERE some_conditions LIMIT 50000;\n ```\n\n If you have 20 nodes, each node can send back to the master a maximum of 50K records, resulting in 20 x 50K = 1M records. However, since the master sends back only 50K (out of 1M), it might be good enough for the nodes to send only the top 10K records. With sub-select, you can rewrite the query as:\n\n ```sql\n SELECT * FROM\n (SELECT * FROM my_dist_index WHERE some_conditions LIMIT 10000)\n ORDER by some_attr LIMIT 50000;\n ```\n\n In this case, the nodes receive only the inner query and execute it. This means the master will receive only 20x10K=200K records. The master will take all the records received, reorder them by the OUTER clause, and return the best 50K records. The sub-select helps reduce the traffic between the master and the nodes, as well as reduce the master's computation time (since it processes only 200K instead of 1M records).\n<!-- proofread -->\n\n",
  14. "updated_at": 1768530797,
  15. "source_md5": "10ddf90ecde0988b416b8782c42308a1"
  16. }
  17. }