博客 / 詳情

返回

Elasticsearch 緩存相關(qbit)

前言

  • 本文對 Elasticsearch 7.17 適用
  • Elasticsearch 主要有 3 種緩存:fields cachequery cacherequest cache
  • 3 種 cache 的配置狀況都可以用命令查看

    # 集羣
    GET _cluster/settings?include_defaults&flat_settings
    # 索引
    GET my-index/_settings?include_defaults&flat_settings

Field Data Cache

  • 官方文檔:Field data cache settings
  • Field data cache 包含 fielddataglobal ordinals,兩者都主要用於聚合。
  • fielddata 和 doc_value 的相同點

    都要創建正排索引,數據結構類似於列式存儲
    都是為了可以聚合,排序之類的操作
  • fielddata 和 doc_value 的不同點

    # 存儲索引數據的方式不一樣:
    fielddata: heap 內存存儲;doc_values: OS Cache + 磁盤存儲
    
    # 針對的類型,也不一樣
    fielddata 主要針對的是分詞字段(text);doc_values 針對大是不分詞字段(keyword、int 等)
    
    # 是否開啓
    fielddata 默認不開啓;doc_values 默認是開啓
  • 靜態配置項 indices.fielddata.cache.size 是一個節點級的配置,默認無限制
  • 查看集羣的 fielddata

    GET _stats/fielddata?human
  • 查看所有節點的 fielddata

    GET _nodes/stats/indices/fielddata?human
  • 查看單個節點 mzjaGC52QXeaHBXcJ1jp3gfielddata

    GET _nodes/mzjaGC52QXeaHBXcJ1jp3g/stats/indices/fielddata?human
  • 查看單個索引的 request_cache

    GET my_index/_stats/fielddata?human

Shard Request Cache

  • 官方文檔:Shard request cache settings
  • request_cache 特點

    request_cache 是一個“分片級”的緩存,shard 是緩存 key 的一部分
    request_cache 主要用於 size=0 的查詢,如純聚合查詢(aggs)、hits.total 和 suggestions
    request_cache 是自動失效的,失效時間就是索引的 refresh 時間(index.refresh_interval),失效的前提是分片內容確實發生了變化
    request_cache 緩存的默認大小是 JVM 堆內存的 1%,可以通過參數 indices.request.cache.size 手動設置
  • 靜態配置項 indices.requests.cache.size 是一個節點級的配置,默認為 1%
  • 查看集羣的 request_cache

    GET _stats/request_cache?human
  • 查看所有節點的 request_cache

    GET _nodes/stats/indices/query_cache?human
  • 查看單個節點 mzjaGC52QXeaHBXcJ1jp3grequest_cache

    GET _nodes/mzjaGC52QXeaHBXcJ1jp3g/stats/indices/request_cache?human
  • 查看單個索引的 request_cache

    GET my_index/_stats/request_cache?human
  • 應注意在請求語句中使用 request_cache=true 參數。官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/...

Node Query Cache

  • 官方文檔:Node query cache settings
  • query_cache 是 Node 級別的緩存,主要用於緩存 filter 上下文返回的結果
  • 默認情況下,query_cache 最多可容納 10000 個查詢,最多佔 JVM 堆內存的 10%。
  • indices.queries.cache.countindices.queries.cache.size 都是節點級別的配置,不能動態修改,需要修改配置文件並重啓集羣
  • 查看集羣的 query_cache

    GET _stats/query_cache?human
  • 查看所有節點的 query_cache

    GET _nodes/stats/indices/query_cache?human
  • 查看單個節點 mzjaGC52QXeaHBXcJ1jp3gquery_cache

    GET _nodes/mzjaGC52QXeaHBXcJ1jp3g/stats/indices/query_cache?human
  • 查看單個索引的 query_cache

    GET my_index/_stats/query_cache?human

緩存清理

  • 官方文檔:Clear cache API

    不分緩存類型清理緩存

    # 清理集羣所有緩存
    POST _cache/clear
    # 清理單個索引緩存
    POST my_index/_cache/clear

    按緩存類型清理緩存

  • 清理 fielddata 緩存

    POST _cache/clear?fielddata=true  
    POST my-index/_cache/clear?fielddata=true  
  • 清理 query 緩存

    POST _cache/clear?query=true 
    POST my-index/_cache/clear?query=true 
  • 清理 request 緩存

    POST _cache/clear?request=true 
    POST my-index/_cache/clear?request=true 

參考文獻

  • Elasticsearch2.x 三種緩存介紹:Query Cache、Request Cache、Fielddata Cache
  • 張超:扒一扒查詢緩存的褲子
  • ElasticSearch中doc values和fielddata
  • ElasticSearch 緩存概覽
  • Elasticsearch的Query Cache 知識梳理
本文出自 qbit snap
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.