【发布时间】:2016-12-19 09:06:50
【问题描述】:
我在 mongoDB 中有 4000 万条数据。我正在从集合中并行读取该数据,对其进行处理并转储到另一个集合中。
作业初始化示例代码。
ExecutorService executor = Executors.newFixedThreadPool(10);
int count = total_number_of_records in reading collection
int pageSize = 5000;
int counter = (int) ((count%pageSize==0)?(count/pageSize):(count/pageSize+1));
for (int i = 1; i <= counter; i++) {
Runnable worker = new FinalParallelDataProcessingStrategyOperator(mongoDatabase,vendor,version,importDate,vendorId,i,securitiesId);
executor.execute(worker);
}
每个线程都在做以下事情
public void run() {
try {
List<SecurityTemp> temps = loadDataInBatch();
populateToNewCollection(temps);
populateToAnotherCollection(temps);
} catch (IOException e) {
e.printStackTrace();
}
}
使用以下查询对加载数据进行分页
mongoDB.getCollection("reading_collection").find(whereClause).
.skip(pagesize*(n-1)).limit(pagesize).batchSize(1000).iterator();
机器配置: 2 个 CPU,每个 CPU 1 个核心
并行实现提供与顺序实现几乎相同的性能。 数据子集的统计信息(319568 条记录)
No. of Threads Execution Time(minutes)
1 16
3 15
8 17
10 17
15 16
20 12
50 30
如何提高此应用程序的性能?
【问题讨论】:
-
增加线程数不会自动提高性能,过多的线程会导致开销问题。很难说为什么你在 1 - 10 个线程上有相同的性能,也许你的瓶颈是数据库?是本地数据库吗?
-
也可以是JVM配置,如果它运行在一个只能访问一个核心的隔离环境中,那么你也不会看到太大的改进。
-
是的,它是本地数据库
标签: java multithreading mongodb processor cpu-cores