dlooby

Query scenario

Jul 17th, 2013
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DROP TABLE inner_tab_2;
  2. DROP TABLE inner_tab_1;
  3. DROP TABLE outer_tab CASCADE;
  4.  
  5. CREATE TABLE outer_tab (outer_key INTEGER,
  6.                         CONSTRAINT outer_tab_pk PRIMARY KEY (outer_key));
  7.  
  8. CREATE TABLE inner_tab_1(key_to INTEGER, key_from INTEGER, type INTEGER,
  9.                         CONSTRAINT inner_tab_1_pk PRIMARY KEY (type, key_to, key_from),
  10.             CONSTRAINT outer_from_fk FOREIGN KEY (key_from)
  11.             REFERENCES outer_tab (outer_key) MATCH SIMPLE
  12.             ON UPDATE CASCADE ON DELETE CASCADE,
  13.             CONSTRAINT outer_to_fk FOREIGN KEY (key_to)
  14.             REFERENCES outer_tab (outer_key) MATCH SIMPLE
  15.             ON UPDATE CASCADE ON DELETE CASCADE);
  16.  
  17. CREATE TABLE inner_tab_2(outer_key INTEGER, group_id INTEGER,
  18.             CONSTRAINT outer_fk FOREIGN KEY (outer_key)
  19.             REFERENCES outer_tab (outer_key) MATCH SIMPLE
  20.             ON UPDATE CASCADE ON DELETE CASCADE);
  21.  
  22. DO $$DECLARE rnd_outer_key integer; rnd_group_key integer;
  23. BEGIN
  24.     FOR idx IN 0..5000 LOOP
  25.     INSERT INTO outer_tab (outer_key) VALUES (idx);
  26.     END LOOP;
  27.  
  28.     FOR idx IN 0..16000 LOOP
  29.         SELECT INTO rnd_outer_key trunc(random() * 5000);
  30.         SELECT INTO rnd_group_key trunc(random() * 8);
  31.     INSERT INTO inner_tab_2 (outer_key, group_id) VALUES (rnd_outer_key, rnd_group_key);
  32.     END LOOP;
  33.  
  34.     FOR idx IN 0..1000 LOOP
  35.     INSERT INTO inner_tab_1 (key_to, key_from, type) VALUES (idx, idx, 1);
  36.     END LOOP;
  37.    
  38. END$$;
  39.  
  40.  
  41.  
  42. ANALYSE inner_tab_2;
  43. ANALYSE inner_tab_1;
  44. ANALYSE outer_tab;
  45.  
  46.  
  47. \timing
  48. --First the slow query with a group_id that is not in the inner_tab_2 table.
  49. SELECT outer_tab.outer_key
  50.   FROM outer_tab
  51.  WHERE outer_tab.outer_key IN (
  52.            SELECT inner_tab_1.key_to
  53.              FROM inner_tab_2
  54.             INNER JOIN inner_tab_1 ON (inner_tab_2.outer_key = inner_tab_1.key_from AND type = 2)
  55.             WHERE outer_tab.outer_key = inner_tab_1.key_to AND inner_tab_2.group_id = 9
  56.             );
  57. \echo Slow execution group id not in table
  58. \echo ===
  59. \echo
  60. --Fast because it can drive of an index scan of inner_tab_1
  61. SELECT outer_tab.outer_key
  62.   FROM outer_tab
  63.  WHERE outer_tab.outer_key IN (
  64.            SELECT inner_tab_1.key_to
  65.              FROM inner_tab_1
  66.             INNER JOIN inner_tab_2 ON (inner_tab_2.outer_key = inner_tab_1.key_from AND type = 2)
  67.             WHERE outer_tab.outer_key = inner_tab_1.key_to AND inner_tab_2.group_id = 9
  68.             );
  69. \echo Fast execution group id not in table but tables in from and join swapped
  70. \echo ===
  71. \echo
  72.  
  73. --Now fast execution because the group id is in inner_tab_2
  74. SELECT outer_tab.outer_key
  75.   FROM outer_tab
  76.  WHERE outer_tab.outer_key IN (
  77.            SELECT inner_tab_1.key_to
  78.              FROM inner_tab_2
  79.             INNER JOIN inner_tab_1 ON (inner_tab_2.outer_key = inner_tab_1.key_from AND type = 2)
  80.             WHERE outer_tab.outer_key = inner_tab_1.key_to AND inner_tab_2.group_id = 1
  81.             );
  82. \echo Fast execution group id in table
  83. \echo ===
  84. \echo
  85. \timing
Advertisement
Add Comment
Please, Sign In to add comment