All pastes #1353156 Raw Edit

major sql optimisation

public sql v1 · immutable
#1353156 ·published 2009-03-05 02:03 UTC
rendered paste body
EXPLAIN SELECT DISTINCT article.id, article.title, article.extra, article.date, article."lastModified", article.intro,                article."imageUrl", article.visible, article."productLine_id", article.name, article.content           FROM article, category_articles__article_categories AS category_articles__article_categories_1,                category_articles__article_categories AS category_articles__article_categories_2,                category_articles__article_categories AS category_articles__article_categories_3,                category_articles__article_categories AS category_articles__article_categories_4          WHERE article.content IS NOT NULL            AND article.content != ''            AND article.visible = true            AND (article.id = category_articles__article_categories_1.article_id            AND 6 = category_articles__article_categories_1.category_id OR article.id = category_articles__article_categories_2.article_id            AND 13 = category_articles__article_categories_2.category_id OR article.id = category_articles__article_categories_3.article_id            AND 22 = category_articles__article_categories_3.category_id OR article.id = category_articles__article_categories_4.article_id            AND 32 = category_articles__article_categories_4.category_id)            AND article.id != 67       ORDER BY article.date DESC;                                                                                                                                                    QUERY PLAN                     ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Unique  (cost=17363849.97..17397877.17 rows=48 width=1124)   ->  Sort  (cost=17363849.97..17366685.57 rows=1134240 width=1124)         Sort Key: article.date, article.id, article.title, article.extra, article."lastModified", article.intro, article."imageUrl", article.visible, article."productLine_id", article.name, article.content         ->  Nested Loop  (cost=59.88..14043563.46 rows=1134240 width=1124)               Join Filter: ((("outer".id = "outer".article_id) AND (6 = "outer".category_id)) OR (("outer".id = "outer".article_id) AND (13 = "outer".category_id)) OR (("outer".id = "inner".article_id) AND (22 = "inner".category_id)) OR (("outer".id = "inner".article_id) AND (32 = "inner".category_id)))               ->  Nested Loop  (cost=0.00..5200.86 rows=129792 width=1140)                     ->  Nested Loop  (cost=0.00..109.02 rows=2496 width=1132)                           ->  Seq Scan on article  (cost=0.00..11.10 rows=48 width=1124)                                 Filter: ((content IS NOT NULL) AND (content <> ''::text) AND (visible = true) AND (id <> 67))                           ->  Seq Scan on category_articles__article_categories category_articles__article_categories_1  (cost=0.00..1.52 rows=52 width=8)                     ->  Seq Scan on category_articles__article_categories category_articles__article_categories_2  (cost=0.00..1.52 rows=52 width=8)               ->  Materialize  (cost=59.88..86.92 rows=2704 width=16)                     ->  Nested Loop  (cost=1.57..57.17 rows=2704 width=16)                           ->  Seq Scan on category_articles__article_categories category_articles__article_categories_3  (cost=0.00..1.52 rows=52 width=8)                           ->  Materialize  (cost=1.57..2.09 rows=52 width=8)                                 ->  Seq Scan on category_articles__article_categories category_articles__article_categories_4  (cost=0.00..1.52 rows=52 width=8)(16 rows)EXPLAIN SELECT DISTINCT article.id, article.name, article.date           FROM article, category_articles__article_categories AS cs          WHERE article.content IS NOT NULL            AND article.content != ''            AND article.visible = true            AND article.id = cs.article_id            AND cs.category_id in (select c.category_id from category_articles__article_categories as c where c.article_id = 67)            AND cs.article_id = article.id            AND article.id != 67       ORDER BY article.date DESC;                                                           QUERY PLAN--------------------------------------------------------------------------------------------------------------------------------- Unique  (cost=14.98..15.03 rows=5 width=59)   ->  Sort  (cost=14.98..15.00 rows=5 width=59)         Sort Key: article.date, article.id, article.name         ->  Hash Join  (cost=3.53..14.92 rows=5 width=59)               Hash Cond: ("outer".id = "inner".article_id)               ->  Seq Scan on article  (cost=0.00..11.10 rows=48 width=59)                     Filter: ((content IS NOT NULL) AND (content <> ''::text) AND (visible = true) AND (id <> 67))               ->  Hash  (cost=3.52..3.52 rows=6 width=4)                     ->  Hash Join  (cost=1.67..3.52 rows=6 width=4)                           Hash Cond: ("outer".category_id = "inner".category_id)                           ->  Seq Scan on category_articles__article_categories cs  (cost=0.00..1.52 rows=52 width=8)                           ->  Hash  (cost=1.66..1.66 rows=2 width=4)                                 ->  HashAggregate  (cost=1.66..1.66 rows=2 width=4)                                       ->  Seq Scan on category_articles__article_categories c  (cost=0.00..1.65 rows=5 width=4)                                             Filter: (article_id = 67)