| 37 | | Иако базата се обидува да користи постоечки '''Primary Key''' индекси, главниот проблем се јавува во спојувањето на четирите табели `Performer`, `Event_Happening_Performer`, `Event_Happening`, `Event`. |
| 38 | | |
| 39 | | Најбавниот дел е кај '''Nested Loop''' операциите и проверката на '''Foreign Keys''' при '''INSERT''' (1.424 s). |
| 40 | | |
| 41 | | Времето на планирање е исто така високо (1.8 s), што укажува на комплексност во резолвирањето на релациите без соодветни патеки. |
| | 40 | При '''SELECT''' операцијата, PostgreSQL паметно ги користи веќе постоечките уникатни индекси генерирани од бизнис констреинтите, овозможувајќи брз '''Index Scan'''. Меѓутоа, при '''INSERT''' во табелата `Seat`, базата троши дури 19.752 ms само на тригерот за проверка на '''foreign key''' (`fk_seat_section`), бидејќи без ажурирана статистика, планерот мора рачно да ја проверува релацијата на диск. |
| 53 | | ||Nested Loop (cost\=1.14..73.38 rows\=7 width\=68) (actual time\=1505.862..3249.242 rows\=4 loops\=1)|| |
| 54 | | || -> Nested Loop (cost\=0.86..70.92 rows\=7 width\=37) (actual time\=1079.648..2259.465 rows\=4 loops\=1)|| |
| 55 | | || -> Index Scan using "Performer_pkey" on "Performer" p (cost\=0.28..8.30 rows\=1 width\=21) (actual time\=701.003..701.005 rows\=1 loops\=1)|| |
| 56 | | || Index Cond: (performer_id \= 10)|| |
| 57 | | || -> Nested Loop (cost\=0.57..62.55 rows\=7 width\=24) (actual time\=378.637..1558.442 rows\=4 loops\=1)|| |
| 58 | | || -> Index Only Scan using uq_performer_at_time on "Event_Happening_Performer" ehp (cost\=0.29..4.41 rows\=7 width\=16) (actual time\=90.817..90.838 rows\=4 loops\=1)|| |
| 59 | | || Index Cond: (performer_id \= 10)|| |
| 60 | | || Heap Fetches: 0|| |
| 61 | | || -> Index Scan using "Event_Happening_pkey" on "Event_Happening" eh (cost\=0.29..8.30 rows\=1 width\=24) (actual time\=366.888..366.889 rows\=1 loops\=4)|| |
| 62 | | || Index Cond: (event_happening_id \= ehp.event_happening_id)|| |
| 63 | | || -> Index Scan using "Event_pkey" on "Event" e (cost\=0.29..0.35 rows\=1 width\=39) (actual time\=247.432..247.432 rows\=1 loops\=4)|| |
| 64 | | || Index Cond: (event_id \= eh.event_id)|| |
| 65 | | ||Planning Time: 1816.633 ms|| |
| 66 | | ||Execution Time: 3249.314 ms|| |
| | 53 | ||Nested Loop (cost\=1.01..8110.27 rows\=1886 width\=54) (actual time\=0.152..0.475 rows\=775.00 loops\=1)|| |
| | 54 | || Buffers: shared hit\=21 read\=13 dirtied\=1|| |
| | 55 | || -> Nested Loop (cost\=0.57..23.73 rows\=5 width\=38) (actual time\=0.129..0.131 rows\=5.00 loops\=1)|| |
| | 56 | || Buffers: shared read\=6|| |
| | 57 | || -> Index Scan using ""Venue_pkey"" on ""Venue"" v (cost\=0.29..8.30 rows\=1 width\=28) (actual time\=0.061..0.061 rows\=1.00 loops\=1)|| |
| | 58 | || Index Cond: (venue_id \= 1)|| |
| | 59 | || Index Searches: 1|| |
| | 60 | || Buffers: shared read\=3|| |
| | 61 | || -> Index Scan using uq_section_venue_name on ""Section"" s (cost\=0.29..15.38 rows\=5 width\=18) (actual time\=0.051..0.052 rows\=5.00 loops\=1)|| |
| | 62 | || Index Cond: (venue_id \= 1)|| |
| | 63 | || Index Searches: 1|| |
| | 64 | || Buffers: shared read\=3|| |
| | 65 | || -> Index Scan using uq_seat_section_number on ""Seat"" st (cost\=0.44..1608.23 rows\=908 width\=24) (actual time\=0.009..0.050 rows\=155.00 loops\=5)|| |
| | 66 | || Index Cond: (section_id \= s.section_id)|| |
| | 67 | || Index Searches: 5|| |
| | 68 | || Buffers: shared hit\=21 read\=7 dirtied\=1|| |
| | 69 | ||Planning:|| |
| | 70 | || Buffers: shared hit\=4 read\=11|| |
| | 71 | ||Planning Time: 0.631 ms|| |
| | 72 | ||Execution Time: 0.577 ms|| |
| 79 | | ||Insert on "Event_Happening_Performer" (cost\=0.00..0.01 rows\=0 width\=0) (actual time\=558.243..558.244 rows\=0 loops\=1)|| |
| 80 | | || -> Result (cost\=0.00..0.01 rows\=1 width\=16) (actual time\=0.001..0.002 rows\=1 loops\=1)|| |
| 81 | | ||Planning Time: 0.042 ms|| |
| 82 | | ||Trigger for constraint fk_ehp_event_happening: time\=826.105 calls\=1|| |
| 83 | | ||Trigger for constraint fk_ehp_performer: time\=40.075 calls\=1|| |
| 84 | | ||Execution Time: 1424.451 ms|| |
| | 85 | ||Insert on ""Seat"" (cost\=0.00..0.01 rows\=0 width\=0) (actual time\=0.135..0.136 rows\=0.00 loops\=1)|| |
| | 86 | || Buffers: shared hit\=5 read\=3 dirtied\=1|| |
| | 87 | || -> Result (cost\=0.00..0.01 rows\=1 width\=24) (actual time\=0.001..0.001 rows\=1.00 loops\=1)|| |
| | 88 | ||Planning Time: 0.058 ms|| |
| | 89 | ||Trigger for constraint fk_seat_section: time\=19.752 calls\=1|| |
| | 90 | ||Execution Time: 19.912 ms|| |
| 98 | | ||Update on "Event_Happening_Performer" (cost\=0.29..8.31 rows\=0 width\=0) (actual time\=0.219..0.219 rows\=0 loops\=1)|| |
| 99 | | || -> Index Scan using uq_performer_at_time on "Event_Happening_Performer" (cost\=0.29..8.31 rows\=1 width\=14) (actual time\=0.120..0.121 rows\=1 loops\=1)|| |
| 100 | | || Index Cond: ((performer_id \= 10) AND (event_happening_id \= 1))|| |
| 101 | | ||Planning Time: 0.139 ms|| |
| 102 | | ||Trigger for constraint fk_ehp_performer: time\=0.279 calls\=1|| |
| 103 | | ||Execution Time: 23.621 ms|| |
| | 104 | ||Update on ""Seat"" (cost\=0.44..8.46 rows\=0 width\=0) (actual time\=0.106..0.106 rows\=0.00 loops\=1)|| |
| | 105 | || Buffers: shared hit\=12|| |
| | 106 | || -> Index Scan using ""Seat_pkey"" on ""Seat"" (cost\=0.44..8.46 rows\=1 width\=10) (actual time\=0.048..0.049 rows\=1.00 loops\=1)|| |
| | 107 | || Index Cond: (seat_id \= 99999999)|| |
| | 108 | || Index Searches: 1|| |
| | 109 | || Buffers: shared hit\=4|| |
| | 110 | ||Planning Time: 0.171 ms|| |
| | 111 | ||Execution Time: 0.137 ms|| |
| 126 | | По додавањето на индексите, времето на извршување на '''SELECT''' се намали на 0.533 ms. |
| 127 | | |
| 128 | | Подобрување: Ова е забрзување од над 6000 пати. |
| 129 | | |
| 130 | | Операциите за '''INSERT''' и '''UPDATE''' сега се извршуваат за помалку од 1ms, што значи дека индексите не го забавуваат системот, туку помагаат дури и кај тригерите за '''Foreign Keys'''. |
| 131 | | |
| 132 | | * '''SELECT''' |
| 133 | | |
| 134 | | {{{ |
| 135 | | |
| 136 | | EXPLAIN ANALYZE |
| 137 | | SELECT * FROM "Performer_Events" WHERE performer_id = 10; |
| 138 | | |
| 139 | | }}} |
| 140 | | |
| 141 | | ||= QUERY PLAN =|| |
| 142 | | ||Nested Loop (cost\=1.14..77.38 rows\=7 width\=68) (actual time\=0.251..0.479 rows\=4 loops\=1)|| |
| 143 | | || -> Nested Loop (cost\=0.86..74.92 rows\=7 width\=37) (actual time\=0.201..0.306 rows\=4 loops\=1)|| |
| 144 | | || -> Index Scan using "Performer_pkey" on "Performer" p (cost\=0.28..8.30 rows\=1 width\=21) (actual time\=0.093..0.094 rows\=1 loops\=1)|| |
| 145 | | || Index Cond: (performer_id \= 10)|| |
| 146 | | || -> Nested Loop (cost\=0.57..66.55 rows\=7 width\=24) (actual time\=0.105..0.207 rows\=4 loops\=1)|| |
| 147 | | || -> Index Only Scan using uq_performer_at_time on "Event_Happening_Performer" ehp (cost\=0.29..8.41 rows\=7 width\=16) (actual time\=0.062..0.065 rows\=4 loops\=1)|| |
| 148 | | || Index Cond: (performer_id \= 10)|| |
| 149 | | || Heap Fetches: 1|| |
| 150 | | || -> Index Scan using "Event_Happening_pkey" on "Event_Happening" eh (cost\=0.29..8.30 rows\=1 width\=24) (actual time\=0.032..0.032 rows\=1 loops\=4)|| |
| 151 | | || Index Cond: (event_happening_id \= ehp.event_happening_id)|| |
| 152 | | || -> Index Scan using "Event_pkey" on "Event" e (cost\=0.29..0.35 rows\=1 width\=39) (actual time\=0.041..0.041 rows\=1 loops\=4)|| |
| 153 | | || Index Cond: (event_id \= eh.event_id)|| |
| 154 | | ||Planning Time: 1.600 ms|| |
| 155 | | ||Execution Time: 0.533 ms|| |
| 156 | | |
| 157 | | * '''INSERT''' |
| 158 | | |
| 159 | | {{{ |
| 160 | | |
| 161 | | EXPLAIN ANALYZE |
| 162 | | INSERT INTO "Event_Happening_Performer" (event_happening_id, performer_id) |
| 163 | | VALUES (2, 10); |
| 164 | | |
| 165 | | }}} |
| 166 | | |
| 167 | | ||= QUERY PLAN =|| |
| 168 | | ||Insert on "Event_Happening_Performer" (cost\=0.00..0.01 rows\=0 width\=0) (actual time\=0.307..0.308 rows\=0 loops\=1)|| |
| 169 | | || -> Result (cost\=0.00..0.01 rows\=1 width\=16) (actual time\=0.001..0.001 rows\=1 loops\=1)|| |
| 170 | | ||Planning Time: 0.046 ms|| |
| 171 | | ||Trigger for constraint fk_ehp_event_happening: time\=0.332 calls\=1|| |
| 172 | | ||Trigger for constraint fk_ehp_performer: time\=0.169 calls\=1|| |
| 173 | | ||Execution Time: 0.833 ms|| |
| 174 | | |
| 175 | | * '''UPDATE''' |
| 176 | | |
| 177 | | {{{ |
| 178 | | |
| 179 | | EXPLAIN ANALYZE |
| 180 | | UPDATE "Event_Happening_Performer" |
| 181 | | SET performer_id = 12 |
| 182 | | WHERE event_happening_id = 2 AND performer_id = 10; |
| 183 | | |
| 184 | | }}} |
| 185 | | |
| 186 | | ||= QUERY PLAN =|| |
| 187 | | ||Update on "Event_Happening_Performer" (cost\=0.29..8.31 rows\=0 width\=0) (actual time\=0.437..0.438 rows\=0 loops\=1)|| |
| 188 | | || -> Index Scan using idx_ehp_happening_id on "Event_Happening_Performer" (cost\=0.29..8.31 rows\=1 width\=14) (actual time\=0.174..0.175 rows\=1 loops\=1)|| |
| 189 | | || Index Cond: (event_happening_id \= 2)|| |
| 190 | | || Filter: (performer_id \= 10)|| |
| 191 | | || Rows Removed by Filter: 1|| |
| 192 | | ||Planning Time: 0.161 ms|| |
| 193 | | ||Trigger for constraint fk_ehp_performer: time\=0.271 calls\=1|| |
| 194 | | ||Execution Time: 0.763 ms|| |
| | 125 | По извршување на '''ANALYZE''', базата стекна целосен увид во дистрибуцијата на податоците, со што времето на '''INSERT''' се намали на 0.628 ms, што претставува забрзување од околу 30 пати. Операциите за '''SELECT''' и '''UPDATE''' ги задржаа своите врвни перформанси во под-милисекунден опсег. |