Skip to content

Commit 69b4189

Browse files
authored
Merge pull request #379 from dbt-msft/index-no-lock
add nolock to sys.indexes calls
2 parents 9d34bb2 + 8751610 commit 69b4189

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

dbt/include/sqlserver/macros/adapters/indexes.sql

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
{%- set full_relation = '"' ~ relation.schema ~ '"."' ~ relation.identifier ~ '"' -%}
55
use [{{ relation.database }}];
66
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}}'
910
AND object_id=object_id('{{relation_name}}')
1011
)
1112
DROP index {{full_relation}}.{{cci_name}}
@@ -32,8 +33,9 @@
3233
declare @drop_xml_indexes nvarchar(max);
3334
select @drop_xml_indexes = (
3435
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
3739
where sys.indexes.[name] is not null
3840
and sys.indexes.type_desc = 'XML'
3941
and sys.tables.[name] = '{{ this.table }}'
@@ -52,8 +54,9 @@ select @drop_xml_indexes = (
5254
declare @drop_spatial_indexes nvarchar(max);
5355
select @drop_spatial_indexes = (
5456
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
5760
where sys.indexes.[name] is not null
5861
and sys.indexes.type_desc = 'Spatial'
5962
and sys.tables.[name] = '{{ this.table }}'
@@ -116,8 +119,9 @@ select @drop_pk_constraints = (
116119
declare @drop_remaining_indexes_last nvarchar(max);
117120
select @drop_remaining_indexes_last = (
118121
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
121125
where sys.indexes.[name] is not null
122126
and sys.tables.[name] = '{{ this.table }}'
123127
for xml path('')
@@ -132,10 +136,10 @@ select @drop_remaining_indexes_last = (
132136

133137
{% set idx_name = this.table + '__clustered_index_on_' + columns|join('_') %}
134138

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 }}')
139143
)
140144
begin
141145

@@ -160,10 +164,10 @@ end
160164
{% set idx_name = this.table + '__index_on_' + columns|join('_')|replace(" ", "_") %}
161165
{% endif %}
162166

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 }}')
167171
)
168172
begin
169173
create nonclustered index

dbt/include/sqlserver/macros/adapters/relation.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}'
4646
IF EXISTS(
4747
SELECT *
48-
FROM sys.indexes
48+
FROM sys.indexes with (nolock)
4949
WHERE name='{{ from_relation.schema }}_{{ from_relation.identifier }}_cci' and object_id = OBJECT_ID('{{ from_relation.schema }}.{{ to_relation.identifier }}'))
5050
EXEC sp_rename N'{{ from_relation.schema }}.{{ to_relation.identifier }}.{{ from_relation.schema }}_{{ from_relation.identifier }}_cci', N'{{ from_relation.schema }}_{{ to_relation.identifier }}_cci', N'INDEX'
5151
{%- endcall %}

0 commit comments

Comments
 (0)