Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.4) with ESMTP id g07JfUB62678 for ; Mon, 7 Jan 2002 14:41:30 -0500 (EST) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.4/8.11.4) with ESMTP id g07Jf7g14152; Mon, 7 Jan 2002 14:41:08 -0500 (EST) To: Daniel Kalchev cc: pgsql-hackers@postgresql.org Subject: Re: again on index usage In-reply-to: <200201071841.UAA26328@dcave.digsys.bg> References: <200201071841.UAA26328@dcave.digsys.bg> Comments: In-reply-to Daniel Kalchev message dated "Mon, 07 Jan 2002 20:41:07 +0200" Date: Mon, 07 Jan 2002 14:41:07 -0500 Message-ID: <14149.1010432467@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200201/285 X-Sequence-Number: 17338 Daniel Kalchev writes: > explain > SELECT sum(input) FROM iplog_gate200112 > WHERE > '2001-12-01 00:00:00+02' <= ipdate AND ipdate < '2001-12-02 00:00:00+02' AND > '2001-12-01 00:00:00+02' <= ipdate AND ipdate < '2002-01-01 00:00:00+02' AND > network_subeq(ipaddr, '193.68.240.0/20') AND 'uni-gw' ~ router; > results in > NOTICE: QUERY PLAN: > Aggregate (cost=51845.51..51845.51 rows=1 width=8) > -> Seq Scan on iplog_gate200112 (cost=0.00..51845.04 rows=190 width=8) > Why would it not want to use index scan? It's difficult to tell from this what it thinks the selectivity of the ipdate index would be, since the rows estimate includes the effect of the ipaddr and router restrictions. What do you get from just explain SELECT sum(input) FROM iplog_gate200112 WHERE '2001-12-01 00:00:00+02' <= ipdate AND ipdate < '2001-12-02 00:00:00+02' AND '2001-12-01 00:00:00+02' <= ipdate AND ipdate < '2002-01-01 00:00:00+02'; If you say "set enable_seqscan to off", does that change the plan? BTW, the planner does not associate function calls with indexes. If you want to have the ipaddr index considered for this query, you need to write ipaddr <<= '193.68.240.0/20' not network_subeq(ipaddr, '193.68.240.0/20'). (But IIRC, that only works in 7.2 anyway, not earlier releases :-() regards, tom lane