我有一個(gè)代碼可以按名稱在 MongoDB 中搜索人員。我的代碼的問題是它沒有擴(kuò)展,我試圖用 40 個(gè)線程運(yùn)行這段代碼,但是當(dāng)我在我的數(shù)據(jù)庫中看到反應(yīng)時(shí),我不得不關(guān)閉它,因?yàn)樗加昧?100% 的 CPU。正在搜索的所有三個(gè)字段都在 MongoDB 中建立索引。有沒有辦法改進(jìn)查詢?我需要運(yùn)行 500 萬個(gè)名字,而我的數(shù)據(jù)庫有超過 2 億人。public Person searchPerson(string name){ string MongoUser = "MONGO_USERNAME"; string MongoPass = "MONGO_PASSWORD"; string MongoURL = "MONGO_URL"; string MongoDB = "MONGO_DB"; MongoClient _client = new MongoClient("mongodb://" + MongoUser + ":" + MongoPass + "@" + MongoURL); IMongoCollection<BazingaPerson> myCollection = _client.GetDatabase (MongoDB).GetCollection<Person> ("COLLECTION_NAME"); List<Person> peopleList = myCollection.AsQueryable<Person>().Where(e => e.Name == name).ToList<Person>(); // both functions below only transform string like replace, substring or splits . They dont query in a DB or make web requests string nameInitials = getInitials(name); string phoneticName = getPhoneticName(name); if(peopleList.Count() == 0) peopleList = myCollection.AsQueryable<Person>().Where(e => e.StandardName.Equals (phoneticName)).ToList<Person>(); if(peopleList.Count() == 0) peopleList = myCollection.AsQueryable<Person>().Where(e => e.Initials.Equals (nameInitials)).ToList<Person>(); if(peopleList.Count() == 0) return null; return peopleList[0];}
- 2 回答
- 0 關(guān)注
- 181 瀏覽
添加回答
舉報(bào)
0/150
提交
取消