【问题标题】:Regex query MongoDB Performance issue正则表达式查询 MongoDB 性能问题
【发布时间】:2017-09-16 06:29:28
【问题描述】:

我有一个包含单个字段的 Mongodb 集合,每天我收到 31000 个文档,并且在集合中我有将近 6 个月的数据

这是我的数据在数据库中的样子

 {
"_id" : ObjectId("59202aa3f32dfba00d0773c3"),
"Data" : "20-05-2017 18:38:13 SYSTEM_000_00_SAVING ",
"__v" : 0
 }
 {
"_id" : ObjectId("59202aa3f32dfba00d0773c4"),
"Data" : "20-05-2017 18:38:13 SyTime_000_09_00:00 ",
"__v" : 0
}

这是我的查询代码

 DBObject query = new BasicDBObject();
 Pattern regex = Pattern.compile("20-05-2017"); 
 query.put("Data", regex);

我已经创建了索引,但它仍然很慢

                   [
                {
                    "v" : 1,
                    "key" : {
                        "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "NOB_SRB.fdevices"
                },
                {
                    "v" : 1,
                    "unique" : true,
                    "key" : {
                        "Data" : 1.0
                    },
                    "name" : "Data_1",
                    "ns" : "NOB_SRB.fdevices"
                }
            ]

【问题讨论】:

标签: java mongodb


【解决方案1】:

在正则表达式的开头添加一个输入开始^

Pattern regex = Pattern.compile("^20-05-2017");

因为您的正则表达式没有锚点,所以会在整个字段中搜索日期anywhere,这需要比较字段中的每个字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2013-04-13
    • 2011-11-05
    相关资源
    最近更新 更多