{"id":4596,"date":"2012-02-08T11:43:19","date_gmt":"2012-02-08T09:43:19","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=4596"},"modified":"2012-02-08T11:43:19","modified_gmt":"2012-02-08T09:43:19","slug":"queryscript-sql-scripting-language","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/queryscript-sql-scripting-language","title":{"rendered":"QueryScript: SQL scripting language"},"content":{"rendered":"<p>Introducing <strong><a href=\"http:\/\/www.queryscript.com\/\">QueryScript<\/a><\/strong>: a programming language aimed for SQL scripting, seamlessly combining scripting power such as flow control &amp; variables with standard SQL statements or RDBMS-specific commands.<\/p>\n<p>QueryScript is available fro MySQL via <a href=\"http:\/\/code.google.com\/p\/common-schema\">common_schema<\/a>, which adds MySQL-specific usage.<\/p>\n<p><em>What does QueryScript look like?<\/em> Here are a few code samples:<\/p>\n<p>Turn a bulk DELETE operation into smaller tasks. Throttle in between.<\/p>\n<blockquote>\n<pre>while (DELETE FROM archive.events WHERE ts &lt; CURDATE() LIMIT 1000)\r\n{\r\n\u00a0 throttle 2;\r\n}<\/pre>\n<\/blockquote>\n<p>Convert all InnoDB tables in the &#8216;sakila&#8217; database to compressed format:<\/p>\n<blockquote>\n<pre>foreach ($table, $schema, $engine: table in sakila)\r\n{\r\n  if ($engine = 'InnoDB')\r\n    ALTER TABLE :$schema.:$table ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;\r\n}<\/pre>\n<\/blockquote>\n<p>Shard your data across multiple schemata:<\/p>\n<blockquote>\n<pre>foreach($shard: {USA, GBR, JAP, FRA})\r\n{\r\n  CREATE DATABASE db_:$shard;\r\n  CREATE TABLE db_:$shard.city LIKE world.City;\r\n  INSERT INTO db_:$shard.city SELECT * FROM world.City WHERE CountryCode = $shard;\r\n}<\/pre>\n<\/blockquote>\n<p><!--more-->This tight integration between script and SQL, with the power of iteration, conditional statements, variables, variable expansion, throttling etc., makes QueryScript a power tool, with capabilities superseding those of stored routines, and allowing for simplified, dynamic code.<\/p>\n<p>QueryScript code is interpreted. It&#8217;s just a text, so it can be read from a @user_defined_variable, a table column, text file, what have you. For example:<\/p>\n<blockquote>\n<pre>mysql&gt; <strong>set<\/strong> @script := \"while (TIME(SYSDATE()) &lt; '17:00:00') SELECT * FROM world.City WHERE id = 1 + FLOOR((RAND()*4079));\";\r\nmysql&gt; <strong>call<\/strong> run(@script);<\/pre>\n<\/blockquote>\n<p>For more details, consult the <strong><a href=\"http:\/\/www.queryscript.com\/\">QueryScript<\/a><\/strong> site.<\/p>\n<p>If you&#8217;re a MySQL user\/DBA, better read the <a href=\"http:\/\/common-schema.googlecode.com\/svn\/trunk\/common_schema\/doc\/html\/query_script.html\"><strong>common_schema QueryScript documentation<\/strong><\/a>, to better understand the specific <em>common_schema<\/em> implementation and enhanced features.<\/p>\n<p><em>common_schema<\/em>, including the QueryScript interpreter, can be downloaded from the <a href=\"http:\/\/code.google.com\/p\/common-schema\">common_schema project page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introducing QueryScript: a programming language aimed for SQL scripting, seamlessly combining scripting power such as flow control &amp; variables with standard SQL statements or RDBMS-specific commands. QueryScript is available fro MySQL via common_schema, which adds MySQL-specific usage. What does QueryScript look like? Here are a few code samples: Turn a bulk DELETE operation into smaller [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[5],"tags":[79,67,120,57,76,50],"class_list":["post-4596","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-administration","tag-common_schema","tag-development","tag-open-source","tag-queryscript","tag-scripts"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-1c8","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=4596"}],"version-history":[{"count":27,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4596\/revisions"}],"predecessor-version":[{"id":4702,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4596\/revisions\/4702"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=4596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=4596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=4596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}