0

I found my MongoDB remove data is very slow, but query or insert action is ok.

This is my shard status:

shards:
{  "_id" : "shard0000",  "host" : "192.168.1.50:27017",  "tags" : [ "global" ] }
{  "_id" : "shard0001",  "host" : "192.168.2.50:27017",  "tags" : [ "china" ] }

The two databases are setup in different countries. But I'm sure I put all data to the same database in my test.

This is the record structure:

{
"_id": ObjectId("56cd5cdae4b0323bc94339e6"),
"eventId": NumberLong(5680),
"opuId": NumberLong(2576),
"eventTime": ISODate("2016-02-24T07:33:46.471Z")},
{
"_id": ObjectId("56cd5cdae4b0323bc94339e7"),
"eventId": NumberLong(5681),
"opuId": NumberLong(2576),
"eventTime": ISODate("2016-02-24T07:34:46.471Z")}

I use spring framework and here is my code:

Query:

@Query(value = "{'eventId' : ?1}")
public Event findByEventId(long eventId);

Delete:

mongoTemplate.remove(query(where("eventId").is(event.getEventId())), Event.class);

Generally, saving or querying 100 records would spend 100ms. But removing 100 records case by case takes over 30 seconds.

But if I use batch delete, it spend less then 50ms. It looks normally.

mongoTemplate.remove(query(where("opuId").is(opuId)), Event.class);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chi Shin Hsu
  • 261
  • 1
  • 2
  • 12
  • do you have index for "opuId" ? if you run db.collection.find({"opuId" : 1234}).explain() which cursoer do you get (default?) the query is running on eventId which is probably indexed or even used as a shard key which probably explain why it is fast. – Yair Harel Feb 24 '16 at 08:28

1 Answers1

0

I found the reason.

Because my shard key id opuId, I edit the query string to:

mongoTemplate.remove(query(where("eventId").is(event.getEventId())
                    .andOperator(where("opuId").is(event.getOpuId()))), Event.class);

But I still can't understand why querying can do without asking shard0001, but removing doesn't.

Chi Shin Hsu
  • 261
  • 1
  • 2
  • 12