【发布时间】:2015-07-10 17:13:52
【问题描述】:
以下代码从两个表 table1 和 table2 中提取数据,在字段 3 上对它们执行 JOIN 并将其索引到 Elasticsearch。需要索引的总数或行数约为 5 亿。该代码在一小时内插入了 500 万条记录,因此这样需要 100 小时才能完成。有什么方法可以让它更快吗?
public static void selection()
{
Uri node = new Uri("http://localhost:9200");
ConnectionSettings settings = new ConnectionSettings(node);
ElasticClient client = new ElasticClient(settings);
int batchsize = 100;
string query = "select table1.field1, table2.field2 from table1 JOIN table2 ON table1.field3=table2.field3";
try
{
OracleCommand command = new OracleCommand(query, con);
OracleDataReader reader = command.ExecuteReader();
List<Record> l = new List<Record>(batchsize);
string[] str = new string[2];
int currentRow = 0;
while (reader.Read())
{
for (int i = 0; i < 2; i++)
str[i] = reader[i].ToString();
l.Add(new Record(str[0], str[1]));
if (++currentRow == batchsize)
{
Commit(l, client);
l.Clear();
currentRow = 0;
}
}
Commit(l, client);
}
catch(Exception er)
{
Console.WriteLine(er.Message);
}
}
public static void Commit(List<Record> l, ElasticClient client)
{
BulkDescriptor a = new BulkDescriptor();
foreach (var x in l)
a.Index<Record>(op => op.Object(x).Index("index").Type("type"));
var res = client.Bulk(d => a);
Console.WriteLine("100 records more inserted.");
}
感谢任何帮助! :)
【问题讨论】:
-
不,我已经实施了该问题的解决方案中建议的技术。我想进一步提高速度。
-
您是否尝试更改批量块大小? elastic.co/guide/en/elasticsearch/guide/current/…
-
@Rob 我确实尝试过这样做,我将块大小从 100 更改为 10,000。我的表中也有大量列,有什么方法可以更快地获取 50 列?
标签: c# oracle elasticsearch nest