2017-10-13 1 views
0

私が使用しているデータベースにオブジェクトが存在するかどうかを知る必要があります。私は結果がゼロであるかどうかをテストするためにquery.countを使用できることを知っています。しかし、誰かがカウントが高価だと私に言ったので、私はquery.firstがより効率的かもしれないと思います。それは本当ですか、そして、dbのオブジェクトの存在をテストする最良の方法は何ですか?find、count、またはfirstを使用して存在をテストする

答えて

0

クエリは、最初のあなたが見つけると、あなたはいくつかのコードを示した場合、それが参考になるより0か

+0

私は存在のみをテストする必要があります。結果の数は必要ありません –

0

場合Array.lengthとを使用して、配列の長さをテストするクエリを使用することができ、あなたの結果をカウントされませんが、これはありますあまりにも楽しい質問を渡す。

以下のテストコードですが、私の非科学的な答えは、クエリの索引付けが適切であれば、どのメソッドを使用するかは関係ありません。大量のオブジェクトを数えているときには数が高価ですが、検索しようとしていてクエリのインデックスが作成されていると、高価になるとは思われません。

これでコードが完成しました。私は、私が存在をテストするために考えることができる3つの異なる方法をテストするために、私のパースサーバーレポで単体テストをいくつか調理しました。 3つのケースすべてで、私は特別なobjectIdを使用していますので、簡単に調整できます。私はまた、それぞれのテストを1回だけ行いますが、それについて科学的にするには、それぞれ100回程度、平均したいと思います。しかし、クエリキャッシングについて考える必要があるので、単純にしておき、同じパターンを使って自分のデータで実際にテストすることができます。

[OK]を、今私はすべての私の警告を与えてくれたこと:

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 

だから私の素敵な空の中を、メモリモンゴで、そのすべての超高速燃えます(これらはミリ秒です)、クエリが適切に索引付けされていると仮定すると、最もわかりやすいコードを生成するものを選択してください;)

関連する問題