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.










share|improve this question























  • 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














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.










share|improve this question























  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












1 Answer
1






active

oldest

votes

















up vote
3
down vote














  1. Move vehicle.longitude.present? to database level:



    .where.not(longitude: [nil, ''])



  2. 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:



    1. Add PostGIS extension to your database.

    2. (Optional) add an adapter that will make it easier to work with the data.

    3. Migrate your existing latitudes/longitudes to st_point.

    4. Change your select to a scope which will leverage the ST_Distance function.


  3. 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.






share|improve this answer




















    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
    );



    );













    draft saved

    draft discarded


















    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














    1. Move vehicle.longitude.present? to database level:



      .where.not(longitude: [nil, ''])



    2. 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:



      1. Add PostGIS extension to your database.

      2. (Optional) add an adapter that will make it easier to work with the data.

      3. Migrate your existing latitudes/longitudes to st_point.

      4. Change your select to a scope which will leverage the ST_Distance function.


    3. 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.






    share|improve this answer
























      up vote
      3
      down vote














      1. Move vehicle.longitude.present? to database level:



        .where.not(longitude: [nil, ''])



      2. 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:



        1. Add PostGIS extension to your database.

        2. (Optional) add an adapter that will make it easier to work with the data.

        3. Migrate your existing latitudes/longitudes to st_point.

        4. Change your select to a scope which will leverage the ST_Distance function.


      3. 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.






      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote










        1. Move vehicle.longitude.present? to database level:



          .where.not(longitude: [nil, ''])



        2. 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:



          1. Add PostGIS extension to your database.

          2. (Optional) add an adapter that will make it easier to work with the data.

          3. Migrate your existing latitudes/longitudes to st_point.

          4. Change your select to a scope which will leverage the ST_Distance function.


        3. 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.






        share|improve this answer













        1. Move vehicle.longitude.present? to database level:



          .where.not(longitude: [nil, ''])



        2. 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:



          1. Add PostGIS extension to your database.

          2. (Optional) add an adapter that will make it easier to work with the data.

          3. Migrate your existing latitudes/longitudes to st_point.

          4. Change your select to a scope which will leverage the ST_Distance function.


        3. 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 2:35









        Marcin Kołodziej

        4,067315




        4,067315



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線