<?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: Optimize transaction query in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442722#M125665</link>
    <description>&lt;P&gt;Transaction, while a powerful command, is also very resource-intensive.  When possible, you should always try to use stats, eventstats or streamstats instead of transaction.&lt;/P&gt;

&lt;P&gt;This method gets you what the transaction got you, but with much less machine cost.  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=shop sourcetype="shop1" (component=logout) OR (component=login) Id1!=NULL
| fields _time Id1 Id2 api component
| eval login_time=case(component="login",_time)
| eval logout_time=case(component="logout",_time)

| rename COMMENT as "Assign a login group number to each login for each Id combination"   
| sort 0 _time 
| streamstats count(login_time) as groupno by Id1 Id2 

| rename COMMENT as "Calculate the duration of each group" 
| stats min(login_time) as login_time max(logout_time) as logout_time range(_time) as duration by Id1 Id2 groupno  

| rename COMMENT as "Set defaults for missing logins and logouts" 
| eval login_time=coalesce(login_time,  some default start time of your choice...)
| eval logout_time=coalesce(logout_time,  some default end time of your choice...)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;From that point, your logic should be pretty much the same. &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Note - your logic shows both fields &lt;CODE&gt;component&lt;/CODE&gt; and &lt;CODE&gt;api&lt;/CODE&gt; as being tested for values "login" and "logout".  We assumed &lt;CODE&gt;component&lt;/CODE&gt; was correct.&lt;/P&gt;</description>
    <pubDate>Wed, 02 Jan 2019 15:37:21 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2019-01-02T15:37:21Z</dc:date>
    <item>
      <title>Optimize transaction query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442719#M125662</link>
      <description>&lt;P&gt;Hello I have a transaction query which I would like to optimize. It is impossible to run the query for a few hours. Is there anyway I can rephrase the query to have the same results?&lt;/P&gt;

&lt;P&gt;Query: transaction Id1,Id2 startswith=login endswith=logout keepevicted=true&lt;/P&gt;

&lt;P&gt;A unique event is mapped by combination of Id1 and Id2&lt;BR /&gt;
I want to map all users who have logged in and logged out in the window&lt;BR /&gt;
Also all users who have logged in but not logged out&lt;BR /&gt;
And finally users who have logged out in the given time frame. &lt;/P&gt;

&lt;P&gt;I am not sure how can I use subsearches or xyseries to optimize this query. &lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 15:42:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442719#M125662</guid>
      <dc:creator>AnujaJ</dc:creator>
      <dc:date>2018-12-20T15:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize transaction query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442720#M125663</link>
      <description>&lt;P&gt;Can you paste your whole query? I strongly suggest users not to use the transaction command as it's non-streaming (i.e. forces the results to be pushed to the search head which loses the parallalism of Splunk). &lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 16:27:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442720#M125663</guid>
      <dc:creator>skoelpin</dc:creator>
      <dc:date>2018-12-20T16:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize transaction query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442721#M125664</link>
      <description>&lt;P&gt;index=shop sourcetype="shop1" (component=logout) OR (component=login) Id1!=NULL&lt;BR /&gt;
|transaction Id1,Id2 startswith=login endswith=logout keepevicted=true&lt;BR /&gt;
|table Id1,Id2,api,duration,_time,evicted&lt;BR /&gt;
|fillnull value=0&lt;BR /&gt;
| eval l_time=relative_time(now(),"-15m")&lt;BR /&gt;
| eval duration=if(match(api,"logout"),_time-l_time,if(match(api,"login"),now()-max(_time),duration)) &lt;BR /&gt;
| convert dur2sec(duration) AS duration&lt;BR /&gt;
| search duration&amp;gt;360&lt;BR /&gt;
|table Id1,Id2,api,duration&lt;BR /&gt;
|stats sum(duration) as  watch_time, count(Id1) as total_Ids by Id2&lt;BR /&gt;
|eventstats sum(total_Ids) as total&lt;BR /&gt;
|eval total_time =round(time/60,2),quoten=(total_Ids/total)*100&lt;BR /&gt;
|eval average=total_time/total_Ids&lt;BR /&gt;
|lookup userLookup Id2 as Id2 OUTPUT username as username&lt;BR /&gt;
|table Id2, username,total_time, total_Ids,quoten,average | sort -quoten&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 22:27:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442721#M125664</guid>
      <dc:creator>AnujaJ</dc:creator>
      <dc:date>2020-09-29T22:27:14Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize transaction query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442722#M125665</link>
      <description>&lt;P&gt;Transaction, while a powerful command, is also very resource-intensive.  When possible, you should always try to use stats, eventstats or streamstats instead of transaction.&lt;/P&gt;

&lt;P&gt;This method gets you what the transaction got you, but with much less machine cost.  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=shop sourcetype="shop1" (component=logout) OR (component=login) Id1!=NULL
| fields _time Id1 Id2 api component
| eval login_time=case(component="login",_time)
| eval logout_time=case(component="logout",_time)

| rename COMMENT as "Assign a login group number to each login for each Id combination"   
| sort 0 _time 
| streamstats count(login_time) as groupno by Id1 Id2 

| rename COMMENT as "Calculate the duration of each group" 
| stats min(login_time) as login_time max(logout_time) as logout_time range(_time) as duration by Id1 Id2 groupno  

| rename COMMENT as "Set defaults for missing logins and logouts" 
| eval login_time=coalesce(login_time,  some default start time of your choice...)
| eval logout_time=coalesce(logout_time,  some default end time of your choice...)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;From that point, your logic should be pretty much the same. &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Note - your logic shows both fields &lt;CODE&gt;component&lt;/CODE&gt; and &lt;CODE&gt;api&lt;/CODE&gt; as being tested for values "login" and "logout".  We assumed &lt;CODE&gt;component&lt;/CODE&gt; was correct.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jan 2019 15:37:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442722#M125665</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2019-01-02T15:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize transaction query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442723#M125666</link>
      <description>&lt;P&gt;If you post sample events and a desired final output, I will be able to get you an answer.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2019 01:37:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442723#M125666</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-01-03T01:37:15Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize transaction query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442724#M125667</link>
      <description>&lt;P&gt;I did step 14,15 (coalesce) before I did stats(step 11) so that he counts the evicted results as well. And instead of range I just use eval to substract time between logout and login. Somehow range was not able to calculate the correct duration. Thank you for your help. &lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 09:50:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Optimize-transaction-query/m-p/442724#M125667</guid>
      <dc:creator>AnujaJ</dc:creator>
      <dc:date>2019-01-09T09:50:55Z</dc:date>
    </item>
  </channel>
</rss>

