我需要知道一個對象是否存在於我正在使用的db中。我知道我可以使用query.count來測試結果是否爲零,否則它不存在。但有人告訴我,計數很貴,所以我認爲query.first可能更有效。這是真的,測試db中對象存在的最好方法是什麼?使用find,count或first in parse來測試是否存在
0
A
回答
0
查詢首先將不計入你的結果,你可以使用查詢發現,然後如果使用array.length測試數組的長度大於0或不
0
這將是有益的,如果你表現出一定的代碼,但是這是太好玩了一個問題就過去了。
測試代碼在下面,但我的非科學答案是:只要查詢索引良好,它可能無所謂你使用哪種方法。當你計算大量的對象時,計數是很昂貴的,但如果你只是想找到一個並且查詢被索引,我認爲它不會很昂貴。
好的,現在有些代碼。我在我的parse-server repo中編譯了一些單元測試,以測試我能想到的三種測試存在的方式。在所有三種情況下,我使用的可能是特殊的objectId,因此您可以輕鬆調整它。我也只做了一次每個測試,而要做到科學性,我們想每次做100次,平均每次測試。但是之後我們需要考慮查詢緩存,所以我只是保持簡單,並且可以使用相同的模式對自己的數據進行真實測試。
好了,現在我已經給了我所有的注意事項:
describe('way to find if an object exists....', function() {
beforeEach(function (done) {
new Parse.Object('Test')
.save()
.then((testObj) => {
this.testObj = testObj;
done();
})
.catch(done.fail);
});
it('should use get if we know object id', function(done) {
this.start = new Date();
new Parse.Query('Test').get(this.testObj.id)
.then(result => {
console.log('get time exists', new Date() - this.start);
return expect(result.id).toBe(this.testObj.id);
})
.then(() => {
this.start = new Date();
return new Parse.Query('Test').get('not the id');
})
.then(
() => done.fail('we shouldn\'t get here, cause the obj doesn\'t exist'),
// so if the get is rejected you know it doesn't exist.
e => {
console.log('get time does not exist', new Date() - this.start);
expect(e).toEqual(new Parse.Error(101, 'Object not found.'));
})
.then(done)
.catch(done.fail);
});
it('should also work with find', function (done) {
this.start = new Date();
new Parse.Query('Test')
.equalTo('objectId', this.testObj.id)
.find()
.then(result => {
console.log('find time exists', new Date() - this.start);
return expect(result.length).toBe(1);
})
.then(() => {
this.start = new Date();
return new Parse.Query('Test')
.equalTo('objectId', 'not the id')
.find();
})
.then((result) => {
console.log('find time does not exist', new Date() - this.start);
expect(result.length).toEqual(0);
})
.then(done)
.catch(done.fail);
});
it('should also work with count', function (done) {
this.start = new Date();
new Parse.Query('Test')
.equalTo('objectId', this.testObj.id)
.count()
.then(result => {
console.log('count time exists', new Date() - this.start);
return expect(result).toBe(1);
})
.then(() => {
this.start = new Date();
return new Parse.Query('Test')
.equalTo('objectId', 'not the id')
.count();
})
.then((result) => {
console.log('count time does not exist', new Date() - this.start);
expect(result).toEqual(0);
})
.then(done)
.catch(done.fail);
});
});
這將產生這樣的結果:
Jasmine started
way to find if an object exists....
✓ should use get if we know object id
get time exists 19
get time does not exist 8
✓ should also work with find
find time exists 12
find time does not exist 9
✓ should also work with count
count time exists 7
count time does not exist 6
所以在我的漂亮的空,在內存蒙戈,其所有的超級速度極快(那些是毫秒),所以,假設你的查詢索引很好,選擇一個產生最可讀代碼的那個;)
相關問題
- 1. 查找('first'),Find('count')或Field('id')...哪個更快?
- 2. 使用VBA或Excel而不使用「DIR」來測試文件是否存在
- 3. 使用普通測試來檢查實例是否存在
- 4. 使用OCUnit測試是否存在UIAlertView
- 5. 使用模擬來測試目錄是否存在
- 6. 使用sfinae來測試命名空間成員是否存在
- 7. JQuery .not(find())或 - 是否可以使用?
- 8. 是否可以使用string :: find來檢測新的行字符?
- 9. 是否存在不區分大小寫的find-file-in-tags?
- 10. 如何使用這個宏來測試內存是否對齊?
- 11. 測試單元測試是否存在保存方法
- 12. 使用.First()和.Count()打破了IEnumerable?
- 13. 如何使用-not in find?
- 14. unordered_map:哪一個更快find()或count()?
- 15. 單元測試(是或否)
- 16. 'if'是否測試'=='或'!='
- 17. 測試屬性是否存在(即numberOfValidItemsForDrop)
- 18. 您寧願使用測試Build.VERSION.SDK_INT或反射來測試存在嗎?
- 19. 測試文件是否存在
- 20. 測試是否存在javascript小部件或不是
- 21. ,是否存在`int`的`parse` for`bool`?
- 22. 函數測試變量是否存在
- 23. iPhone - 測試是否通知存在
- 24. Perl:測試是否存在類
- 25. 測試是否已經存在
- 26. 測試一個目錄是否存在
- 27. 使用$ in在find()中查詢casbah
- 28. 測試,看是否存在DIV ID
- 29. 測試相關模型是否存在
- 30. 要測試是否存在gmail帳戶
我需要測試存在只有我不需要結果數 –