docbrown/spec/services/articles
Jamie Gaskins 68025ae472
Optimize Article.published scope to use an index (#13071)
* Optimize Article.published scope

The index on `articles.published` is not being invoked due to lack of
diversity in the values in that column (it only has 2 possible values
because it's a boolean).

The index on `articles.published_at` is extremely diverse and will be
invoked any time it can reduce the scope of a query by an order of
magnitude or more.

On DEV, this does not change the row count for the scope:

    irb(main):001:0> Article.where(published: true).count == Article.where(published: true).where(Arel.sql("published_at < now()")).count
    => true

The query plan for the `Articles::Feeds::LargeForemExperimental` service
invoked in the `Stories::FeedController#show` endpoint goes from this:

    Planning Time: 0.271 ms
    Execution Time: 329.258 ms

to this:

    Planning Time: 0.330 ms
    Execution Time: 0.468 ms

This is a reduction in query time of 99.7%

* Set published_at in articles factory

An article that is published should always have a `published_at`
timestamp

* Use Time.current for Zonebie

* Add clarifying comment to Article.published scope

* Generate timestamp in Ruby instead of SQL

I think using transactions for specs was interfering with comparing
timestamps generated in SQL, so this commit generates the timestamp in
Ruby.

* Move published_at outside of the transient block

This was causing articles to be marked as `published_at` right now even
if `published_at` was specified in the `FactoryBot.create` call.

* published_at: nil is no longer the factory default

* published_at is no longer nil by default

* published_at is no longer nil by default

* We didn't actually want to clear this published_at

This was intentionally left out to show that published_at does not get
cleared when we flip published true->false.
2021-03-23 13:07:27 -04:00
..
feeds Optimize published_articles_by_tag fetch speed (#12584) 2021-02-05 09:25:04 -05:00
attributes_spec.rb Articles update refactoring (#12913) 2021-03-10 13:08:36 +03:00
builder_spec.rb Rubocop: fix Metrics/CyclomaticComplexity and disable Metrics/AbcSize (#9221) 2020-07-10 15:26:05 +02:00
creator_spec.rb Remove active job remainders (#6603) 2020-03-12 15:31:43 +01:00
destroyer_spec.rb Change Notifications::RemoveAllJob to Notifications::RemoveAllWorker and move to sidekiq (#5451) 2020-01-15 12:34:12 -05:00
suggest_spec.rb Mark flaky specs as flaky (#12898) 2021-03-04 09:27:04 -05:00
updater_spec.rb Optimize Article.published scope to use an index (#13071) 2021-03-23 13:07:27 -04:00