构建一个基于函数的索引oracle sql

时间:2016-10-23 05:44:46

标签: sql oracle function indexing

对于这样的查询:

WITH params as (SELECT -28 as lat, 151 as lon
              FROM dual)
SELECT sighting_id, sqrt(power(lat - latitude, 2) + power(lon - longitude,  2)) 

AS distance FROM sightings CROSS JOIN params

WHERE sqrt(power(lat - latitude, 2) + power(lon - longitude, 2)) =

(SELECT MAX(sqrt(power(lat - latitude, 2) + power(lon - longitude, 2)))

FROM sightings CROSS JOIN params);

我将如何构建基于函数的索引呢?瞄准_是主键,这是否意味着id必须在长和拉上构建它?香港专业教育学院看了一些例子,但似乎没有人真正告诉我如何在这里建立一个。

执行计划如下:

id   operation            name
0    select statement        
1    table access full    sighting 
2    sort aggregate
3    table access full    sighting

1 个答案:

答案 0 :(得分:2)

正如我所强调的那样,您需要按sqrt(power(lat - latitude, 2) + power(lon - longitude, 2))构建索引,并且问题的根本原因是来自不同表的列。但在你的例子中,lat总是“-28”而lon =“151”。 您可以将查询重写为

SELECT sighting_id
     , sqrt(power(-28 - latitude, 2) + power(151 - longitude,  2))  AS distance 
FROM sightings 
WHERE sqrt(power(-28 - latitude, 2) + power(151 - longitude, 2)) =
(SELECT MAX(sqrt(power(-28 - latitude, 2) + power(151 - longitude, 2)))
FROM sightings ); 

并创建FB indnex

create index I_sightings_distance on sightings (sighting_id
     , sqrt(power(-28 - latitude, 2) + power(151 - longitude,  2)) ); 

我为instnace添加了sighting_id。它的indeax能够在快速全扫描中使用。

NB。使用FBIndex有一些意见 https://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_5012.htm#SQLRF01212