Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Yp1RY-00080l-9z for pgsql-performance@arkaria.postgresql.org; Sun, 03 May 2015 21:28:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Yp1RX-00025f-Lb for pgsql-performance@arkaria.postgresql.org; Sun, 03 May 2015 21:28:55 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Yp1RW-00025U-Nm for pgsql-performance@postgresql.org; Sun, 03 May 2015 21:28:54 +0000 Received: from mail-wg0-x22f.google.com ([2a00:1450:400c:c00::22f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Yp1RT-0004hc-5S for pgsql-performance@postgresql.org; Sun, 03 May 2015 21:28:53 +0000 Received: by wgin8 with SMTP id n8so133559655wgi.0 for ; Sun, 03 May 2015 14:28:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:reply-to:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=dp2FKW0U6Qohu9X2XfVljmLgNb3jeqsF0nZO170EbMk=; b=Y1UN9U7EQHE9MMaUd/yjcIADuEU3SjXPpq2KehTKXaJzFSq1KO3YXcdzOmdtEDjlNC qd6WeMZEjYD5Fgz7eD/1dlE8ImNGI0fB0fCyOLhzyL1GHj4gtbNixaAW+hR95lecy9uW wSw3QmrOoHul1M/uoIHS2eg465NLAt3VaIqVwblhTRfKZaqdJLzL/N+B2xilvzLNtlzt yP+I7vjaAk5ajPKPsOOt6pyoxSL8zExDaUYjaqqMa4BWea6k1SppCLTI/mEVs4xr+opR O4RSm6Ss8Wa/2pEMB0A4KdQqu5ADHLKrz82YPJj7kij+jYTFoMAjrfm7Fd/WmmwB2wkl cgHA== X-Received: by 10.194.250.98 with SMTP id zb2mr26873332wjc.90.1430688529626; Sun, 03 May 2015 14:28:49 -0700 (PDT) Received: from [192.168.1.99] (dsl-hkibrasgw1-58c38f-82.dhcp.inet.fi. [88.195.143.82]) by mx.google.com with ESMTPSA id bg4sm1501506wjc.10.2015.05.03.14.28.47 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 May 2015 14:28:48 -0700 (PDT) Message-ID: <5546930E.5080201@iki.fi> Date: Mon, 04 May 2015 00:28:46 +0300 From: Heikki Linnakangas Reply-To: hlinnaka@iki.fi User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.6.0 MIME-Version: 1.0 To: Anton Bushmelev , pgsql-performance@postgresql.org Subject: Re: optimization join on random value References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.3 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 05/04/2015 12:23 AM, Anton Bushmelev wrote: > Hello guru of postgres, it's possoble to tune query with join on random > string ? > i know that it is not real life example, but i need it for tests. > > soe=# explain > soe-# SELECT ADDRESS_ID, > soe-# CUSTOMER_ID, > soe-# DATE_CREATED, > soe-# HOUSE_NO_OR_NAME, > soe-# STREET_NAME, > soe-# TOWN, > soe-# COUNTY, > soe-# COUNTRY, > soe-# POST_CODE, > soe-# ZIP_CODE > soe-# FROM ADDRESSES > soe-# WHERE customer_id = trunc( random()*45000) ; > QUERY > PLAN > ------------------------------------------------------------------------------------------- > Seq Scan on addresses (cost=0.00..165714.00 rows=22500 width=84) > Filter: ((customer_id)::double precision = trunc((random() * > 45000::double precision))) > (2 rows) > There are two problems here that prohibit the index from being used: 1. random() is volatile, so it's recalculated for each row. 2. For the comparison, customer_id is cast to a float, and the index is on the bigint value. To work around the first problem, put the random() call inside a subquery. And for the second problem, cast to bigint. SELECT ... FROM addresses WHERE customer_id = (SELECT random()*45000)::bigint - Heikki -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance