当前位置:   article > 正文

postgresql 优化 order by 对索引使用的影响

pg order by优化

  1. --原始sql
  2. SELECT
  3. *
  4. FROM
  5. tops_order.eticket
  6. WHERE
  7. (
  8. issue_complete_date >= '2015-10-01 00:00:00+08'
  9. AND issue_complete_date < '2016-03-28 00:00:00+08'
  10. AND is_domestic = 't'
  11. AND customer_id IN ('53a3bfa545cebf2700d727e3')
  12. )
  13. ORDER BY create_date DESC
  14. OFFSET 0
  15. LIMIT 10;
  16. --执行计划,使用的是idx_create_date索引
  17. Limit (cost=0.56..13959.07 rows=10 width=2633)
  18. -> Index Scan Backward using idx_create_date on eticket (cost=0.56..9207036.72 rows=6596 width=2633)
  19. Filter: (is_domestic AND (issue_complete_date >= '2015-10-01 00:00:00+08'::timestamp with time zone) AND (issue_complete_date < '2016-03-28 00:00:00+08'::timestamp with time zone) AND ((customer_id)::text = '53a3bfa545cebf2700d727e3'::text))
  20. --表索引信息
  21. Indexes:
  22. "pk_eticket" UNIQUE CONSTRAINT, btree (id)
  23. "idx_create_date" btree (create_date)
  24. "idx_customer_id" btree (customer_id)
  25. --修改其排序方式,使用idx_customer_id索引
  26. SELECT
  27. *
  28. FROM
  29. tops_order.eticket
  30. WHERE
  31. (
  32. issue_complete_date >= '2015-10-01 00:00:00+08'
  33. AND issue_complete_date < '2016-03-28 00:00:00+08'
  34. AND is_domestic = 't'
  35. AND customer_id IN ('53a3bfa545cebf2700d727e3')
  36. )
  37. ORDER BY to_char(create_date,'yyyy-mm-dd hh24:mi:ss:ms') DESC
  38. OFFSET 0
  39. LIMIT 10;
  40. Limit (cost=96657.89..96657.92 rows=10 width=2633)
  41. -> Sort (cost=96657.89..96674.38 rows=6596 width=2633)
  42. Sort Key: (to_char(create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text))
  43. -> Bitmap Heap Scan on eticket (cost=1819.88..96515.36 rows=6596 width=2633)
  44. Recheck Cond: ((customer_id)::text = '53a3bfa545cebf2700d727e3'::text)
  45. Filter: (is_domestic AND (issue_complete_date >= '2015-10-01 00:00:00+08'::timestamp with time zone) AND (issue_complete_date < '2016-03-28 00:00:00+08'::timestamp with time zone))
  46. -> Bitmap Index Scan on idx_customer_id (cost=0.00..1818.23 rows=25823 width=0)
  47. Index Cond: ((customer_id)::text = '53a3bfa545cebf2700d727e3'::text)
  48. 在上面的sql中由于ORDER BY create_date DESC的原因,pg使用了idx_create_date索引,但其并不是最有效的,请问pg为什么会使用那个索引,而不使用 idx_customer_id这个索引(其更高效啊),除了我上面的方法,还有什么办法可以避免吗?



  1. devflight=# set work_mem='1GB';
  2. SET
  3. devflight=# show work_mem;
  4. work_mem
  5. ----------
  6. 1GB
  7. explain (analyze,verbose,costs,timing,buffers)
  8. SELECT
  9. *
  10. FROM
  11. tops_order.eticket
  12. WHERE
  13. (
  14. issue_complete_date >= '2015-10-01 00:00:00+08'
  15. AND issue_complete_date < '2016-03-28 00:00:00+08'
  16. AND is_domestic = 't'
  17. AND customer_id IN ('53a3bfa545cebf2700d727e3')
  18. )
  19. ORDER BY create_date DESC
  20. OFFSET 0
  21. LIMIT 10;
  22. --第一次执行,不考虑缓存
  23. Limit (cost=0.56..14494.42 rows=10 width=2628) (actual time=77326.839..423643.180 rows=10 loops=1)
  24. Output: id, order_type...其它字段省略
  25. Buffers: shared hit=778155 read=410205
  26. -> Index Scan Backward using idx_create_date on tops_order.eticket (cost=0.56..9209400.56 rows=6354 width=2628) (actual time=77326.834..423643.149 rows=10 loops=1)
  27. Output: id, order_type...其它字段省略
  28. Filter: (eticket.is_domestic AND (eticket.issue_complete_date >= '2015-10-01 00:00:00+08'::timestamp with time zone) AND (eticket.issue_complete_date < '2016-03-28 00:00:00+08'::timestamp with time zone) AND ((eticket.customer_id)::text = '53a3bfa545cebf2700d72
  29. 7e3'::text))
  30. Rows Removed by Filter: 1279957
  31. Buffers: shared hit=778155 read=410205
  32. Total runtime: 423643.357 ms
  33. --第二次执行,考虑缓存
  34. Limit (cost=0.56..14494.44 rows=10 width=2628) (actual time=767.403..4279.628 rows=10 loops=1)
  35. Output: id, order_type...其它字段省略
  36. Buffers: shared hit=1189156
  37. -> Index Scan Backward using idx_create_date on tops_order.eticket (cost=0.56..9209412.56 rows=6354 width=2628) (actual time=767.400..4279.606 rows=10 loops=1)
  38. Output: id, order_type...其它字段省略
  39. Filter: (eticket.is_domestic AND (eticket.issue_complete_date >= '2015-10-01 00:00:00+08'::timestamp with time zone) AND (eticket.issue_complete_date < '2016-03-28 00:00:00+08'::timestamp with time zone) AND ((eticket.customer_id)::text = '53a3bfa545cebf2700d72
  40. 7e3'::text))
  41. Rows Removed by Filter: 1280127
  42. Buffers: shared hit=1189156
  43. Total runtime: 4279.777 ms
  44. explain (analyze,verbose,costs,timing,buffers)
  45. SELECT
  46. *
  47. FROM
  48. tops_order.eticket
  49. WHERE
  50. (
  51. issue_complete_date >= '2015-10-01 00:00:00+08'
  52. AND issue_complete_date < '2016-03-28 00:00:00+08'
  53. AND is_domestic = 't'
  54. AND customer_id IN ('53a3bfa545cebf2700d727e3')
  55. )
  56. ORDER BY to_char(create_date,'yyyy-mm-dd hh24:mi:ss:ms') DESC
  57. OFFSET 0
  58. LIMIT 10;
  59. --第一次执行,不考虑缓存
  60. Limit (cost=93008.42..93008.44 rows=10 width=2628) (actual time=24797.439..24797.444 rows=10 loops=1)
  61. Output: id, order_type, (to_char(create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text))...其它字段省略
  62. Buffers: shared hit=2419 read=16685
  63. -> Sort (cost=93008.42..93024.30 rows=6354 width=2628) (actual time=24797.436..24797.439 rows=10 loops=1)
  64. Output: id, order_type, (to_char(create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text)) ...其它字段省略
  65. Sort Key: (to_char(eticket.create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text))
  66. Sort Method: top-N heapsort Memory: 45kB
  67. Buffers: shared hit=2419 read=16685
  68. -> Bitmap Heap Scan on tops_order.eticket (cost=1744.15..92871.11 rows=6354 width=2628) (actual time=1473.866..24796.587 rows=32 loops=1)
  69. Output: id, order_type, to_char(create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text)...其它字段省略
  70. Recheck Cond: ((eticket.customer_id)::text = '53a3bfa545cebf2700d727e3'::text)
  71. Filter: (eticket.is_domestic AND (eticket.issue_complete_date >= '2015-10-01 00:00:00+08'::timestamp with time zone) AND (eticket.issue_complete_date < '2016-03-28 00:00:00+08'::timestamp with time zone))
  72. Rows Removed by Filter: 20282
  73. Buffers: shared hit=2414 read=16685
  74. -> Bitmap Index Scan on idx_customer_id (cost=0.00..1742.56 rows=24800 width=0) (actual time=1467.166..1467.166 rows=20314 loops=1)
  75. Index Cond: ((eticket.customer_id)::text = '53a3bfa545cebf2700d727e3'::text)
  76. Buffers: shared hit=1 read=387
  77. Total runtime: 24798.126 ms
  78. --第二次执行,考虑缓存
  79. Limit (cost=93008.42..93008.44 rows=10 width=2628) (actual time=90.570..90.576 rows=10 loops=1)
  80. Output: id, order_type, (to_char(create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text)) ...其它字段省略
  81. Buffers: shared hit=19099
  82. -> Sort (cost=93008.42..93024.30 rows=6354 width=2628) (actual time=90.567..90.569 rows=10 loops=1)
  83. Output: id, order_type, (to_char(create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text)) ...其它字段省略
  84. Sort Key: (to_char(eticket.create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text))
  85. Sort Method: top-N heapsort Memory: 45kB
  86. Buffers: shared hit=19099
  87. -> Bitmap Heap Scan on tops_order.eticket (cost=1744.15..92871.11 rows=6354 width=2628) (actual time=13.735..90.135 rows=32 loops=1)
  88. Output: id, order_type, to_char(create_date, 'yyyy-mm-dd hh24:mi:ss:ms'::text) ...其它字段省略
  89. Recheck Cond: ((eticket.customer_id)::text = '53a3bfa545cebf2700d727e3'::text)
  90. Filter: (eticket.is_domestic AND (eticket.issue_complete_date >= '2015-10-01 00:00:00+08'::timestamp with time zone) AND (eticket.issue_complete_date < '2016-03-28 00:00:00+08'::timestamp with time zone))
  91. Rows Removed by Filter: 20282
  92. Buffers: shared hit=19099
  93. -> Bitmap Index Scan on idx_customer_id (cost=0.00..1742.56 rows=24800 width=0) (actual time=8.259..8.259 rows=20314 loops=1)
  94. Index Cond: ((eticket.customer_id)::text = '53a3bfa545cebf2700d727e3'::text)
  95. Buffers: shared hit=388
  96. Total runtime: 90.875 ms


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/135209
推荐阅读
相关标签
  

闽ICP备14008679号