- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
meta_map = {}
results = db.meta.find({'corpus_id':id, 'method':method}) #this Mongo query only takes 3ms
print results.explain()
#result is mongo queryset of 2000 documents
count = 0
for r in results:
count += 1
print count
word = r.get('word')
data = r.get('data',{})
if not meta_map.has_key(word):
meta_map[word] = data
return meta_map
由于某种原因,这 super 、 super 慢。
总共有 2000 个结果。下面是一个 result
文档的示例(来自 Mongo)。所有其他结果的长度都相似。
{ "word" : "articl", "data" : { "help" : 0.42454812322341984, "show" : 0.24099054286865948, "lack" : 0.2368313038407821, "steve" : 0.20491936823259457, "gb" : 0.18757527934987422, "feedback" : 0.2855335862138559, "categori" : 0.28210549642632016, "itun" : 0.23615623082085788, "articl" : 0.21378509220044106, "black" : 0.22720575131038662, "hidden" : 0.26172127252557625, "holiday" : 0.27662433827306804, "applic" : 0.1802411089325281, "digit" : 0.20491936823259457, "sourc" : 0.21909218369809863, "march" : 0.2632736571995878, "ceo" : 0.2153108869289692, "donat" : 1, "volum" : 0.2572042432755638, "octob" : 0.2802470156773559, "toolbox" : 0.2153108869289692, "discuss" : 0.26973295489368615, "list" : 0.3698592948408095, "upload" : 0.1802411089325281, "random" : 1, "default" : 0.33044754314072383, "februari" : 0.2899936154686609, "januari" : 0.25228424754983525, "septemb" : 0.1802411089325281, "page" : 0.24675067183234803, "view" : 0.20019523259334138, "pleas" : 0.2839965947961194, "mdi" : 0.2731217555354, "unsourc" : 0.2709524603813144, "direct" : 0.18757527934987422, "dead" : 0.22720575131038662, "smartphon" : 0.2839965947961194, "jump" : 0.3004203939398161, "see" : 0.33044754314072383, "design" : 0.2839965947961194, "download" : 0.19574598998663462, "home" : 0.3004203939398161, "event" : 0.651573574681647, "wikipedia" : 0.21909218369809863, "content" : 0.2471475889083912, "version" : 0.42454812322341984, "gener" : 0.3004203939398161, "refer" : 0.2188507485718582, "navig" : 0.27662433827306804, "june" : 0.2153108869289692, "screen" : 0.27662433827306804, "free" : 0.22720575131038662, "job" : 0.19574598998663462, "key" : 0.3004203939398161, "addit" : 0.22484486630589545, "search" : 0.2878804276884952, "current" : 0.5071530767683105, "worldwid" : 0.20491936823259457, "iphon" : 0.2230524329516571, "action" : 0.24099054286865948, "chang" : 0.18757527934987422, "summari" : 0.33044754314072383, "origin" : 0.2572042432755638, "softwar" : 0.651573574681647, "point" : 0.27662433827306804, "extern" : 0.22190187748860113, "mobil" : 0.2514880028687207, "cloud" : 0.18757527934987422, "use" : 0.2731217555354, "log" : 0.27662433827306804, "commun" : 0.33044754314072383, "interact" : 0.5071530767683105, "devic" : 0.3004203939398161, "long" : 0.2839965947961194, "avail" : 0.19574598998663462, "appl" : 0.24099054286865948, "disambigu" : 0.3195885490528538, "statement" : 0.2737499468972353, "namespac" : 0.3004203939398161, "season" : 0.3004203939398161, "juli" : 0.27243508666247285, "relat" : 0.19574598998663462, "phone" : 0.26973295489368615, "link" : 0.2178125232318433, "line" : 0.42454812322341984, "pilot" : 0.27243508666247285, "account" : 0.2572042432755638, "main" : 0.34870313981256423, "provid" : 0.2153108869289692, "histori" : 0.2714135089366041, "vagu" : 0.24875213214603717, "featur" : 0.24099054286865948, "creat" : 0.26645207330844684, "ipod" : 0.2230524329516571, "player" : 0.20491936823259457, "io" : 0.2447908314834019, "need" : 0.2580912994161046, "develop" : 0.27662433827306804, "began" : 0.24099054286865948, "client" : 0.19574598998663462, "also" : 0.42454812322341984, "cleanup" : 0.24875213214603717, "split" : 0.26973295489368615, "tool" : 0.2878804276884952, "product" : 0.42454812322341984, "may" : 0.2676701118192027, "assist" : 0.1802411089325281, "variant" : 0.2514880028687207, "portal" : 0.3004203939398161, "user" : 0.20491936823259457, "consid" : 0.27662433827306804, "date" : 0.2731217555354, "recent" : 0.24099054286865948, "read" : 0.2572042432755638, "reliabl" : 0.2388872270166464, "sale" : 0.22720575131038662, "ambigu" : 0.23482106920048526, "person" : 0.260801274024785, "contact" : 0.24099054286865948, "encyclopedia" : 0.2153108869289692, "time" : 0.2368313038407821, "model" : 0.24099054286865948, "audio" : 0.19574598998663462 }}
整个过程大约需要 15 秒...什么鬼?我怎样才能加快速度? :)
编辑:我意识到当我在控制台中打印计数时,它会非常快地从 0 变为 101,然后卡住 10 秒,然后从 102 继续到 2000
这可能是 MongoDB 的问题吗?
编辑 2:我打印了下面查询的 Mongo EXPLAIN():
{u'allPlans': [{u'cursor': u'BtreeCursor corpus_id_1_method_1_word_1',
u'indexBounds': {u'corpus_id': [[u'iphone', u'iphone']],
u'method': [[u'advanced', u'advanced']],
u'word': [[{u'$minElement': 1},
{u'$maxElement': 1}]]}}],
u'cursor': u'BtreeCursor corpus_id_1_method_1_word_1',
u'indexBounds': {u'corpus_id': [[u'iphone', u'iphone']],
u'method': [[u'advanced', u'advanced']],
u'word': [[{u'$minElement': 1}, {u'$maxElement': 1}]]},
u'indexOnly': False,
u'isMultiKey': False,
u'millis': 3,
u'n': 2443,
u'nChunkSkips': 0,
u'nYields': 0,
u'nscanned': 2443,
u'nscannedObjects': 2443,
u'oldPlan': {u'cursor': u'BtreeCursor corpus_id_1_method_1_word_1',
u'indexBounds': {u'corpus_id': [[u'iphone', u'iphone']],
u'method': [[u'advanced', u'advanced']],
u'word': [[{u'$minElement': 1},
{u'$maxElement': 1}]]}}}
这些是 mongo 集合的统计数据:
> db.meta.stats();
{
"ns" : "inception.meta",
"count" : 2450,
"size" : 3001068,
"avgObjSize" : 1224.9257142857143,
"storageSize" : 18520320,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 13893632,
"paddingFactor" : 1.009999999999931,
"flags" : 1,
"totalIndexSize" : 368640,
"indexSizes" : {
"_id_" : 114688,
"corpus_id_1_method_1_word_1" : 253952
},
"ok" : 1
}
> db.meta.getIndexes();
[
{
"name" : "_id_",
"ns" : "inception.meta",
"key" : {
"_id" : 1
},
"v" : 0
},
{
"ns" : "inception.meta",
"name" : "corpus_id_1_method_1_word_1",
"key" : {
"corpus_id" : 1,
"method" : 1,
"word" : 1
},
"v" : 0
}
]
最佳答案
代替
if not meta_map.has_key(word):
你应该使用
if word not in meta_map:
如果你不打算使用 data = r.get('data',{})
,那么它是没有意义的。
不清楚你为什么要这样做 word = r.get('word')
...如果 r
中总是存在'word',你应该使用word = r['word']
;否则你应该在get之后测试word
是否为None
。
同样获取数据。
试试这个:
for r in results:
word = r['word']
if word not in meta_map:
meta_map[word] = r['data']
无论如何,你引用的时间是巨大的......那里肯定有其他事情发生。我很想看到您的代码用于计时并计算 results
中的条目数。
关于python - 这需要很长时间......我如何加快这本词典的速度? (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6714438/
我正在尝试这样做: var myBeacons: [NSUUID: [Int]] = [NSUUID(UUIDString:"74278BDA-B644-4520-8F0C-720EAF059935"
我的字典有问题。如果我将一个对象添加到字典中,它会用添加的项目覆盖整个包含项目。 添加所有元素后,Dictionary 包含正确数量的项目,但项目都是最后添加的项目。 For Each shp In
我使用字典,我将有大约一百万个条目,我将定期添加、删除、编辑和轮询..我想知道所有条目的上/下边是什么,如果有一种更高效的方式。 最佳答案 这取决于你想做什么。如果您想要一个具有快速插入、查找和删除功
我在 Swift 类中的字典数组方面遇到问题。我的代码无法在类或结构中运行,但可以在外部运行。 var data = [Dictionary]() data.append([123: "test"])
有没有一种方法可以添加注释来记录 Dictionary 或 ConcurrentDictionary 以了解键/值的含义? 例如: Dictionary _users; 这个例子有一个用户字典。 gu
我正在基于 Android AOSP LatinIME 项目创建自己的输入法应用。我设法找到了一些用于自动更正和预测的字典文件(main_en.dict、main_fr.dict 等)。 但对于许多其
我已经通过 Locale::Maketext 使我的网站支持多种语言(或更具体地说是 CatalystX::I18N::Model::Maketext )。 我的 maketext 类在编译时通过从数
我不会说英语,而且我的英语也不是很好。我自以为是。我没有和其他人一起在一个共同的代码库上工作过。我没有任何编程的 friend 。我不与其他程序员一起工作(至少没有人关心这些事情)。 我想这可能解释了
我需要做 currentKey+1。所以我想找到键值的索引并获取下一个键(如果在末尾则为第一个)。我如何找到 key 的当前索引? 我正在使用 Dictionary我用 Linq 查找 .Find 或
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我使用 python 2.7 中的 shelve 模块保存了一个数据文件,该文件不知何故已损坏。我可以用 db = shelve.open('file.db') 加载它,但是当我调用 len(db)
我想试试这个抽认卡的想法,为即将到来的测试尝试学习关键字及其含义。我想在 python 上创建一个字典,我可以用它来帮助解决这个问题。这个想法是向我显示定义,然后我必须猜测已定义的词。我在下面展示了如
当尝试 .format() 一次列表中的多个词典时,控制台会给我一个 AttributeError:'list' object has no attribute 'items'。 我尝试滚动浏览提示的
我在公共(public)类(class)中有一个公共(public)词典如下: namespace ApiAssembly { public static class TypeStore
我需要做 currentKey+1。所以我想找到键值的索引并获取下一个键(如果在末尾则为第一个)。我如何找到 key 的当前索引? 我正在使用 Dictionary我用 Linq 查找 .Find 或
我的字典总是零,想了解为什么会这样。我的代码: var dic = [NSDate : MCACalendar]?() dic?[currentDate!] = calendar 最佳答案 @Kirs
给定(简化描述) 我们的一项服务在内存中有很多实例。大约 85% 是独一无二的。我们需要对这些项目进行非常快速的基于键的访问,因为它们在单个堆栈/调用中被非常频繁查询。这个单一上下文的性能得到了极大的
我想为“Sinhala Language speech recognition”僧伽罗语建立新的声学模型、新词典、新语言模型字符是基于 Unicode 的。例如 A=අ,I=ඉ,U=උ,KA=ක,BA
我需要一个带有 的正面和负面词的列表重量 根据单词的强度和周数分配单词。我有 : 1.) WordNet - 它为每个单词提供 + 或 - 分数。 2.) SentiWordNet - 在 [0,1]
我有一个 Jinja2 字典,我想要一个可以修改它的表达式 - 通过更改其内容或与另一个字典合并。 >>> import jinja2 >>> e = jinja2.Environment() 修改字
我是一名优秀的程序员,十分优秀!