duankaolei2921 2011-11-30 12:25
浏览 41
已采纳

MongoDB的性能各不相同

i have a mongo collection like this:


{
"A2_AboutMe": "",
"A2_Attributes": "|av|nv|",
"A2_Birthday": "",
"A2_DateCreated": "2010-11-25 22: 59: 00",
"A2_DateLast": "2011-11-18 12: 09: 36",
"A2_FK_A1_IDPerson": "0",
"A2_Firstname": "José Luis",
"A2_FirstnameC": "Jose Luis",
"A2_Gender": "m",
"A2_IDProfile": "1",
"A2_Keywords": "...|..",
"A2_Lastname": "test - test",
"A2_LastnameC": "_test test",
"A2_Locale": "",
"A2_Middlename": "",
"A2_Name": "José Luis test",
"A2_NameC": "Jose Luis test",
...
}

with indexies on A2_LastnameC and A2_FirstnameC 3.000.000 docs in this collection, 8 GB data storage

following query(PHP) in done in 3-4 sec

$collection->find(array(«A2_FirstnameC» => new MongoRegex("/jose/i")))->sort(array(«A2_LastnameC» => -1))->limit(10)

but sometimes the similar queries are done in less than 100 msec.

what can i do to get this performance each time?

test computer is i7, 8GB Ram(7 is used by mongo), Windows 7

  • 写回答

2条回答 默认 最新

  • douding6266 2011-11-30 12:57
    关注

    First of all index won't be used for non-prefix-like, case-insensitive regular expressions. But in the query above index can be used for sorting by A2_LastnameC field so this is fast. Now having the sorted data MongoDB will need to get A2_FirstnameC value and match it against the regexp stopping when there's 10 matches ready (it will be also relatively fast because it will use index to retrieve the data instead of reading whole documents from disk). Depending on data order it can happen to match the first 10 documents - this is the best case and it will be very fast, the worst case would be the matches to occur on the last 10 docs having to scan all the previous index entries.

    How to speed this up? Either use query that can use index, like: «A2_FirstnameC» => new MongoRegex("/^jose/"). Or you have to use some kind of full-text search. A simple way would be to split the field (A2_Firstname in your case) into words, normalize them (convert to lower case, replace accents) and store as an array. Now an index for the array field will be used to do fast searches.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容