У меня есть этот метод в моем Rails API, который продолжает посылать мне оповещения с высоким временем ответа . Я пытался оптимизировать его настолько, насколько мог, в соответствии с моими текущими знаниями, но, видимо, он все еще не выполняет свою работу.
Любая помощь по оптимизации этих запросов будет принята с благодарностью:
Это мой метод получения маркеров и отправки их в мой API
Сначала я выбираю адреса
longitude = params[:longitude]
latitude = params[:latitude]
@addresses = Address.joins('INNER JOIN users ON users.id = addresses.addressable_id')
.joins('INNER JOIN items ON items.user_id = users.id')
.where('items.name IS NOT NULL').where("items.name <> ''")
.where('items.visibility = TRUE')
.where('items.photo IS NOT NULL').where("items.photo <> ''")
.where('addresses.latitude IS NOT NULL AND addresses.addressable_type = ? ', "User")
.near([latitude, longitude], (params[:distance].to_i + 1000))
Во-вторых, я использую эти адреса для рендеринга объекта JSON обратно в мой API
У меня есть метод checkitem
def checkitem(item)
begin
requests = Request.where('item_id = ? AND created_at < ? AND created_at > ?', item.id, (DateTime.now - 1.day), (DateTime.now - 6.months)).pluck(:status)
if (requests.exists? && requests.count > 2)
if requests.count('pending') >= 3 && (item.user.current_sign_in_at.present? && item.user.current_sign_in_at < (DateTime.now - 2.weeks))
false
else
true
end
elsif (requests == [] || requests.count <= 2)
true
elsif (item.user.current_sign_in_at.present? && item.user.current_sign_in_at > (DateTime.now - 2.weeks)) || item.user.created_at > (DateTime.now - 2.weeks)
true
else
false
end
rescue
true
end
end
Затем я отрисовываю свой JSON
@places = Address.where(addressable_type: 'Item').where.not(type_add: nil).near([latitude, longitude], 10)
render json: {markers: @addresses.uniq.map { |address|
[{
name: address.user.items.first.name,
photo: { uri: address.user.items.first.photo.url },
id: Item.where(user_id: address.addressable_id).first.id,
latitude: address.latitude,
longitude: address.longitude,
breed: address.user.items.first.breed.id,
innactive: checkitem(address.user.items.first) ? false : true,
power: (address.user.items.first.requests.count >= 2 && address.user.items.first.requests.last(3).map(&:status).count('pending') < 1) ? true : false,
}]
}.reject { |e| e.nil? }.flatten.first(100)
}
end
@address.explain
=> EXPLAIN for: SELECT addresses.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((45.501689 - addresses.latitude) * PI() / 180 / 2), 2) + COS(45.501689 * PI() / 180) * COS(addresses.latitude * PI() / 180) * POWER(SIN((-73.567256 - addresses.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((addresses.longitude - -73.567256) / 57.2957795), ((addresses.latitude - 45.501689) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "addresses" INNER JOIN users ON users.id = addresses.addressable_id INNER JOIN items ON items.user_id = users.id WHERE (items.name IS NOT NULL) AND (items.name <> '') AND (items.visibility = TRUE) AND (items.photo IS NOT NULL) AND (items.photo <> '') AND (addresses.latitude IS NOT NULL AND addresses.addressable_type = 'User' ) AND (addresses.latitude BETWEEN 31.028510688915205 AND 59.97486731108479 AND addresses.longitude BETWEEN -94.21702228070411 AND -52.91748971929589 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((45.501689 - addresses.latitude) * PI() / 180 / 2), 2) + COS(45.501689 * PI() / 180) * COS(addresses.latitude * PI() / 180) * POWER(SIN((-73.567256 - addresses.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 1000) ORDER BY distance ASC
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sort (cost=224.28..224.28 rows=1 width=138)
Sort Key: (('7917.511728464'::double precision * asin(sqrt((power(sin((((('45.501689'::double precision - addresses.latitude) * '3.14159265358979'::double precision) / '180'::double precision) / '2'::double precision)), '2'::double precision) + (('0.70088823836273'::double precision * cos(((addresses.latitude * '3.14159265358979'::double precision) / '180'::double precision))) * power(sin((((('-73.567256'::double precision - addresses.longitude) * '3.14159265358979'::double precision) / '180'::double precision) / '2'::double precision)), '2'::double precision)))))))
-> Nested Loop (cost=0.11..224.28 rows=1 width=138)
-> Nested Loop (cost=0.06..207.10 rows=39 width=8)
-> Seq Scan on items (cost=0.00..126.62 rows=39 width=4)
Filter: ((name IS NOT NULL) AND visibility AND (photo IS NOT NULL) AND ((name)::text <> ''::text) AND ((photo)::text <> ''::text))
-> Index Only Scan using users_pkey on users (cost=0.06..2.06 rows=1 width=4)
Index Cond: (id = items.user_id)
-> Index Scan using index_addresses_on_addressable_type_and_addressable_id on addresses (cost=0.06..0.44 rows=1 width=98)
Index Cond: (((addressable_type)::text = 'User'::text) AND (addressable_id = users.id))
Filter: ((latitude IS NOT NULL) AND (latitude >= '31.0285106889152'::double precision) AND (latitude <= '59.9748673110848'::double precision) AND (longitude >= '-94.2170222807041'::double precision) AND (longitude <= '-52.9174897192959'::double precision) AND (('7917.511728464'::double precision * asin(sqrt((power(sin((((('45.501689'::double precision - latitude) * '3.14159265358979'::double precision) / '180'::double precision) / '2'::double precision)), '2'::double precision) + (('0.70088823836273'::double precision * cos(((latitude * '3.14159265358979'::double precision) / '180'::double precision))) * power(sin((((('-73.567256'::double precision - longitude) * '3.14159265358979'::double precision) / '180'::double precision) / '2'::double precision)), '2'::double precision)))))) >= '0'::double precision) AND (('7917.511728464'::double precision * asin(sqrt((power(sin((((('45.501689'::double precision - latitude) * '3.14159265358979'::double precision) / '180'::double precision) / '2'::double precision)), '2'::double precision) + (('0.70088823836273'::double precision * cos(((latitude * '3.14159265358979'::double precision) / '180'::double precision))) * power(sin((((('-73.567256'::double precision - longitude) * '3.14159265358979'::double precision) / '180'::double precision) / '2'::double precision)), '2'::double precision)))))) <= '1000'::double precision))
(11 rows)