Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 6A1D21337B59 for ; Thu, 14 Oct 2010 17:32:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 84902-02 for ; Thu, 14 Oct 2010 20:32:27 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 52C1C133616B for ; Thu, 14 Oct 2010 17:32:25 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1P6UTM-00070N-WC for pgsql-performance@postgresql.org; Thu, 14 Oct 2010 22:32:21 +0200 Received: from 93-138-172-206.adsl.net.t-com.hr ([93.138.172.206]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 14 Oct 2010 22:32:20 +0200 Received: from ivoras by 93-138-172-206.adsl.net.t-com.hr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 14 Oct 2010 22:32:20 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-performance@postgresql.org From: Ivan Voras Subject: Re: oracle to psql migration - slow query in postgres Date: Thu, 14 Oct 2010 22:32:09 +0200 Lines: 53 Message-ID: References: <1287085384.1684.52.camel@tony1.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 93-138-172-206.adsl.net.t-com.hr User-Agent: Mozilla/5.0 (X11; U; FreeBSD amd64; en-US; rv:1.9.2.9) Gecko/20101008 Thunderbird/3.1.4 In-Reply-To: <1287085384.1684.52.camel@tony1.localdomain> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_FILL_THIS_FORM_SHORT=0.01, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/238 X-Sequence-Number: 40656 On 10/14/10 21:43, Tony Capobianco wrote: > We have 4 quad-core processors and 32GB of RAM. The below query uses > the members_sorted_idx_001 index in oracle, but in postgres, the > optimizer chooses a sequential scan. > > explain analyze create table tmp_srcmem_emws1 > as > select emailaddress, websiteid > from members > where emailok = 1 > and emailbounced = 0; > QUERY > PLAN > ------------------------------------------------------------------------------------------------------------------------------ > Seq Scan on members (cost=0.00..14137154.64 rows=238177981 width=29) > (actual time=0.052..685834.785 rows=236660930 loops=1) > Filter: ((emailok = 1::numeric) AND (emailbounced = 0::numeric)) > Total runtime: 850306.220 ms > (3 rows) > Indexes: > "email_website_unq" UNIQUE, btree (emailaddress, websiteid), > tablespace "members_idx" > "member_addeddateid_idx" btree (addeddate_id), tablespace > "members_idx" > "member_changedateid_idx" btree (changedate_id), tablespace > "members_idx" > "members_fdate_idx" btree (to_char_year_month(addeddate)), > tablespace "esave_idx" > "members_memberid_idx" btree (memberid), tablespace "members_idx" > "members_mid_emailok_idx" btree (memberid, emailaddress, zipcode, > firstname, emailok), tablespace "members_idx" > "members_sorted_idx_001" btree (websiteid, emailok, emailbounced, > addeddate, memberid, zipcode, statecode, emailaddress), tablespace > "members_idx" > "members_src_idx" btree (websiteid, emailbounced, sourceid), > tablespace "members_idx" > "members_wid_idx" btree (websiteid), tablespace "members_idx" PostgreSQL doesn't fetch data directly from indexes, so there is no way for it to reasonably use an index declared like: "members_sorted_idx_001" btree (websiteid, emailok, emailbounced, addeddate, memberid, zipcode, statecode, emailaddress) You need a direct index on the fields you are using in your query, i.e. an index on (emailok, emailbounced). OTOH, those columns look boolean-like. It depends on what your data set is, but if the majority of records contain (emailok=1 and emailbounced=0) an index may not help you much.