我有一个函数,并且在此函数中声明了我的光标。功能运行非常慢(大约14秒)。我认为这是因为它确实很复杂。我决定在探查器中测试我的功能。令我震惊的是,它花了90%的时间处理与光标相关的事情。 4秒钟检查光标是否为空,9秒钟打开FOR LOOP的光标。结果如下:
我该如何减少这些事情所需的时间?
这是我的光标。它通常仅包含1-6行。并且此查询在瞬间返回行。
FUNCTION finder (...) RETURN NUMBER IS
...
CURSOR near_clinics IS
SELECT a.id, a.full_address, me.answer, me.name, a.nlat, a.nlong, me.in_partner, a.city
FROM user.table1 a,
(SELECT pnradius AS radius, 111.045 AS distance_unit, 57.2957795 AS rad2deg, 0.01745329251994 AS deg2rad
FROM dual) geo,
user.table2 me
WHERE a.nlat BETWEEN pnlatitude - (geo.radius / geo.distance_unit) AND
pnlatitude + (geo.radius / geo.distance_unit)
AND a.nlong BETWEEN pnlongitude - (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude)))) AND
pnlongitude + (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude))))
AND geo.distance_unit * rad2deg *
(acos(cos(deg2rad * (pnlatitude)) * cos(deg2rad * (a.nlat)) * cos(deg2rad * (pnlongitude - a.nlong)) +
sin(deg2rad * (pnlatitude)) * sin(deg2rad * (a.nlat)))) < pnradius
AND a.parent_id = me.answer
AND a.parent_table = 'user.table2';
...
BEGIN
...
END.