-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathindexes.sql
More file actions
188 lines (149 loc) · 7.46 KB
/
indexes.sql
File metadata and controls
188 lines (149 loc) · 7.46 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
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
{% macro sqlserver__create_clustered_columnstore_index(relation) -%}
{%- set cci_name = (relation.schema ~ '_' ~ relation.identifier ~ '_cci') | replace(".", "") | replace(" ", "") -%}
{%- set relation_name = relation.schema ~ '_' ~ relation.identifier -%}
{%- set full_relation = '"' ~ relation.schema ~ '"."' ~ relation.identifier ~ '"' -%}
use [{{ relation.database }}];
if EXISTS (
SELECT *
FROM sys.indexes {{ information_schema_hints() }}
WHERE name = '{{cci_name}}'
AND object_id=object_id('{{relation_name}}')
)
DROP index {{full_relation}}.{{cci_name}}
CREATE CLUSTERED COLUMNSTORE INDEX {{cci_name}}
ON {{full_relation}}
{% endmacro %}
{% macro drop_xml_indexes() -%}
{{ log("Running drop_xml_indexes() macro...") }}
declare @drop_xml_indexes nvarchar(max);
select @drop_xml_indexes = (
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
from sys.indexes {{ information_schema_hints() }}
inner join sys.tables {{ information_schema_hints() }}
on sys.indexes.object_id = sys.tables.object_id
where sys.indexes.[name] is not null
and sys.indexes.type_desc = 'XML'
and sys.tables.[name] = '{{ this.table }}'
for xml path('')
); exec sp_executesql @drop_xml_indexes;
{%- endmacro %}
{% macro drop_spatial_indexes() -%}
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
{# and https://stackoverflow.com/a/33785833/10415173 #}
{{ log("Running drop_spatial_indexes() macro...") }}
declare @drop_spatial_indexes nvarchar(max);
select @drop_spatial_indexes = (
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
from sys.indexes {{ information_schema_hints() }}
inner join sys.tables {{ information_schema_hints() }}
on sys.indexes.object_id = sys.tables.object_id
where sys.indexes.[name] is not null
and sys.indexes.type_desc = 'Spatial'
and sys.tables.[name] = '{{ this.table }}'
for xml path('')
); exec sp_executesql @drop_spatial_indexes;
{%- endmacro %}
{% macro drop_fk_constraints() -%}
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
{{ log("Running drop_fk_constraints() macro...") }}
declare @drop_fk_constraints nvarchar(max);
select @drop_fk_constraints = (
select 'IF OBJECT_ID(''' + SCHEMA_NAME(CONVERT(VARCHAR(MAX), sys.foreign_keys.[schema_id])) + '.' + sys.foreign_keys.[name] + ''', ''F'') IS NOT NULL ALTER TABLE [' + SCHEMA_NAME(sys.foreign_keys.[schema_id]) + '].[' + OBJECT_NAME(sys.foreign_keys.[parent_object_id]) + '] DROP CONSTRAINT [' + sys.foreign_keys.[name]+ '];'
from sys.foreign_keys
inner join sys.tables on sys.foreign_keys.[referenced_object_id] = sys.tables.[object_id]
where sys.tables.[name] = '{{ this.table }}'
for xml path('')
); exec sp_executesql @drop_fk_constraints;
{%- endmacro %}
{% macro drop_pk_constraints() -%}
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
{# and https://stackoverflow.com/a/33785833/10415173 #}
{{ drop_xml_indexes() }}
{{ drop_spatial_indexes() }}
{{ drop_fk_constraints() }}
{{ log("Running drop_pk_constraints() macro...") }}
declare @drop_pk_constraints nvarchar(max);
select @drop_pk_constraints = (
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL ALTER TABLE [' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + sys.tables.[name] + '] DROP CONSTRAINT [' + sys.indexes.[name]+ '];'
from sys.indexes
inner join sys.tables on sys.indexes.[object_id] = sys.tables.[object_id]
where sys.indexes.is_primary_key = 1
and sys.tables.[name] = '{{ this.table }}'
for xml path('')
); exec sp_executesql @drop_pk_constraints;
{%- endmacro %}
{% macro drop_all_indexes_on_table() -%}
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
{# and https://stackoverflow.com/a/33785833/10415173 #}
{{ drop_pk_constraints() }}
{{ log("Dropping remaining indexes...") }}
declare @drop_remaining_indexes_last nvarchar(max);
select @drop_remaining_indexes_last = (
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
from sys.indexes {{ information_schema_hints() }}
inner join sys.tables {{ information_schema_hints() }}
on sys.indexes.object_id = sys.tables.object_id
where sys.indexes.[name] is not null
and SCHEMA_NAME(sys.tables.schema_id) = '{{ this.schema }}'
and sys.tables.[name] = '{{ this.table }}'
for xml path('')
); exec sp_executesql @drop_remaining_indexes_last;
{%- endmacro %}
{% macro create_clustered_index(columns, unique=False) -%}
{{ log("Creating clustered index...") }}
{% set idx_name = "clustered_" + local_md5(columns | join("_")) %}
if not exists(select *
from sys.indexes {{ information_schema_hints() }}
where name = '{{ idx_name }}'
and object_id = OBJECT_ID('{{ this }}')
)
begin
create
{% if unique -%}
unique
{% endif %}
clustered index
{{ idx_name }}
on {{ this }} ({{ '[' + columns|join("], [") + ']' }})
end
{%- endmacro %}
{% macro create_nonclustered_index(columns, includes=False) %}
{{ log("Creating nonclustered index...") }}
{% if includes -%}
{% set idx_name = (
"nonclustered_"
+ local_md5(columns | join("_"))
+ "_incl_"
+ local_md5(includes | join("_"))
) %}
{% else -%}
{% set idx_name = "nonclustered_" + local_md5(columns | join("_")) %}
{% endif %}
if not exists(select *
from sys.indexes {{ information_schema_hints() }}
where name = '{{ idx_name }}'
and object_id = OBJECT_ID('{{ this }}')
)
begin
create nonclustered index
{{ idx_name }}
on {{ this }} ({{ '[' + columns|join("], [") + ']' }})
{% if includes -%}
include ({{ '[' + includes|join("], [") + ']' }})
{% endif %}
end
{% endmacro %}
{% macro sqlserver__get_create_index_sql(relation, index_dict) -%}
{%- set index_config = adapter.parse_index(index_dict) -%}
{%- set comma_separated_columns = ", ".join(index_config.columns) -%}
{%- set index_name = index_config.render(relation) -%}
{# Validations are made on the adapter class SQLServerIndexConfig to control resulting sql #}
create
{% if index_config.unique -%} unique {% endif %}{{ index_config.type }}
index "{{ index_name }}"
on {{ relation }}
({{ comma_separated_columns }})
{% if index_config.included_columns -%}
include ({{ ", ".join(index_config.included_columns) }})
{% endif %}
{%- endmacro %}