Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TOH4e-0002Cp-KJ for pgsql-performance@postgresql.org; Wed, 17 Oct 2012 00:01:24 +0000 Received: from mail-la0-f46.google.com ([209.85.215.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TOH4Z-0004Fm-AY for pgsql-performance@postgresql.org; Wed, 17 Oct 2012 00:01:23 +0000 Received: by mail-la0-f46.google.com with SMTP id h6so4603337lag.19 for ; Tue, 16 Oct 2012 17:01:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=vHATYEyc+Vmwf5rUof1be5O0+wyVEH3JG3SUV/VAeI8=; b=IefLgvmurJduYr+opursDd3VkBlMUw2hNBVn6V4TY4v+McmTxkaHmI+cLuMSE2JAxx plXXawhbPi3G5RKFYBm17sjDpR8fwPK/L7XxzYkbJM9eD7Ualev1CJhCgtQnIHy4WfFy Cgu7gQnMT4RqiakkZxTJIJ6hCvjWmWlRL9OcfHDPqYrJcu748nUzXmfXjZf3KXzxzTLu R3oWjCn0HyMUml7hiE6mmle4tze58aYfcmbwozLG4mtK3RXKB9+owSSOyBuo0HEgRTsp rb1KLh2O4lJAh12alcHJwz177ZsTR/FPiCVr2C31vW81C8IIru0UbfGd83sosMCOJPDT VvUQ== Received: by 10.152.111.71 with SMTP id ig7mr14388035lab.28.1350432077903; Tue, 16 Oct 2012 17:01:17 -0700 (PDT) Received: from [192.168.20.2] (128-69-108-68.broadband.corbina.ru. [128.69.108.68]) by mx.google.com with ESMTPS id oz12sm5931278lab.17.2012.10.16.17.01.16 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 16 Oct 2012 17:01:17 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.1 \(1498\)) Subject: Re: Have: Seq Scan - Want: Index Scan - what am I doing wrong? From: Evgeny Shishkin In-Reply-To: <91417DD6-C9FF-40EA-8EA9-87DF7E696927@cdrbill.com> Date: Wed, 17 Oct 2012 04:01:19 +0400 Cc: "pgsql-performance@postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <9BB26B73-2824-4C97-92B5-5E059108C071@gmail.com> References: <91417DD6-C9FF-40EA-8EA9-87DF7E696927@cdrbill.com> To: Chris Ruprecht X-Mailer: Apple Mail (2.1498) X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201210/220 X-Sequence-Number: 48179 On Oct 17, 2012, at 3:52 AM, Chris Ruprecht wrote: > Hi guys, >=20 > PG =3D 9.1.5 > OS =3D winDOS 2008R8 >=20 > I have a table that currently has 207 million rows. > there is a timestamp field that contains data. > more data gets copied from another database into this database. > How do I make this do an index scan instead? > I did an "analyze audittrailclinical" to no avail. > I tested different indexes - no same behavior. >=20 > The query does this: >=20 > SELECT =20 > audittrailclinical.pgid, =20 > audittrailclinical.timestamp, =20 > mmuser.logon, =20 > audittrailclinical.entityname, =20 > audittrailclinical.clinicalactivity, =20 > audittrailclinical.audittraileventcode, =20 > account.accountnumber, =20 > patient.dnsortpersonnumber =20 > FROM =20 > public.account, =20 > public.audittrailclinical, =20 > public.encounter, =20 > public.entity, =20 > public.mmuser, =20 > public.patient, =20 > public.patientaccount=20 > WHERE =20 > audittrailclinical.encountersid =3D encounter.encountersid=20 > and audittrailclinical.timestamp >=3D '2008-01-01'::timestamp = without time zone=20 > and audittrailclinical.timestamp <=3D '2012-10-05'::timestamp = without time zone > AND encounter.practiceid =3D patient.practiceid=20 > AND encounter.patientid =3D patient.patientid=20 > AND encounter.staffid =3D patient.staffid=20 > AND entity.entitysid =3D audittrailclinical.entitysid=20 > AND mmuser.mmusersid =3D audittrailclinical.mmusersid=20 > AND patient.practiceid =3D patientaccount.practiceid=20 > AND patient.patientid =3D patientaccount.patientid=20 > AND patientaccount.accountsid =3D account.accountsid=20 > AND patientaccount.defaultaccount =3D 'Y'=20 > AND patient.dnsortpersonnumber =3D '347450' ; >=20 > The query plan says: >=20 > " -> Seq Scan on audittrailclinical = (cost=3D0.00..8637598.76 rows=3D203856829 width=3D62)" > " Filter: (("timestamp" >=3D '2008-01-01 = 00:00:00'::timestamp without time zone) AND ("timestamp" <=3D = '2012-10-05 00:00:00'::timestamp without time zone))" >=20 > which takes forever. >=20 Selecting 5 yours of data is not selective at all, so postgres decides = it is cheaper to do seqscan.=20 Do you have an index on patient.dnsortpersonnumber? Can you post a = result from=20 select count(*) from patient where dnsortpersonnumber =3D '347450'; ? > How do I make this do an index scan instead? > I did an "analyze audittrailclinical" to no avail. >=20 > the table definitions are (the createstamp field is empty - I know, = bad data): >=20 > CREATE TABLE audittrailclinical > ( > audittrailid text, > audittraileventcode text, > clinicalactivity text, > eventsuccessful text, > externalunique text, > recordstamp timestamp without time zone, > recorddescription text, > encountersid integer, > eventuserlogon text, > computername text, > applicationcode text, > practiceid integer, > mmusersid integer, > entitysid integer, > entityname text, > "timestamp" timestamp without time zone, > lastuser integer, > createstamp timestamp without time zone, > pgid bigint DEFAULT nextval(('"bravepoint_seq"'::text)::regclass) > ) > WITH ( > OIDS=3DFALSE > ); > ALTER TABLE audittrailclinical > OWNER TO intergy; > GRANT ALL ON TABLE audittrailclinical TO intergy; > GRANT SELECT ON TABLE audittrailclinical TO rb; >=20 > -- Index: atc_en_time >=20 > CREATE INDEX atc_en_time > ON audittrailclinical > USING btree > (entitysid , "timestamp" ); >=20 > -- Index: atc_id >=20 > -- DROP INDEX atc_id; >=20 > CREATE INDEX atc_id > ON audittrailclinical > USING btree > (audittrailid COLLATE pg_catalog."default" ); >=20 >=20 > --=20 > Sent via pgsql-performance mailing list = (pgsql-performance@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance