我正在使用postgreSQL
,并试图将Elasticsearch
的功能引入我们的系统。我使用此算法将数据(每次1000行的数据(大容量插入Elasticsearch。问题是它非常非常慢,仅20000行的数据就花了大约15小时。
根据我的估计,仅此表的索引大约需要26天(约有1200万条记录(。
有可能以某种方式优化这种方法并创建一个更快的方法吗?这就是我目前正在做的:
public function run()
{
$es_client = new ElasticaClient();
$es_index = $es_client->getIndex("vehicle");
$es_type = $es_index->getType("_doc");
$vehicle_ins = new Vehicle;
$step = 1000;
$min_vehicle_id = $vehicle_ins->query()->min('id');
$max_vehicle_id = $vehicle_ins->query()->max('id');
$insert_counter = 1;
$docs = [];
for ($i = $min_vehicle_id ; $i <= $max_vehicle_id ; $i += $step) {
$x = $i;
$y = $i + $step;
$vehicles = $vehicle_ins->query()
->where('id', '>=', $x)
->where('id', '<', $y)
->get();
foreach ($vehicles as $vehicle) {
$docs[] = new ElasticaDocument(
$vehicle->id,
[
// implementing my columns (91 columns)
]);
echo ".";
if ($insert_counter % $step == 0) {
$es_type->addDocuments($docs);
$es_type->getIndex()->refresh();
$docs = [];
echo "n";
echo $step . " rows inserted!";
echo "n";
}
$insert_counter++;
}
}
if (!empty($docs)) {
$es_type->addDocuments($docs);
$es_type->getIndex()->refresh();
$docs = [];
}
}
p.S:我使用elastica
来处理Elasticsearch
,应用程序在Laravel 5.7
中,postgreSQL
是主数据库。
p.S:这种方法也在ElasticSearch网站中提出,但对于我正在处理的数据量来说,它仍然太慢了。
问题是因为我的Vehicle model
中的一些方法在从数据库中获取每个属性后对其进行了一些更改,而这些不需要的更改使过程非常缓慢。
解决方案是使用Laravel DB facade
查询数据,直到这些属性更改方法从车型中删除或转移到其他地方。
所以查询应该是这样的:
$min_vehicle_id = IlluminateSupportFacadesDB::table('vehicle')->min('id');
$max_vehicle_id = IlluminateSupportFacadesDB::table('vehicle')->max('id');
这个用于获取数据:
$vehicles = IlluminateSupportFacadesDB::table('vehicle')
->where('id', '>=', $x)
->where('id', '<', $y)
->get();
尝试将映射添加到索引。还要检查是否需要对每个字段进行分析-https://www.elastic.co/guide/en/elasticsearch/reference/2.3/mapping-index.html