在我的rails应用程序中,我具有允许查找最接近当前登录用户的用户的功能。我正在使用Geocoder gem。在用户模型中,我有这样的范围:
scope :close_to, -> (user:, distance:) {
where.not(id: user.id)
.near([user.latitude, user.longitude], distance)
}
这种方法非常有效,但对于较大的用户集合来说速度很慢。当我调用此范围时,它会生成以下sql查询:
SELECT users.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((48.471645 - users.latitude) * PI() / 180 / 2), 2) + COS(48.471645 * PI() / 180) * COS(users.latitude * PI() / 180) * POWER(SIN((-83.102801 - users.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((users.longitude - -83.102801) / 57.2957795), ((users.latitude - 48.471645) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "users" WHERE ("users"."id" != 43362) AND (users.latitude BETWEEN 39.4784289408127 AND 57.46486105918731 AND users.longitude BETWEEN -96.6674214298497 AND -69.5381805701503 AND (6371.0 * 2 * ASIN(SQRT(POWER(SIN((48.471645 - users.latitude) * PI() / 180 / 2), 2) + COS(48.471645 * PI() / 180) * COS(users.latitude * PI() / 180) * POWER(SIN((-83.102801 - users.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 1000) ORDER BY distance ASC;
我正在尝试为此创建索引,但它们不起作用。我正在尝试以下组合:
1.
add_index :users, [:id, :latitude]
add_index :users, [:id, :longitude]
2. add_index :users, [:id, :latitude, :longitude]
3. add_index :users, [:latitude]
add_index :users, [:longitude]
4. add_index :users, [:id, :latitude]
我应该如何添加索引以加快此查询?
编辑:我忘了添加我的纬度和经度列是小数。
此查询的ANALYZE返回类似的内容:
Sort (cost=7141.66..7142.14 rows=191 width=327) (actual time=575.995..585.543 rows=36598 loops=1)
Sort Key: ((12742::double precision * asin(sqrt((power(sin((((((48.471645 - latitude))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision) + ((0.662990616338754::double precision * cos((((latitude)::double precision * 3.14159265358979::double precision) / 180::double precision))) * power(sin(((((((-83.102801) - longitude))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision)))))))
Sort Method: external merge Disk: 4672kB
-> Seq Scan on users (cost=0.00..7134.43 rows=191 width=327) (actual time=0.381..517.615 rows=36598 loops=1)
Filter: ((id <> 43362) AND (latitude >= 39.4784289408127) AND (latitude <= 57.46486105918731) AND (longitude >= (-96.6674214298497)) AND (longitude <= (-69.5381805701503)) AND ((12742::double precision * asin(sqrt((power(sin((((((48.471645 - latitude))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision) + ((0.662990616338754::double precision * cos((((latitude)::double precision * 3.14159265358979::double precision) / 180::double precision))) * power(sin(((((((-83.102801) - longitude))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision)))))) >= 0::double precision) AND ((12742::double precision * asin(sqrt((power(sin((((((48.471645 - latitude))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision) + ((0.662990616338754::double precision * cos((((latitude)::double precision * 3.14159265358979::double precision) / 180::double precision))) * power(sin(((((((-83.102801) - longitude))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision)))))) <= 1000::double precision))
Rows Removed by Filter: 6756
Planning time: 1.041 ms
Execution time: 587.695 ms
(8 rows)
编辑2:
我注意到postgresql使用了我的
add_index :users, [:latitude, :longitude]
只有当我输入小距离前。用户距离近10公里。
答案 0 :(得分:1)
减速可能是由数学运算引起的,而不是由获取表数据引起的。您的部分标准不是针对记录字段,而是针对其他记录的数学运算结果,因此它变为O(N 2 )。
Postgres不使用索引并选择Seq扫描的原因是因为它决定在查询时必须获取大多数表记录。当要获取表中的大多数记录时,索引可能不会带来太多好处。
为了加快速度,您应该考虑使用PostGis的空间索引和基于邻近区域的搜索,或者使用Geo Distance Query使用Elasticsearch。