{"id":4898,"date":"2023-02-12T20:50:03","date_gmt":"2023-02-12T19:50:03","guid":{"rendered":"https:\/\/launix.de\/launix\/?p=4898"},"modified":"2023-07-11T08:10:08","modified_gmt":"2023-07-11T06:10:08","slug":"memcp-first-sql-query-is-correctly-executed","status":"publish","type":"post","link":"https:\/\/launix.de\/launix\/en\/memcp-first-sql-query-is-correctly-executed\/","title":{"rendered":"MemCP: First SQL Query is correctly executed"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">MemCP now executed its first SQL Query:<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre>MySQL [(none)]&gt; select 1+2 AS x;\n+------+\n| x    |\n+------+\n| 3    |\n+------+\n<b>1 row in set (0,000 sec)<\/b>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, MemCP uses the MySQL protocol to communicate with its clients. There is also a REST endpoint but the REST endpoint is more suited for custom REST APIs (microservices and such) that want to run inside the same memory space as the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SQL compilation in MemCP works in two stages: Parse and Build Queryplan.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">During the parse phase, a string called &#8220;SQL Query&#8221; is transformed into multiple lists:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>List of SELECT fields together with their column names and their expressions (an expression is a data structure that describes how to compute a value)<\/li>\n\n\n\n<li>List of FROM tables with their table aliases as well as their table names (or subqueries)<\/li>\n\n\n\n<li>WHERE, GROUP, HAVING, ORDER BY, LIMIT clauses and so on<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Then, there is a plan to flatten down any subqueries into a single FROM clause (according to this paper: <a href=\"https:\/\/cs.emis.de\/LNI\/Proceedings\/Proceedings241\/383.pdf\" target=\"_blank\" aria-label=\"https:\/\/cs.emis.de\/LNI\/Proceedings\/Proceedings241\/383.pdf (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">https:\/\/cs.emis.de\/LNI\/Proceedings\/Proceedings241\/383.pdf<\/a>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After that, the function <code>build_queryplan<\/code> decides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The join order<\/li>\n\n\n\n<li>When to calculate what<\/li>\n\n\n\n<li>Which conditions have to be evaluated when<\/li>\n\n\n\n<li>The exact nesting of <code>(scan)<\/code> calls (see <a href=\"https:\/\/launix.de\/launix\/accessing-memcp-via-scheme\/\" class=\"ek-link\">https:\/\/launix.de\/launix\/accessing-memcp-via-scheme\/<\/a>)<\/li>\n\n\n\n<li>The innermost loop will contain a call to <code>(resultrow)<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">After that is done, the query can be executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Result lines are provided as records that are passed through the MySQL protocol or in case of the REST API printed as a stream of JSON lines.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MemCP now executed its first SQL Query:<\/p>","protected":false},"author":2,"featured_media":4842,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":1,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","footnotes":""},"categories":[129],"tags":[],"class_list":["post-4898","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-memcp","single-item"],"uagb_featured_image_src":{"full":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo.png",256,216,false],"thumbnail":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo-150x150.png",150,150,true],"medium":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo.png",256,216,false],"medium_large":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo.png",256,216,false],"large":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo.png",256,216,false],"1536x1536":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo.png",256,216,false],"2048x2048":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo.png",256,216,false],"trp-custom-language-flag":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo.png",14,12,false],"xs-thumb":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo-64x64.png",64,64,true],"appku-shop-single":["https:\/\/launix.de\/launix\/wp-content\/uploads\/2023\/01\/memcp-logo.png",256,216,false]},"uagb_author_info":{"display_name":"Carl-Philip H\u00e4nsch","author_link":"https:\/\/launix.de\/launix\/en\/author\/carli\/"},"uagb_comment_info":0,"uagb_excerpt":"MemCP now executed its first SQL Query:","_links":{"self":[{"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/posts\/4898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/comments?post=4898"}],"version-history":[{"count":2,"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/posts\/4898\/revisions"}],"predecessor-version":[{"id":4900,"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/posts\/4898\/revisions\/4900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/media\/4842"}],"wp:attachment":[{"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/media?parent=4898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/categories?post=4898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/launix.de\/launix\/en\/wp-json\/wp\/v2\/tags?post=4898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}