-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path07-demo-cluster-index-01-setup.sql
executable file
·194 lines (170 loc) · 9.28 KB
/
07-demo-cluster-index-01-setup.sql
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* source 07-demo-cluster-index-01-setup.sql */
/* Setup the schema for CLUSTERED vs. NONCLUSTERED primary keys */
/* Table t1: Clustered */
DROP TABLE IF EXISTS test.auto_random_t1_clustered;
CREATE TABLE test.auto_random_t1_clustered (
id BIGINT PRIMARY KEY AUTO_RANDOM,
id2 BIGINT,
name CHAR(255),
varname VARCHAR(200));
/* Table t2: Non-Clustered */
DROP TABLE IF EXISTS test.t2_nonclustered;
CREATE TABLE test.t2_nonclustered (
id BIGINT PRIMARY KEY NONCLUSTERED,
id2 BIGINT,
name CHAR(255),
varname CHAR(200));
/* Table t3: Non-Clustered */
DROP TABLE IF EXISTS test.t3_nonclustered;
CREATE TABLE test.t3_nonclustered (
id VARCHAR(32) PRIMARY KEY,
id2 BIGINT,
name CHAR(255),
varname CHAR(200));
/* Table t4: Clustered */
DROP TABLE IF EXISTS test.t4_clustered;
CREATE TABLE test.t4_clustered (
id VARCHAR(32) PRIMARY KEY CLUSTERED,
id2 BIGINT,
name CHAR(255),
varname CHAR(200));
/* Table t5: Clustered with no primary key */
DROP TABLE IF EXISTS test.t5_nonclustered;
CREATE TABLE test.t5_nonclustered (
id BIGINT,
id2 BIGINT,
name CHAR(255),
varname CHAR(200));
/* Table t6: Non-Clustered Composite PK */
DROP TABLE IF EXISTS test.t6_nonclustered;
CREATE TABLE test.t6_nonclustered (
id BIGINT,
id2 BIGINT,
name CHAR(255),
varname CHAR(200),
PRIMARY KEY (id, id2));
/* Table t7: Clustered Composite PK */
DROP TABLE IF EXISTS test.t7_clustered;
CREATE TABLE test.t7_clustered (
id BIGINT,
id2 BIGINT,
name CHAR(255),
varname CHAR(200),
PRIMARY KEY (id, id2) CLUSTERED);
/* Populate Seed */
INSERT INTO test.auto_random_t1_clustered (name, varname) VALUES ('A','V1'), ('B','V1'), ('C','V2'), ('D','V2');
UPDATE test.auto_random_t1_clustered SET id2=id;
/* Flooding with data 2M rows to t1 */
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
INSERT INTO test.auto_random_t1_clustered select null, id2, name, varname from test.auto_random_t1_clustered;
/* Flooding with data 2M rows t0 t2 */
INSERT INTO test.t2_nonclustered select * from test.auto_random_t1_clustered where name = 'A';
INSERT INTO test.t2_nonclustered select * from test.auto_random_t1_clustered where name = 'B';
INSERT INTO test.t2_nonclustered select * from test.auto_random_t1_clustered where name = 'C';
INSERT INTO test.t2_nonclustered select * from test.auto_random_t1_clustered where name = 'D';
/* Flooding with data 2M rows to t3 */
INSERT INTO test.t3_nonclustered select * from test.auto_random_t1_clustered where name = 'A';
INSERT INTO test.t3_nonclustered select * from test.auto_random_t1_clustered where name = 'B';
INSERT INTO test.t3_nonclustered select * from test.auto_random_t1_clustered where name = 'C';
INSERT INTO test.t3_nonclustered select * from test.auto_random_t1_clustered where name = 'D';
/* Flooding with data 2M rows to t4 */
INSERT INTO test.t4_clustered select * from test.auto_random_t1_clustered where name = 'A';
INSERT INTO test.t4_clustered select * from test.auto_random_t1_clustered where name = 'B';
INSERT INTO test.t4_clustered select * from test.auto_random_t1_clustered where name = 'C';
INSERT INTO test.t4_clustered select * from test.auto_random_t1_clustered where name = 'D';
/* Flooding with data 2M rows to t5 */
INSERT INTO test.t5_nonclustered select * from test.auto_random_t1_clustered where name = 'A';
INSERT INTO test.t5_nonclustered select * from test.auto_random_t1_clustered where name = 'B';
INSERT INTO test.t5_nonclustered select * from test.auto_random_t1_clustered where name = 'C';
INSERT INTO test.t5_nonclustered select * from test.auto_random_t1_clustered where name = 'D';
/* Flooding with data 2M rows to t6_nonclustered */
INSERT INTO test.t6_nonclustered select * from test.auto_random_t1_clustered where name = 'A';
INSERT INTO test.t6_nonclustered select * from test.auto_random_t1_clustered where name = 'B';
INSERT INTO test.t6_nonclustered select * from test.auto_random_t1_clustered where name = 'C';
INSERT INTO test.t6_nonclustered select * from test.auto_random_t1_clustered where name = 'D';
/* Flooding with data 2M rows to t7_clustered */
INSERT INTO test.t7_clustered select * from test.auto_random_t1_clustered where name = 'A';
INSERT INTO test.t7_clustered select * from test.auto_random_t1_clustered where name = 'B';
INSERT INTO test.t7_clustered select * from test.auto_random_t1_clustered where name = 'C';
INSERT INTO test.t7_clustered select * from test.auto_random_t1_clustered where name = 'D';
/* Greetings to CBO */
ANALYZE table test.auto_random_t1_clustered;
ANALYZE table test.t2_nonclustered;
ANALYZE table test.t3_nonclustered;
ANALYZE table test.t4_clustered;
ANALYZE table test.t5_nonclustered;
ANALYZE table test.t6_nonclustered;
ANALYZE table test.t7_clustered;
select 'Table test.auto_random_t1_clustered created/populated' as Result;
/* Data */
select count(distinct id), count(distinct name), count(*), min(id), max(id) from test.auto_random_t1_clustered;
/* Show information_schema for table */
select 'Table test.auto_random_t1_clustered' as Information_Schema;
SELECT tidb_row_id_sharding_info, tidb_pk_type
FROM information_schema.tables
WHERE table_name='auto_random_t1_clustered';
select 'Table test.t2_nonclustered created/populated' as Result;
/* Data */
select count(distinct id), count(distinct name), count(*), min(id), max(id) from test.t2_nonclustered;
/* Show information_schema for table */
select 'Table test.t2_nonclustered' as Information_Schema;
SELECT tidb_row_id_sharding_info, tidb_pk_type
FROM information_schema.tables
WHERE table_name='t2_nonclustered';
select 'Table test.t3_nonclustered created/populated' as Result;
/* Data */
select count(distinct id), count(distinct name), count(*), min(id), max(id) from test.t3_nonclustered;
/* Show information_schema for table */
select 'Table test.t3_nonclustered' as Information_Schema;
SELECT tidb_row_id_sharding_info, tidb_pk_type
FROM information_schema.tables
WHERE table_name='t3_nonclustered';
select 'Table test.t4_clustered created/populated' as Result;
/* Data */
select count(distinct id), count(distinct name), count(*), min(id), max(id) from test.t4_clustered;
/* Show information_schema for table */
select 'Table test.t4_clustered' as Information_Schema;
SELECT tidb_row_id_sharding_info, tidb_pk_type
FROM information_schema.tables
WHERE table_name='t4_clustered';
select 'Table test.t5_nonclustered created/populated' as Result;
/* Data */
select count(distinct id), count(distinct name), count(*), min(id), max(id) from test.t5_nonclustered;
/* Show information_schema for table */
select 'Table test.t5_nonclustered' as Information_Schema;
SELECT tidb_row_id_sharding_info, tidb_pk_type
FROM information_schema.tables
WHERE table_name='t5_nonclustered';
select 'Table test.t6_nonclustered created/populated' as Result;
/* Data */
select count(distinct id), count(distinct name), count(*), min(id), max(id) from test.t6_nonclustered;
/* Show information_schema for table */
select 'Table test.t6_nonclustered' as Information_Schema;
SELECT tidb_row_id_sharding_info, tidb_pk_type
FROM information_schema.tables
WHERE table_name='t6_nonclustered';
select 'Table test.t7_clustered created/populated' as Result;
/* Data */
select count(distinct id), count(distinct name), count(*), min(id), max(id) from test.t7_clustered;
/* Show information_schema for table */
select 'Table test.t7_clustered' as Information_Schema;
SELECT tidb_row_id_sharding_info, tidb_pk_type
FROM information_schema.tables
WHERE table_name='t7_clustered';