<?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 Re: Efficiently filter subsequent search statement using results from previous statement in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545360#M154451</link>
    <description>&lt;P&gt;Try something like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=myindex sourcetype=mysourcetype
| stats first(_time) as _time by objectid versionid
| eval fixed=if(versionid=2,_time,null)
| stats first(_time) as _time values(fixed) as fixed by objectid
| eval fixed=if(isnotnull(fixed), strftime(fixed,"%Y-%m-%d %H:%M:%S"),"Not fixed")&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 25 Mar 2021 11:30:25 GMT</pubDate>
    <dc:creator>ITWhisperer</dc:creator>
    <dc:date>2021-03-25T11:30:25Z</dc:date>
    <item>
      <title>Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545356#M154449</link>
      <description>&lt;P&gt;I am using Splunk Enterprise Version 8.0.5.1&lt;/P&gt;&lt;P&gt;Consider an index with half a million events being generated every day.&lt;/P&gt;&lt;P&gt;There are three fields in the index I am particularly interested in.&lt;/P&gt;&lt;P&gt;sourcetype has 20 different values, but I am interested in one sourcetype that accounts for 10,000 events per day&lt;/P&gt;&lt;P&gt;objectid is populated on each event and there are multiple events for the same objectid.&lt;/P&gt;&lt;P&gt;versionid can be two different values : 1 or 2 - objects move from 1 to 2, but never back to 1.&lt;/P&gt;&lt;P&gt;For the query period, there should be no objectids with a versionid of 1.&lt;/P&gt;&lt;P&gt;For those objectids that have an event with versionid 1, I want to know when they changed to a 2.&lt;/P&gt;&lt;P&gt;The problem I have is that there are so many 2s in the index, querying all of them just to then join to the 1s is taking forever and generates a job of over 1GB.&lt;/P&gt;&lt;P&gt;So, what I'd really like to do is to query the 1s first, and then feed that list into a subsequent search where it only finds the rows with the objectid in the results of the first search for the 1s.&lt;/P&gt;&lt;P&gt;If I do this, it just takes forever...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=myindex sourcetype=mysourcetype versionid=1
| reverse | table _time objectid | dedup objectid
| join objectid [ index=myindex sourcetype=mysourcetype versionid=2 | reverse | eval fixed=_time | table fixed objectid | dedup objectid ]
| table _time objectid fixed&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to do is to reference the results of the first query in an IN statement inside the second, but I can't find a way to do that.&amp;nbsp; If I could create a dashboard with a base query, and a panel uses the results of that base query in an IN statement, that might work, but at the moment I am stuck.&lt;BR /&gt;&lt;BR /&gt;I know that Splunk is not SQL, but to make it a bit clearer what I am trying to achieve...&lt;BR /&gt;&lt;BR /&gt;SELECT *&lt;BR /&gt;FROM MYINDEX&lt;BR /&gt;WHERE OBJECTID IN (SELECT OBJECTID FROM MYINDEX WHERE VERSIONID=1)&lt;BR /&gt;AND VERSIONID=2&lt;/P&gt;&lt;P&gt;i.e. it evaluates the versionid=1 objectids first, and then the outer query only returns rows that match those ids.&amp;nbsp; When I do this manually with a small number of IDs and put them in an explicit IN clause, it runs very quickly.&lt;/P&gt;&lt;P&gt;Any suggestions would be much appreciated.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 12:48:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545356#M154449</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-03-25T12:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545360#M154451</link>
      <description>&lt;P&gt;Try something like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=myindex sourcetype=mysourcetype
| stats first(_time) as _time by objectid versionid
| eval fixed=if(versionid=2,_time,null)
| stats first(_time) as _time values(fixed) as fixed by objectid
| eval fixed=if(isnotnull(fixed), strftime(fixed,"%Y-%m-%d %H:%M:%S"),"Not fixed")&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 25 Mar 2021 11:30:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545360#M154451</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-03-25T11:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545361#M154452</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I think that you got the idea from this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval _raw = "time,objectid,versionid
1616661604,123,1
1616662604,124,1
1616663604,122,1
1616664604,123,2
1616665604,125,1
1616666604,124,2
1616667604,124,2"
| multikv forceheader=1
| eval _time=time
``` Generate test data ```
| stats values(versionid) as versions range(_time) as duration by objectid
| where mvcount(versions) &amp;gt; 1
| eval duration = tostring(duration, "duration")&lt;/LI-CODE&gt;&lt;P&gt;r. Ismo&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 11:31:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545361#M154452</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2021-03-25T11:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545365#M154454</link>
      <description>&lt;P&gt;Thanks for the suggestion, but that takes much longer, unfortunately.&lt;/P&gt;&lt;P&gt;I am really looking for some way to quickly ONLY return the versionid=2 events that are for the subset of objectids that have any versionid=1 events&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 11:49:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545365#M154454</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-03-25T11:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545366#M154455</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Thanks for the suggestion, but it takes much longer, unfortunately, because it still evaluates every event with versionid=2 instead of only bringing back those for objectid=1 first and then filtering on those first.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 11:53:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545366#M154455</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-03-25T11:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545369#M154457</link>
      <description>&lt;P&gt;Have you try these proposals in your environment or have you just decide that those didn't work?&lt;/P&gt;&lt;P&gt;I have evaluated those with our data (of course not exactly same than you have, but amount and cardinality should be enough equal).&lt;/P&gt;&lt;P&gt;With your example:&lt;/P&gt;&lt;P&gt;This search has completed and has returned 757 results by scanning 73,213 events in 7.786 seconds&lt;/P&gt;&lt;P&gt;The following messages were returned by the search subsystem:&lt;/P&gt;&lt;P&gt;info : [subsearch]: Subsearch produced 68838 results, truncating to maxout 50000.&lt;/P&gt;&lt;P&gt;===&amp;gt; not correct result&lt;/P&gt;&lt;P&gt;With my:&lt;/P&gt;&lt;P&gt;This search has completed and has returned 1,169 results by scanning 141,222 events in 3.862 seconds&lt;/P&gt;&lt;P&gt;And when I use smaller samples where sub search even works those execution times was relative&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yours/my: 1.335 vs 0.324s&lt;/P&gt;&lt;P&gt;Both queries returns same amount of events.&lt;/P&gt;&lt;P&gt;r. Ismo&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 12:29:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545369#M154457</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2021-03-25T12:29:03Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545371#M154458</link>
      <description>&lt;P&gt;I said it takes longer, so, I would only know that from testing, right?&lt;/P&gt;&lt;P&gt;But, I can also see from the query that there is nothing in there that achieves what I have specifically asked for.&amp;nbsp; Although the results may achieve the same dataset, it still does not do it in an efficient way.&amp;nbsp; The query uses basic functions of Splunk that I am very familiar with and have tried in so many different ways.&amp;nbsp; But the specific ask here is about how to quickly retrieve JUST the rows that match the ids returned in the first query.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 12:42:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545371#M154458</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-03-25T12:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545375#M154461</link>
      <description>&lt;P&gt;I know that Splunk is not SQL, but to make it a bit clearer what I am trying to achieve...&lt;BR /&gt;&lt;BR /&gt;SELECT *&lt;BR /&gt;FROM MYINDEX&lt;BR /&gt;WHERE OBJECTID IN (SELECT OBJECTID FROM MYINDEX WHERE VERSIONID=1)&lt;BR /&gt;AND VERSIONID=2&lt;BR /&gt;&lt;BR /&gt;I have updated the original post with this clarity.&amp;nbsp; Hope it helps.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 12:48:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545375#M154461</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-03-25T12:48:32Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545377#M154463</link>
      <description>Maybe this helps you to figure out correct join? &lt;A href="https://community.splunk.com/t5/Splunk-Search/What-is-the-relation-between-the-Splunk-inner-left-join-and-the/m-p/391288" target="_blank"&gt;https://community.splunk.com/t5/Splunk-Search/What-is-the-relation-between-the-Splunk-inner-left-join-and-the/m-p/391288&lt;/A&gt;</description>
      <pubDate>Thu, 25 Mar 2021 12:55:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545377#M154463</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2021-03-25T12:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545384#M154466</link>
      <description>&lt;P&gt;Thanks for the link to a very good description of joins.&amp;nbsp; I am very familiar with all the join options and have chosen the inner join as I would expect that to just bring back the rows where there is a matching id on both sides, but Splunk still returns all 5 million rows (or whatever the count is for the various periods I have tried) rather than just return the 200 rows for the ids that I am interested in, which I can get back quickly if I put the long list of IDs in the query.&amp;nbsp; But I can't specify the list up front because I don't know which ids are going to still be on versionid=1.&lt;/P&gt;&lt;P&gt;How can I force the second query to only bring back the ids from the first query?&amp;nbsp; I am worried that I am missing an obvious attribute of the join (and have checked the Splunk docs &lt;A href="https://splk.it/3smaaNf" target="_blank"&gt;https://splk.it/3smaaNf&lt;/A&gt;) that would allow this filtering to happen.&lt;/P&gt;&lt;P&gt;I appreciate the time you are spending on this.&amp;nbsp; Many thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 13:06:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545384#M154466</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-03-25T13:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545397#M154472</link>
      <description>&lt;P&gt;Can you try this one:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=myindex sourcetype=mysourcetype versionid IN (1, 2)
| fields _time objectid versionid &amp;lt;other fields which you are needing&amp;gt;
| stats range(_time) as duration values(*) as * by objectid
| where mvcount(versionid) &amp;gt; 1 AND isnotnull(mvfind(versionid, 1)) AND isnotnull(mvfind(versionid, 2))
| eval duration = tostring(duration, "duration")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;This works for me.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 13:48:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545397#M154472</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2021-03-25T13:48:47Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545427#M154486</link>
      <description>&lt;P&gt;Thanks again!&amp;nbsp; &amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This also takes a long time to run, basically because the very first statement pulls back everything.&amp;nbsp; It is pulling back all the 2s, when I only want the 2s that have a 1.&amp;nbsp; Forget all the other processing after that statement to calculate the duration (although there is some interesting syntax in there that I haven't used before that looks cool that I am going to investigate separately, thank you) but if you can trim the query down to just the first statement and get that to just return the 1s and the 2s that have a 1, and do it without first pulling back all the 2s and then filtering afterwards, that will solve my problem.&amp;nbsp; I am guessing there is no way to achieve that as no-one on any forums seems to have a solution.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 15:03:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545427#M154486</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-03-25T15:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545527#M154542</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/232884"&gt;@shanebough&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Please try below;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=myindex sourcetype=mysourcetype versionid=2 
    [ index=myindex sourcetype=mysourcetype versionid=1 
    | stats count by objectid 
    | fields objectid ]&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 26 Mar 2021 07:01:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545527#M154542</guid>
      <dc:creator>scelikok</dc:creator>
      <dc:date>2021-03-26T07:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545601#M154579</link>
      <description>&lt;P&gt;Thank you for the suggestion.&amp;nbsp; Unfortunately, this is the longest running version so far.&amp;nbsp; But I don't see which part of this achieves the objective of only returning the rows that match the objectids in the initial query of versionid=1.&amp;nbsp; How is this supposed to achieve that?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 14:14:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/545601#M154579</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-03-26T14:14:14Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently filter subsequent search statement using results from previous statement</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/548007#M155398</link>
      <description>&lt;P&gt;The best I can come up with so far is the following...&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=myindex sourcetype=mysourcetype versionid=1
| reverse | table _time objectid | dedup objectid
| join objectid 
  [ search index=myindex sourcetype=mysourcetype versionid=2 
           [ search index=myindex sourcetype=mysourcetype versionid=1 | dedup objectid | table objectid ]
           | reverse | eval fixed=_time | table fixed objectid | dedup objectid ]
| table _time objectid fixed&lt;/LI-CODE&gt;&lt;P&gt;This seems a bit expensive as the outer query is being executed twice.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have any better ideas?&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Shane&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 12:30:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Efficiently-filter-subsequent-search-statement-using-results/m-p/548007#M155398</guid>
      <dc:creator>shanebough</dc:creator>
      <dc:date>2021-04-15T12:30:06Z</dc:date>
    </item>
  </channel>
</rss>

