Changes between Version 2 and Version 3 of Indexes


Ignore:
Timestamp:
02/04/26 20:08:29 (4 days ago)
Author:
233194
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Indexes

    v2 v3  
    7171ON suggestions(user_id);
    7272}}}
    73 The partial index on build with WHERE is_approved = FALSE is smaller and more efficient, since admins only care about unapproved builds. //
     73The partial index on build with WHERE is_approved = FALSE is smaller and more efficient, since admins only care about unapproved builds. \\
    7474
    7575== Scenario 5: Reviews and User Authentication ==
     
    109109}}}
    110110
     111== Scenario 7: Top Performing Components Report ==
     112Users analyze which components are most popular in recent builds with high ratings. This report filters builds by date, joins multiple tables and aggregates ratings with a quality threshold. \\
     113Optimization: Without indexes, the query performs sequential scans on the build table to filter by created_at, then joins to rating_build. \\
     114A date index enables efficient range filtering, reducing the scan from 75000 builds to only those in the relevant time window. \\
     115{{{
     116CREATE INDEX IF NOT EXISTS idx_build_created_at
     117ON build(created_at);
     118
     119CREATE INDEX IF NOT EXISTS idx_rating_build_build_value
     120ON rating_build(build_id, value);
     121}}}
     122
     123The date index on build(created_at) eliminates full table scans for time-windowed queries \\
     124
     125== Scenario 8: User Reputation Leaderboard Report ==
     126This report calculates user reputation scores by aggregating approved builds, favorites received, and average ratings per user. \\
     127Optimization: The partial index on approved builds significantly reduces the working dataset. Combined with indexes on the JOIN keys, this reduces buffer reads and eliminates unnecessary data processing. \\
     128{{{
     129CREATE INDEX IF NOT EXISTS idx_build_approved_user
     130ON build(is_approved, user_id) WHERE is_approved = TRUE;
     131
     132CREATE INDEX IF NOT EXISTS idx_favorite_build_build_id
     133ON favorite_build(build_id);
     134}}}
     135
     136The composite partial index (is_approved, user_id) WHERE is_approved = TRUE is highly selective and perfect for analytics that only considers approved content. \\
     137
     138== Scenario 9: Budget Tier Popularity Report ==
     139This report segments builds into price tiers (Budget, Mid-Range, High-End, Enthusiast) and calculates engagement metrics within the last 6 months. It requires filtering by both approval status and creation date, then aggregates favorites and ratings for each tier. \\
     140Optimization: A composite index covering all three filter columns (is_approved, created_at, total_price) which enables index-only scans for the initial filter. The partial index with WHERE clause reduces index size by excluding unapproved builds entirely. \\
     141{{{
     142CREATE INDEX IF NOT EXISTS idx_build_approved_created_price
     143ON build(is_approved, created_at, total_price) WHERE is_approved = TRUE;
     144}}}
     145
    111146== Testing with EXPLAIN ANALYZE ==
     147
    112148Example test for scenario 4:
    113149{{{
     
    121157}}}
    122158
     159Example test for scenario 7:
     160{{{
     161EXPLAIN (ANALYZE, BUFFERS)
     162SELECT * FROM get_report_top_components();
     163}}}
     164
     165Example test for scenario 8:
     166{{{
     167EXPLAIN (ANALYZE, BUFFERS)
     168SELECT * FROM get_report_user_reputation_leaderboard();
     169}}}
     170
     171Example test for scenario 9:
     172{{{
     173EXPLAIN (ANALYZE, BUFFERS)
     174SELECT * FROM get_report_budget_tier_popularity();
     175}}}
     176
    123177== Performance Comparison ==
    124178
    125 === Without partial index: ===
     179=== Scenario 4: Admin Panel - Pending Approvals ===
     180Without partial index:
    126181Index Scan using idx_build_approved_date on build b
    127182  Index Cond: (is_approved = false)
     
    129184Execution Time: 0.351 ms \\
    130185
    131 === With partial index: ===
     186With partial index:
    132187Index Scan using idx_build_pending_date on build b
    133188  Buffers: shared hit=68 read=2
    134189Execution Time: 0.219 ms \\
    135190
    136 Result: The partial index improved query performance by dropping the execution time from 0.351ms to 0.219ms. Partial indexes are more efficient for frequently filtered subsets of data, so that's why they are ideal for the admin queries that only target pending builds.
    137 
    138 
    139 
    140 
    141 
     191Result: The partial index improved query performance by dropping the execution time from 0.351ms to 0.219ms. \\
     192Partial indexes are more efficient for frequently filtered subsets of data, so that's why they are ideal for the admin queries that only target pending builds. \\
     193
     194=== Scenario 7: Top Performing Components Report ===
     195
     196Without indexes:
     197Buffers: shared hit=7684
     198Planning Time: 0.021 ms
     199Execution Time: 506.953 ms
     200
     201With indexes:
     202Buffers: shared hit=6379 read=351
     203Planning Time: 0.022 ms
     204Execution Time: 468.190 ms
     205
     206Result: The date index improved query performance, while not much, still a modest improvement reducing it from 506.953ms to 468.190ms. \\
     207
     208=== Scenario 8: User Reputation Leaderboard Report ===
     209
     210Without indexes:
     211Buffers: shared hit=142884 dirtied=1, temp read=495 written=849
     212Planning Time: 0.029 ms
     213Execution Time: 553.368 ms
     214
     215With indexes:
     216Buffers: shared hit=142892 read=1, temp read=231 written=584
     217Planning Time: 0.022 ms
     218Execution Time: 345.192 ms
     219
     220Result: Here the improvement is much more noticeable than on Scenario 7, reducing the time from 553.368ms to 345.192ms. \\
     221
     222=== Scenario 9: Budget Tier Popularity Report ===
     223
     224Without indexes:
     225Buffers: shared hit=1366 read=30
     226Planning Time: 0.020 ms
     227Execution Time: 131.234 ms
     228
     229With indexes:
     230Buffers: shared hit=1396
     231Planning Time: 0.029 ms
     232Execution Time: 125.615 ms
     233
     234Result: Modest improvement, but the index still helped out with unnecessary reads, reducing time from 131.234ms to 125.615ms. \\
     235
     236
     237
     238