Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Mailing Lists -> Oracle-L -> Re: (stupid) Question about indexing
probably more than you were looking for in regards to lat/lon queries, but here you go.
>2. if you were to try, how would you know the exact value and precision? -109.92638889 or -109.92639,
you would use a locator nearest neighbor query, and just have the user put in a point and oracle would return the nearest point to the location they put in.
if they want to do a box search, they would just use sdo_filter operator for that query given a user defined region of interest, if they want all points within 5 miles, they use a within distance operator.
> 3. I don't believe that indexes are used when > or < symbols are used in sql, am I correct?
yes, they are used, but for this type of location query ideally, you would utilize locator data type to store lat/lon and than any queries where a user specifies a box(geography) of interest would perform much better with a spatial r-tree index vs b-trees on lat/lon numeric data types.
given lower-left (x1,y1), upper right (x2,y2) you can do this:
select * from table
where
lon between x1 and x2
and
lat between y1 and y2
given indexes on lat,lon, oracle will use both indexes to find the box of interest if stats/cbo indicate it is a good idea.
but, if all the geometries in the table are just points, performance will be much better if the query was instead a:
SELECT * FROM table A
WHERE SDO_FILTER(a.locations,
SDO_GEOMETRY(2003, NULL, NULL,
SDO_ELEM_INFO_ARRAY(1,1003,3), SDO_ORDINATE_ARRAY(:x1,:y1, :x2,:y2))) = 'TRUE'; --
you can create a function that returns a location object, and create a spatial index on the result of the function.
for example, if your data model looks like this:
create table LONG_LAT_TABLE (longitude number, latitude number, name varchar2(32)); insert into LONG_LAT_TABLE values (10,10, 'Place1'); insert into LONG_LAT_TABLE values (20,20, 'Place2'); insert into LONG_LAT_TABLE values (30,30, 'Place3');
create or replace function get_long_lat_pt(longitude in number,
latitude in number)return SDO_GEOMETRY deterministic is
return sdo_geometry(2001, 8307, sdo_point_type(longitude, latitude, NULL),NULL, NULL);end;
Update the USER_SDO_GEOM_METADATA view, using dot-notation to specify the schema name and function name. The following example specifies SCOTT.GET_LONG_LAT_PT(LONGITUDE,LATITUDE) as the COLUMN_NAME (explained in Section 2.6.2) in the metadata view.
create index LONG_LAT_TABLE_IDX on LONG_LAT_TABLE(get_long_lat_pt(longitude,latitude)) indextype is mdsys.spatial_index; Perform queries on the data.
select name from LONG_LAT_TABLE a
where sdo_filter(get_long_lat_pt(a.longitude,a.latitude),
SDO_GEOMETRY(2003, 8307, NULL, SDO_ELEM_INFO_ARRAY(1,1003,3), SDO_ORDINATE_ARRAY(:x1,:y1, :x2,:y2))) = 'TRUE';
The docs cover all of this (and much of this is a cut and paste with mods from here: http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14255/toc.htm
-- http://www.freelists.org/webpage/oracle-lReceived on Thu Jul 12 2007 - 08:45:48 CDT
![]() |
![]() |