Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The below compares the performance of the the query before recreating the view, and then after recreating the view.
- Before: 195.0 ms
- After: 0.310 ms
- The last command, the 'diff', shows that the view itself hasn't changed.
- postgres@postgresql-dev:~$ psql --cluster 8.1/matrix-2008-09-03 itm_matrix_prod
- Welcome to psql 8.1.11, the PostgreSQL interactive terminal.
- Type: \copyright for distribution terms
- \h for help with SQL commands
- \? for help with psql commands
- \g or terminate with semicolon to execute query
- \q to quit
- itm_matrix_prod=# \o /tmp/pre_view_recreate.txt
- itm_matrix_prod=# \d sq_vw_ast_perm
- itm_matrix_prod=# \o
- itm_matrix_prod=# EXPLAIN ANALYZE SELECT DISTINCT assetid, userid, granted FROM sq_vw_ast_perm WHERE assetid = '7337' AND permission = 1;
- QUERY PLAN
- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Unique (cost=8690.55..8690.76 rows=21 width=96) (actual time=194.035..194.039 rows=2 loops=1)
- -> Sort (cost=8690.55..8690.60 rows=21 width=96) (actual time=194.033..194.034 rows=2 loops=1)
- Sort Key: assetid, userid, "granted"
- -> Subquery Scan sq_vw_ast_perm (cost=8689.62..8690.09 rows=21 width=96) (actual time=193.948..193.956 rows=2 loops=1)
- -> Unique (cost=8689.62..8689.88 rows=21 width=71) (actual time=193.945..193.951 rows=2 loops=1)
- -> Sort (cost=8689.62..8689.67 rows=21 width=71) (actual time=193.944..193.946 rows=2 loops=1)
- Sort Key: assetid, userid, permission, "granted"
- -> Append (cost=8236.91..8689.16 rows=21 width=71) (actual time=193.910..193.916 rows=2 loops=1)
- -> Hash Join (cost=8236.91..8683.17 rows=19 width=71) (actual time=193.888..193.888 rows=0 loops=1)
- Hash Cond: ((("outer".userid)::text = ("inner".roleid)::text) AND (("outer".assetid)::text = ("inner".assetid)::text))
- -> Unique (cost=3663.88..3996.33 rows=5681 width=22) (actual time=193.726..193.726 rows=1 loops=1)
- -> Sort (cost=3663.88..3730.37 rows=26596 width=22) (actual time=193.723..193.723 rows=1 loops=1)
- Sort Key: sq_ast_perm.userid, sq_ast_perm.permission, sq_ast_perm."granted", sq_ast_perm.assetid
- -> Seq Scan on sq_ast_perm (cost=0.00..1342.20 rows=26596 width=22) (actual time=0.010..33.566 rows=27239 loops=1)
- Filter: (permission = 1)
- -> Hash (cost=4572.35..4572.35 rows=136 width=96) (actual time=0.048..0.048 rows=0 loops=1)
- -> Unique (cost=4569.63..4570.99 rows=136 width=96) (actual time=0.046..0.046 rows=0 loops=1)
- -> Sort (cost=4569.63..4569.97 rows=136 width=96) (actual time=0.045..0.045 rows=0 loops=1)
- Sort Key: sq_vw_ast_role.assetid, sq_vw_ast_role.userid, sq_vw_ast_role.roleid
- -> Subquery Scan sq_vw_ast_role (cost=4562.09..4564.81 rows=136 width=96) (actual time=0.039..0.039 rows=0 loops=1)
- -> Unique (cost=4562.09..4563.45 rows=136 width=309) (actual time=0.037..0.037 rows=0 loops=1)
- -> Sort (cost=4562.09..4562.43 rows=136 width=309) (actual time=0.035..0.035 rows=0 loops=1)
- Sort Key: assetid, roleid, userid
- -> Append (cost=1410.52..4557.27 rows=136 width=309) (actual time=0.024..0.024 rows=0 loops=1)
- -> Hash Join (cost=1410.52..4552.19 rows=135 width=196) (actual time=0.020..0.020 rows=0 loops=1)
- Hash Cond: (("outer"."?column2?")::text = ("inner".roleid)::text)
- -> Append (cost=1406.23..4140.96 rows=27039 width=317) (never executed)
- -> Hash Join (cost=1406.23..3858.97 rows=26879 width=190) (never executed)
- Hash Cond: (("outer".minorid)::text = ("inner".assetid)::text)
- -> Seq Scan on sq_ast_lnk l (cost=0.00..827.79 rows=26879 width=45) (never executed)
- -> Hash (cost=1047.38..1047.38 rows=14338 width=145) (never executed)
- -> Seq Scan on sq_ast a (cost=0.00..1047.38 rows=14338 width=145) (never executed)
- -> Subquery Scan "*SELECT* 2" (cost=0.00..13.20 rows=160 width=317) (never executed)
- -> Seq Scan on sq_shdw_ast_lnk s (cost=0.00..11.60 rows=160 width=317) (never executed)
- -> Hash (cost=4.29..4.29 rows=1 width=164) (actual time=0.009..0.009 rows=0 loops=1)
- -> Index Scan using ast_role_pk on sq_ast_role r (cost=0.00..4.29 rows=1 width=164) (actual time=0.007..0.007 rows=0 loops=1)
- Index Cond: (((assetid)::text = '7337'::text) AND ((userid)::text = '0'::text))
- -> Index Scan using ast_role_pk on sq_ast_role (cost=0.00..3.72 rows=1 width=309) (actual time=0.001..0.001 rows=0 loops=1)
- Index Cond: ((assetid)::text = '7337'::text)
- Filter: ((userid)::text <> '0'::text)
- -> Index Scan using ast_perm_pk on sq_ast_perm p (cost=0.00..5.78 rows=2 width=22) (actual time=0.020..0.023 rows=2 loops=1)
- Index Cond: (((assetid)::text = '7337'::text) AND (permission = 1))
- Total runtime: 195.207 ms
- (43 rows)
- itm_matrix_prod=# CREATE OR REPLACE VIEW sq_vw_ast_perm AS
- itm_matrix_prod-# SELECT
- itm_matrix_prod-# r.assetid, r.userid, p.permission, p.granted
- itm_matrix_prod-# FROM
- itm_matrix_prod-# (
- itm_matrix_prod(# SELECT DISTINCT
- itm_matrix_prod(# assetid, userid, roleid
- itm_matrix_prod(# FROM
- itm_matrix_prod(# sq_vw_ast_role
- itm_matrix_prod(# ) r
- itm_matrix_prod-# JOIN
- itm_matrix_prod-# (
- itm_matrix_prod(# SELECT DISTINCT
- itm_matrix_prod(# userid, permission, granted, assetid
- itm_matrix_prod(# FROM
- itm_matrix_prod(# sq_ast_perm
- itm_matrix_prod(# ) p
- itm_matrix_prod-# ON r.roleid = p.userid
- itm_matrix_prod-# AND r.assetid = p.assetid
- itm_matrix_prod-# UNION
- itm_matrix_prod-# SELECT
- itm_matrix_prod-# p.assetid, p.userid, p.permission, p.granted
- itm_matrix_prod-# FROM
- itm_matrix_prod-# sq_ast_perm p;
- CREATE VIEW
- itm_matrix_prod=# \o /tmp/post_view_recreate.txt
- itm_matrix_prod=# \d sq_vw_ast_perm
- itm_matrix_prod=# \o
- itm_matrix_prod=# EXPLAIN ANALYZE SELECT DISTINCT assetid, userid, granted FROM sq_vw_ast_perm WHERE assetid = '7337' AND permission = 1;
- QUERY PLAN
- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Unique (cost=4584.76..4584.79 rows=3 width=96) (actual time=0.138..0.143 rows=2 loops=1)
- -> Sort (cost=4584.76..4584.77 rows=3 width=96) (actual time=0.136..0.137 rows=2 loops=1)
- Sort Key: assetid, userid, "granted"
- -> Subquery Scan sq_vw_ast_perm (cost=4584.67..4584.74 rows=3 width=96) (actual time=0.110..0.117 rows=2 loops=1)
- -> Unique (cost=4584.67..4584.71 rows=3 width=71) (actual time=0.109..0.114 rows=2 loops=1)
- -> Sort (cost=4584.67..4584.68 rows=3 width=71) (actual time=0.108..0.109 rows=2 loops=1)
- Sort Key: assetid, userid, permission, "granted"
- -> Append (cost=4578.48..4584.65 rows=3 width=71) (actual time=0.093..0.097 rows=2 loops=1)
- -> Hash Join (cost=4578.48..4578.84 rows=1 width=71) (actual time=0.087..0.087 rows=0 loops=1)
- Hash Cond: (("outer".userid)::text = ("inner".roleid)::text)
- -> Unique (cost=5.79..5.81 rows=1 width=22) (actual time=0.039..0.039 rows=1 loops=1)
- -> Sort (cost=5.79..5.79 rows=2 width=22) (actual time=0.038..0.038 rows=1 loops=1)
- Sort Key: sq_ast_perm.userid, sq_ast_perm.permission, sq_ast_perm."granted", sq_ast_perm.assetid
- -> Index Scan using ast_perm_pk on sq_ast_perm (cost=0.00..5.78 rows=2 width=22) (actual time=0.017..0.022 rows=2 loops=1)
- Index Cond: (('7337'::text = (assetid)::text) AND (permission = 1))
- -> Hash (cost=4572.35..4572.35 rows=136 width=96) (actual time=0.033..0.033 rows=0 loops=1)
- -> Unique (cost=4569.63..4570.99 rows=136 width=96) (actual time=0.032..0.032 rows=0 loops=1)
- -> Sort (cost=4569.63..4569.97 rows=136 width=96) (actual time=0.031..0.031 rows=0 loops=1)
- Sort Key: sq_vw_ast_role.assetid, sq_vw_ast_role.userid, sq_vw_ast_role.roleid
- -> Subquery Scan sq_vw_ast_role (cost=4562.09..4564.81 rows=136 width=96) (actual time=0.024..0.024 rows=0 loops=1)
- -> Unique (cost=4562.09..4563.45 rows=136 width=309) (actual time=0.023..0.023 rows=0 loops=1)
- -> Sort (cost=4562.09..4562.43 rows=136 width=309) (actual time=0.021..0.021 rows=0 loops=1)
- Sort Key: assetid, roleid, userid
- -> Append (cost=1410.52..4557.27 rows=136 width=309) (actual time=0.018..0.018 rows=0 loops=1)
- -> Hash Join (cost=1410.52..4552.19 rows=135 width=196) (actual time=0.015..0.015 rows=0 loops=1)
- Hash Cond: (("outer"."?column2?")::text = ("inner".roleid)::text)
- -> Append (cost=1406.23..4140.96 rows=27039 width=317) (never executed)
- -> Hash Join (cost=1406.23..3858.97 rows=26879 width=190) (never executed)
- Hash Cond: (("outer".minorid)::text = ("inner".assetid)::text)
- -> Seq Scan on sq_ast_lnk l (cost=0.00..827.79 rows=26879 width=45) (never executed)
- -> Hash (cost=1047.38..1047.38 rows=14338 width=145) (never executed)
- -> Seq Scan on sq_ast a (cost=0.00..1047.38 rows=14338 width=145) (never executed)
- -> Subquery Scan "*SELECT* 2" (cost=0.00..13.20 rows=160 width=317) (never executed)
- -> Seq Scan on sq_shdw_ast_lnk s (cost=0.00..11.60 rows=160 width=317) (never executed)
- -> Hash (cost=4.29..4.29 rows=1 width=164) (actual time=0.003..0.003 rows=0 loops=1)
- -> Index Scan using ast_role_pk on sq_ast_role r (cost=0.00..4.29 rows=1 width=164) (actual time=0.003..0.003 rows=0 loops=1)
- Index Cond: (((assetid)::text = '7337'::text) AND ((userid)::text = '0'::text))
- -> Index Scan using ast_role_pk on sq_ast_role (cost=0.00..3.72 rows=1 width=309) (actual time=0.001..0.001 rows=0 loops=1)
- Index Cond: ((assetid)::text = '7337'::text)
- Filter: ((userid)::text <> '0'::text)
- -> Index Scan using ast_perm_pk on sq_ast_perm p (cost=0.00..5.78 rows=2 width=22) (actual time=0.004..0.006 rows=2 loops=1)
- Index Cond: (((assetid)::text = '7337'::text) AND (permission = 1))
- Total runtime: 0.310 ms
- (43 rows)
- itm_matrix_prod=#
- itm_matrix_prod=# \q
- postgres@postgresql-dev:~$ diff -u /tmp/pre_view_recrecreate.txt /tmp/post_view_recrecreate.txt
- postgres@postgresql-dev:~$
Add Comment
Please, Sign In to add comment