Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 7F040632CB2 for ; Mon, 15 Feb 2010 07:11:44 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 58758-03 for ; Mon, 15 Feb 2010 11:11:26 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f212.google.com (mail-fx0-f212.google.com [209.85.220.212]) by mail.postgresql.org (Postfix) with ESMTP id 73A5D6326E8 for ; Mon, 15 Feb 2010 07:11:32 -0400 (AST) Received: by fxm4 with SMTP id 4so130652fxm.34 for ; Mon, 15 Feb 2010 03:11:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=o/uM/AMm2DZbafK6X3ynunFHMl+Mpvx8Ib7BxppXgtI=; b=mWzXjeB50vQ/xQScBK8OaVf60Z2vLjAOh1WnlVNhY2i8Obqz7ZUfpc7ZKVND9jH+0E UaQ/VsLNCGuXqtMc3cQXeADXH/1SKsK/xYLdaLVWlk3yDOfuPF8HfYcgZ0/xtML9vSoY MURm9mPJorOc0dZ9OTljeCRrnZB2wZk3p1fJI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=YMPV1L+3j+mKG21/T+/BLsGgP0kS8fEgqLF7NA0Uw571Shf70+OBkbsDMc0lchtrC2 CDvSREGf5s0m4P2r/ENPgsu4r8E5kvJ96/dIb2CPaAcqmm3BAknJ5EyJAR8kuw38IlrW pczHbPL6Tk/HMwGUVLvY+RUuqYoe3EziDeCkM= Received: by 10.103.125.40 with SMTP id c40mr3694874mun.73.1266232290047; Mon, 15 Feb 2010 03:11:30 -0800 (PST) Received: from ?0.0.0.0? (5357DA75.cable.casema.nl [83.87.218.117]) by mx.google.com with ESMTPS id 23sm33043680mun.8.2010.02.15.03.11.27 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 15 Feb 2010 03:11:27 -0800 (PST) Message-ID: <4B792BDB.5090609@gmail.com> Date: Mon, 15 Feb 2010 12:11:23 +0100 From: Yeb Havinga User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: AI Rumman CC: pgsql-performance@postgresql.org Subject: Re: Why primary key index are not using in joining? References: <2a7905441002150135x1ba61b19p89f3c3b94d252843@mail.gmail.com> In-Reply-To: <2a7905441002150135x1ba61b19p89f3c3b94d252843@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.413 tagged_above=-10 required=5 tests=AWL=0.186, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/235 X-Sequence-Number: 37656 AI Rumman wrote: > > explain analyze > select * > from vtiger_crmentity > inner JOIN vtiger_users > ON vtiger_users.id = > vtiger_crmentity.smownerid > where vtiger_crmentity.deleted = 0 ; > > QUERY > PLAN > > ------------------------------------------------------------------------------------------------------------------------------------------------------------ > Hash Join (cost=3665.17..40019.25 rows=640439 width=1603) (actual > time=115.613..3288.436 rows=638081 loops=1) > Hash Cond: ("outer".smownerid = "inner".id) > -> Bitmap Heap Scan on vtiger_crmentity (cost=3646.54..30394.02 > rows=640439 width=258) (actual time=114.763..986.504 rows=638318 loops=1) > Recheck Cond: (deleted = 0) > -> Bitmap Index Scan on vtiger_crmentity_deleted_idx > (cost=0.00..3646.54 rows=640439 width=0) (actual time=107.851..107.851 > rows=638318 loops=1) > Index Cond: (deleted = 0) > -> Hash (cost=18.11..18.11 rows=211 width=1345) (actual > time=0.823..0.823 rows=211 loops=1) > -> Seq Scan on vtiger_users (cost=0.00..18.11 rows=211 > width=1345) (actual time=0.005..0.496 rows=211 loops=1) > Total runtime: 3869.022 ms > > Sequential index is occuring on vtiger_users table while it has > primary key index on id. > Could anyone please tell me why? > From the list of indexes you also supplied it seems to me you very much want index scanning, the reason being that 4secs is too slow? The seqscan is not the reason for that - the main reason is that you process almost all rows of the crmentity table. I bet that if you add a LIMIT, or adding a clause that selects only for a specific vtiger_user, the plan looks different on the access to the crmentity table as well as the kind of join, however if your application really needs to process the 600k rows, I'm not sure if it can get any faster than that. Perhaps it would help a bit to shorten the SELECT * to only the attributes you really need. Regards, Yeb Havinga