Advertisement
stegus64

SQLug.se Challenge 2017 Combined solution

Apr 5th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.46 KB | None | 0 0
  1. /*
  2.  
  3. combine1
  4.  
  5. Stefan Gustafsson, Acando
  6.  
  7. Uses ideas borrowed from Mikael Eriksson and Daniel Hutmacher
  8. */
  9.  
  10. truncate table dbo.Customers
  11. set nocount on
  12.  
  13. declare @t0 datetime = getdate()
  14. declare @t1 datetime
  15. declare @ms int
  16. declare @rc int
  17.  
  18. if object_id('tempdb..#ColStoreTable') is not null DROP TABLE #ColStoreTable;
  19. CREATE TABLE #ColStoreTable (
  20.     col     tinyint NOT NULL
  21. );
  22. CREATE CLUSTERED COLUMNSTORE INDEX ColStoreIndex ON #ColStoreTable;
  23. INSERT INTO #ColStoreTable VALUES (0);
  24. ALTER INDEX ColStoreIndex ON #ColStoreTable REORGANIZE;
  25.  
  26.  
  27. -- Build edge list
  28. ;with
  29. p as (
  30.     select PurchaseID, FirstName, LastName, Email, StreetAddress, ZipCode, CreditCard
  31.     from dbo.PurchasesBig
  32. )
  33. , cte1 as (
  34.     select
  35.         PurchaseID,
  36.         Key1 = (Firstname+'/'+CreditCard) collate finnish_swedish_bin,
  37.         Key2 = (Firstname+'/'+Email) collate finnish_swedish_bin,
  38.         Key3 = (Firstname+'/'+LastName+'/'+StreetAddress+'/'+cast(ZipCode as varchar(10))) collate finnish_swedish_bin
  39.     from p
  40. )
  41. , k1 as (
  42.     select Key1, MIN(PurchaseID) as node
  43.     from cte1
  44.     group by Key1
  45. )
  46. , k2 as (
  47.     select Key2, MIN(PurchaseID) as node
  48.     from cte1
  49.     group by Key2
  50. )
  51. , k3 as (
  52.     select Key3, MIN(PurchaseID)as node
  53.     from cte1
  54.     group by Key3
  55. )
  56. ,cte2 as (
  57.     select
  58.         a.PurchaseID,
  59.         k1.node as n1,
  60.         k2.node as n2,
  61.         k3.node as n3
  62.     from cte1 a
  63.     inner hash join k1 on k1.Key1 = a.Key1
  64.     inner hash join k2 on k2.Key2 = a.Key2
  65.     inner hash join k3 on k3.Key3 = a.Key3
  66. )
  67. , cte3 as (
  68.     select
  69.         s = PurchaseID,
  70.         t = case
  71.                 when n1 < n2 then
  72.                     case when n1 < n3 then n1 else n3 end
  73.                 else
  74.                     case when n2 < n3 then n2 else n3 end
  75.             end
  76.     from cte2
  77. )
  78. insert into dbo.Customers with (tablockx)
  79.     (PurchaseID, CustomerID)
  80. select s,t
  81. from cte3
  82. LEFT JOIN #ColStoreTable ON 1=0
  83. option (hash group, hash join)
  84.  
  85. set @rc = @@rowcount
  86.  
  87. set @ms = datediff(ms, @t0, getdate())
  88. RAISERROR ('step1 duration: %d ms rc=%d',0,0,@ms,@rc) WITH NOWAIT
  89.  
  90. while @rc > 0
  91. begin
  92.     set @t1 = getdate()
  93.  
  94.     update c
  95.     set c.CustomerID = p.CustomerID
  96.     from dbo.Customers c
  97.     join dbo.Customers p on
  98.         p.PurchaseID <> p.CustomerID and
  99.         c.CustomerID = p.PurchaseID
  100.  
  101.     set @rc = @@rowcount
  102.  
  103.     set @ms = datediff(ms, @t1, getdate())
  104.     RAISERROR ('After update: %d ms rc=%d',0,0,@ms, @rc) WITH NOWAIT
  105.  
  106. end
  107.  
  108. set @ms = datediff(ms, @t0, getdate())
  109. RAISERROR ('total duration: %d ms',0,0,@ms) WITH NOWAIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement