LoginSignup
1
1

More than 3 years have passed since last update.

効率的なSOQLの書き方

Posted at

面白い質問がありました。

Efficient List Initialization Technique

どちらの書き方がいいのか?

List accList = new List([SELECT Name FROM Account]);

List accList = [SELECT Name FROM Account];

個人的には2番目の書き方が多いですね。
ただ、そんなに違いはあるのかはよくわかりませんでした。

その中で回答がありました。

The second is definitely more efficient.

When executing a query, you are essentially calling a method that instantiates a list, populates it, and returns it to you. In your first example, you are instantiating a second list, and shallow-copying all items from the first list (the one returned from the query) to this second one. This means that if you had 10,000 results, you'd be copying 10,000 pointers to your second list.

回答を読むと

1番目の書き方では2番目のリストをインスタンス化し、最初のリスト(クエリから返されたリスト)からこの2番目のリストにすべてのアイテムを浅くコピーしています。つまり、10,000件の結果があった場合、10,000個のポインターを2番目のリストにコピーすることになります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1