Advertisement
Guest User

Untitled

a guest
Mar 12th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.50 KB | None | 0 0
  1. CREATE TABLE public.test (
  2.   id SERIAL,
  3.   article INTEGER NOT NULL,
  4.   dealer INTEGER NOT NULL,
  5.   price NUMERIC(12,2) NOT NULL,
  6.   PRIMARY KEY(id)
  7. ) ;
  8.  
  9.  
  10. CREATE OR REPLACE FUNCTION public.test_filler ()
  11. RETURNS void AS
  12. $body$
  13. DECLARE
  14.   i INTEGER = 0;
  15. BEGIN
  16.     WHILE i < 1000000
  17.     LOOP
  18.         INSERT INTO test (article, dealer, price) VALUES (CEIL(RANDOM() * 9999), CEIL(RANDOM() * 999), RANDOM() * 9999);
  19.         i = i + 1;
  20.     END LOOP;
  21. END;
  22. $body$
  23. LANGUAGE 'plpgsql'
  24. VOLATILE
  25. CALLED ON NULL INPUT
  26. SECURITY INVOKER
  27. COST 100;
  28.  
  29.  
  30. CREATE INDEX test_article_idx ON public.test
  31.   USING btree (article);
  32.  
  33.  
  34. SELECT test_filler()
  35.  
  36.  
  37. /**********************************************************************/
  38.  
  39. SELECT article, dealer, price
  40. FROM   test s1
  41. WHERE  price=(SELECT MAX(s2.price)
  42.     FROM test s2
  43.     WHERE s1.article = s2.article);
  44. -- 9999 rows returned (execution time: 00:01:23; total time: 00:01:23)
  45.  
  46.  
  47. CREATE INDEX test_article_price_idx ON public.test
  48.   USING btree (article, price);
  49.  
  50.  
  51. SELECT s1.article, dealer, s1.price
  52. FROM test s1
  53. JOIN (
  54.     SELECT article, MAX(price) AS price
  55.     FROM test
  56.     GROUP BY article
  57. ) AS s2 ON s1.article = s2.article AND s1.price = s2.price;
  58. -- 9999 rows returned (execution time: 375 ms; total time: 375 ms)
  59.  
  60.  
  61. SELECT s1.article, s1.dealer, s1.price
  62. FROM test s1
  63. LEFT JOIN test s2 ON s1.article = s2.article AND s1.price < s2.price
  64. WHERE s2.article IS NULL;
  65. -- 9999 rows returned (execution time: 1,859 sec; total time: 1,875 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement