How to make the select query comparatively faster in rails?
up vote
1
down vote
favorite
I was looking for best approach to get this query faster in rails. Currently it takes lots of time due to Geocoder calculations.
Vehicle.includes(:user)
.where.not('users.id' => nil)
.where(country: 'United Kingdom')
.where.not(name: [nil, ''])
.select
Note: Currently the db used is PostgreSQL 9.5.14.
ruby-on-rails ruby activerecord rails-activerecord
|
show 3 more comments
up vote
1
down vote
favorite
I was looking for best approach to get this query faster in rails. Currently it takes lots of time due to Geocoder calculations.
Vehicle.includes(:user)
.where.not('users.id' => nil)
.where(country: 'United Kingdom')
.where.not(name: [nil, ''])
.select
Note: Currently the db used is PostgreSQL 9.5.14.
ruby-on-rails ruby activerecord rails-activerecord
Currently the db used is PostgreSQL 9.5.14.
– Bibek Khadka
Nov 12 at 0:45
3
Have you considered using PostGIS? You wouldn't have to load all the records as you would be able to filter directly on DB level using ST_Distance.
– Marcin Kołodziej
Nov 12 at 0:49
Done; nope haven't tried postgis yet. Currently for the location 'geocoder' gem is used.
– Bibek Khadka
Nov 12 at 0:52
2
Right now you can index only the fields you're searching by (users.id
,country
,name
, etc.), indexing longitudes and latitudes without PostGIS won't help.
– Marcin Kołodziej
Nov 12 at 1:46
1
As mentioned by @MarcinKołodziej PostGIS is the best and logic solution and any improve won't help because you are fetching lots of data which won't be in use, you can check the example in this link medium.com/@hin556/…
– Shiko
Nov 12 at 2:22
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I was looking for best approach to get this query faster in rails. Currently it takes lots of time due to Geocoder calculations.
Vehicle.includes(:user)
.where.not('users.id' => nil)
.where(country: 'United Kingdom')
.where.not(name: [nil, ''])
.select
Note: Currently the db used is PostgreSQL 9.5.14.
ruby-on-rails ruby activerecord rails-activerecord
I was looking for best approach to get this query faster in rails. Currently it takes lots of time due to Geocoder calculations.
Vehicle.includes(:user)
.where.not('users.id' => nil)
.where(country: 'United Kingdom')
.where.not(name: [nil, ''])
.select
Note: Currently the db used is PostgreSQL 9.5.14.
ruby-on-rails ruby activerecord rails-activerecord
ruby-on-rails ruby activerecord rails-activerecord
edited Nov 12 at 0:50
asked Nov 12 at 0:38
Bibek Khadka
448
448
Currently the db used is PostgreSQL 9.5.14.
– Bibek Khadka
Nov 12 at 0:45
3
Have you considered using PostGIS? You wouldn't have to load all the records as you would be able to filter directly on DB level using ST_Distance.
– Marcin Kołodziej
Nov 12 at 0:49
Done; nope haven't tried postgis yet. Currently for the location 'geocoder' gem is used.
– Bibek Khadka
Nov 12 at 0:52
2
Right now you can index only the fields you're searching by (users.id
,country
,name
, etc.), indexing longitudes and latitudes without PostGIS won't help.
– Marcin Kołodziej
Nov 12 at 1:46
1
As mentioned by @MarcinKołodziej PostGIS is the best and logic solution and any improve won't help because you are fetching lots of data which won't be in use, you can check the example in this link medium.com/@hin556/…
– Shiko
Nov 12 at 2:22
|
show 3 more comments
Currently the db used is PostgreSQL 9.5.14.
– Bibek Khadka
Nov 12 at 0:45
3
Have you considered using PostGIS? You wouldn't have to load all the records as you would be able to filter directly on DB level using ST_Distance.
– Marcin Kołodziej
Nov 12 at 0:49
Done; nope haven't tried postgis yet. Currently for the location 'geocoder' gem is used.
– Bibek Khadka
Nov 12 at 0:52
2
Right now you can index only the fields you're searching by (users.id
,country
,name
, etc.), indexing longitudes and latitudes without PostGIS won't help.
– Marcin Kołodziej
Nov 12 at 1:46
1
As mentioned by @MarcinKołodziej PostGIS is the best and logic solution and any improve won't help because you are fetching lots of data which won't be in use, you can check the example in this link medium.com/@hin556/…
– Shiko
Nov 12 at 2:22
Currently the db used is PostgreSQL 9.5.14.
– Bibek Khadka
Nov 12 at 0:45
Currently the db used is PostgreSQL 9.5.14.
– Bibek Khadka
Nov 12 at 0:45
3
3
Have you considered using PostGIS? You wouldn't have to load all the records as you would be able to filter directly on DB level using ST_Distance.
– Marcin Kołodziej
Nov 12 at 0:49
Have you considered using PostGIS? You wouldn't have to load all the records as you would be able to filter directly on DB level using ST_Distance.
– Marcin Kołodziej
Nov 12 at 0:49
Done; nope haven't tried postgis yet. Currently for the location 'geocoder' gem is used.
– Bibek Khadka
Nov 12 at 0:52
Done; nope haven't tried postgis yet. Currently for the location 'geocoder' gem is used.
– Bibek Khadka
Nov 12 at 0:52
2
2
Right now you can index only the fields you're searching by (
users.id
,country
,name
, etc.), indexing longitudes and latitudes without PostGIS won't help.– Marcin Kołodziej
Nov 12 at 1:46
Right now you can index only the fields you're searching by (
users.id
,country
,name
, etc.), indexing longitudes and latitudes without PostGIS won't help.– Marcin Kołodziej
Nov 12 at 1:46
1
1
As mentioned by @MarcinKołodziej PostGIS is the best and logic solution and any improve won't help because you are fetching lots of data which won't be in use, you can check the example in this link medium.com/@hin556/…
– Shiko
Nov 12 at 2:22
As mentioned by @MarcinKołodziej PostGIS is the best and logic solution and any improve won't help because you are fetching lots of data which won't be in use, you can check the example in this link medium.com/@hin556/…
– Shiko
Nov 12 at 2:22
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
3
down vote
Move
vehicle.longitude.present?
to database level:.where.not(longitude: [nil, ''])
Your distance calculation has to be moved to be on database level. Loading all records and calculating distance for each of them in Ruby will be too slow. Simplest solution is to add PostGIS:
- Add PostGIS extension to your database.
- (Optional) add an adapter that will make it easier to work with the data.
- Migrate your existing latitudes/longitudes to
st_point
. - Change your select to a scope which will leverage the ST_Distance function.
As an alternative to 2), you may consider moving your distance querying to a search engine like ElasticSearch. I'd only consider that if you will have any performance problems with the complex query you're creating.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254668%2fhow-to-make-the-select-query-comparatively-faster-in-rails%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Move
vehicle.longitude.present?
to database level:.where.not(longitude: [nil, ''])
Your distance calculation has to be moved to be on database level. Loading all records and calculating distance for each of them in Ruby will be too slow. Simplest solution is to add PostGIS:
- Add PostGIS extension to your database.
- (Optional) add an adapter that will make it easier to work with the data.
- Migrate your existing latitudes/longitudes to
st_point
. - Change your select to a scope which will leverage the ST_Distance function.
As an alternative to 2), you may consider moving your distance querying to a search engine like ElasticSearch. I'd only consider that if you will have any performance problems with the complex query you're creating.
add a comment |
up vote
3
down vote
Move
vehicle.longitude.present?
to database level:.where.not(longitude: [nil, ''])
Your distance calculation has to be moved to be on database level. Loading all records and calculating distance for each of them in Ruby will be too slow. Simplest solution is to add PostGIS:
- Add PostGIS extension to your database.
- (Optional) add an adapter that will make it easier to work with the data.
- Migrate your existing latitudes/longitudes to
st_point
. - Change your select to a scope which will leverage the ST_Distance function.
As an alternative to 2), you may consider moving your distance querying to a search engine like ElasticSearch. I'd only consider that if you will have any performance problems with the complex query you're creating.
add a comment |
up vote
3
down vote
up vote
3
down vote
Move
vehicle.longitude.present?
to database level:.where.not(longitude: [nil, ''])
Your distance calculation has to be moved to be on database level. Loading all records and calculating distance for each of them in Ruby will be too slow. Simplest solution is to add PostGIS:
- Add PostGIS extension to your database.
- (Optional) add an adapter that will make it easier to work with the data.
- Migrate your existing latitudes/longitudes to
st_point
. - Change your select to a scope which will leverage the ST_Distance function.
As an alternative to 2), you may consider moving your distance querying to a search engine like ElasticSearch. I'd only consider that if you will have any performance problems with the complex query you're creating.
Move
vehicle.longitude.present?
to database level:.where.not(longitude: [nil, ''])
Your distance calculation has to be moved to be on database level. Loading all records and calculating distance for each of them in Ruby will be too slow. Simplest solution is to add PostGIS:
- Add PostGIS extension to your database.
- (Optional) add an adapter that will make it easier to work with the data.
- Migrate your existing latitudes/longitudes to
st_point
. - Change your select to a scope which will leverage the ST_Distance function.
As an alternative to 2), you may consider moving your distance querying to a search engine like ElasticSearch. I'd only consider that if you will have any performance problems with the complex query you're creating.
answered Nov 12 at 2:35
Marcin Kołodziej
4,067315
4,067315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254668%2fhow-to-make-the-select-query-comparatively-faster-in-rails%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Currently the db used is PostgreSQL 9.5.14.
– Bibek Khadka
Nov 12 at 0:45
3
Have you considered using PostGIS? You wouldn't have to load all the records as you would be able to filter directly on DB level using ST_Distance.
– Marcin Kołodziej
Nov 12 at 0:49
Done; nope haven't tried postgis yet. Currently for the location 'geocoder' gem is used.
– Bibek Khadka
Nov 12 at 0:52
2
Right now you can index only the fields you're searching by (
users.id
,country
,name
, etc.), indexing longitudes and latitudes without PostGIS won't help.– Marcin Kołodziej
Nov 12 at 1:46
1
As mentioned by @MarcinKołodziej PostGIS is the best and logic solution and any improve won't help because you are fetching lots of data which won't be in use, you can check the example in this link medium.com/@hin556/…
– Shiko
Nov 12 at 2:22