一、测试环境

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. react hooks 如何自定义组件(react函数组件的封装)

    前言 这里写一下如何封装可复用组件.首先技术栈 react hooks + props-type + jsx封装纯函数组件.类组件和typeScript在这不做讨论,大家别白跑一趟. 接下来会说一下封 ...

  2. pandas tutorial

    目录 Series 利用dict来创建series 利用标量创建series 取 Dataframe 利用dict创建dataframe 选择 添加列 列移除 行的选择, 添加, 移除 Panel B ...

  3. freeswitch APR-UTIL库线程池实现分析

    概述 freeswitch的核心源代码是基于apr库开发的,在不同的系统上有很好的移植性. APR库在之前的文章中已经介绍过了,APR-UTIL库是和APR并列的工具库,它们都是由APACHE开源出来 ...

  4. [C/C++]linux下c-c++语法知识点归纳和总结

    1.c/c++申请动态内存 在c++中,申请动态内存是使用new和delete,这两个关键字实际上是运算符,并不是函数. 而在c中,申请动态内存则是使用malloc和free,这两个函数是c的标准库函 ...

  5. CapstoneCS5212|DP to VGA|CS5212设计电路方案

    CS5212功能概述 CS5212是一款DisplayPort端口到VGA转换器,它结合了DisplayPort输入接口和模拟RGB DAC输出接口.嵌入式单片机基于工业标准8051核心. CS521 ...

  6. Android物联网应用程序开发(智慧城市)—— 查询购物信息界面开发

    效果: 布局代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  7. 使用JavaScript控制HTML元素的显示和隐藏

    利用来JS控制页面控件显示和隐藏有两种方法,两种方法分别利用HTML的style中的两个属性,两种方法的不同之处在于控件隐藏后是否还在页面上占空位. 方法一: document.getElementB ...

  8. Zookeeper基础教程(六):.net core使用Zookeeper

    Demo代码已提交到gitee,感兴趣的更有可以直接克隆使用,地址:https://gitee.com/shanfeng1000/dotnetcore-demo/tree/master/Zookeep ...

  9. django 使用createView创建视图是form_valid()没有通过?

    django 使用createView创建视图是form_valid()没有通过的原因: fields中定义的字段要与from表单中的字段相对应 修改后 接着又报错: 查看没有取到id,最后通过req ...

  10. xshell 所选的用户密钥未在远程主机上注册;无法加载密钥

    他山之石 https://zhuanlan.zhihu.com/p/92528287 安全起见,服务器最近的安全策略准备进行更改,逐渐由原来的密码登录更换为密钥登录认证. 于是今天把服务器上的id_r ...