I benchmarked 2 version of my solr index the first with the following include statement:
searchable(:auto_index => false, :auto_remove => true,
:include => { :account => true,
:user_practice_contact => [:city],
:user_professional_detail => [:specialty, :subspecialties]}) do
The second:
searchable(:auto_index => false, :auto_remove => true) do
I was expecting to see a speed bump on the version with includes but here is the outcome:
version with includes:
Benchmark.measure { User.limit(50).each do |u|; u.index; end; Sunspot.commit; }
=> #<Benchmark::Tms:0x1130b34e8 @real=6.8079788684845, @utime=5.05, @cstime=0.0, @cutime=0.0, @total=5.2, @label="", @stime=0.149999999999999>
and without the includes:
Benchmark.measure { User.limit(50).each do |u|; u.index; end; Sunspot.commit; }
=> #<Benchmark::Tms:0x112ef0fe8 @real=6.82465195655823, @utime=4.92, @cstime=0.0, @cutime=0.0, @total=5.07, @label="", @stime=0.15>
Does anybody know if the includes are supposed to work? And if so, am I doing it wrong? I looked at the docs: http://outoftime.github.com/sunspot/rails/docs/ and see no mention of that.
According to the API, :include will:
allow ActiveRecord to load required associations when indexing.
Your benchmark does not work properly because you are indexing individual records in an ordinary Ruby iterator. As you are indexing a single record 50 times, Sunspot wouldn't be able to utilize the eager loading at all. Instead you should do:
Sunspot.index( User.limit(50) );
Sunspot.commit
Oh and could you test if the following is faster than above? I really want to know.
Sunspot.index( User.includes(:account).limit(50) );
Sunspot.commit
Also there is a bug currently that STI models will ignore the :include.
By looking at the SQL queries in the Rails log, you can see that :include on searchable causes eager loading while indexing. :include on #search caused eager loading while searching.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With