Re: How do I speed up this query ? (1.7M table entries)

From: Seeker (seeker_at_mailinator.com)
Date: 06/19/04


Date: 19 Jun 2004 16:13:27 -0700

Alan,

A way to improve queries of this type is to consider the fact that if
an x and y are not in the bounding square (x-r, y-r)-(x+r, y+r), then
neither will they be in the circle bounded by radius r (the distance).
 Filtering on the bounding square first allows us to use sargable
arguments, and limits the heavy calculation later. See amended proc
below.

I hope this helps you,
Keith, EBSCO
------------------------------------
create procedure NearPostcodes
        @postcode varchar(8),
        @radius int -- distance
as
begin
        -- This method assumes a sufficiently small radius
        -- is chosen, so that cost of populating the local
        -- table var, is offset by the performance gain
        -- of using sargable arguments.
        declare @tblWithinSquare TABLE
        (
                PostCode varchar(8),
                Northing int,
                Easting int
        )
        declare @n int
        declare @e int
        declare @left int -- bounded square extents
        declare @right int
        declare @top int
        declare @bottom int

        -- center of square/circle
        select
                @n=Northing,
                @e=Easting
        from
                Postcodes with (nolock)
        where
                Postcode = @postcode
                -- consider adding a indexed CHECKSUM on the postcode column
                -- it improves exact match string lookups
                -- since the CBO can filter on a smaller integer index
                -- prior to the expensive string compare
                -- AND PostcodeCheckSum = CHECKSUM(@postcode)

        -- bounding square extents
         select
                @left = @e - @radius
                @right = @e + @radius
                @bottom = @n - radius
                @top = @n + radius

        -- pre-filter post codes within bounding square
        insert into @tblWithinSquare
        (
                Postcode,
                Northing,
                Easting
        )
        select
                Postcode,
                Northing,
                Easting
        from
                Postcodes with (nolock)
        where
                -- recommend unique index on (Northing, Easting)
                -- since two postcodes should always have different coordinates
                Northing between @top and @bottom
                and Easting between @left and @right

        -- Now it is safe to do the heavy calculation on the
        -- hopefully much smaller set of postcodes. (keep radius small)
        -- Or even better stop now, :) and consider anything in the
        -- bounding square as worthy of reporting.
        select
                Postcode,
                        (Northing - @n) * (Northing - @n)
                        + (Easting - @e) * (Easting - @e)
                as Distance
        from
                @tblWithinSquare
        where
                (Northing - @n) * (Northing - @n)
                + (Easting - @e) * (Easting - @e)
                < (@radius * @radius)
   order by
                Distance,
                Postcode
end
go

First apply the following efficient filter.

"Aaron W. West" <tallpeak@hotmail.NO.SPAM> wrote in message news:<ccucnVokeYzVfk7d38DK-g@speakeasy.net>...
> I was surprised no one mentioned rtree/spatial indexes (in a post).
> PostgreSQL, Oracle, and to some extent recent versions of MySQL (4.x) each
> have spatial index support using r-tree indexes.
>
> I found this library, but fear it may be hard to integrate with MS SQL:
>
> Spatial Index Library 0.4b
> http://freshmeat.net/projects/spatialindexlib/?branch_id=35217&release_id=105467
>
> I thought MS FoxPro was supposed to have some support for distance queries
> too, but not sure.
>
> My only other recommendation was to reduce the "resolution" of your search,
> if you don't need such accuracy. For example, create a table of 10,000 or
> 100,000 zip code areas (like US 5-digit zips), and a cross-referencing table
> to your 1.7 million row one, and run your query against the zip code area
> table instead. I would probably use rectangular areas, and assign all zips
> in that area the same area_id. Find the areas in the desired distance, then
> find all zips in those areas with a join.
>
> An article I found:
> Microsoft SQL Server: Future Plans for Supporting Spatial Data
> http://www.directionsmag.com/article.php?article_id=430
>
> I suppose you could install Cygwin and PostgreSQL and write an extended
> stored procedure in C to query a Postgresql table.... Ugh! It'd be easier to
> migrate to PostgreSQL, probably.



Relevant Pages

  • Re: Need input from Firebird users for a PostgreSQL vs Firebird comparison.
    ... The problem with using a domain is it's not out of the box support. ... There is TONS of new and cool features I could add from the upcoming ... PostgreSQL 8.3 but I wont do that because it's not a released version yet. ... When Firebird 2.1 is released I will update the comparison showing CLOB ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: PostgreSQL and Firebird comparison via digg
    ... Do you know if using PostgreSQL a query or connection can have a priority set, so it can run quicker than other queries? ... in a POS system the reporting queries should have lower priority than the generated invoices insert queries. ... I know a lot of people are basing this whole threading thing on when PostgreSQL only ran on windows via cygwin Unix emulation and yes that did suck, but that is a thing of the way way past. ...
    (borland.public.delphi.non-technical)
  • Re: {Spam?} RE: The funny thing about numbers is that theyre just
    ... Picking a PostgreSQL support vendor at random, I got a price of $2995 ... Well, yeah, but the guy I looked at for PostgreSQL support charged extra for any funny uses. ...
    (comp.databases.informix)
  • Re: How do I speed up this query ? (1.7M table entries)
    ... I was surprised no one mentioned rtree/spatial indexes. ... PostgreSQL, Oracle, and to some extent recent versions of MySQL each ... have spatial index support using r-tree indexes. ... I thought MS FoxPro was supposed to have some support for distance queries ...
    (microsoft.public.sqlserver)
  • Latest Apache13 with PHP5 crashes when PostgreSQL support is added
    ... time with php5 and extensions to support PostgreSQL 8.0.9. ... I have updated Apache to ... I am compiling php5-extensions with only pgsql support. ...
    (freebsd-questions)