More Associations = Faster

I have a Party record that I’m creating from Legacy data stored in another table in the database, accessed through a model created temporarily:

class Legacy < ActiveRecord::Base;end

And from that I’m creating records for parties, addresses and contacts table. Parties has_one on both addresses and contacts. I’m doing this for the first 1000 records in the legacy table and the benchmark I get back is, using Benchmark.measure:

44.200000 4.150000 48.350000 ( 58.145813)

Which I think to be “OK”, but could be improved.

Now here’s where it gets weird. I added in one more association because I forgot to add it the first time. This association is another has_one and is called client. So again, another 1000 records using Benchmark.measure:

31.280000 3.760000 35.040000 ( 41.497573)

So I made a little ruby script that does this for me, ran the one without the client first and then ran the one with the client second, and got back very similar results. So I thought that maybe it was me doing something on the computer, so I just left it running until the end, and again got similar results. Reversing the order yielded similar results (WITH client ran faster than without)

Why is this happening?