-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTAP Demo.sql
More file actions
140 lines (126 loc) · 4.45 KB
/
HTAP Demo.sql
File metadata and controls
140 lines (126 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
USE tempdb;
GO
CREATE TABLE orders
(
AccountKey INT NOT NULL ,
customername NVARCHAR(50) ,
OrderNumber BIGINT ,
PurchasePrice DECIMAL(9, 2) ,
OrderStatus SMALLINT NOT NULL ,
OrderStatusDesc NVARCHAR(50)
);
CREATE CLUSTERED INDEX orders_ci
ON orders ( OrderStatus );
-- insert into the main table load 3 million rows
DECLARE @outerloop INT = 0;
DECLARE @i INT = 0;
DECLARE @purchaseprice DECIMAL(9, 2);
DECLARE @customername NVARCHAR(50);
DECLARE @accountkey INT;
DECLARE @orderstatus SMALLINT;
DECLARE @orderstatusdesc NVARCHAR(50);
DECLARE @ordernumber BIGINT;
WHILE ( @outerloop < 3000000 )
BEGIN
SELECT @i = 0;
BEGIN TRAN;
WHILE ( @i < 2000 )
BEGIN
SET @ordernumber = @outerloop + @i;
SET @purchaseprice = RAND() * 1000.0;
SET @accountkey = CONVERT(INT, RAND() * 1000);
SET @orderstatus = CONVERT(SMALLINT, RAND() * 100);
IF ( @orderstatus >= 5 )
SET @orderstatus = 5;
SET @orderstatusdesc = CASE @orderstatus
WHEN 0 THEN 'Order Started'
WHEN 1 THEN 'Order Closed'
WHEN 2 THEN 'Order Paid'
WHEN 3 THEN 'Order Fullfillment'
WHEN 4 THEN 'Order Shipped'
WHEN 5 THEN 'Order Received'
END;
INSERT orders
VALUES ( @accountkey ,
( CONVERT(VARCHAR(6), @accountkey) + 'firstname' ) ,
@ordernumber ,@purchaseprice, @orderstatus ,
@orderstatusdesc );
SET @i += 1;
END;
COMMIT;
SET @outerloop = @outerloop + 2000;
SET @i = 0;
END;
GO
--create NCCI
CREATE NONCLUSTERED COLUMNSTORE INDEX orders_ncci
ON orders
(
AccountKey ,
customername ,
PurchasePrice ,
OrderStatus ,
OrderStatusDesc );
--insert additional 200k rows
DECLARE @outerloop INT = 3000000;
DECLARE @i INT = 0;
DECLARE @purchaseprice DECIMAL(9, 2);
DECLARE @customername NVARCHAR(50);
DECLARE @accountkey INT;
DECLARE @orderstatus SMALLINT;
DECLARE @orderstatusdesc NVARCHAR(50);
DECLARE @ordernumber BIGINT;
WHILE ( @outerloop < 3200000 )
BEGIN
SELECT @i = 0;
BEGIN TRAN;
WHILE ( @i < 2000 )
BEGIN
SET @ordernumber = @outerloop + @i;
SET @purchaseprice = RAND() * 1000.0;
SET @accountkey = CONVERT(INT, RAND() * 1000);
SET @orderstatus = CONVERT(SMALLINT, RAND() * 5);
SET @orderstatusdesc = CASE @orderstatus
WHEN 0 THEN 'Order Started'
WHEN 1 THEN 'Order Closed'
WHEN 2 THEN 'Order Paid'
WHEN 3 THEN 'Order Fullfillment'
WHEN 4 THEN 'Order Shipped'
WHEN 5 THEN 'Order Received'
END;
INSERT INTO orders
VALUES ( @accountkey ,
( CONVERT(VARCHAR(6), @accountkey) + 'firstname' ) ,
@ordernumber ,@purchaseprice, @orderstatus ,
@orderstatusdesc );
SET @i += 1;
END;
COMMIT;
SET @outerloop = @outerloop + 2000;
SET @i = 0;
END;
GO
GO
--run simple query
SELECT MAX(PurchasePrice)
FROM orders;
-- run the query without using NCCI
SELECT MAX(PurchasePrice)
FROM orders
option (IGNORE_NONCLUSTERED_COLUMNSTORE_INDEX);
-- a more complex query
SELECT TOP 5 customername ,
SUM(PurchasePrice) ,
AVG(PurchasePrice)
FROM orders
WHERE PurchasePrice > 90.0
AND OrderStatus = 5
GROUP BY customername;
SELECT TOP 5 customername ,
SUM(PurchasePrice) ,
AVG(PurchasePrice)
FROM orders
WHERE PurchasePrice > 90.0
AND OrderStatus = 5
GROUP BY customername
option (IGNORE_NONCLUSTERED_COLUMNSTORE_INDEX);