<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Search Optimization saved search using time vs where clause in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434175#M123939</link>
    <description>&lt;P&gt;I currently have a search, which takes 5 minutes to complete, I did not write the search query, and would like to see if I can optimize it. &lt;/P&gt;

&lt;P&gt;This includes a | where _time &amp;lt; now()-30, over a 7 day span.&lt;BR /&gt;&lt;BR /&gt;
I am a bit confused at this where clause. Is this searching for anything within the last 7 days, but older than "30"ms ago?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="car" OR index="keys" source=key.* | bucket _time span=1h | stats count by source, _time | timechart span=1h values(count) by source | where _time &amp;lt; now()-30 | rename key.* as * | table *
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Results:&lt;/STRONG&gt;&lt;BR /&gt;
Execution Time: 5 Minutes&lt;BR /&gt;
Events Searched: 115,096,419&lt;BR /&gt;
Events Found:  169 &lt;/P&gt;

&lt;P&gt;My optimization removes the where clause and adds  earliest=-7d latest=now()-30.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Optimized&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="car" OR index="keys" source=key.*  earliest=-7d latest=now()-30  | bucket _time span=1h | stats count by source, _time | timechart span=1h values(count) by source  | rename key.* as * | table *
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Results:&lt;/STRONG&gt;&lt;BR /&gt;
Execution Time: 1 Minute&lt;BR /&gt;
Events Searched: 17,152&lt;BR /&gt;
Events Found:  169 &lt;/P&gt;

&lt;P&gt;Although the 169 results are the same, this search changes and comparing exactly results will not work. &lt;BR /&gt;
Is this a good optimization? &lt;/P&gt;</description>
    <pubDate>Wed, 07 Aug 2019 00:38:23 GMT</pubDate>
    <dc:creator>wrussell12</dc:creator>
    <dc:date>2019-08-07T00:38:23Z</dc:date>
    <item>
      <title>Search Optimization saved search using time vs where clause</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434175#M123939</link>
      <description>&lt;P&gt;I currently have a search, which takes 5 minutes to complete, I did not write the search query, and would like to see if I can optimize it. &lt;/P&gt;

&lt;P&gt;This includes a | where _time &amp;lt; now()-30, over a 7 day span.&lt;BR /&gt;&lt;BR /&gt;
I am a bit confused at this where clause. Is this searching for anything within the last 7 days, but older than "30"ms ago?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="car" OR index="keys" source=key.* | bucket _time span=1h | stats count by source, _time | timechart span=1h values(count) by source | where _time &amp;lt; now()-30 | rename key.* as * | table *
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Results:&lt;/STRONG&gt;&lt;BR /&gt;
Execution Time: 5 Minutes&lt;BR /&gt;
Events Searched: 115,096,419&lt;BR /&gt;
Events Found:  169 &lt;/P&gt;

&lt;P&gt;My optimization removes the where clause and adds  earliest=-7d latest=now()-30.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Optimized&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="car" OR index="keys" source=key.*  earliest=-7d latest=now()-30  | bucket _time span=1h | stats count by source, _time | timechart span=1h values(count) by source  | rename key.* as * | table *
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Results:&lt;/STRONG&gt;&lt;BR /&gt;
Execution Time: 1 Minute&lt;BR /&gt;
Events Searched: 17,152&lt;BR /&gt;
Events Found:  169 &lt;/P&gt;

&lt;P&gt;Although the 169 results are the same, this search changes and comparing exactly results will not work. &lt;BR /&gt;
Is this a good optimization? &lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2019 00:38:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434175#M123939</guid>
      <dc:creator>wrussell12</dc:creator>
      <dc:date>2019-08-07T00:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Search Optimization saved search using time vs where clause</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434176#M123940</link>
      <description>&lt;P&gt;Hi @wrussell&lt;/P&gt;

&lt;P&gt;Your first search query is searching for All data 30mins ago. This is equivalent to - &lt;CODE&gt;earliest="01/01/1970 11:00:00.000" latest=now()-30&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Where-as your second query is time bound. Search for Data older than 30mins and until last 7 days. - &lt;CODE&gt;earliest=-7d latest=now()-30&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;If your indexes have data older than 7 days, your 1st search will obviously be expensive than your 2nd search.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2019 04:14:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434176#M123940</guid>
      <dc:creator>nareshinsvu</dc:creator>
      <dc:date>2019-08-07T04:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Search Optimization saved search using time vs where clause</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434177#M123941</link>
      <description>&lt;P&gt;@wrussell12 &lt;CODE&gt;now()-30&lt;/CODE&gt; means pick events from 30 seconds before the search query runs. Not sure if this is to accommodate for delay in data coming to Splunk or for Real-Time search with 30 seconds window or for Search panel with refresh every 30 seconds.&lt;/P&gt;

&lt;P&gt;Since you are working only with metadata fields index and source and _time you can write tstats which will work way faster than what you have!&lt;/P&gt;

&lt;P&gt;Try the following run anywhere example based on Splunk's _internal index and confirm&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;&lt;STRONG&gt;With regular index search&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index IN ("_internal","_audit") source=*  earliest=-7d latest=@s-30s
| timechart span=1h count by source
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;169 results by scanning 609,716 events in &lt;CODE&gt;4.832&lt;/CODE&gt; second&lt;/STRONG&gt;&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;&lt;STRONG&gt;With tstats&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats count as EventCount where index IN ("_internal","_audit") source=*  earliest=-7d latest=@s-30s by source _time span=1h
| timechart span=1h sum(EventCount) as EventCount by source
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you need null values for selected time span you would need the following hack with appendpipe (as per one of my older answers for this: &lt;A href="https://answers.splunk.com/answers/595248/timechart-with-no-data-gives-no-results-found.html"&gt;https://answers.splunk.com/answers/595248/timechart-with-no-data-gives-no-results-found.html&lt;/A&gt; )&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats count as EventCount where index IN ("_internal","_audit") source=* earliest=-7d latest=@s-30s by source _time span=1h 
| timechart span=1h sum(EventCount) as EventCount by source
| appendpipe [| makeresults
          | eval Time=relative_time(now(),"-7d")."|".relative_time(now(),"-30s")
          | makemv Time delim="|"
          | mvexpand Time
          | eval _time=Time
          | fields - Time
          | bin _time span=1h
          | eval EventCount=0]
| dedup _time
| sort _time
| fillnull value=0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;This search has completed and has returned 169 results by scanning 610,450 events in &lt;CODE&gt;0.121&lt;/CODE&gt; seconds&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2019 04:14:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434177#M123941</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2019-08-07T04:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: Search Optimization saved search using time vs where clause</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434178#M123942</link>
      <description>&lt;P&gt;Thank you very much.&lt;/P&gt;

&lt;P&gt;I received 169 results, and the search was reduced from 5 minutes, to 5 seconds. &lt;/P&gt;

&lt;P&gt;Amazing.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2019 18:43:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434178#M123942</guid>
      <dc:creator>wrussell12</dc:creator>
      <dc:date>2019-08-07T18:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Search Optimization saved search using time vs where clause</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434179#M123943</link>
      <description>&lt;P&gt;Awesome, if too many events would have been an issue for your previous query now you can try -30d and what not &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; with tstats.&lt;/P&gt;

&lt;P&gt;Happy Splunking!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2019 02:26:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Optimization-saved-search-using-time-vs-where-clause/m-p/434179#M123943</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2019-08-08T02:26:41Z</dc:date>
    </item>
  </channel>
</rss>

