Spring-Data-MongoDB - 更新需要几秒钟才能完成?

时间:2014-07-14 09:49:54

标签: mongodb spring-data-mongodb

我使用spring数据mongodb与我的mongodb设置进行交互。我正在测试不同的写入问题,并注意到未确认的写入问题,更新1000个文档的时间大约为5-6秒,即使未确认的写入问题并不等待任何确认。 我使用原始java驱动程序进行了相同的测试,时间大约为40毫秒。

原始java驱动程序和spring数据mongodb update之间的巨大时差可能是什么原因?

请注意,我使用的是未确认的写入问题,mongodb v2.6.1使用默认配置。

添加用于比较的代码: -

Raw Java驱动程序代码: -

        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB( "testdb" );

        DBCollection collection = db.getCollection("product");
        WriteResult wr = null;
        try {
            long start = System.currentTimeMillis();
            wr = collection.update(
                    new BasicDBObject("productId", new BasicDBObject("$gte", 10000000)
                                                       .append("$lt", 10001000)),
                    new BasicDBObject("$inc", new BasicDBObject("price", 100)),
                    false, true, WriteConcern.UNACKNOWLEDGED);
            long end = System.currentTimeMillis();

            System.out.println(wr + "  Time taken: " + (end - start) + " ms.");
        }

春季代码: - 的Config.xml

<mongo:mongo host="localhost" port="27017" />

<mongo:db-factory dbname="testdb" mongo-ref="mongo" />

<bean id="Unacknowledged" class="com.mongodb.WriteConcern">
    <constructor-arg name="w" type="int" value="0"/>
</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <property name="writeConcern" ref="Unacknowledged"/>
</bean>

更新功能的Java代码,它是ProductDAOImpl的一部分: -

public int update(long fromProductId, long toProductId, double changeInPrice)
{
    Query query = new Query(new Criteria().andOperator(
                            Criteria.where("productId").gte(fromProductId),
                            Criteria.where("productId").lt(toProductId)));

    Update update = new Update().inc("price", changeInPrice);

    WriteResult writeResult =
        mongoTemplate.updateMulti(query, update, Product.class);

    return writeResult.getN();
}

访问代码: -

    ProductDAOImpl productDAO = new ProductDAOImpl();

    productDAO.setMongoTemplate(mongoTemplate);

    long start = System.currentTimeMillis();
    productDAO.update(10000000, 10001000, 100);
    long end = System.currentTimeMillis();

    System.out.println("Time taken = " + (end - start) + " ms.");

架构: -

   {
       "_id" : ObjectId("53b64d000cf273a0d95a1a3d"),
       "_class" : "springmongo.domain.Product",
       "productId" : NumberLong(6),
       "productName" : "product6",
       "manufacturer" : "company30605739",
       "supplier" : "supplier605739",
       "category" : "category30605739",
       "mfgDate" : ISODate("1968-04-26T05:00:00.881Z"),
       "price" : 665689.7224373372,
       "tags" : [
              "tag82",
              "tag61",
              "tag17"
       ],
       "reviews" : [
              {
                     "name" : "name528965",
                     "rating" : 6.5
              },
              {
                     "name" : "name818975",
                     "rating" : 7.5
              },
              {
                     "name" : "name436239",
                     "rating" : 3.9
              }
       ],
       "manufacturerAdd" : {
              "state" : "state55",
              "country" : "country155",
              "zipcode" : 718
       },
       "supplierAdd" : {
              "state" : "state69",
              "country" : "country69",
              "zipcode" : 691986
       }
   }

希望它有所帮助。

0 个答案:

没有答案