You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

87 lines
3.5 KiB

  1. # Before indexing
  2. EXPLAIN ANALYZE SELECT * from customer WHERE name='John Smith';
  3. QUERY PLAN
  4. ------------------------------------------------------------------------------------------------------
  5. Seq Scan on customer (cost=0.00..429.00 rows=3 width=211) (actual time=0.299..3.124 rows=3 loops=1)
  6. Filter: (name = 'John Smith'::text)
  7. Rows Removed by Filter: 9997
  8. Planning Time: 0.129 ms
  9. Execution Time: 3.146 ms
  10. (5 rows)
  11. # After indexing using btree(name):
  12. CREATE INDEX idx_name on customer using btree(name);
  13. EXPLAIN ANALYZE SELECT * from customer WHERE name='John Smith';
  14. QUERY PLAN
  15. -------------------------------------------------------------------------------------------------------
  16. ----------
  17. Bitmap Heap Scan on customer (cost=4.31..15.45 rows=3 width=211) (actual time=0.051..0.056 rows=3 loo
  18. ps=1)
  19. Recheck Cond: (name = 'John Smith'::text)
  20. Heap Blocks: exact=3
  21. -> Bitmap Index Scan on idx_name (cost=0.00..4.31 rows=3 width=0) (actual time=0.044..0.044 rows=3
  22. loops=1)
  23. Index Cond: (name = 'John Smith'::text)
  24. Planning Time: 0.423 ms
  25. Execution Time: 0.086 ms
  26. # A complicated query with like doesn't benefit from hash indexing
  27. EXPLAIN ANALYZE SELECT * FROM customer WHERE address LIKE '%Warren%Port%';
  28. QUERY PLAN
  29. ------------------------------------------------------------------------------------------------------
  30. Seq Scan on customer (cost=0.00..429.00 rows=1 width=211) (actual time=3.544..4.110 rows=1 loops=1)
  31. Filter: (address ~~ '%Warren%Port%'::text)
  32. Rows Removed by Filter: 9999
  33. Planning Time: 0.125 ms
  34. Execution Time: 4.131 ms
  35. (5 rows)
  36. The query can be improved and benefit from the btree indexing:
  37. EXPLAIN ANALYZE SELECT * FROM customer WHERE name >= 'Lisa' and name <= 'Lisb';
  38. QUERY PLAN
  39. -------------------------------------------------------------------------------------------------------
  40. ------------
  41. Bitmap Heap Scan on customer (cost=4.41..45.44 rows=12 width=211) (actual time=0.068..0.202 rows=90 l
  42. oops=1)
  43. Recheck Cond: ((name >= 'Lisa'::text) AND (name <= 'Lisb'::text))
  44. Heap Blocks: exact=83
  45. -> Bitmap Index Scan on idx_name (cost=0.00..4.41 rows=12 width=0) (actual time=0.042..0.042 rows=
  46. 90 loops=1)
  47. Index Cond: ((name >= 'Lisa'::text) AND (name <= 'Lisb'::text))
  48. Planning Time: 0.174 ms
  49. Execution Time: 0.233 ms
  50. Before indexing address:
  51. EXPLAIN ANALYZE SELECT * FROM customer WHERE address='USS Oneill FPO AE 11865';
  52. QUERY PLAN
  53. ------------------------------------------------------------------------------------------------------
  54. Seq Scan on customer (cost=0.00..429.00 rows=1 width=211) (actual time=2.639..2.639 rows=0 loops=1)
  55. Filter: (address = 'USS Oneill FPO AE 11865'::text)
  56. Rows Removed by Filter: 10000
  57. Planning Time: 0.084 ms
  58. Execution Time: 2.657 ms
  59. (5 rows)
  60. After indexing address:
  61. CREATE INDEX idx_address on customer using HASH(address);
  62. EXPLAIN ANALYZE SELECT * FROM customer WHERE address='USS Oneill FPO AE 11865';
  63. QUERY PLAN
  64. -------------------------------------------------------------------------------------------------------
  65. -----------------
  66. Index Scan using idx_address on customer (cost=0.00..8.02 rows=1 width=211) (actual time=0.016..0.016
  67. rows=0 loops=1)
  68. Index Cond: (address = 'USS Oneill FPO AE 11865'::text)
  69. Planning Time: 0.224 ms
  70. Execution Time: 0.035 ms
  71. (4 rows)