Splunk Search

More Efficient Query Dedup to Stats Distinct Count

IRHM73
Motivator

Hi, I wonder whether someone could help me please.

I'm using the search below to successfully produce a pie chart with desired results.

tags.transactionName = "Send Email Alert"  auditType="TxSucceeded"  | eval shortForm='detail.formId'."  " | eval shortForm =  substr(shortForm, 1, 6) | sort 0  detail.messageId  | dedup detail.messageId  | chart count by shortForm  | eval pieSlice=shortForm  + " " + count  | fields pieSlice, count

Because this search takes a while to load I'm looking at making this more efficient by removing the dedup element of search, so I've changed this to:

tags.transactionName = "Send Email Alert"  auditType="TxSucceeded"  | eval shortForm='detail.formId'."  " | eval shortForm =  substr(shortForm, 1, 6) | sort 0  detail.messageId  | **stats dc(detail.messageId)**  | chart count by shortForm  | eval pieSlice=shortForm  + " " + count  | fields pieSlice, count

The problem is, is that this no longer produces any results so I've clearly done something wrong.

I just wondered whether someone may be able to look at this please and let me know where I've gone wrong.

Many thanks and kindest regards

Chris

Tags (2)
0 Karma
1 Solution

tom_frotscher
Builder

Hi,

i would try something like this:

 tags.transactionName = "Send Email Alert"  auditType="TxSucceeded"  | eval shortForm='detail.formId'."  " | eval shortForm =  substr(shortForm, 1, 6) | sort 0  detail.messageId  | stats dc(detail.messageId) first(shortForm) as shortForm by "detail.messageId" | chart count by shortForm  | eval pieSlice=shortForm  + " " + count  | fields pieSlice, count

to adjust your search. But verify your results.

In general if you use something like this in your search: ...| sort 0 detail.messageId | dedup detail.messageId |.... Try to think about what the search execute here. You first sort a huge list of events, just to throw out the most events with the dedup in the next step. It would ofcourse be faster if you first throw out the most events and after that, sort the now smaller list of events.

Greetings

Tom

View solution in original post

tom_frotscher
Builder

Hi,

i would try something like this:

 tags.transactionName = "Send Email Alert"  auditType="TxSucceeded"  | eval shortForm='detail.formId'."  " | eval shortForm =  substr(shortForm, 1, 6) | sort 0  detail.messageId  | stats dc(detail.messageId) first(shortForm) as shortForm by "detail.messageId" | chart count by shortForm  | eval pieSlice=shortForm  + " " + count  | fields pieSlice, count

to adjust your search. But verify your results.

In general if you use something like this in your search: ...| sort 0 detail.messageId | dedup detail.messageId |.... Try to think about what the search execute here. You first sort a huge list of events, just to throw out the most events with the dedup in the next step. It would ofcourse be faster if you first throw out the most events and after that, sort the now smaller list of events.

Greetings

Tom

IRHM73
Motivator

Hi Tom, thank you for this, it works perfectly. Thank you also for the guidance on the 'sort' and 'dedup' steps and the steps that they should be introduced.

Kind Regards

Chris

0 Karma

IRHM73
Motivator

Hi Tom, thank you very much for taking the time to reply to my post and for the guidance.

Many thanks and kind regards

Chris

0 Karma

tom_frotscher
Builder

Hi,

if you run your search up to this point:

 tags.transactionName = "Send Email Alert"  auditType="TxSucceeded"  | eval shortForm='detail.formId'."  " | eval shortForm =  substr(shortForm, 1, 6) | sort 0  detail.messageId  | **stats dc(detail.messageId)** 

you will see that after the statd dc there is only one field "dc(detail.messageId)" left. Thats why the rest of your search:

| chart count by shortForm  | eval pieSlice=shortForm  + " " + count  | fields pieSlice, count

produces no result. Because it uses fields like count and shortForm. But these fields are no longer existent.

Greetings

Tom

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Observability Simplified: Combining User Experience, Application Performance & ...

Tech Talk Observability Simplified: Combining User Experience, Application Performance & Network ...

Event Series May & June: From Network Visibility to Service Intelligence

Unifying the Network: Moving from Alert Noise to Service Intelligence with Splunk ITSI In today’s hybrid ...