Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 3F15A6377A2 for ; Wed, 25 Mar 2009 19:14:52 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 04969-06 for ; Wed, 25 Mar 2009 19:14:43 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from yx-out-2324.google.com (yx-out-2324.google.com [74.125.44.29]) by mail.postgresql.org (Postfix) with ESMTP id 43AA8637792 for ; Wed, 25 Mar 2009 19:14:47 -0300 (ADT) Received: by yx-out-2324.google.com with SMTP id 8so185489yxm.73 for ; Wed, 25 Mar 2009 15:14:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=KfQAWFMcJHhKTv7JnAz2vqbLpWxrMXTLPPuAge1mQgw=; b=uX68kXfvMIhgQf4WZoMivJl+kXlOLAqLwHqDxZ3Hx9HqnhJGNPOwXnUL+Q3+xE+G3P OI9mFYT7BFB9cxwgesO+8lhfnwYPB9qbP78n20yzIYw5cXGrdvRprJbPLjJghNN2Fbu8 q2tAVRz56ns5NxpITm9NDRyh2liMof/PUrdaQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=FqudQChbZJ8pSu+urg9BCRwQntqUcqmLTOgvoKJzqY13EHVhpx7Vy8kfQTN0nOvzwW 0ufd3Pd/pMC2qS160OpOogsz7t3TWH5+wLM5sJO0QXAVhYrFIBr1SfEXrI64mRwTyEQd oL8yHpAHS0QVYTUkOuzuV75u7wOkE3kmb03Dg= MIME-Version: 1.0 Received: by 10.90.68.20 with SMTP id q20mr34444aga.117.1238019285672; Wed, 25 Mar 2009 15:14:45 -0700 (PDT) In-Reply-To: <49CAA85C.8080402@agliodbs.com> References: <7fd310d10903231203r1bd37b8cn8baeadae83253f44@mail.gmail.com> <7fd310d10903241521v1cbf5d7aueca653e03ed98649@mail.gmail.com> <7fd310d10903242047t1a7b7daw1d61c3de7f20b7c6@mail.gmail.com> <49CAA85C.8080402@agliodbs.com> Date: Wed, 25 Mar 2009 17:14:45 -0500 Message-ID: <7fd310d10903251514x1dd522f3o8d13a9218a6eda6c@mail.gmail.com> Subject: Re: Help Me Understand Why I'm Getting a Bad Query Plan From: Bryan Murphy To: Josh Berkus Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.5 tagged_above=0 required=5 tests=AWL=0.500 X-Spam-Level: X-Archive-Number: 200903/370 X-Sequence-Number: 33292 On Wed, Mar 25, 2009 at 4:55 PM, Josh Berkus wrote: > Oh, I see. =A0It thinks that it'll need to pull 260,000 redundant rows in > order to get 1800 unique ones. =A0Only it's wrong; you're only pulling ab= out > 4000. > > Try increasing some stats still further: itemexperiencelog.visitorid and > visitors.user_id both to 500. I tried that already, but I decided to try again in case I messed up something last time. Here's what I ran. As you can see, it still chooses to do a sequential scan. Am I changing the stats for those columns correctly? Thanks, Bryan First, the query: SELECT MAX(l.Id) as Id, l.ItemId FROM ItemExperienceLog l INNER JOIN Items_Primary p ON p.Id =3D l.ItemId INNER JOIN Feeds f ON f.Id =3D p.FeedId INNER JOIN Visitors v ON v.Id =3D l.VisitorId WHERE v.UserId =3D 'fbe2537f21d94f519605612c0bf7c2c5' AND LOWER(f.Slug) =3D LOWER('Wealth_Building_by_NightingaleConant') GROUP BY l.ItemId The query plan: HashAggregate (cost=3D130291.23..130291.35 rows=3D9 width=3D37) (actual time=3D8385.428..8385.433 rows=3D4 loops=3D1) -> Nested Loop (cost=3D2649.02..130291.19 rows=3D9 width=3D37) (actual time=3D3707.336..8385.388 rows=3D11 loops=3D1) -> Nested Loop (cost=3D2649.02..129744.01 rows=3D1888 width=3D70) (actual time=3D8.881..8322.029 rows=3D3210 loops=3D1) -> Hash Join (cost=3D2649.02..125273.81 rows=3D1888 width=3D37) (actual time=3D8.836..8196.469 rows=3D3210 loops=3D1) Hash Cond: (l.visitorid =3D v.id) -> Seq Scan on itemexperiencelog l (cost=3D0.00..112491.03 rows=3D2697303 width=3D70) (actual time=3D0.048..4459.139 rows=3D2646177 loops=3D1) -> Hash (cost=3D2631.24..2631.24 rows=3D1422 width=3D33) (actual time=3D7.751..7.751 rows=3D899 loops=3D1) -> Bitmap Heap Scan on visitors v (cost=3D23.44..2631.24 rows=3D1422 width=3D33) (actual time=3D0.577..6.347 rows=3D899 loops=3D1) Recheck Cond: (userid =3D 'fbe2537f21d94f519605612c0bf7c2c5'::bpchar) -> Bitmap Index Scan on visitors_userid_index2 (cost=3D0.00..23.08 rows=3D1422 width=3D0) (actual time=3D0.419..0.419 rows=3D899 loops=3D1) Index Cond: (userid =3D 'fbe2537f21d94f519605612c0bf7c2c5'::bpchar) -> Index Scan using items_primary_pkey on items_primary p (cost=3D0.00..2.36 rows=3D1 width=3D66) (actual time=3D0.024..0.025 rows= =3D1 loops=3D3210) Index Cond: (p.id =3D l.itemid) -> Index Scan using feeds_pkey on feeds f (cost=3D0.00..0.28 rows=3D1 width=3D33) (actual time=3D0.018..0.018 rows=3D0 loops=3D3210) Index Cond: (f.id =3D p.feedid) Filter: (lower((f.slug)::text) =3D 'wealth_building_by_nightingaleconant'::text) Total runtime: 8385.538 ms Bump up the stats: ALTER TABLE ItemExperienceLog ALTER COLUMN VisitorId SET STATISTICS 500; ALTER TABLE ItemExperienceLog ALTER COLUMN ItemId SET STATISTICS 500; ANALYZE ItemExperienceLog; ALTER TABLE Visitors ALTER COLUMN UserId SET STATISTICS 500; ALTER TABLE Visitors ALTER COLUMN Id SET STATISTICS 500; ANALYZE Visitors; The new query plan: HashAggregate (cost=3D127301.63..127301.72 rows=3D7 width=3D37) (actual time=3D11447.033..11447.037 rows=3D4 loops=3D1) -> Nested Loop (cost=3D1874.67..127301.60 rows=3D7 width=3D37) (actual time=3D4717.880..11446.987 rows=3D11 loops=3D1) -> Nested Loop (cost=3D1874.67..126923.09 rows=3D1306 width=3D70) (actual time=3D20.565..11345.756 rows=3D3210 loops=3D1) -> Hash Join (cost=3D1874.67..123822.53 rows=3D1306 width=3D37) (actual time=3D20.445..8292.235 rows=3D3210 loops=3D1) Hash Cond: (l.visitorid =3D v.id) -> Seq Scan on itemexperiencelog l (cost=3D0.00..112010.04 rows=3D2646604 width=3D70) (actual time=3D0.065..4438.481 rows=3D2646549 loops=3D1) -> Hash (cost=3D1862.32..1862.32 rows=3D988 width=3D33) (actual time=3D19.360..19.360 rows=3D899 loops=3D1) -> Bitmap Heap Scan on visitors v (cost=3D18.08..1862.32 rows=3D988 width=3D33) (actual time=3D0.666..17.788 rows=3D899 loops=3D1) Recheck Cond: (userid =3D 'fbe2537f21d94f519605612c0bf7c2c5'::bpchar) -> Bitmap Index Scan on visitors_userid_index2 (cost=3D0.00..17.83 rows=3D988 width=3D0) (actual time=3D0.520..0.520 rows=3D899 loops=3D1) Index Cond: (userid =3D 'fbe2537f21d94f519605612c0bf7c2c5'::bpchar) -> Index Scan using items_primary_pkey on items_primary p (cost=3D0.00..2.36 rows=3D1 width=3D66) (actual time=3D0.944..0.945 rows= =3D1 loops=3D3210) Index Cond: (p.id =3D l.itemid) -> Index Scan using feeds_pkey on feeds f (cost=3D0.00..0.28 rows=3D1 width=3D33) (actual time=3D0.029..0.029 rows=3D0 loops=3D3210) Index Cond: (f.id =3D p.feedid) Filter: (lower((f.slug)::text) =3D 'wealth_building_by_nightingaleconant'::text) Total runtime: 11447.155 ms