Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1emRUy-0000qh-8Z for pgsql-general@arkaria.postgresql.org; Thu, 15 Feb 2018 21:55:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1emRUx-000227-3C for pgsql-general@arkaria.postgresql.org; Thu, 15 Feb 2018 21:55:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1emRUw-00021w-RB for pgsql-general@lists.postgresql.org; Thu, 15 Feb 2018 21:55:22 +0000 Received: from virtual1.potentialtech.com ([69.25.178.43] helo=mail.potentialtech.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1emRUu-0006qB-7B for pgsql-general@postgresql.org; Thu, 15 Feb 2018 21:55:22 +0000 Received: from beezelbub (c-67-171-70-156.hsd1.pa.comcast.net [67.171.70.156]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.potentialtech.com (Postfix) with ESMTPSA id 81ED724BEB7; Thu, 15 Feb 2018 16:55:18 -0500 (EST) Date: Thu, 15 Feb 2018 16:55:17 -0500 From: PT To: hmidi slim Cc: pgsql-general@postgresql.org Subject: Re: query's performance Message-Id: <20180215165517.2fc49bf2c8e19ba03a2284cd@potentialtech.com> In-Reply-To: References: X-Mailer: Sylpheed 3.5.0 (GTK+ 2.24.30; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, 15 Feb 2018 22:43:59 +0100 hmidi slim wrote: > Hi, > I have a table establishment which contains these columns: id, name, > longitude, latitude. > I want to select all the establishments in the table establishment within a > radius from a reference point which is a given establishment. > For example: > I have a given establishment called establishment1 and has an id=1. > I want to display all the establishments within a radius 1km from this > establishment. > I wrote this query: > select e.name,e1.name, e1.longitude, e1.latitude > from establishment as e, establishment as e1 > where e.id = 1 > and e.id <> e1.id > and ST_DWithin(ST_SetSRID(ST_MakePoint(e1.longitude, e1.latitude), > 4326),ST_MakePoint(e.longitude, e.latitude)::geography, 1000) ; > > Is it a good practice to iterate the rows of the table like I did and does > not have an effect in the performance if the number of table's rows > increase? If performance is a concern, don't use long/lat in 2 columns. Instead use a GEOGRAPHY type to store the point. You can then index on that GEOGRAPHY column and queries against it will be quite fast. The way you have it now is going to degrate in performance linearally with the number of rows in the table. With the proper types and indexes, performance degredation will be considerably less. -- Bill Moran