| | 10 | == View 2 - vw_payment_status |
| | 11 | == View 3 - vw_property_summary |
| | 12 | == View 4 - vw_host_performance |
| | 13 | == View 5 - vw_guest_activity |
| | 14 | == View 6 - vw_revenue_by_property |
| | 15 | Can't optimize the queries because the view needs all the bookings for a property to calculate averaget rating,total booking etc. So even with indexes the time doesn't change. |
| | 16 | {{{ |
| | 17 | [2026-05-03 17:32:09] postgres.public> select * from vw_revenue_by_property where review_count >5 and avg_revenue_per_booking>30000 and cancelled_bookings<3 |
| | 18 | [2026-05-03 17:35:09] 500 rows retrieved starting from 1 in 3 m 0 s 196 ms (execution: 2 m 59 s 730 ms, fetching: 466 ms) |
| | 19 | }}} |
| | 20 | == View 7 - vw_review_summary |
| | 21 | Without optimization |
| | 22 | {{{ |
| | 23 | [2026-05-03 17:16:41] postgres.public> select * from vw_review_summary where rating <3 and booking_status = 'COMPLETED' and property_avg_rating>3 |
| | 24 | [2026-05-03 17:17:26] 500 rows retrieved starting from 1 in 45 s 44 ms (execution: 44 s 521 ms, fetching: 523 ms) |
| | 25 | }}} |
| | 26 | * Can't optimze this query because it needs to get all the reviews to calculate the average rating. |
| | 27 | == View 8 - vw_room_details |
| | 28 | Without optimization |
| | 29 | {{{ |
| | 30 | [2026-05-03 17:01:01] postgres.public> select * from vw_room_details where max_capacity> 4 and room_status='ACTIVE' and price_per_night<50 |
| | 31 | [2026-05-03 17:01:05] 500 rows retrieved starting from 1 in 4 s 716 ms (execution: 4 s 246 ms, fetching: 470 ms) |
| | 32 | }}} |
| | 33 | With adding index on listing_types we get |
| | 34 | {{{ |
| | 35 | [2026-05-03 17:04:05] postgres.public> select * from vw_room_details where max_capacity> 4 and room_status='ACTIVE' and price_per_night<50 |
| | 36 | [2026-05-03 17:04:07] 500 rows retrieved starting from 1 in 1 s 729 ms (execution: 1 s 336 ms, fetching: 393 ms) |
| | 37 | }}} |
| | 38 | Final |
| | 39 | Specifying what do we need and not with just *, we optimze the query plus I added an index to rooms; |
| | 40 | {{{ |
| | 41 | CREATE INDEX idx_rooms_active_lowprice_capacity |
| | 42 | ON rooms (price_per_night, capacity, extra_capacity) |
| | 43 | WHERE status = 'ACTIVE'; |
| | 44 | }}} |
| | 45 | {{{ |
| | 46 | [2026-05-03 17:11:49] postgres.public> select |
| | 47 | room_name, |
| | 48 | room_status, |
| | 49 | capacity, |
| | 50 | extra_capacity, |
| | 51 | max_capacity, |
| | 52 | price_per_night, |
| | 53 | extra_guest_price, |
| | 54 | room_description, |
| | 55 | room_type, |
| | 56 | property_title, |
| | 57 | property_status, |
| | 58 | property_base_price, |
| | 59 | listing_type, |
| | 60 | street, |
| | 61 | country_name, |
| | 62 | host_full_name, |
| | 63 | amenity_count |
| | 64 | from vw_room_details where max_capacity> 4 and room_status='ACTIVE' and price_per_night<50 |
| | 65 | [2026-05-03 17:11:51] 500 rows retrieved starting from 1 in 1 s 596 ms (execution: 1 s 146 ms, fetching: 450 ms) |
| | 66 | }}} |
| | 67 | This is acceptable. |
| | 68 | == View 9 - vw_availability_windows |
| | 69 | Without optimization |
| | 70 | {{{ |
| | 71 | [2026-05-03 16:54:28] postgres.public> SELECT |
| | 72 | availability_window_id, |
| | 73 | room_id, |
| | 74 | room_name, |
| | 75 | property_id, |
| | 76 | available_date_range, |
| | 77 | price_per_night, |
| | 78 | room_status |
| | 79 | FROM vw_availability_windows |
| | 80 | WHERE available_date_range && daterange(DATE '2026-05-01', DATE '2026-05-10', '[]') |
| | 81 | AND room_status = 'ACTIVE' and price_per_night<50 |
| | 82 | [2026-05-03 16:54:30] 500 rows retrieved starting from 1 in 1 s 382 ms (execution: 800 ms, fetching: 582 ms) |
| | 83 | }}} |
| | 84 | * This is an acceptable time so we don't need to optimize this query. |