一、测试环境

python 3.7

elasticsearch 6.8

elasticsearch-dsl 7

安装elasticsearch-dsl

pip install elasticsearch-dsl

测试elasticsearch连通性

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search client = Elasticsearch(hosts=['http://127.0.0.1:9200'])
s = Search(using=client, index="my_store_index") .query("match_phrase_prefix", name="us")
s = s.source(['id'])
s = s.params(http_auth=["test", "test"])
response = s.execute() for hit in response:
print(hit.meta.score, hit.name) 11.642133 945d0426-033e-4a8a-86db-b776c6c9a082
11.642133 3c1aead4-aa6f-4256-a126-f29f84c9ac89
11.642133 77782add-ab58-4eb6-85af-bcbe79be9623
11.642133 75a02b9a-be31-4a78-a3d9-9af72f98cbf9
11.642133 d5aacf16-61fc-4f0c-b05d-3d57c8ab6236
11.642133 30912e1d-4662-4f24-bd5b-5a997e44c290
11.642133 95c28501-66a6-4786-917b-0f1e38707648
11.642133 605f4e11-08c8-4d60-b803-7925cf325cea
11.642133 5dd93a29-e75c-44e3-9f26-bd90e588bc1d
11.642133 84e97af5-4e99-466f-bd82-10cd2b79aa18

二、from + size一次性返回大量数据性能测试

通过以下code,直接使用from + size返回100000记录,耗时17279ms;

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q def from_size_query(client):
s = Search(using=client, index="my_store_index")
s = s.params(http_auth=["test", "test"], request_timeout=50);
q = Q('bool',
must_not=[Q('match_phrase_prefix', name='us')]
)
s = s.query(q) s = s.source(['id'])
s = s[0:100000]
response = s.execute() print(f'hit total {response.hits.total}')
print(f'request time {response.took}ms') client = Elasticsearch(hosts=['http://127.0.0.1:9200'])
from_size_query(client) hit total 485070
request time 17279ms

三、使用search after分页返回大量数据性能测试

通过以下code,使用search_after分多次共返回100000记录;从执行结果可以看到当每页获取记录达到5000时,执行的时间基本变化不大;考虑到size增大对cpu和内存的影响,在测试数据情况下,size设置为3000或者4000比较合适;

def search_after_query(client, result):
s = Search(using=client, index="my_store_index")
s = s.params(http_auth=["test", "test"], request_timeout=50);
q = Q('bool',
must_not=[Q('match_phrase_prefix', name='us')]
)
s = s.query(q)
if result['after_value']:
s = s.extra(search_after= [result['after_value']]) s = s.source(['id'])
s = s[:result['size']]
s = s.sort('id')
response = s.execute() fetch = len(response.hits)
result['total'] += response.took
result['times'] -= 1 while fetch == result['size'] and result['times'] > 0:
sort_val = response.hits.hits[-1].sort[-1]
s = s.extra(search_after=[sort_val])
response = s.execute() fetch = len(response.hits)
result['total'] += response.took
result['times'] -= 1 client = Elasticsearch(hosts=['http://127.0.0.1:9200'])
times = 100
result = {"total": 0, "times":times, "size": 1000, "after_value":None}
search_after_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 50
result = {"total": 0, "times":times, "size": 2000, "after_value":None}
search_after_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 25
result = {"total": 0, "times":times, "size": 4000, "after_value":None}
search_after_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 20
result = {"total": 0, "times":times, "size": 5000, "after_value":None}
search_after_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 10
result = {"total": 0, "times":times, "size": 10000, "after_value":None}
search_after_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 5
result = {"total": 0, "times":times, "size": 20000, "after_value":None}
search_after_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 2
result = {"total": 0, "times":times, "size": 50000, "after_value":None}
search_after_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') size 1000 request 100 times total 14111ms
size 2000 request 50 times total 11987ms
size 4000 request 25 times total 11167ms
size 5000 request 20 times total 10589ms
size 10000 request 10 times total 9930ms
size 20000 request 5 times total 9978ms
size 50000 request 2 times total 9946ms

四、使用scroll分页返回大量数据性能测试

通过以下code,使用search_after分多次共取回100000记录;从执行结果通过不同的size获取数据,执行的时间变化不大,所以elasticsearch官方也不建议使用scroll;

def search_scroll_query(client, result):
s = Search(using=client, index="my_store_index")
s = s.params( request_timeout=50, scroll='1m');
q = Q('bool',
must_not=[Q('match_phrase_prefix', name='us')]
)
s = s.query(q) s = s.source(['id'])
s = s[:result['size']]
response = s.execute() fetch = len(response.hits)
result['total'] += response.took
result['times'] -= 1
scroll_id = response._scroll_id while fetch == result['size'] and result['times'] > 0:
response = client.scroll(scroll_id=scroll_id, scroll='1m', request_timeout=50)
scroll_id = response['_scroll_id']
fetch = len(response['hits']['hits'])
result['total'] += response['took']
result['times'] -= 1 client = Elasticsearch(hosts=['http://127.0.0.1:9200'], http_auth=["test", "test"]) times = 100
result = {"total": 0, "times":times, "size": 1000}
search_scroll_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 50
result = {"total": 0, "times":times, "size": 2000}
search_scroll_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 25
result = {"total": 0, "times":times, "size": 4000}
search_scroll_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 20
result = {"total": 0, "times":times, "size": 5000}
search_scroll_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 10
result = {"total": 0, "times":times, "size": 10000}
search_scroll_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 5
result = {"total": 0, "times":times, "size": 20000}
search_scroll_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') times = 2
result = {"total": 0, "times":times, "size": 50000}
search_scroll_query(client, result)
print(f'size {result["size"]} request {times} times total {result["total"]}ms ') size 1000 request 100 times total 16573ms
size 2000 request 50 times total 17678ms
size 4000 request 25 times total 16719ms
size 5000 request 20 times total 16031ms
size 10000 request 10 times total 16008ms
size 20000 request 5 times total 16074ms
size 50000 request 2 times total 14390ms

elasticsearch查询之大数据集分页性能测试的更多相关文章

  1. elasticsearch查询之大数据集分页查询

    一. 要解决的问题 search命中的记录特别多,使用from+size分页,直接触发了elasticsearch的max_result_window的最大值: { "error" ...

  2. python连接 elasticsearch 查询数据,支持分页

    使用python连接es并执行最基本的查询 from elasticsearch import Elasticsearch es = Elasticsearch(["localhost:92 ...

  3. [NewLife.XCode]高级查询(化繁为简、分页提升性能)

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netcore,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和 ...

  4. 大数据学习[16]--使用scroll实现Elasticsearch数据遍历和深度分页[转]

    题目:使用scroll实现Elasticsearch数据遍历和深度分页 作者:星爷 出处: http://lxWei.github.io/posts/%E4%BD%BF%E7%94%A8scroll% ...

  5. elasticsearch查询之三种fetch id方式性能测试

    一.使用场景介绍 elasticsearch除了普通的全文检索之外,在很多的业务场景中都有使用,各个业务模块根据自己业务特色设置查询条件,通过elasticsearch执行并返回所有命中的记录的id: ...

  6. EF查询百万级数据的性能测试--多表连接复杂查询

    相关文章:EF查询百万级数据的性能测试--单表查询 一.起因  上次做的是EF百万级数据的单表查询,总结了一下,在200w以下的数据量的情况(Sql Server 2012),EF是可以使用,但是由于 ...

  7. ElasticSearch查询 第一篇:搜索API

    <ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...

  8. Elasticsearch入门教程(五):Elasticsearch查询(一)

    原文:Elasticsearch入门教程(五):Elasticsearch查询(一) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...

  9. 报表性能优化方案之单数据集分页SQL实现层式报表

    1.概述 我们知道,行式引擎按页取数只适用于Oracle,mysql,hsql和sqlserver2008及以上数据库,其他数据库,如access,sqlserver2005,sqlite等必须编写分 ...

随机推荐

  1. 家用路由器也能充当Web服务器?路由器插件开发心得

    起因 最近刚刚结束考研,开始有时间写文章了.在复习的时候中,经常忍不住折腾各种东西,于是有一天看中了我手上的华为路由器.什么?华为路由器,你可能有这样的疑问,华为路由器不是自研的芯片吗,就像我手上这台 ...

  2. C++判断月份天数(判断闰年)

    题目描述 输入年份和月份,输出这一年的这一月有多少天.需要考虑闰年. 输入格式 无 输出格式 无 输入输出样例 输入 #1 输出 #1 1926 8 31 输入 #2 输出 #2 2000 2 29 ...

  3. Java初学者作业——为某超市设计管理系统,需要在控制台展示系统菜单,菜单之间可以完成跳转。

    返回本章节 返回作业目录 需求说明: 为某超市设计管理系统,需要在控制台展示系统菜单,菜单之间可以完成跳转. 实现思路: 定义mainMenu方法,用于显示主菜单. 主菜单主要负责显示4个选项,分别是 ...

  4. pod存在,但是deployment和statefulset不存在

    pod存在,但是deployment和statefulset不存在 这样的话,可以看一下是不是ReplicaSet, kubectl get ReplicaSet  -n iot

  5. Kafka基础教程(四):.net core集成使用Kafka消息队列

    .net core使用Kafka可以像上一篇介绍的封装那样使用(Kafka基础教程(三):C#使用Kafka消息队列),但是我还是觉得再做一层封装比较好,同时还能使用它做一个日志收集的功能. 因为代码 ...

  6. 【操作系统】I/O多路复用 select poll epoll

    @ 目录 I/O模式 I/O多路复用 select poll epoll 事件触发模式 I/O模式 阻塞I/O 非阻塞I/O I/O多路复用 信号驱动I/O 异步I/O I/O多路复用 I/O 多路复 ...

  7. CSS基础 华为渐变色产品列表 综合实战

    华为网页链接:https://www.huawei.com/cn/?ic_medium=direct&ic_source=surlent html代码部分: <div class=&qu ...

  8. MYSQL实现上一条下一条功能

    select id from(select *, (@i:=@i+1) as rownum from pre_bet_zhibo,(select @i:=0) as itwhere link_cone ...

  9. Python实战案例系列(一)

    本节目录 烟草扫码数据统计 奖学金统计 实战一.烟草扫码数据统计 1. 需求分析 根据扫码信息在数据库文件中匹配相应规格详细信息,并进行个数统计 条码库.xls 扫码.xlsx 一个条码对应多个规格名 ...

  10. 网络协议学习笔记(七)流媒体协议和P2P协议

    概述 上一篇讲解了http和https的协议的相关的知识,现在我们谈一下流媒体协议和P2P协议. 流媒体协议:如何在直播里看到美女帅哥 最近直播比较火,很多人都喜欢看直播,那一个直播系统里面都有哪些组 ...