{"id":95,"date":"2009-12-04T16:25:33","date_gmt":"2009-12-04T22:25:33","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=95"},"modified":"2009-12-04T16:25:33","modified_gmt":"2009-12-04T22:25:33","slug":"on-unions-and-derivatives","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=95","title":{"rendered":"On unions and derivatives"},"content":{"rendered":"<p>On occasion I need to generate a number of rows. I don&#8217;t care about the contents, just how many rows there are. Maybe I want to create a table filled with a sequence of numbers. One common way to do this is to join a bunch of subqueries built out of unions:<\/p>\n<p><code><br \/>\nSET @count := 0;<br \/>\nINSERT INTO table SELECT @count := @count + 1<br \/>\nFROM (SELECT 1 UNION SELECT 2) a<br \/>\nJOIN (SELECT 1 UNION SELECT 2) b<br \/>\nJOIN (SELECT 1 UNION SELECT 2) c<\/code><br \/>\n&#8230;<\/p>\n<p>Of course, I could union more than two selects (e.g. <code>SELECT 1 UNION SELECT 2 UNION SELECT 3<\/code>). That makes each derived table longer, but reduces how many must be joined.<\/p>\n<p>Question: What&#8217;s the optimal size of each derived table?<\/p>\n<p>Defining &#8220;optimal&#8221;: since this is a one-off query, I don&#8217;t much care about performance (within reason), but I do care about how much typing I have to do. So I will favor &#8220;fewer characters&#8221; over &#8220;fewer joins&#8221;.<\/p>\n<p>Each derived table in the query takes up 15 * numSelects + 2 characters.<\/p>\n<p>To generate a given number of rows requires CEILING(LOG(numRows) \/ LOG(numSelects)) derived tables.<\/p>\n<p>If I want 1000 rows in the result, I&#8217;d need 10 derived tables of two rows each (320 characters), or 7 of three rows (329 characters), or 3 of ten rows (456 characters).<\/p>\n<p>For any given numRows, what&#8217;s the minimum of CEILING(LOG(numRows) \/ LOG(numSelects)) * (15 * numSelects + 2) ?<\/p>\n<p>The answer varies with numRows &#8211; 2, 3, or 4 are alternately best when generating less than 130 rows, but any higher than that and the best choice usually turns out to be 3.<\/p>\n<p><code>(SELECT 1 UNION SELECT 2 UNION SELECT 3)<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>On occasion I need to generate a number of rows. I don&#8217;t care about the contents, just how many rows there are. Maybe I want to create a table filled with a sequence of numbers. One common way to do this is to join a bunch of subqueries built out of unions: SET @count := [&hellip;]<\/p>\n","protected":false},"author":1,"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,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-mysql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-1x","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=95"}],"version-history":[{"count":0,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}