Optimizing Microsoft Sentinel Hunting Queries with Time Filters

SC-200 By islever Editorial Last updated

Short answer

Apply Timestamp > ago(1h) to filter emails in the last hour, and use the join option that includes where Timestamp > ago(1h) to optimize performance by filtering the right-side table.

Question

HOTSPOT
You have a Microsoft 365 E5 subscription.
You need to create a hunting query that will return every email that contains an attachment named Document.pdf. The query must meet the following requirements:
• Only show emails sent during the last hour.
• Optimize query performance.
How should you complete the query? To answer, select the appropriate options in the answer area.
NOTE: Each correct selection is worth one point.
Exhibit: Optimizing Microsoft Sentinel Hunting Queries with Time Filters

Key Takeaways

The main trap is confusing the direction of the time comparison; ago() defines a start point in the past, so you need timestamps greater than that point to get recent data.

This page explains how to correctly filter email attachments by time using the greater-than operator and optimize performance by pushing time filters into the join subquery.

Common mistake: Learners often choose the less-than operator (`<`) because they misinterpret `ago()` as an end-time boundary rather than a start-time boundary, resulting in querying historical data instead of recent activity.

Expert Analysis

To satisfy the requirement of showing emails sent during the last hour while optimizing query performance, you must apply time filters to both sides of a join operation in KQL. The ago(1h) function returns a timestamp representing one hour prior to the current UTC time. To select records from the past hour (between that timestamp and now), you must use the greater-than operator (>). Using the less-than operator (<) would incorrectly filter for all logs older than an hour, which is logically opposite to the requirement.

Furthermore, applying the time filter inside the subquery on the right side of the join statement is critical for performance optimization. This technique, known as "filter pushdown," ensures that only recent records are retrieved from the DeviceFileEvents table before the join occurs. This reduces the dataset size significantly compared to filtering after the join, thereby meeting the performance optimization constraint.

Official Reference

Exam Strategy

Always verify the logic of time-based functions like ago(). Since it calculates a past timestamp, use > to include everything from that point forward to the present. Additionally, always look for opportunities to filter the smaller or more specific table within a join clause to reduce the volume of data processed during the join operation.

Frequently Asked Questions

What does the ago() function do in KQL?

The ago() function subtracts a specified timespan from the current UTC time. For example, ago(1h) returns the exact time one hour ago.

Why filter both sides of a join in KQL?

Filtering both sides reduces the number of rows entering the join operation. This improves performance by ensuring only relevant, recent data is matched between tables.

Discussion 15 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 15

  1. teouba 👍 11
    Ago() function subtracts the given timespan from the current UTC time. So if current time is 9.00am, then using ago(1h) means that time goes to 8.00am, so in order to check the timespace 8.00-9.00 you need to use timestamp > ago(1h)
  2. Pradeep064 👍 6
    This question seems a bit unusual, and I'm wondering if it's still relevant. The reason being, the "EmailAttachmentInfo" schema doesn't appear to include a "Subject" column, and the question suggests filtering based on this "Subject" column.
  3. omar_alhajsalem 👍 6
    EmailAttachmentInfo | where Timestamp > ago(1h) | where Subject == "Document Attachment" and FileName == "Document.pdf" | join (DeviceFileEvents | where Timestamp > ago(1h)) on SHA256
  4. gg7648 👍 3
    Wondering is this typo? | where Timestamp < ago (1h) --> show only less than one hour which matches. This satisfy the requirement of “Only show emails sent during the last hour”. This should be the correct one. But Answer: (| where Timestamp > ago (1h)) how come has greater than is correct answer?? This returns more than our of dataset results right? Ref: https://learn.microsoft.com/en-us/sharepoint/dev/general-development/keyword-query-language-kql-syntax-reference
  5. Adam7777 👍 3
    I don't know man, It seems like the second table of options is a mistake/typo. you need "join (DeviceFileEvents | where Timestamp > ago(1h)) on SHA256" to appropriately select the relevant rows to apply the time filter on. seems outdated on the current Schema
Show 10 more comments
  1. Holii 👍 2
    Wrong. This will search for logs from an hour ago till infinity in the past (until your specified time range). Even plugging this into KQL logs for a test run will throw an error on the statement with a recommendation to change it from < to > Recommendation: The query time filter may not be efficient. The TimeGenerated filter is looking for smaller than rather than larger than. This may result in querying high volume of very old data that is retained in the system. It is recommended to add a minimum TimeGenerated to be evaluated or to specify a time range to this query (see: https://aka.ms/logqueryperf/time).
  2. ultraRunningCA 👍 2
    This is correct, the answer should be 'where timestamp < ago(1h)' to meet the requirement of “Only show emails sent during the last hour” and to "optimise the query" the time filter needs to be applied first, so the Join should be the second option
  3. Edindude 👍 2
    EmailAttachmentInfo | where Timestamp > ago(1h) | where FileName == "Document.pdf" | join kind=inner (DeviceFileEvents | where Timestamp > ago(1h)) on SHA256
  4. Ramye 👍 2
    you have to choose 1 option from each box. You seem to have chosen 2 options from the bottom box.
  5. ultraRunningCA 👍 1
    now I'm not so sure... on the page given by mwoodc the answer provided is shown as : Apply time filters on both sides — Even if you're not investigating a specific time window, applying time filters on both the left and right tables can reduce the number of records to check and improve join performance. The query below applies Timestamp > ago(1h) to both tables so that it joins only records from the past hour: EmailAttachmentInfo | where Timestamp > ago(1h) | where Subject == "Document Attachment" and FileName == "Document.pdf" | join kind=inner (DeviceFileEvents | where Timestamp > ago(1h)) on SHA256 if you ask bing chat/chatGPT, this is what the query retrieves This KQL query will retrieve information about the email attachments with the subject “Document Attachment” and the file name “Document.pdf” that were sent within the last hour. It will then join this information with the DeviceFileEvents table on the SHA256 hash value which is what the question is asking for...
  6. Gurulee 👍 1
    https://learn.microsoft.com/en-us/microsoft-365/security/defender/advanced-hunting-best-practices?view=o365-worldwide#optimize-the-join-operator "Apply time filters on both sides—Even if you're not investigating a specific time window, applying time filters on both the left and right tables can reduce the number of records to check and improve join performance."
  7. neogeek87 👍 1
    Answer is provided here on the link: https://learn.microsoft.com/en-us/defender-xdr/advanced-hunting-best-practices. The given answer is correct
  8. 9802f06 👍 1
    Why the query looks for device file events? Is this relevant in this case?
  9. chepeerick 👍 1
    this is correct
  10. Nikki0222 👍 1
    Correct

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Practice All SC-200 Questions

Access 80 questions with complete answers and detailed explanations.

View Full SC-200 Practice Test →

← Back to SC-200 Study Guide