Changes between Version 5 and Version 6 of OtherTopics


Ignore:
Timestamp:
02/13/26 17:50:43 (2 weeks ago)
Author:
231017
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OtherTopics

    v5 v6  
    173173
    174174Заклучуваме дека перформансите на процедурата ќе бидат многу подобри со овој индекс и го додаваме истиот.
     175
     176== Сценарио 2
     177
     178Ќе го разгледуваме прашалникот кој се извршува при преземање на сите песни/албуми објавени од артистот.
     179
     180{{{
     181EXPLAIN ANALYZE SELECT
     182    me.id,
     183    me.title,
     184    me.genre,
     185    CASE
     186        WHEN s.id IS NOT NULL AND s.album_id IS NULL THEN 'SONG'
     187        WHEN a.id IS NOT NULL THEN 'ALBUM'
     188        ELSE NULL
     189        END AS type,
     190    me.cover,
     191    me.release_date
     192FROM musical_entities me
     193         LEFT JOIN songs s ON s.id = me.id
     194         LEFT JOIN albums a ON a.id = me.id
     195WHERE me.released_by = :artistId
     196ORDER BY me.release_date DESC
     197}}}
     198
     199Додаваме 1M нови записи во musical_entities табелата:
     200
     201{{{
     202}}}
     203
     204Креираме композитен индекс на `released_by` и `release_date`
     205
     206{{{
     207CREATE INDEX me_released_by_release_date_idx
     208    ON musical_entities (released_by, release_date DESC);
     209}}}
     210
     211Без индекс добиваме:
     212
     213{{{
     214+--------------------------------------------------------------------------------------------------------------------------------------------------------+
     215|QUERY PLAN                                                                                                                                              |
     216+--------------------------------------------------------------------------------------------------------------------------------------------------------+
     217|Gather Merge  (cost=48470.13..48470.59 rows=4 width=82) (actual time=100.621..107.725 rows=3 loops=1)                                                   |
     218|  Workers Planned: 2                                                                                                                                    |
     219|  Workers Launched: 2                                                                                                                                   |
     220|  ->  Sort  (cost=47470.10..47470.11 rows=2 width=82) (actual time=76.312..76.315 rows=1 loops=3)                                                       |
     221|        Sort Key: me.release_date DESC                                                                                                                  |
     222|        Sort Method: quicksort  Memory: 25kB                                                                                                            |
     223|        Worker 0:  Sort Method: quicksort  Memory: 25kB                                                                                                 |
     224|        Worker 1:  Sort Method: quicksort  Memory: 25kB                                                                                                 |
     225|        ->  Nested Loop Left Join  (cost=0.84..47470.09 rows=2 width=82) (actual time=55.582..76.259 rows=1 loops=3)                                    |
     226|              ->  Nested Loop Left Join  (cost=0.42..47461.22 rows=2 width=66) (actual time=55.561..76.234 rows=1 loops=3)                              |
     227|                    ->  Parallel Seq Scan on musical_entities_temp me  (cost=0.00..47444.33 rows=2 width=50) (actual time=55.513..76.174 rows=1 loops=3)|
     228|                          Filter: (released_by = 123456)                                                                                                |
     229|                          Rows Removed by Filter: 333332                                                                                                |
     230|                    ->  Index Scan using temp_songs_pkey on temp_songs s  (cost=0.42..8.44 rows=1 width=16) (actual time=0.049..0.049 rows=1 loops=3)   |
     231|                          Index Cond: (id = me.id)                                                                                                      |
     232|              ->  Index Only Scan using temp_albums_pkey on temp_albums a  (cost=0.42..4.44 rows=1 width=8) (actual time=0.020..0.020 rows=0 loops=3)   |
     233|                    Index Cond: (id = me.id)                                                                                                            |
     234|                    Heap Fetches: 0                                                                                                                     |
     235|Planning Time: 0.525 ms                                                                                                                                 |
     236|Execution Time: 107.796 ms                                                                                                                              |
     237+--------------------------------------------------------------------------------------------------------------------------------------------------------+
     238}}}
     239
     240Просечното `Execution Time` од 10 извршувања е **100,59 ms**
     241
     242Со индекс добиваме:
     243
     244{{{
     245+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
     246|QUERY PLAN                                                                                                                                                            |
     247+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
     248|Nested Loop Left Join  (cost=1.27..88.91 rows=5 width=82) (actual time=0.061..0.084 rows=3 loops=1)                                                                   |
     249|  ->  Nested Loop Left Join  (cost=0.85..66.72 rows=5 width=66) (actual time=0.026..0.041 rows=3 loops=1)                                                             |
     250|        ->  Index Scan using me_released_by_release_date_idx on musical_entities_temp me  (cost=0.42..24.51 rows=5 width=50) (actual time=0.013..0.018 rows=3 loops=1)|
     251|              Index Cond: (released_by = 123456)                                                                                                                      |
     252|        ->  Index Scan using temp_songs_pkey on temp_songs s  (cost=0.42..8.44 rows=1 width=16) (actual time=0.005..0.005 rows=1 loops=3)                             |
     253|              Index Cond: (id = me.id)                                                                                                                                |
     254|  ->  Index Only Scan using temp_albums_pkey on temp_albums a  (cost=0.42..4.44 rows=1 width=8) (actual time=0.013..0.013 rows=0 loops=3)                             |
     255|        Index Cond: (id = me.id)                                                                                                                                      |
     256|        Heap Fetches: 0                                                                                                                                               |
     257|Planning Time: 0.491 ms                                                                                                                                               |
     258|Execution Time: 0.127 ms                                                                                                                                              |
     259+----------------------------------------------------------------------------------------------------------------------------------------------------------------------++
     260}}}
     261
     262
     263Можеме да забележиме користење на новокреираниот индекс преку `Index Scan using me_released_by_index on musical_entities me`, сега просечното време за 10 извршувања е **0,105 ms**,
     264што претставува значително подобрување на перформансите.
     265
    175266
    176267== Безбедност и заштита