{"id":3278,"date":"2018-03-07T19:27:32","date_gmt":"2018-03-08T00:27:32","guid":{"rendered":"https:\/\/antipaucity.com\/?p=3278"},"modified":"2018-03-07T19:27:32","modified_gmt":"2018-03-08T00:27:32","slug":"splunk-oddity-17681-stats-vs-table","status":"publish","type":"post","link":"https:\/\/merikebi.warrenmyers.com\/?p=3278","title":{"rendered":"splunk oddity #17681 &#8211; stats vs table"},"content":{"rendered":"<p>It&#8217;s fairly common to want to <a href=\"http:\/\/docs.splunk.com\/Documentation\/Splunk\/7.0.2\/SearchReference\/Table\">table<\/a> the data you&#8217;ve found in a search in {{Splunk}} &#8211; heck, if you&#8217;re not prettying the data up <em>somewhy<\/em>, why are you bothering with the tool?<\/p>\n<p>But I digress.<\/p>\n<p>There are two (at least) ways of making a table &#8211; you can use the <code>|table &lt;field(s)&gt;<\/code> syntax, or you can use <code>|stats &lt;some function&gt; &lt;field(s)&gt;<\/code> approach.<\/p>\n<p>Interestingly, in my testing in both test and production environments, using the <code>|stats...<\/code> approach is <em>consistently<\/em> 10-15% <strong>faster<\/strong> than the <code>|table...<\/code> option.<\/p>\n<p>Why? I don&#8217;t know. He&#8217;s on third. And I don&#8217;t give a darn!<\/p>\n<p>This is another case of <a href=\"https:\/\/antipaucity.com\/2011\/08\/08\/why-technical-intricacies-matter\/#.WqCAqIJOngo\">technical intricacies mattering<\/a> &#8230; but I don&#8217;t know what is going on under the hood that makes the apparently-more-complex option run faster than the apparently-simler option.<\/p>\n<p>Maybe someday someone in Splunk engineering will be able to enlighten me to that.<\/p>\n<p>This reminds me a bit of an optimization I was able to help a friend with upwards of 12 years ago &#8211; they had queries running in {{MySQL}} that were taking <em>forever<\/em> to complete (and by &#8220;forever&#8221;, I mean they were running sometimes 4-5 times a long as the interval between running them (they ran every 5 mintues, but could take 20+ minutes to finish!)).<\/p>\n<p>What I found, at least back in the dark days of MySQL 3.x was that using <code>IN(...)<\/code> was loads faster than using <code>OR<\/code> statements.<\/p>\n<p>So a query that had a clause <code>WHERE name IN(\"bob\",\"sarah\",\"mike\",\"terry\",\"sue\") would run anywhere from 20-90% quicker than the logically-equivalent <\/code><code>WHERE name=\"bob\" OR name=\"sarah\" OR name=\"mike\" OR name=\"terry\" OR name=\"sue\"<\/code> (given a large enough dataset overwhich it was running &#8230; on small [enough] tables (say up to a couplefew thousand records), the <code>OR<\/code> version would run equally, or occasionally faster).<\/p>\n<p>In <em>their<\/em> case, by switching to the <code>IN(...)<\/code> form, queries went from taking 20+ minutes to finishing in ~20 <code>seconds<\/code>! <\/p>\n<h4>Bonus tidbit:<\/h4>\n<p>It is well-known in Splunkland that using <code>dedup<\/code> is an &#8220;expensive&#8221; operation. Want a clever way around it (that is <em>much<\/em> faster)? Instead of doing something like <code>index=myndx | fields ip host | dedup host<\/code>, run <code>index=myndx | fields ip host | stats count by host | fields -count<\/code>. The <code>|stats .. |fields -count<\/code> trick seems to run anywhere from 15-30% faster than <code>dedup<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s fairly common to want to table the data you&#8217;ve found in a search in {{Splunk}} &#8211; heck, if you&#8217;re not prettying the data up somewhy, why are you bothering with the tool? But I digress. There are two (at least) ways of making a table &#8211; you can use the |table &lt;field(s)&gt; syntax, or &hellip;<br \/><a href=\"https:\/\/merikebi.warrenmyers.com\/?p=3278\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\">Continue reading <span class=\"screen-reader-text\">splunk oddity #17681 &#8211; stats vs table<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[19,41],"tags":[535,705],"keyring_services":[],"class_list":["post-3278","post","type-post","status-publish","format-standard","hentry","category-hmmm","category-technical","tag-optimization","tag-splunk"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/3278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3278"}],"version-history":[{"count":0,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/3278\/revisions"}],"wp:attachment":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3278"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fkeyring_services&post=3278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}