{"id":3280,"date":"2018-03-08T12:46:43","date_gmt":"2018-03-08T17:46:43","guid":{"rendered":"https:\/\/antipaucity.com\/?p=3280"},"modified":"2018-03-08T12:46:43","modified_gmt":"2018-03-08T17:46:43","slug":"more-thoughts-on-stats-vs-dedup-in-splunk","status":"publish","type":"post","link":"https:\/\/merikebi.warrenmyers.com\/?p=3280","title":{"rendered":"more thoughts on `|stats` vs `|dedup` in splunk"},"content":{"rendered":"<p><a href=\"https:\/\/antipaucity.com\/2018\/03\/07\/splunk-oddity-17681-stats-vs-table\/#.WqFxFoJOngo\">Yesterday<\/a> I wrote-up a neat little find in {{Splunk}} wherein running <code>stats count by ...<\/code> is\u00a0<em>substantially<\/em> faster than running <code>dedup ...<\/code>.<\/p>\n<p>After some further reflection over dinner, I figured out the major portion of why this is &#8211; and I feel a little dumb for not having thought of it before. (A coworker added some more context, but it&#8217;s a smaller reason of why one is faster then the other.)<\/p>\n<p>The major reason <code>stats count by...<\/code> is faster than <code>dedup ...<\/code> is that <code>stats<\/code> can hand-off the counting process to something else (though, even if it <em>doesn&#8217;t<\/em>, incrementing a hashtable entry by 1 every time you encounter an instance isn&#8217;t terribly computationally complex) and keep going.<\/p>\n<p>In contrast, <code>dedup<\/code> <strong>must<\/strong> compare every individual returned event&#8217;s field that matches what you&#8217;re trying to <code>dedup<\/code> to it&#8217;s growing list of unique entries for that field.<\/p>\n<p>In the particular case I was seeing yesterday, that means that <em>every single event<\/em> in the list of 4,000,000 events returned by the search has to be compared <em>one at a time<\/em> to a list (that I know is going to top out at about 11,000). To use <a href=\"https:\/\/www.khanacademy.org\/computing\/computer-science\/algorithms\/asymptotic-notation\/a\/big-o-notation\">Big-O<\/a> <a href=\"https:\/\/en.wikipedia.org\/wiki\/Big_O_notation\">Notation<\/a>, this is an <strong>O(n*m)<\/strong> operation (bordering on <em>O(n<sup>2<\/sup>)<\/em>)!<\/p>\n<p>That initial list of length <em>m<\/em> fills pretty quickly (it <em>is<\/em>, after all, only going to get to ~11,000 total entries (in this case)), but as it grows to its max, it gets progressively harder and hard to check whether or not the next event has already been dedup&#8217;d.<\/p>\n<p>At ~750,000 events returned (roughly 1\/5 my total), the list is unique field values was 98% complete &#8211; yet there were still ~3.2 <em>million<\/em> events left to go (to find just 2% more unique field values).<\/p>\n<p>Those last 3.2 million events <em>each<\/em> need to check against the list of &gt;10,500 entries &#8211; which means, roughly,\u00a016,8 <strong><em>billion<\/em><\/strong> comparisons still need to be made!<\/p>\n<blockquote><p>(Because linear searching finds what it&#8217;s looking for on average by the time it has traversed half the list. If the list is being created in a slighly more efficient manner (say a heap or [balanced] binary search tree), it will still take ~43 million comparisons (<code>3.2 million * log<sub>2<\/sub>(11,000)<\/code>).)<\/p><\/blockquote>\n<p>Compare this to the relative complexity of using <code>|stats count by ...<\/code> &#8211; it still has to run through all 4 million events, but all it is doing is adding one to the list for every value that shows up in that particular field &#8211; IOW, it &#8220;only&#8221; has to do a total of 4 million [simple] things (because it does need to look at every event returned). <code>dedup<\/code> <em>at a minimum<\/em> is going to do ~54 million comparison (and <em>probably<\/em> a <strong>lot<\/strong> more &#8211; given it doesn&#8217;t merely take 13x the time to run, but closer to 25x).<\/p>\n<p>The secondary contributing factor &#8211; important, but not as much a factor as what I covered above &#8211; is that <code>dedup<\/code> <em>must<\/em> process the whole event, whereas <code>stats<\/code> chucks everything that isn&#8217;t part of what it&#8217;s counting (so if an event is 1kb in size, <code>dedup<\/code> has to return the whole kb, while <code>stats<\/code> is only looking at maybe 1\/10 the total (if you include a coupld extra fields)).<\/p>\n<p>Another neat aspect of using <code>|stats<\/code> is that it creates a table for you &#8211; if you&#8217;re running <code>|dedup<\/code>, you <em>then<\/em> have to <code>|table ...<\/code> to get the fields you want displayed how you want.<\/p>\n<p>And adding <code>|table<\/code> adds to the run time.<\/p>\n<p>So there you have it &#8211; turns out those CompSci 201 classes <em>do<\/em> come in handy 18 years later ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday I wrote-up a neat little find in {{Splunk}} wherein running stats count by &#8230; is\u00a0substantially faster than running dedup &#8230;. After some further reflection over dinner, I figured out the major portion of why this is &#8211; and I feel a little dumb for not having thought of it before. (A coworker added some &hellip;<br \/><a href=\"https:\/\/merikebi.warrenmyers.com\/?p=3280\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\">Continue reading <span class=\"screen-reader-text\">more thoughts on `|stats` vs `|dedup` in splunk<\/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":[23,41],"tags":[705],"keyring_services":[],"class_list":["post-3280","post","type-post","status-publish","format-standard","hentry","category-insights","category-technical","tag-splunk"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/3280","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=3280"}],"version-history":[{"count":0,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/3280\/revisions"}],"wp:attachment":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3280"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fkeyring_services&post=3280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}