一聚教程网:一个值得你收藏的教程网站

热门教程

MongoDB string字段索引方案

时间:2022-06-29 10:32:39 编辑:袖梨 来源:一聚教程网


在研究MongoDB的索引是发现一个奇怪的问题,给一个string类型的field设置 text索引 ,但是在查询的时候并没有使用索引。比如:

db.tomcat_access_logs.ensureIndex( { url : 'text' });

db.tomcat_access.logs.find( { url : '1' } ).explain();
db.tomcat_access_logs.find( { url : /1/ } ).explain();

{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 0,
    "nscannedObjects" : 100,
    "nscanned" : 100,
    "nscannedObjectsAllPlans" : 100,
    "nscannedAllPlans" : 100,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    ...
}
从 explain() 的结果可以发现,在查询的时候只用了 BasicCursor ,也就是说没有使用索引。

后发现只有当使用 $text 查询的时候才会用到 text索引 :

db.tomcat_access_logs.find( { $text : { $search : '1'} } ).explain();

{
    "cursor" : "TextCursor",
    "n" : 0,
    "nscannedObjects" : 0,
    "nscanned" : 0,
    "nscannedObjectsAllPlans" : 0,
    "nscannedAllPlans" : 0,
    "scanAndOrder" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    ...
}
只不过这样的话,就没有办法针对某个特定field进行查询了,因为 $text 是对所有 text索引 的field进行的全文搜索。此时只需要做一般的索引即可:

db.tomcat_access_logs.ensureIndex( { url : 1 } );

db.tomcat_access.logs.find( { url : '1' } ).explain();
db.tomcat_access.logs.find( { url : /.*1.*/g } ).explain();
{
    "cursor" : "BtreeCursor url_1",
    "isMultiKey" : false,
    "n" : 0,
    "nscannedObjects" : 0,
    "nscanned" : 100,
    "nscannedObjectsAllPlans" : 0,
    "nscannedAllPlans" : 100,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 1,
    "indexBounds" : {
        "url" : [
            [
                "",
                {
                   
                }
            ],
            [
                /.*1.*/,
                /.*1.*/
            ]
        ]
    },
    ...
}

总结

使用 db.collection.find( { url : '1'} ) 或者 db.collection.find( { url : /.*a.*/} ) ,不会使用的text索引,而是一般索引。

建立了text索引后,只能对text索引包含的所有字段进行全文搜索,无法对某个字段进行搜索

一般索引和text索引可以同时建立,以满足不同查询需求


索引的效率

MongoDB的索引到底能不能提高查询效率呢?我们在这里通过一个例子来测试。比较同样的数据在无索引和有索引的情况下的查询速度。

首先,我们通过这样一个方法插入10W条数据:

public void InsertBigData()
{
    var random = new Random();
    for (int i = 1; i < 100000; i++)
    {
        Document doc = new Document();
 
        doc["ID"] = i;
        doc["Data"] = "data" + random.Next(100000);
 
        mongoCollection.Save(doc);
    }
 
    Console.WriteLine("当前有" + mongoCollection.FindAll().Documents.Count() + "条数据");
}

然后,实现一个方法用来创建索引:

public void CreateIndexForData()
{
    mongoCollection.Metadata.CreateIndex(new Document { { "Data", 1 } }, false);
}

还有排序的方法:

public void SortForData()
{
    mongoCollection.FindAll().Sort(new Document { { "Data", 1 } });
}

运行测试代码如下:

static void Main(string[] args)
{
    IndexBLL indexBll = new IndexBLL();
    indexBll.DropAllIndex();
    indexBll.DeleteAll();
    indexBll.InsertBigData();
 
    Stopwatch watch1 = new Stopwatch();
    watch1.Start();
    for (int i = 0; i < 1; i++) indexBll.SortForData();
    Console.WriteLine("无索引排序执行时间:" + watch1.Elapsed);
 
    indexBll.CreateIndexForData();
 
    Stopwatch watch2 = new Stopwatch();
    watch2.Start();
    for (int i = 0; i < 1; i++) indexBll.SortForData();
    Console.WriteLine("有索引排序执行时间:" + watch2.Elapsed);
 
}

最后执行程序查看结果:  

多次测试表明在有索引的情况下,查询效率要高于无索引的效率。

热门栏目