Eliminate N+1 queries in PipelinesController#index

Merged Stan Hu requested to merge sh-optimize-pipeline-sql into master

!20615 (merged) introduced a performance regression: each failed build would generate additional SQL queries, since they would be rendered by JobEntity. This commit fixes that by preloading failed builds among other items and adds an N+1 query test. This adds 4 additional queries per pipeline:

  1. user
  2. latest_statuses_ordered_by_stage
  3. failed_builds
  4. failed_builds metadata

Closes #118427 (closed)

Edited by Stan Hu