4
4
{%- set full_relation = ' "' ~ relation .schema ~ ' "."' ~ relation .identifier ~ ' "' - %}
5
5
use [{{ relation.database }}];
6
6
if EXISTS (
7
- SELECT * FROM
8
- sys .indexes WHERE name = ' {{cci_name}}'
7
+ SELECT *
8
+ FROM sys .indexes with (nolock )
9
+ WHERE name = ' {{cci_name}}'
9
10
AND object_id = object_id (' {{relation_name}}' )
10
11
)
11
12
DROP index {{full_relation}}.{{cci_name}}
32
33
declare @drop_xml_indexes nvarchar(max);
33
34
select @drop_xml_indexes = (
34
35
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]) + ' ]; '
35
- from sys .indexes
36
- inner join sys .tables on sys .indexes .object_id = sys .tables .object_id
36
+ from sys .indexes with (nolock )
37
+ inner join sys .tables with (nolock )
38
+ on sys .indexes .object_id = sys .tables .object_id
37
39
where sys .indexes .[name] is not null
38
40
and sys .indexes .type_desc = ' XML'
39
41
and sys .tables .[name] = ' {{ this.table }}'
@@ -52,8 +54,9 @@ select @drop_xml_indexes = (
52
54
declare @drop_spatial_indexes nvarchar (max );
53
55
select @drop_spatial_indexes = (
54
56
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]) + ' ]; '
55
- from sys .indexes
56
- inner join sys .tables on sys .indexes .object_id = sys .tables .object_id
57
+ from sys .indexes with (nolock )
58
+ inner join sys .tables with (nolock )
59
+ on sys .indexes .object_id = sys .tables .object_id
57
60
where sys .indexes .[name] is not null
58
61
and sys .indexes .type_desc = ' Spatial'
59
62
and sys .tables .[name] = ' {{ this.table }}'
@@ -116,8 +119,9 @@ select @drop_pk_constraints = (
116
119
declare @drop_remaining_indexes_last nvarchar (max );
117
120
select @drop_remaining_indexes_last = (
118
121
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]) + ' ]; '
119
- from sys .indexes
120
- inner join sys .tables on sys .indexes .object_id = sys .tables .object_id
122
+ from sys .indexes with (nolock )
123
+ inner join sys .tables with (nolock )
124
+ on sys .indexes .object_id = sys .tables .object_id
121
125
where sys .indexes .[name] is not null
122
126
and sys .tables .[name] = ' {{ this.table }}'
123
127
for xml path (' ' )
@@ -132,10 +136,10 @@ select @drop_remaining_indexes_last = (
132
136
133
137
{% set idx_name = this .table + ' __clustered_index_on_' + columns|join (' _' ) %}
134
138
135
- if not exists (select * from sys . indexes
136
- where
137
- name = ' {{ idx_name }}' and
138
- object_id = OBJECT_ID (' {{ this }}' )
139
+ if not exists (select *
140
+ from sys . indexes with ( nolock )
141
+ where name = ' {{ idx_name }}'
142
+ and object_id = OBJECT_ID (' {{ this }}' )
139
143
)
140
144
begin
141
145
@@ -160,10 +164,10 @@ end
160
164
{% set idx_name = this .table + ' __index_on_' + columns|join (' _' )|replace (" " , " _" ) %}
161
165
{% endif %}
162
166
163
- if not exists (select * from sys . indexes
164
- where
165
- name = ' {{ idx_name }}' and
166
- object_id = OBJECT_ID (' {{ this }}' )
167
+ if not exists (select *
168
+ from sys . indexes with ( nolock )
169
+ where name = ' {{ idx_name }}'
170
+ and object_id = OBJECT_ID (' {{ this }}' )
167
171
)
168
172
begin
169
173
create nonclustered index
0 commit comments