| | 16 | == Indexes |
| | 17 | |
| | 18 | To optimize query performance and reduce full table scans as data volume grows, the following indexes were added based on real usage patterns in analytical queries, views, and joins. |
| | 19 | |
| | 20 | |
| | 21 | 1. Most queries join trip and route using route_id, for example in company_performance_view, top_selling_routes_view, ticket statistics, and weighted usage reports. Without this index, every join would trigger a sequential scan of the trip table. With the index, the database can directly locate all trips for a given route. |
| | 22 | |
| | 23 | {{{ |
| | 24 | CREATE INDEX idx_trip_route_id ON trip(route_id); |
| | 25 | }}} |
| | 26 | |
| | 27 | |
| | 28 | ---- |
| | 29 | |
| | 30 | |
| | 31 | |
| | 32 | |
| | 33 | 2. These two indexes accelerate lookups and joins involving route endpoints. They support queries that find trips between specific locations, which for our application is especially iseful to detect **subroutes**, one of the most important features. They're also useful in displaying routes with origin - destination metadata. |
| | 34 | |
| | 35 | |
| | 36 | {{{ |
| | 37 | CREATE INDEX idx_route_from_location_id ON route(from_location_id); |
| | 38 | CREATE INDEX idx_route_to_location_id ON route(to_location_id); |
| | 39 | }}} |
| | 40 | |
| | 41 | |
| | 42 | ---- |
| | 43 | |
| | 44 | |
| | 45 | 3. Filtering by trip status (e.g., 'COMPLETED', 'NOT_STARTED') is common in **ticket statistics** and reporting queries. This index ensures that the database can filter relevant trips quickly instead of scanning the full trip table. It's especially important in analytical use cases and dashboards. |
| | 46 | |
| | 47 | |
| | 48 | {{{ |
| | 49 | CREATE INDEX idx_trip_status ON trip(status); |
| | 50 | }}} |
| | 51 | |
| | 52 | |
| | 53 | ---- |
| | 54 | |
| | 55 | |
| | 56 | 4. Views such as company_performance_view and top_selling_routes_view frequently join route and transport_organizer through transport_organizer_id. This index speeds up company-based route lookups, revenue calculations and per-organizer reporting. So, this index is useful for when we're calculating company metrics/revenue by organizer. |
| | 57 | |
| | 58 | |
| | 59 | {{{ |
| | 60 | CREATE INDEX idx_route_transport_organizer_id ON route(transport_organizer_id); |
| | 61 | }}} |
| | 62 | |
| | 63 | |
| | 64 | |
| | 65 | |