我使用 elasticsearch-php 在 elasticsearch 上运行基准测试。 我将 10 000 个索引逐个与 10 000 与 1 000 个文档的批量进行比较。
在我的 vpn 服务器 3 核 2 Gb 内存上,无论有没有批量索引,性能都完全相同。
我的 php 代码(灵感来自 à post):
<?php
set_time_limit(0); // no timeout
require 'vendor/autoload.php';
$es = new Elasticsearch\Client([
'hosts'=>['127.0.0.1:9200']
]);
$max = 10000;
// ELASTICSEARCH BULK INDEX
$temps_debut = microtime(true);
for ($i = 0; $i <= $max; $i++) {
$params['body'][] = array(
'index' => array(
'_index' => 'articles',
'_type' => 'article',
'_id' => 'cle' . $i
)
);
$params['body'][] = array(
'my_field' => 'my_value' . $i
);
if ($i % 1000) { // Every 1000 documents stop and send the bulk request
$responses = $es->bulk($params);
$params = array(); // erase the old bulk request
unset($responses); // unset to save memory
}
}
$temps_fin = microtime(true);
echo 'Elasticsearch bulk: ' . round($i / round($temps_fin - $temps_debut, 4)) . ' per sec <br>';
// ELASTICSEARCH WITHOUT BULK INDEX
$temps_debut = microtime(true);
for ($i = 1; $i <= $max; $i++) {
$params = array();
$params['index'] = 'my_index';
$params['type'] = 'my_type';
$params['id'] = "key".$i;
$params['body'] = array('testField' => 'valeur'.$i);
$ret = $es->index($params);
}
$temps_fin = microtime(true);
echo 'Elasticsearch One by one : ' . round($i / round($temps_fin - $temps_debut, 4)) . 'per sec <br>';
?>
Elasticsearch 批量:每秒 1209 个 Elasticsearch 一个接一个:1197per sec
我的批量索引是否有问题以获得更好的性能?
谢谢
最佳答案
替换:
if ($i % 1000) { // Every 1000 documents stop and send the bulk request
与:
if (($i + 1) % 1000 === 0) { // Every 1000 documents stop and send the bulk request
或者您将查询每个非 0 值(即 1000 中的 999)...
显然,这仅在 $max
是 1000 的倍数时有效。
此外,更正此错误:
for ($i = 0; $i <= $max; $i++) {
将遍历 $max + 1
项。将其替换为:
for ($i = 0; $i < $max; $i++) {
初始化 $params
的方式也可能存在问题。你不应该在循环之外设置它并且只在每次 ->bulk()
之后清理 $params['body']
吗?当您使用 $params = array();
重置时,您将失去所有这些。
此外,请记住 ES 可能分布在一个集群上。然后可以将批量操作分配给工作负载。因此,某些性能扩展在单个物理节点上是不可见的。
关于php - Elasticsearch PHP 批量索引性能与索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30558158/