Which approach should I take in this case?
<p>I have implemented the Data Mapper pattern with the Identity Map pattern. In short: When I want to have 2000 objects from the database, the mapper checks the result set against an hash map which contains references to already created objects. If an id is already in the hash map, the old object is added to the return array instead. Otherwise a new object is created and added to the return array. The return array will contain 2000 objects.</p>

<p>Note: This numbers are theoretical! The platform may be highly frequented, so this may happen many times per minute or even second.</p>

<p>Question: Which option is better and why?</p>

<p><strong>A)</strong> Retrieve all 2000 objects from the database. Iterate over the record set (2000 rows), and check every id against the identity map. If it's in there, add the referenced object of the identity map to the object array. If not, create a new object and add it to the result array.</p>

<p><strong>B)</strong> Create a (maybe HUGE) sql query which excludes all id's which are in the identity map. Get a record set that contains only data for new objects. Create new objects without checking the identity map for every row. Incorporates a lot of string concatenation operations to build the query, but may save a whole bunch of hash map lookups.</p>

<p>Which approach would you take?
(yes I know, I should just implement both versions and make a performance test, but maybe someone can answer this from practical experience)</p>
