pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
query's performance
2+ messages / 2 participants
[nested] [flat]

* query's performance
@ 2018-02-15 21:43  hmidi slim <hmidi.slim2@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: hmidi slim @ 2018-02-15 21:43 UTC (permalink / raw)
  To: pgsql-general

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?

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: query's performance
@ 2018-02-15 21:55  PT <wmoran@potentialtech.com>
  parent: hmidi slim <hmidi.slim2@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: PT @ 2018-02-15 21:55 UTC (permalink / raw)
  To: hmidi slim <hmidi.slim2@gmail.com>; +Cc: pgsql-general

On Thu, 15 Feb 2018 22:43:59 +0100
hmidi slim <hmidi.slim2@gmail.com> 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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-02-15 21:55 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-02-15 21:43 query's performance hmidi slim <hmidi.slim2@gmail.com>
2018-02-15 21:55 ` PT <wmoran@potentialtech.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox