Issue with re-create index/constraint after dropping it - sql

I am using SQL Server 2008 R2.
I encountered this issue when re-create index.
As I need to alter column, so I drop constraint/index first and create back my constraint/index.
However, it shows error message saying
The operation failed because an index or statistics with name 'ABC' already exists on table 'test_table'
I wonder why would this error message shown? Since I have drop constraint
I wrote this to drop index
DROP INDEX [ABC] ON [dbo].[test_table] WITH ( ONLINE = OFF )
I then re-create index
CREATE NONCLUSTERED INDEX [ABC] ON [dbo].[test_table]
(
[col_1] ASC,
[col_2] ASC,
[col_3] ASC
)
INCLUDE ( [col_4],
[col_5]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
Anyone has any idea what's wrong here?

Related

Why is my filtered index not being used for one statement?

I have the following table (an excerpt follows w/o other columns):
USE [opg-systems-dev]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Orders]
(
[SyncChannelEngineOrder] [bit] NOT NULL,
[IsSyncing] [bit] NOT NULL,
CONSTRAINT [PK_Orders]
PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Orders]
ADD DEFAULT (CONVERT([bit], (0))) FOR [IsSyncing]
GO
-- -----
CREATE NONCLUSTERED INDEX [IX_Orders_SyncChannelEngineOrder]
ON [dbo].[Orders] ([SyncChannelEngineOrder] ASC)
WHERE ([SyncChannelEngineOrder] <> (0))
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_Orders_IsSyncing]
ON [dbo].[Orders] ([IsSyncing] ASC)
WHERE ([IsSyncing] <> (0))
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
I'm trying to use a filtered index here - but it seems when I'm querying that the the index for SyncChannelEngineOrder is not being used for some reason.
The setup is supposed to be the same for both columns.
The query is requesting all columns with a select *. The indexes only contain one column (ignoring the clustered key). To provide the missing column data that is not on the index, the query optimizer is calculating a lower cost to just scan all of the rows in the source table.
If SQL Server chose to use the existing indexes, it would first scan those rows, and then it would perform a bookmark lookup to get the rest of the columns from the table. If there are very few rows in the filtered index, and the table is very large, the SQL Server query optimizer could theoretically calculate a low enough cost to use the filtered index to limit the number of rows. Otherwise, bookmark lookups are expensive on a large number of rows. I am guessing the number of rows in the index is over this threshold.
SQL Server will add the clustered key to a non-clustered index to facilitate the bookmark lookup. If the query were to request only the clustered key and the index filter columns, the indexes would cover the query, and I expect the query optimizer would choose to use the filtered indexes.

What is the function of this query statement of sql

USE [DataStore]
GO
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [idx_alf_cass_qnln] ON [dbo].[alf_child_assoc]
(
[parent_node_id] ASC,
[qname_ns_id] ASC,
[qname_localname] ASC,
[qname_crc] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
It creates an nonclustered index.
Generally, nonclustered indexes are created to improve the performance of frequently used queries not covered by the clustered index or to locate rows in a table without a clustered index (called a heap). You can create multiple nonclustered indexes on a table or indexed view.

Non Clustered Index on UniqueIdentifier columns

WE have a simple SQL query selecting two columns like below
SELECT TEAMID,iSvALID FROM teams where teamid='{{guid}}' and subid = {{guid}}.
we had issues with performance of this query as there was no index which I also have added now... like
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20170725-191322] ON [dbo].[TableName]
(
[subid ] ASC,
[TeamId] ASC,
[Date] ASC
)
INCLUDE ([IsVAlid])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
The problem now is that it takes a few Milliseconds sometimes to execute the query but a few seconds!!! some times .
And because of some constraints we can't change the type of GUID column and also we get this from an exernal system.
Is there a way we can still make sure performance will be good

Index creation job failing because of quotation

I have a job that is part of my staging process and it includes indexing a table post population.
One of the indexes is a filtered index:
CREATE NONCLUSTERED INDEX [IDX_IP_ActivePAss] ON [dbo].[IPStg]
(
[SIP] ASC,
[EIP] ASC
)
WHERE ([Status] IN ("Active", "Private"))
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
GO
The job fails with the following error:
CREATE INDEX failed because the following SET options have incorrect
settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct
for use with indexed views and/or indexes on computed columns and/or
filtered indexes and/or query notifications and/or XML data type
methods and/or spatial index operations. [SQLSTATE 42000] (Error
1934). The step failed.
Please advise.
I would expect to see single quotes not double. I think its a typo.
CREATE NONCLUSTERED INDEX [IDX_IP_ActivePAss] ON [dbo].[IPStg] (
[SIP] ASC,
[EIP] ASC
) WHERE ( [Status] IN ('Active', 'Private' )
) WITH ( PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY] GO

SQL stored procedure drop and create indexes

I need to truncate a table to refresh data, however this table has an indexed view depends on it. I'm trying to drop indexes, then truncate table, then recreate indexes. I get an error that the non-clustered indexes don't exist or I don't have permission to drop them... code lives inside a stored procedure shown below. I am assuming it has something to do with the order of execution in the stored procedure as executing the code (drop/truncate/create) manually works.
DROP INDEX [IDX_VDetailEmergency] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_facility_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_mode_of_separation_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_period_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
truncate table DetailEmergency
CREATE UNIQUE CLUSTERED INDEX [IDX_VDetailEmergency] ON [dbo].[vFactEmergencySummary]
(
[facility_refno] ASC,
[period_refno] ASC,
[mode_of_separation_refno] ASC,
[ed_visit_type_refno] ASC,
[triage_category] ASC,
[UDG_refno] ASC,
[URG_refno] ASC,
[URG_MDB_refno] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_facility_refno] ON [dbo].[vFactEmergencySummary]
(
[facility_refno] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_mode_of_separation_refno] ON [dbo].[vFactEmergencySummary]
(
mode_of_separation_refno ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_period_refno] ON [dbo].[vFactEmergencySummary]
(
period_refno ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
You may follow these steps with SQL Server Management Studio if you are okay with dropping and recreating the table :
Right click on the database in which your table is available
Click Tasks -> Generate scripts
Press next to navigate to Choose Objects
Select specific database objects
Select your table
Press Advanced
Find Script DROP and CREATE in the list and change the drop down list as Script DROP and CREATE
Scroll down if you want to include Indexes or Constraints etc. in your script
Press OK and choose Save to new query window to see your script on a new query window
Press Next and Finish
Happy administrating!