How to optimize query with order by












0















Have query which generate OpenERP ORM. Table have 100k rows.



SELECT "tbl".id 
FROM "tbl"
WHERE (("tbl"."active" = 'True') AND ("tbl"."is_company" IS NULL or "tbl"."is_company" = false ))
ORDER BY "tbl"."display_name"


With indexes :



    "ix_tbl_pkey" PRIMARY KEY, btree (id)
"ix_active" btree (active)
"ix_displayname" btree (display_name)
"ix_iscompany" btree (is_company)


Query with order by takes 57735.775 ms.
Plan is :



 Sort  (cost=13031.73..13269.13 rows=94960 width=47) (actual time=57711.753..57725.079 rows=94967 loops=1)
Sort Key: display_name
Sort Method: quicksort Memory: 12918kB
-> Seq Scan on tbl (cost=0.00..5180.90 rows=94960 width=47) (actual time=0.009..57.056 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 57735.775 ms
(7 rows)


When i try without order by it takes 65.969 ms.
Plan is :



 Seq Scan on tbl (cost=0.00..5180.90 rows=94960 width=4) (actual time=0.026..60.782 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 65.969 ms
(4 rows)


With set enable_sort = off; it takes 1206.157 ms plan is :



 Index Scan using ix_displayname on tbl(cost=0.00..21479.14 rows=94960 width=47) (actual time=29.912..1194.954 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 1206.157 ms
(4 rows)


Any way to optimize it with indexes ? Because we cant change something in ORM .










share|improve this question
















bumped to the homepage by Community 17 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Something is really wrong in that sort. What sort of hardware are you on? Also, what are you doing with 90+k rows? Could you please include the table definition, too?

    – dezso
    May 19 '16 at 8:24











  • @dezso ORM use this query for sorting data. I just check slow querys and one of them is this. Field display_name contains name and surnames

    – GeoVIP
    May 19 '16 at 8:33











  • I'm simulating a similar scenario in one of my own tables with 100k rows of real data, and get the same results: fast with enable_sort = off, slow with on.

    – Ziggy Crueltyfree Zeitgeister
    May 19 '16 at 8:41











  • @ZiggyCrueltyfreeZeitgeister and did you find some solution ?

    – GeoVIP
    May 19 '16 at 10:45











  • @GeoVIP no, but I didn't know about enable_sort before, and I'm sure it will come in handy for some of my own queries! It looks like that is the solution, at least one that would be satisfactory to me.

    – Ziggy Crueltyfree Zeitgeister
    May 19 '16 at 22:25
















0















Have query which generate OpenERP ORM. Table have 100k rows.



SELECT "tbl".id 
FROM "tbl"
WHERE (("tbl"."active" = 'True') AND ("tbl"."is_company" IS NULL or "tbl"."is_company" = false ))
ORDER BY "tbl"."display_name"


With indexes :



    "ix_tbl_pkey" PRIMARY KEY, btree (id)
"ix_active" btree (active)
"ix_displayname" btree (display_name)
"ix_iscompany" btree (is_company)


Query with order by takes 57735.775 ms.
Plan is :



 Sort  (cost=13031.73..13269.13 rows=94960 width=47) (actual time=57711.753..57725.079 rows=94967 loops=1)
Sort Key: display_name
Sort Method: quicksort Memory: 12918kB
-> Seq Scan on tbl (cost=0.00..5180.90 rows=94960 width=47) (actual time=0.009..57.056 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 57735.775 ms
(7 rows)


When i try without order by it takes 65.969 ms.
Plan is :



 Seq Scan on tbl (cost=0.00..5180.90 rows=94960 width=4) (actual time=0.026..60.782 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 65.969 ms
(4 rows)


With set enable_sort = off; it takes 1206.157 ms plan is :



 Index Scan using ix_displayname on tbl(cost=0.00..21479.14 rows=94960 width=47) (actual time=29.912..1194.954 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 1206.157 ms
(4 rows)


Any way to optimize it with indexes ? Because we cant change something in ORM .










share|improve this question
















bumped to the homepage by Community 17 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Something is really wrong in that sort. What sort of hardware are you on? Also, what are you doing with 90+k rows? Could you please include the table definition, too?

    – dezso
    May 19 '16 at 8:24











  • @dezso ORM use this query for sorting data. I just check slow querys and one of them is this. Field display_name contains name and surnames

    – GeoVIP
    May 19 '16 at 8:33











  • I'm simulating a similar scenario in one of my own tables with 100k rows of real data, and get the same results: fast with enable_sort = off, slow with on.

    – Ziggy Crueltyfree Zeitgeister
    May 19 '16 at 8:41











  • @ZiggyCrueltyfreeZeitgeister and did you find some solution ?

    – GeoVIP
    May 19 '16 at 10:45











  • @GeoVIP no, but I didn't know about enable_sort before, and I'm sure it will come in handy for some of my own queries! It looks like that is the solution, at least one that would be satisfactory to me.

    – Ziggy Crueltyfree Zeitgeister
    May 19 '16 at 22:25














0












0








0








Have query which generate OpenERP ORM. Table have 100k rows.



SELECT "tbl".id 
FROM "tbl"
WHERE (("tbl"."active" = 'True') AND ("tbl"."is_company" IS NULL or "tbl"."is_company" = false ))
ORDER BY "tbl"."display_name"


With indexes :



    "ix_tbl_pkey" PRIMARY KEY, btree (id)
"ix_active" btree (active)
"ix_displayname" btree (display_name)
"ix_iscompany" btree (is_company)


Query with order by takes 57735.775 ms.
Plan is :



 Sort  (cost=13031.73..13269.13 rows=94960 width=47) (actual time=57711.753..57725.079 rows=94967 loops=1)
Sort Key: display_name
Sort Method: quicksort Memory: 12918kB
-> Seq Scan on tbl (cost=0.00..5180.90 rows=94960 width=47) (actual time=0.009..57.056 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 57735.775 ms
(7 rows)


When i try without order by it takes 65.969 ms.
Plan is :



 Seq Scan on tbl (cost=0.00..5180.90 rows=94960 width=4) (actual time=0.026..60.782 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 65.969 ms
(4 rows)


With set enable_sort = off; it takes 1206.157 ms plan is :



 Index Scan using ix_displayname on tbl(cost=0.00..21479.14 rows=94960 width=47) (actual time=29.912..1194.954 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 1206.157 ms
(4 rows)


Any way to optimize it with indexes ? Because we cant change something in ORM .










share|improve this question
















Have query which generate OpenERP ORM. Table have 100k rows.



SELECT "tbl".id 
FROM "tbl"
WHERE (("tbl"."active" = 'True') AND ("tbl"."is_company" IS NULL or "tbl"."is_company" = false ))
ORDER BY "tbl"."display_name"


With indexes :



    "ix_tbl_pkey" PRIMARY KEY, btree (id)
"ix_active" btree (active)
"ix_displayname" btree (display_name)
"ix_iscompany" btree (is_company)


Query with order by takes 57735.775 ms.
Plan is :



 Sort  (cost=13031.73..13269.13 rows=94960 width=47) (actual time=57711.753..57725.079 rows=94967 loops=1)
Sort Key: display_name
Sort Method: quicksort Memory: 12918kB
-> Seq Scan on tbl (cost=0.00..5180.90 rows=94960 width=47) (actual time=0.009..57.056 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 57735.775 ms
(7 rows)


When i try without order by it takes 65.969 ms.
Plan is :



 Seq Scan on tbl (cost=0.00..5180.90 rows=94960 width=4) (actual time=0.026..60.782 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 65.969 ms
(4 rows)


With set enable_sort = off; it takes 1206.157 ms plan is :



 Index Scan using ix_displayname on tbl(cost=0.00..21479.14 rows=94960 width=47) (actual time=29.912..1194.954 rows=94967 loops=1)
Filter: (active AND ((is_company IS NULL) OR (NOT is_company)))
Rows Removed by Filter: 623
Total runtime: 1206.157 ms
(4 rows)


Any way to optimize it with indexes ? Because we cant change something in ORM .







postgresql postgresql-9.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 19 '16 at 14:09







GeoVIP

















asked May 19 '16 at 7:48









GeoVIPGeoVIP

183212




183212





bumped to the homepage by Community 17 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 17 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Something is really wrong in that sort. What sort of hardware are you on? Also, what are you doing with 90+k rows? Could you please include the table definition, too?

    – dezso
    May 19 '16 at 8:24











  • @dezso ORM use this query for sorting data. I just check slow querys and one of them is this. Field display_name contains name and surnames

    – GeoVIP
    May 19 '16 at 8:33











  • I'm simulating a similar scenario in one of my own tables with 100k rows of real data, and get the same results: fast with enable_sort = off, slow with on.

    – Ziggy Crueltyfree Zeitgeister
    May 19 '16 at 8:41











  • @ZiggyCrueltyfreeZeitgeister and did you find some solution ?

    – GeoVIP
    May 19 '16 at 10:45











  • @GeoVIP no, but I didn't know about enable_sort before, and I'm sure it will come in handy for some of my own queries! It looks like that is the solution, at least one that would be satisfactory to me.

    – Ziggy Crueltyfree Zeitgeister
    May 19 '16 at 22:25



















  • Something is really wrong in that sort. What sort of hardware are you on? Also, what are you doing with 90+k rows? Could you please include the table definition, too?

    – dezso
    May 19 '16 at 8:24











  • @dezso ORM use this query for sorting data. I just check slow querys and one of them is this. Field display_name contains name and surnames

    – GeoVIP
    May 19 '16 at 8:33











  • I'm simulating a similar scenario in one of my own tables with 100k rows of real data, and get the same results: fast with enable_sort = off, slow with on.

    – Ziggy Crueltyfree Zeitgeister
    May 19 '16 at 8:41











  • @ZiggyCrueltyfreeZeitgeister and did you find some solution ?

    – GeoVIP
    May 19 '16 at 10:45











  • @GeoVIP no, but I didn't know about enable_sort before, and I'm sure it will come in handy for some of my own queries! It looks like that is the solution, at least one that would be satisfactory to me.

    – Ziggy Crueltyfree Zeitgeister
    May 19 '16 at 22:25

















Something is really wrong in that sort. What sort of hardware are you on? Also, what are you doing with 90+k rows? Could you please include the table definition, too?

– dezso
May 19 '16 at 8:24





Something is really wrong in that sort. What sort of hardware are you on? Also, what are you doing with 90+k rows? Could you please include the table definition, too?

– dezso
May 19 '16 at 8:24













@dezso ORM use this query for sorting data. I just check slow querys and one of them is this. Field display_name contains name and surnames

– GeoVIP
May 19 '16 at 8:33





@dezso ORM use this query for sorting data. I just check slow querys and one of them is this. Field display_name contains name and surnames

– GeoVIP
May 19 '16 at 8:33













I'm simulating a similar scenario in one of my own tables with 100k rows of real data, and get the same results: fast with enable_sort = off, slow with on.

– Ziggy Crueltyfree Zeitgeister
May 19 '16 at 8:41





I'm simulating a similar scenario in one of my own tables with 100k rows of real data, and get the same results: fast with enable_sort = off, slow with on.

– Ziggy Crueltyfree Zeitgeister
May 19 '16 at 8:41













@ZiggyCrueltyfreeZeitgeister and did you find some solution ?

– GeoVIP
May 19 '16 at 10:45





@ZiggyCrueltyfreeZeitgeister and did you find some solution ?

– GeoVIP
May 19 '16 at 10:45













@GeoVIP no, but I didn't know about enable_sort before, and I'm sure it will come in handy for some of my own queries! It looks like that is the solution, at least one that would be satisfactory to me.

– Ziggy Crueltyfree Zeitgeister
May 19 '16 at 22:25





@GeoVIP no, but I didn't know about enable_sort before, and I'm sure it will come in handy for some of my own queries! It looks like that is the solution, at least one that would be satisfactory to me.

– Ziggy Crueltyfree Zeitgeister
May 19 '16 at 22:25










1 Answer
1






active

oldest

votes


















0














Using an index consisting of the columns display_name, id, active and is_company would enable the query to be run using the index alone. Ordering the columns in the order provided would remove the sort requirement. This comes at a slightly update cost, and could replace the ix_displayname index.



If active were rare, it would be faster to order the columns active, display_name, id, 'is_company. However, updating the active column would have higher update costs.



You may need to select the display_name in an inner query, or use an index hint.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2fdba.stackexchange.com%2fquestions%2f138977%2fhow-to-optimize-query-with-order-by%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









    0














    Using an index consisting of the columns display_name, id, active and is_company would enable the query to be run using the index alone. Ordering the columns in the order provided would remove the sort requirement. This comes at a slightly update cost, and could replace the ix_displayname index.



    If active were rare, it would be faster to order the columns active, display_name, id, 'is_company. However, updating the active column would have higher update costs.



    You may need to select the display_name in an inner query, or use an index hint.






    share|improve this answer




























      0














      Using an index consisting of the columns display_name, id, active and is_company would enable the query to be run using the index alone. Ordering the columns in the order provided would remove the sort requirement. This comes at a slightly update cost, and could replace the ix_displayname index.



      If active were rare, it would be faster to order the columns active, display_name, id, 'is_company. However, updating the active column would have higher update costs.



      You may need to select the display_name in an inner query, or use an index hint.






      share|improve this answer


























        0












        0








        0







        Using an index consisting of the columns display_name, id, active and is_company would enable the query to be run using the index alone. Ordering the columns in the order provided would remove the sort requirement. This comes at a slightly update cost, and could replace the ix_displayname index.



        If active were rare, it would be faster to order the columns active, display_name, id, 'is_company. However, updating the active column would have higher update costs.



        You may need to select the display_name in an inner query, or use an index hint.






        share|improve this answer













        Using an index consisting of the columns display_name, id, active and is_company would enable the query to be run using the index alone. Ordering the columns in the order provided would remove the sort requirement. This comes at a slightly update cost, and could replace the ix_displayname index.



        If active were rare, it would be faster to order the columns active, display_name, id, 'is_company. However, updating the active column would have higher update costs.



        You may need to select the display_name in an inner query, or use an index hint.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 20 '16 at 1:16









        BillThorBillThor

        4,2681412




        4,2681412






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • 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%2fdba.stackexchange.com%2fquestions%2f138977%2fhow-to-optimize-query-with-order-by%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

            Liste der Baudenkmale in Friedland (Mecklenburg)

            Single-Malt-Whisky

            Czorneboh