Changes between Version 7 and Version 8 of QueryOptimization
- Timestamp:
- 07/01/26 21:40:07 (7 days ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
QueryOptimization
v7 v8 7 7 2. Примарен случај на употреба е за real-time мониторинг на активни возења. За овој поглед ни се важни перформансите, бидејќи се користи континуирано. 8 8 3. Иницијалното време за извршување на погледот е 7.38 секунди. Ова не е прифатливо време за апликација која треба да прикажува live статус на возења, па затоа пристапуваме кон индексирање на колоната STATUS во табелата TRIP. 9 4. Набљудувани операции се full scan на табелата TRIP без индекс на колоната STATUS, со повеќе Hash Join операции низ ROUTE, VEHICLE, DRIVER и EMPLOYEE табелите.10 5. Времето изминато во извршување на query-то со индекс изнесува 485 ms (0.48секунди), и тоа е прифатливо подобрување — 15 пати побрзо.11 12 {{{ 13 Hash Left Join (cost=3171.49..15071.96 rows=135727 width=287) (actual time=31.892..479.130rows=135807 loops=1)14 Hash Cond: ( t.vehicle_type_id = vt.vehicle_type_id)15 -> Hash Join (cost= 3151.59..14013.71 rows=135727 width=155) (actual time=31.841..429.095rows=135807 loops=1)16 Hash Cond: (( dr.employee_id)::text = (e.employee_id)::text)17 -> Hash Join (cost=2048.09..12553.87 rows=135727 width=147) (actual time=17.571..354.868 rows=135807 loops=1)18 Hash Cond: ( (t.driver_id)::text = (dr.driver_id)::text)19 -> Hash Join (cost=1670.84..11820.22 rows=135727 width=139) (actual time= 11.744..290.015rows=135807 loops=1)9 4. Набљудувани операции се full scan на табелата TRIP без индекс на колоната STATUS, со повеќе Hash Join операции. 10 5. Времето изминато во извршување на query-то со индекс изнесува 330ms (0.33 секунди), и тоа е прифатливо подобрување — 15 пати побрзо. 11 12 {{{ 13 Hash Join (cost=3171.49..15071.96 rows=135727 width=287) (actual time=24.816..324.837 rows=135807 loops=1) 14 Hash Cond: ((d.employee_id)::text = (e.employee_id)::text) 15 -> Hash Join (cost=2067.99..12933.48 rows=135727 width=261) (actual time=13.814..249.386 rows=135807 loops=1) 16 Hash Cond: ((t.driver_id)::text = (d.driver_id)::text) 17 -> Hash Left Join (cost=1690.74..12199.83 rows=135727 width=253) (actual time=9.300..197.528 rows=135807 loops=1) 18 Hash Cond: (v.vehicle_type_id = vt.vehicle_type_id) 19 -> Hash Join (cost=1670.84..11820.22 rows=135727 width=139) (actual time=9.273..162.371 rows=135807 loops=1) 20 20 Hash Cond: ((t.vehicle_id)::text = (v.vehicle_id)::text) 21 -> Hash Join (cost=1504.34..11297.13 rows=135727 width=1 23) (actual time=9.158..232.483rows=135807 loops=1)21 -> Hash Join (cost=1504.34..11297.13 rows=135727 width=119) (actual time=7.302..110.861 rows=135807 loops=1) 22 22 Hash Cond: ((t.route_id)::text = (r.route_id)::text) 23 -> Bitmap Heap Scan on trip t (cost=1456.72..10891.31 rows=135727 width=9 7) (actual time=8.634..168.601rows=135807 loops=1)23 -> Bitmap Heap Scan on trip t (cost=1456.72..10891.31 rows=135727 width=93) (actual time=6.944..52.513 rows=135807 loops=1) 24 24 Recheck Cond: ((status)::text = ANY ('{SCHEDULED,IN_PROGRESS}'::text[])) 25 25 Heap Blocks: exact=7738 26 -> Bitmap Index Scan on idx_trip_status (cost=0.00..1422.79 rows=135727 width=0) (actual time= 7.136..7.136rows=135807 loops=1)26 -> Bitmap Index Scan on idx_trip_status (cost=0.00..1422.79 rows=135727 width=0) (actual time=5.444..5.444 rows=135807 loops=1) 27 27 Index Cond: ((status)::text = ANY ('{SCHEDULED,IN_PROGRESS}'::text[])) 28 -> Hash (cost=37.83..37.83 rows=783 width=41) (actual time=0. 458..0.458rows=783 loops=1)28 -> Hash (cost=37.83..37.83 rows=783 width=41) (actual time=0.336..0.337 rows=783 loops=1) 29 29 Buckets: 1024 Batches: 1 Memory Usage: 65kB 30 -> Seq Scan on route r (cost=0.00..37.83 rows=783 width=41) (actual time=0.0 53..0.216rows=783 loops=1)31 -> Hash (cost=104.00..104.00 rows=5000 width=2 1) (actual time=2.512..2.513rows=5000 loops=1)32 Buckets: 8192 Batches: 1 Memory Usage: 3 29kB33 -> Seq Scan on vehicle v (cost=0.00..104.00 rows=5000 width=2 1) (actual time=0.009..1.018 rows=5000 loops=1)34 -> Hash (cost= 221.00..221.00 rows=12500 width=20) (actual time=5.693..5.693 rows=12500loops=1)35 Buckets: 1 6384 Batches: 1 Memory Usage: 771kB36 -> Seq Scan on driver dr (cost=0.00..221.00 rows=12500 width=20) (actual time=0.021..2.062 rows=12500loops=1)37 -> Hash (cost= 791.00..791.00 rows=25000 width=20) (actual time=14.020..14.021 rows=25000 loops=1)38 Buckets: 32768 Batches: 1 Memory Usage: 1542kB39 -> Seq Scan on employee e (cost=0.00..791.00 rows=25000 width=20) (actual time=0.025..6.289 rows=25000 loops=1)40 -> Hash (cost= 14.40..14.40 rows=440 width=122) (actual time=0.037..0.038 rows=3loops=1)41 Buckets: 1024 Batches: 1 Memory Usage: 9kB42 -> Seq Scan on vehicle_type vt (cost=0.00..14.40 rows=440 width=122) (actual time=0.010..0.012 rows=3loops=1)43 Planning Time: 4.422ms44 Execution Time: 485.098ms30 -> Seq Scan on route r (cost=0.00..37.83 rows=783 width=41) (actual time=0.023..0.154 rows=783 loops=1) 31 -> Hash (cost=104.00..104.00 rows=5000 width=25) (actual time=1.954..1.955 rows=5000 loops=1) 32 Buckets: 8192 Batches: 1 Memory Usage: 357kB 33 -> Seq Scan on vehicle v (cost=0.00..104.00 rows=5000 width=25) (actual time=0.010..0.778 rows=5000 loops=1) 34 -> Hash (cost=14.40..14.40 rows=440 width=122) (actual time=0.013..0.013 rows=3 loops=1) 35 Buckets: 1024 Batches: 1 Memory Usage: 9kB 36 -> Seq Scan on vehicle_type vt (cost=0.00..14.40 rows=440 width=122) (actual time=0.009..0.010 rows=3 loops=1) 37 -> Hash (cost=221.00..221.00 rows=12500 width=20) (actual time=4.484..4.484 rows=12500 loops=1) 38 Buckets: 16384 Batches: 1 Memory Usage: 771kB 39 -> Seq Scan on driver d (cost=0.00..221.00 rows=12500 width=20) (actual time=0.011..1.769 rows=12500 loops=1) 40 -> Hash (cost=791.00..791.00 rows=25000 width=20) (actual time=10.950..10.952 rows=25000 loops=1) 41 Buckets: 32768 Batches: 1 Memory Usage: 1542kB 42 -> Seq Scan on employee e (cost=0.00..791.00 rows=25000 width=20) (actual time=0.016..5.264 rows=25000 loops=1) 43 Planning Time: 1.249 ms 44 Execution Time: 330.174 ms 45 45 46 46 }}} … … 54 54 2. Примарен случај на употреба е преглед на смени на персоналот по ден и линија. View-от користи UNION ALL па без индекси двете страни — DRIVER_SCHEDULE и CONDUCTOR_SCHEDULE — се скенираат целосно. 55 55 3. Иницијалното време за извршување на погледот е над 30 минути (query прекинато). Ова е сосема неприфатливо, па затоа пристапуваме кон индексирање на двете распоредни табели. 56 4. Набљудувани операции се Seq Scan на DRIVER_SCHEDULE (387,533 редови) и Seq Scan на CONDUCTOR_SCHEDULE (271,712 редови), со повеќе Hash Join операции низ TRIP, EMPLOYEE, SHIFT, ROUTE и VEHICLE.57 5. Времето изминато во извршување на query-то со индекси изнесува 2 2.1секунди, вкупно обработени 659,245 редови.58 59 {{{ 60 Append (cost=21578.58..91196.61 rows=659245 width=269) (actual time=2 65.002..22100.792rows=659245 loops=1)61 -> Hash Join (cost=21578.58..47282.64 rows=387533 width=269) (actual time=2 65.001..11422.640rows=387533 loops=1)56 4. Набљудувани операции се Seq Scan на DRIVER_SCHEDULE (387,533 редови) и Seq Scan на CONDUCTOR_SCHEDULE (271,712 редови), со повеќе Hash Join операции. 57 5. Времето изминато во извршување на query-то со индекси изнесува 2.5 секунди, вкупно обработени 659,245 редови. 58 59 {{{ 60 Append (cost=21578.58..91196.61 rows=659245 width=269) (actual time=227.834..2399.441 rows=659245 loops=1) 61 -> Hash Join (cost=21578.58..47282.64 rows=387533 width=269) (actual time=227.833..1316.971 rows=387533 loops=1) 62 62 Hash Cond: ((t.vehicle_id)::text = (v.vehicle_id)::text) 63 -> Hash Join (cost=21412.08..44160.34 rows=387533 width=216) (actual time=2 63.090..11245.169rows=387533 loops=1)63 -> Hash Join (cost=21412.08..44160.34 rows=387533 width=216) (actual time=226.118..1145.150 rows=387533 loops=1) 64 64 Hash Cond: ((t.route_id)::text = (r.route_id)::text) 65 -> Hash Join (cost=21364.47..43089.94 rows=387533 width=203) (actual time=2 62.753..11133.889rows=387533 loops=1)65 -> Hash Join (cost=21364.47..43089.94 rows=387533 width=203) (actual time=225.779..1031.940 rows=387533 loops=1) 66 66 Hash Cond: (ds.shift_id = sh.shift_id) 67 -> Hash Join (cost=21345.24..42042.93 rows=387533 width=73) (actual time=2 54.783..11030.818rows=387533 loops=1)67 -> Hash Join (cost=21345.24..42042.93 rows=387533 width=73) (actual time=225.757..936.489 rows=387533 loops=1) 68 68 Hash Cond: ((dr.employee_id)::text = (e.employee_id)::text) 69 -> Hash Join (cost=20241.74..39921.98 rows=387533 width=55) (actual time=2 41.790..10861.173 rows=387533 loops=1)69 -> Hash Join (cost=20241.74..39921.98 rows=387533 width=55) (actual time=213.778..773.373 rows=387533 loops=1) 70 70 Hash Cond: ((ds.trip_id)::text = (t.trip_id)::text) 71 -> Hash Join (cost=377.25..9577.20 rows=387533 width=35) (actual time= 27.301..10254.297rows=387533 loops=1)71 -> Hash Join (cost=377.25..9577.20 rows=387533 width=35) (actual time=4.038..223.592 rows=387533 loops=1) 72 72 Hash Cond: ((ds.driver_id)::text = (dr.driver_id)::text) 73 -> Seq Scan on driver_schedule ds (cost=0.00..8182.33 rows=387533 width=35) (actual time= 23.007..10084.784rows=387533 loops=1)74 -> Hash (cost=221.00..221.00 rows=12500 width=12) (actual time= 4.261..4.262rows=12500 loops=1)73 -> Seq Scan on driver_schedule ds (cost=0.00..8182.33 rows=387533 width=35) (actual time=0.009..36.137 rows=387533 loops=1) 74 -> Hash (cost=221.00..221.00 rows=12500 width=12) (actual time=3.998..4.000 rows=12500 loops=1) 75 75 Buckets: 16384 Batches: 1 Memory Usage: 674kB 76 -> Seq Scan on driver dr (cost=0.00..221.00 rows=12500 width=12) (actual time=0.0 19..1.918rows=12500 loops=1)77 -> Hash (cost=11613.33..11613.33 rows=387533 width=41) (actual time=2 13.993..213.994rows=387533 loops=1)76 -> Seq Scan on driver dr (cost=0.00..221.00 rows=12500 width=12) (actual time=0.008..1.704 rows=12500 loops=1) 77 -> Hash (cost=11613.33..11613.33 rows=387533 width=41) (actual time=207.762..207.763 rows=387533 loops=1) 78 78 Buckets: 131072 Batches: 8 Memory Usage: 4531kB 79 -> Seq Scan on trip t (cost=0.00..11613.33 rows=387533 width=41) (actual time=0.01 1..78.219rows=387533 loops=1)80 -> Hash (cost=791.00..791.00 rows=25000 width=30) (actual time=1 2.938..12.939rows=25000 loops=1)79 -> Seq Scan on trip t (cost=0.00..11613.33 rows=387533 width=41) (actual time=0.010..71.737 rows=387533 loops=1) 80 -> Hash (cost=791.00..791.00 rows=25000 width=30) (actual time=11.925..11.926 rows=25000 loops=1) 81 81 Buckets: 32768 Batches: 1 Memory Usage: 1787kB 82 -> Seq Scan on employee e (cost=0.00..791.00 rows=25000 width=30) (actual time=0.01 2..6.373rows=25000 loops=1)83 -> Hash (cost=14.10..14.10 rows=410 width=138) (actual time= 7.960..7.960rows=3 loops=1)82 -> Seq Scan on employee e (cost=0.00..791.00 rows=25000 width=30) (actual time=0.010..5.437 rows=25000 loops=1) 83 -> Hash (cost=14.10..14.10 rows=410 width=138) (actual time=0.013..0.014 rows=3 loops=1) 84 84 Buckets: 1024 Batches: 1 Memory Usage: 9kB 85 -> Seq Scan on shift sh (cost=0.00..14.10 rows=410 width=138) (actual time= 7.954..7.955rows=3 loops=1)86 -> Hash (cost=37.83..37.83 rows=783 width=41) (actual time=0.3 26..0.327rows=783 loops=1)85 -> Seq Scan on shift sh (cost=0.00..14.10 rows=410 width=138) (actual time=0.009..0.011 rows=3 loops=1) 86 -> Hash (cost=37.83..37.83 rows=783 width=41) (actual time=0.330..0.331 rows=783 loops=1) 87 87 Buckets: 1024 Batches: 1 Memory Usage: 65kB 88 -> Seq Scan on route r (cost=0.00..37.83 rows=783 width=41) (actual time=0.02 2..0.155rows=783 loops=1)89 -> Hash (cost=104.00..104.00 rows=5000 width=13) (actual time=1. 888..1.889rows=5000 loops=1)88 -> Seq Scan on route r (cost=0.00..37.83 rows=783 width=41) (actual time=0.026..0.158 rows=783 loops=1) 89 -> Hash (cost=104.00..104.00 rows=5000 width=13) (actual time=1.691..1.691 rows=5000 loops=1) 90 90 Buckets: 8192 Batches: 1 Memory Usage: 288kB 91 -> Seq Scan on vehicle v (cost=0.00..104.00 rows=5000 width=13) (actual time=0.0 25..0.877rows=5000 loops=1)92 -> Hash Join (cost=21500.33..40617.75 rows=271712 width=269) (actual time= 558.212..10626.730rows=271712 loops=1)91 -> Seq Scan on vehicle v (cost=0.00..104.00 rows=5000 width=13) (actual time=0.019..0.724 rows=5000 loops=1) 92 -> Hash Join (cost=21500.33..40617.75 rows=271712 width=269) (actual time=227.537..1031.048 rows=271712 loops=1) 93 93 Hash Cond: ((t_1.vehicle_id)::text = (v_1.vehicle_id)::text) 94 -> Hash Join (cost=21333.83..38378.85 rows=271712 width=216) (actual time= 556.482..10501.186rows=271712 loops=1)94 -> Hash Join (cost=21333.83..38378.85 rows=271712 width=216) (actual time=225.778..905.070 rows=271712 loops=1) 95 95 Hash Cond: ((t_1.route_id)::text = (r_1.route_id)::text) 96 -> Hash Join (cost=21286.22..37614.12 rows=271712 width=203) (actual time= 556.125..10420.599rows=271712 loops=1)96 -> Hash Join (cost=21286.22..37614.12 rows=271712 width=203) (actual time=225.431..822.496 rows=271712 loops=1) 97 97 Hash Cond: (cs.shift_id = sh_1.shift_id) 98 -> Hash Join (cost=21266.99..36874.28 rows=271712 width=73) (actual time= 556.099..10353.492rows=271712 loops=1)98 -> Hash Join (cost=21266.99..36874.28 rows=271712 width=73) (actual time=225.391..754.718 rows=271712 loops=1) 99 99 Hash Cond: ((c.employee_id)::text = (e_1.employee_id)::text) 100 -> Hash Join (cost=20163.49..35057.41 rows=271712 width=55) (actual time= 543.852..10227.463rows=271712 loops=1)100 -> Hash Join (cost=20163.49..35057.41 rows=271712 width=55) (actual time=213.590..633.209 rows=271712 loops=1) 101 101 Hash Cond: ((cs.trip_id)::text = (t_1.trip_id)::text) 102 -> Hash Join (cost=299.00..6826.66 rows=271712 width=35) (actual time=3 33.030..9745.885rows=271712 loops=1)102 -> Hash Join (cost=299.00..6826.66 rows=271712 width=35) (actual time=3.533..154.784 rows=271712 loops=1) 103 103 Hash Cond: ((cs.conductor_id)::text = (c.conductor_id)::text) 104 -> Seq Scan on conductor_schedule cs (cost=0.00..5814.12 rows=271712 width=36) (actual time= 118.020..9414.529rows=271712 loops=1)105 -> Hash (cost=174.00..174.00 rows=10000 width=13) (actual time= 214.970..214.971 rows=10000 loops=1)104 -> Seq Scan on conductor_schedule cs (cost=0.00..5814.12 rows=271712 width=36) (actual time=0.010..25.082 rows=271712 loops=1) 105 -> Hash (cost=174.00..174.00 rows=10000 width=13) (actual time=3.491..3.491 rows=10000 loops=1) 106 106 Buckets: 16384 Batches: 1 Memory Usage: 582kB 107 -> Seq Scan on conductor c (cost=0.00..174.00 rows=10000 width=13) (actual time=0.0 23..212.927rows=10000 loops=1)108 -> Hash (cost=11613.33..11613.33 rows=387533 width=41) (actual time=2 10.327..210.328rows=387533 loops=1)107 -> Seq Scan on conductor c (cost=0.00..174.00 rows=10000 width=13) (actual time=0.012..1.568 rows=10000 loops=1) 108 -> Hash (cost=11613.33..11613.33 rows=387533 width=41) (actual time=208.930..208.931 rows=387533 loops=1) 109 109 Buckets: 131072 Batches: 8 Memory Usage: 4531kB 110 -> Seq Scan on trip t_1 (cost=0.00..11613.33 rows=387533 width=41) (actual time=0.01 3..70.554rows=387533 loops=1)111 -> Hash (cost=791.00..791.00 rows=25000 width=30) (actual time=1 2.196..12.196rows=25000 loops=1)110 -> Seq Scan on trip t_1 (cost=0.00..11613.33 rows=387533 width=41) (actual time=0.011..70.870 rows=387533 loops=1) 111 -> Hash (cost=791.00..791.00 rows=25000 width=30) (actual time=11.748..11.748 rows=25000 loops=1) 112 112 Buckets: 32768 Batches: 1 Memory Usage: 1787kB 113 -> Seq Scan on employee e_1 (cost=0.00..791.00 rows=25000 width=30) (actual time=0.01 1..5.545rows=25000 loops=1)114 -> Hash (cost=14.10..14.10 rows=410 width=138) (actual time=0.01 4..0.014rows=3 loops=1)113 -> Seq Scan on employee e_1 (cost=0.00..791.00 rows=25000 width=30) (actual time=0.010..5.312 rows=25000 loops=1) 114 -> Hash (cost=14.10..14.10 rows=410 width=138) (actual time=0.016..0.017 rows=3 loops=1) 115 115 Buckets: 1024 Batches: 1 Memory Usage: 9kB 116 -> Seq Scan on shift sh_1 (cost=0.00..14.10 rows=410 width=138) (actual time=0.01 0..0.011rows=3 loops=1)117 -> Hash (cost=37.83..37.83 rows=783 width=41) (actual time=0.33 5..0.336rows=783 loops=1)116 -> Seq Scan on shift sh_1 (cost=0.00..14.10 rows=410 width=138) (actual time=0.012..0.013 rows=3 loops=1) 117 -> Hash (cost=37.83..37.83 rows=783 width=41) (actual time=0.336..0.337 rows=783 loops=1) 118 118 Buckets: 1024 Batches: 1 Memory Usage: 65kB 119 -> Seq Scan on route r_1 (cost=0.00..37.83 rows=783 width=41) (actual time=0.02 6..0.162 rows=783 loops=1)120 -> Hash (cost=104.00..104.00 rows=5000 width=13) (actual time=1.7 03..1.704rows=5000 loops=1)119 -> Seq Scan on route r_1 (cost=0.00..37.83 rows=783 width=41) (actual time=0.028..0.162 rows=783 loops=1) 120 -> Hash (cost=104.00..104.00 rows=5000 width=13) (actual time=1.728..1.728 rows=5000 loops=1) 121 121 Buckets: 8192 Batches: 1 Memory Usage: 288kB 122 -> Seq Scan on vehicle v_1 (cost=0.00..104.00 rows=5000 width=13) (actual time=0.018..0.746 rows=5000 loops=1) 123 Planning Time: 543.865 ms 124 Execution Time: 22127.391 ms 125 122 -> Seq Scan on vehicle v_1 (cost=0.00..104.00 rows=5000 width=13) (actual time=0.025..0.765 rows=5000 loops=1) 123 Planning Time: 3.793 ms 124 Execution Time: 2427.455 ms 126 125 }}} 127 126 … … 141 140 WHERE passenger_id = '1000'; 142 141 }}} 143 6. Планерот користи Index Scan на idx_notification_passenger_status наместо Seq Scan, со време на извршување од 0. 060ms — што е практично моментално и целосно прифатливо за production употреба.144 {{{ 145 Nested Loop (cost=0.87..24.98 rows=3 width=121) (actual time=0.0 24..0.025rows=0 loops=1)146 -> Index Scan using pk_passenger on passenger p (cost=0.43..8.45 rows=1 width=24) (actual time=0.0 23..0.024rows=0 loops=1)142 6. Планерот користи Index Scan на idx_notification_passenger_status наместо Seq Scan, со време на извршување од 0.101 ms — што е практично моментално и целосно прифатливо за production употреба. 143 {{{ 144 Nested Loop (cost=0.87..24.98 rows=3 width=121) (actual time=0.052..0.053 rows=0 loops=1) 145 -> Index Scan using pk_passenger on passenger p (cost=0.43..8.45 rows=1 width=24) (actual time=0.051..0.051 rows=0 loops=1) 147 146 Index Cond: ((passenger_id)::text = '1000'::text) 148 147 -> Index Scan using idx_notification_passenger_status on notification n (cost=0.43..16.49 rows=3 width=89) (never executed) 149 148 Index Cond: ((passenger_id)::text = '1000'::text) 150 Planning Time: 0. 173ms151 Execution Time: 0. 053ms149 Planning Time: 0.359 ms 150 Execution Time: 0.101 ms 152 151 }}} 153 152
