Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TOHMY-0002LL-8P for pgsql-performance@postgresql.org; Wed, 17 Oct 2012 00:19:54 +0000 Received: from mail-gh0-f174.google.com ([209.85.160.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TOHMR-0004TH-Ng for pgsql-performance@postgresql.org; Wed, 17 Oct 2012 00:19:53 +0000 Received: by mail-gh0-f174.google.com with SMTP id g15so1939896ghb.19 for ; Tue, 16 Oct 2012 17:19:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer :x-gm-message-state; bh=h4naBfu05bge7iTxYdaAtJ9YNpgOYE42T/YhF/Cc5rc=; b=VqjAE3dCHKpHqxK3f1sKWInl1bWyhngsnyH+ui/K3reausDkmaTzqjDqbHxiVYsplB 3WFD/3ArEV1uRGjg283Y3WSxRdGqBfUthFKA8c6pkM6SsJ6hKIGTPBVF4x6uwMcni543 Rn0PXOPKONysxpsbYK1siedt06lGOEQaiMOSlUpThe3UGdgeaw8jdZq9c2CICwQyj48s Dvk9Ny32opKChzIqWA6qluzAb9HU+UvSaiqO5ihP5pSBFjfftCRsqOqv/ADrAKBlJk2N FY92oH/jXoTpRM8jkbLFYcK77qMf4Hgb5v8fAgcj2Sxg/Bu81Fe1hcpUhJysJz9F8FgT Zrzg== Received: by 10.101.95.15 with SMTP id x15mr4812574anl.83.1350433186566; Tue, 16 Oct 2012 17:19:46 -0700 (PDT) Received: from [192.168.0.11] (75-139-130-220.dhcp.gwnt.ga.charter.com. [75.139.130.220]) by mx.google.com with ESMTPS id j8sm16189585ann.3.2012.10.16.17.19.44 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 16 Oct 2012 17:19:45 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: Have: Seq Scan - Want: Index Scan - what am I doing wrong? From: Chris Ruprecht In-Reply-To: <9BB26B73-2824-4C97-92B5-5E059108C071@gmail.com> Date: Tue, 16 Oct 2012 20:19:43 -0400 Cc: "pgsql-performance@postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <2A64CF51-CC89-4A0A-89CC-49D7ABFD4603@cdrbill.com> References: <91417DD6-C9FF-40EA-8EA9-87DF7E696927@cdrbill.com> <9BB26B73-2824-4C97-92B5-5E059108C071@gmail.com> To: Evgeny Shishkin X-Mailer: Apple Mail (2.1499) X-Gm-Message-State: ALoCoQn2dUs6OR0KJpNFnjqL7PW2Mwl+y948F7/BARbeaW9KyfY16iiPY4znPNxgN9SbKmZ1TZZn X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201210/221 X-Sequence-Number: 48180 On Oct 16, 2012, at 20:01 , Evgeny Shishkin = wrote: > Selecting 5 yours of data is not selective at all, so postgres decides = it is cheaper to do seqscan.=20 >=20 > Do you have an index on patient.dnsortpersonnumber? Can you post a = result from=20 > select count(*) from patient where dnsortpersonnumber =3D '347450'; ? >=20 Yes, there is an index: "Aggregate (cost=3D6427.06..6427.07 rows=3D1 width=3D0)" " -> Index Scan using patient_pracsortpatientnumber on patient = (cost=3D0.00..6427.06 rows=3D1 width=3D0)" " Index Cond: (dnsortpersonnumber =3D '347450'::text)" In fact, all the other criteria is picked using an index. I fear that = the >=3D and <=3D on the timestamp is causing the issue. If I do a "=3D" = of just one of them, I get an index scan. But I need to scan the entire = range. I get queries like "give me everything that was entered into the = system for this patient between these two dates". A single date wouldn't = work.=