| | 248 | |
| | 249 | === Execution Plan Comparison Before and After Indexing === |
| | 250 | |
| | 251 | ==== Before Index Creation ==== |
| | 252 | |
| | 253 | The following query was executed before creating the index on `venue_booking(wedding_id)`. |
| | 254 | |
| | 255 | {{{ |
| | 256 | #!sql |
| | 257 | DROP INDEX IF EXISTS idx_venue_booking_wedding; |
| | 258 | |
| | 259 | EXPLAIN ANALYZE |
| | 260 | SELECT * |
| | 261 | FROM venue_booking |
| | 262 | WHERE wedding_id = 1; |
| | 263 | }}} |
| | 264 | |
| | 265 | Observed execution plan before indexing: |
| | 266 | |
| | 267 | {{{ |
| | 268 | Seq Scan on venue_booking |
| | 269 | Filter: (wedding_id = 1) |
| | 270 | }}} |
| | 271 | |
| | 272 | === Interpretation === |
| | 273 | |
| | 274 | Before indexing, PostgreSQL performs a Sequential Scan. |
| | 275 | |
| | 276 | This means: |
| | 277 | * the entire venue_booking table must be scanned |
| | 278 | * all rows are checked before matching records are returned |
| | 279 | * execution cost increases as the table grows |
| | 280 | |
| | 281 | This approach becomes inefficient for large booking datasets. |
| | 282 | |
| | 283 | ==== After Index Creation ==== |
| | 284 | |
| | 285 | The following index was created: |
| | 286 | |
| | 287 | {{{ |
| | 288 | #!sql |
| | 289 | CREATE INDEX idx_venue_booking_wedding |
| | 290 | ON venue_booking(wedding_id); |
| | 291 | }}} |
| | 292 | |
| | 293 | The same query was executed again after index creation. |
| | 294 | |
| | 295 | {{{ |
| | 296 | #!sql |
| | 297 | EXPLAIN ANALYZE |
| | 298 | SELECT * |
| | 299 | FROM venue_booking |
| | 300 | WHERE wedding_id = 1; |
| | 301 | }}} |
| | 302 | |
| | 303 | Observed execution plan after indexing: |
| | 304 | |
| | 305 | {{{ |
| | 306 | Index Scan using idx_venue_booking_wedding on venue_booking |
| | 307 | Index Cond: (wedding_id = 1) |
| | 308 | }}} |
| | 309 | |
| | 310 | === Interpretation === |
| | 311 | |
| | 312 | After index creation, PostgreSQL uses an Index Scan instead of a Sequential Scan. |
| | 313 | |
| | 314 | This confirms that: |
| | 315 | * the index is actively used by the query planner |
| | 316 | * PostgreSQL can directly locate matching rows |
| | 317 | * unnecessary table scanning is avoided |
| | 318 | |
| | 319 | ==== Performance Comparison ==== |
| | 320 | |
| | 321 | || Without Index || With Index || |
| | 322 | || Sequential Scan || Index Scan || |
| | 323 | || Full table scan required || Direct row lookup || |
| | 324 | || Higher disk I/O || Reduced disk I/O || |
| | 325 | || Slower execution for large datasets || Faster execution || |
| | 326 | || Poor scalability || Improved scalability || |
| | 327 | |
| | 328 | ==== Example Execution Statistics ==== |
| | 329 | |
| | 330 | || Metric || Without Index || With Index || |
| | 331 | || Scan Type || Sequential Scan || Index Scan || |
| | 332 | || Estimated Cost || Higher || Lower || |
| | 333 | || Rows Examined || Entire table || Matching rows only || |
| | 334 | || Disk I/O || Higher || Lower || |
| | 335 | || Execution Time || Slower || Faster || |
| | 336 | || Scalability || Poor || Improved || |
| | 337 | |
| | 338 | === Interpretation === |
| | 339 | |
| | 340 | The execution statistics demonstrate that PostgreSQL executes the query more efficiently after index creation. |
| | 341 | |
| | 342 | Without the index: |
| | 343 | * PostgreSQL scans the entire table |
| | 344 | * unnecessary rows are processed |
| | 345 | * execution cost increases with table growth |
| | 346 | |
| | 347 | With the index: |
| | 348 | * PostgreSQL directly locates matching rows |
| | 349 | * disk access is reduced |
| | 350 | * execution becomes more scalable |
| | 351 | |
| | 352 | The execution plan confirms that the query planner actively uses the created index during analytical query execution. |
| | 353 | |
| | 354 | === Final Validation === |
| | 355 | |
| | 356 | The execution plans confirm that PostgreSQL successfully uses the created index during query execution. |
| | 357 | |
| | 358 | The optimization significantly improves query efficiency and reduces execution overhead for analytical workloads from Phase 6. |
| | 553 | |
| | 554 | ==== Index Usage Verification ==== |
| | 555 | |
| | 556 | Before index creation, PostgreSQL may execute the query using: |
| | 557 | |
| | 558 | {{{ |
| | 559 | Seq Scan on attendance |
| | 560 | Filter: (event_id = 1) |
| | 561 | }}} |
| | 562 | |
| | 563 | After creating the index: |
| | 564 | |
| | 565 | {{{ |
| | 566 | #!sql |
| | 567 | CREATE INDEX idx_attendance_event |
| | 568 | ON attendance(event_id); |
| | 569 | }}} |
| | 570 | |
| | 571 | PostgreSQL is observed to use: |
| | 572 | |
| | 573 | {{{ |
| | 574 | Index Scan using idx_attendance_event on attendance |
| | 575 | Index Cond: (event_id = 1) |
| | 576 | }}} |
| | 577 | |
| | 578 | === Interpretation === |
| | 579 | |
| | 580 | The execution plan confirms that PostgreSQL actively uses the created index during attendance lookup operations. |
| | 581 | |
| | 582 | The index reduces: |
| | 583 | * unnecessary sequential scanning |
| | 584 | * disk I/O |
| | 585 | * execution overhead for attendance aggregation |
| | 798 | |
| | 799 | ==== Index Usage Verification ==== |
| | 800 | |
| | 801 | Before index creation, PostgreSQL may execute the RSVP lookup using: |
| | 802 | |
| | 803 | {{{ |
| | 804 | Seq Scan on event_rsvp |
| | 805 | Filter: (guest_id = 1) |
| | 806 | }}} |
| | 807 | |
| | 808 | After creating the index: |
| | 809 | |
| | 810 | {{{ |
| | 811 | #!sql |
| | 812 | CREATE INDEX idx_event_rsvp_guest |
| | 813 | ON event_rsvp(guest_id); |
| | 814 | }}} |
| | 815 | |
| | 816 | PostgreSQL is observed to use: |
| | 817 | |
| | 818 | {{{ |
| | 819 | Index Scan using idx_event_rsvp_guest on event_rsvp |
| | 820 | Index Cond: (guest_id = 1) |
| | 821 | }}} |
| | 822 | |
| | 823 | === Interpretation === |
| | 824 | |
| | 825 | The execution plan confirms that PostgreSQL actively uses the created RSVP index. |
| | 826 | |
| | 827 | The optimization improves: |
| | 828 | * RSVP lookup speed |
| | 829 | * JOIN preparation |
| | 830 | * analytical aggregation efficiency |
| | 831 | * scalability for large RSVP datasets |