Scheduled View のベスト プラクティスと例
Scheduled View は、集計データを最小限まで削減し、データを生成するのに必要な結果のみが含まれるようにします。クエリの実行前にデータが事前に集計されているため、Scheduled View に対して実行されるクエリは非常に迅速に検索結果を返すことができます。Scheduled View はクエリを毎分 1 回処理します。
以下は Scheduled View のクエリで必須です。
- 常に timeslice operator を使用してください。使用しないと受信時刻が使用されます。
- 常に aggregate operator を使用してください。これにより、データの重複を回避できます。
また、Scheduled View のクエリを作成する際には以下の点にも注意してください。
- 変更されやすいクエリは使用しないでください。Scheduled View の大きな利点の 1 つは、履歴データをインデックス化できる点であり、長期的な傾向も特定できます。クエリが変更されると、履歴的な視点の一部が失われます。
- クエリの柔軟性を保つ。_sourceCategory=*Apache* のように柔軟なクエリを使用することで、メタデータが変化してもクエリは正常に実行されます。
- フィールドでは、汎用的な値を使用してください (フィールドをあまり特異にしないでください)。たとえば、latitude (緯度) フィールドや longitude (経度) フィールドではなく country (国) フィールドや city (都市) フィールドを使用します。
- パーティションを使用してください。パーティションにより、さらにクエリ時間を短縮できます。
- 履歴データにアクセスしてください。Scheduled View は、保持期間全体までデータを遡ることができます。
- グループを多用してください。グループを増やすことで柔軟性が向上します。ただし、Scheduled View 定義をテストして、グループの追加によってどのくらいデータが増えるかを確認してください。
Scheduled View でサポートされる operator
Scheduled View はクエリによって定義され、検索結果がインデックス化されます。
ビューでデータをインデックス化する方法のため、以下の operator のみがサポートされます。
non-aggregate operator
- Ceil
- Concat
- CSV
- Fields
- format
- FormatDate
- JSON (auto オプションはサポートされません)
- Keyvalue
- Length
- Lookup
- Matches
- Now
- Parse
- Parse regex または extract
- Predict
- Replace
- Round
- Substring
- タイムスライス順
- ToInt/ToLong
- where
- Withtime
Aggregate operator
- 数
- Min
- Max
- Sum
Scheduled View の検証
Scheduled View の検証は次のように行われます。
- 非集計クエリの場合は、非集計用のリストの operator のみが使用できます。
- 集計クエリの場合は、最初の group-by ステートメントの前では non-aggregate operator のみしか使用できません。最初の group-by ステートメントの後では、すべての operator が使用できます。
以下の operator は、Scheduled View で使用した場合は信頼できる結果を返しませんが、値を取得するための代替手段を紹介します。
[Average (平均)]。
タイムスライスでの合計値を計算してから合計数で割ることによって算出できます。
元の検索:
_sourceCategory=mySourceCategory "literal search term"
| parse “seconds = *,” as seconds
| avg(seconds)
Scheduled View の定義:
_sourceCategory=mySourceCategory "literal search term"
| parse “seconds = *,” as seconds
| timeslice 1m
| sum(seconds) as secondspertimeslice,count as countpertimeslice by _timeslice
クエリ:
_view=myScheduledView
| sum(secondspertimeslice) as totalseconds, sum(countpertimeslice) as totalcount
| totalseconds / totalcount as theaverage
Count_Distinct、First、Last、Min、Max、Most_Recent、Least_Recent、Pct、Stddev、Math Values、および RollingStd。
サポートされていない operator の代わりに、count operator で集計したいフィールドごとにカウントする Scheduled View を作成します。その後、ビューを参照して、必要な集計を行います。
たとえば、most_recent で Scheduled View を作成したい場合は次のように記述します。
_sourceCategory=receiver
| parse "dataPointCount=*)" as points
| parse "remote_ip=*]" as ip
| withtime points
| most_recent(points_withtime) as last_result by ip
サポートされていない operator である most_recent を削除して、count operator で Scheduled View を作成します。
_sourceCategory=receiver
| parse "dataPointCount=*)" as points
| parse "remote_ip=*]" as ip
| withtime points
| count points_withtime, ip
ビュー名が Points であれば、ビューに対して most_recent operator を次のように使用できます。
_view=Points
| most_recent(points_withtime) as last_result by ip
Scheduled View の例
Scheduled View で最適化できるクエリの例をいくつか示します。元の検索、Scheduled View の定義、そして Scheduled View で実行できるクエリをすべて示します。
集計前のデータ
元の検索:
_sourceCategory=mySourceCategory "literal search term"
| parse "[clientip *]" as clientip
| if (clientip matches "*-*","timeout","slow") as type
| timeslice 1h
| count as value by _timeslice, type
Scheduled View の定義:
_sourceCategory=mySourceCategory "literal search term"
| parse "[clientip *]" as clientip
| if (clientip matches "*-*","timeout","slow") as type
| timeslice 1m
| count as value by _timeslice, type
クエリ:
_view=myScheduledView
| timeslice 1h
| sum(value) as value by _timeslice, type
検索パフォーマンスの向上
元の検索:
_sourceCategory=iis_logs | parse "* * 192.* " as date,time,internalip
Scheduled View の定義:
_sourceCategory=iis_logs | parse "* * 192.* " as date,time,internalip
クエリ:
_view=myScheduledView2
軽量な Scheduled View と強力な Scheduled View
この Scheduled View のクエリは軽量であり、グループが 1 つしかありません。
_sourceCategory=prod/web/iis | timeslice 1m | count by _timeslice
結果は次のようになります。
この Scheduled View のクエリはより堅牢です。ただし、負荷が 5 倍で列が 1 つ追加されています。
_sourceCategory=prod/web/iis | timeslice 1m | count by _timeslice, status_code
結果は次のようになります。
カウントが分断されているため、レコードに対して sum を実行します。たとえば、次のクエリのように sum operator を使用して結果を集計します。
_view=nice_view_man | timeslice 1d | sum(_count) by _timeslice, status_code