Can I have more than one index on a database table? - sql-server

I have an existing Index on a database table, and I am new to Indexing.
I want to place a Clustered Index on a UniqueID column (the auto-increment column) as there is a lot of data.
Is it okay to have a second index on the table?
Existing Index is on a One To Many Value:
USE [Archive]
GO
/****** Object: Index [IX_CustomerWidget] Script Date: 07/22/2014 16:27:32 ******/
CREATE NONCLUSTERED INDEX [IX_CustomerWidget] ON [dbo].[CustomerWidget]
(
[WidgetID] 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]
GO
Also is there any harm in adding an Index - meaning if it is not right it can be dropped with no harm to the data?

Yes. It is perfectly normal to have a clustered index (the order the data is on disk) and non-clustered indexes to support the queries that are executed against the database, or to enforce unquieness of a column.
For example I might have clustered index on the IdentityID, and a non-clustered index on Date because all my queries look like this: Where Date between #Start and #End.

Related

Add non-null unique constraint - not index

Basically I want to turn this:
ALTER TABLE [dbo].[Computer] ADD CONSTRAINT [AK_ResourceId] UNIQUE NONCLUSTERED
(
[ResourceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
Into not affecting NULL values. The other posts only give an answer to make a unique index, but an index does not create violation when a duplicate value occurs on INSERT. I want a constraint, but I can't seem to make a WHERE statement on this..
Using SQL Server 2014
Update
It does work with an index after all :)
SQL:
CREATE UNIQUE NONCLUSTERED INDEX IX_ResourceId
ON dbo.Computer(ResourceId)
WHERE ResourceId IS NOT NULL;
This will create the following error:
Cannot insert duplicate key row in object 'dbo.Computer' with unique index 'IX_ResourceId'. The duplicate key value is (1234)

SQL Server Filtering Index performance

Currently have an index like;
CREATE UNIQUE NONCLUSTERED INDEX [CDPAYAPP_INDEX03] ON [dbo].[CDPAYAPP]
(
[CLSVC_ID] ASC,
[ID] 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, FILLFACTOR = 90)
Unfortunately, at some of our clients the value for CLSVC_ID can be zero for 100k or more of rows out of a couple million total rows. This cardinality appears to cause the optimizer to occasionally consider the index less than optimal, resulting in table scans. Updating stats multiple times a day can help but not always.
I tried to apply a FILTERING INDEX clause, like;
create UNIQUE NONCLUSTERED index CDPAYAPP_INDEX3A ON CDPAYAPP (CLSVC_ID, ID)
WHERE CLSVC_ID > 0;
But noticed that if I requested any column outside the two index columns it uses the original index not the filtered index. If I only select the columns of the index, it uses the filtered index.
Why?

sql: created index does not have my flags when I check the script in the database

In my database project I have an index:
CREATE NONCLUSTERED INDEX [index_name] ON [schema].[tablename]
(
[Result] ASC,
[Date] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = ON, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90, MAXDOP =1)
GO
If I run this script and after that, I go to table, right click on the index and select "Script index as", I don't see MAXDOP flag and ONLINE flag is set to off.
Why is that?
Because those are not features of the index, per se. They change the operation that builds the index but once the index is built, it's no different to an index that was built offline or with a different maxdop value. Or to put it shorter, they're build settings, not index settings.
And when someone makes changes to the index and thus forces a rebuild, they're free to choose whatever settings make sense at that time for the next build. So it's not worth storing them.

Why FREETEXTTABLE scan entire table if there is a fulltext index available?

There is a simple table of cities
CREATE TABLE [dbo].[cities](
[id] [numeric](6, 0) NOT NULL,
[cityname] [varchar](48) NOT NULL,
)
and a full-text catalog which index not only names of cities:
The table has 6259 rows.
I have a query:
SELECT * FROM FREETEXTTABLE([cities],[cityname],
N'Hello, I am looking for cities inside this text,
my house is not -far from London PJ-')
The results contains 11 rows and the query execution takes about 1-2 seconds.
According to execution plan, 6092 rows were scanned in the index.
Why so many rows are scanned if there is index?
I Use SQL Server 2012.
UPDATE:
There is no special index on table cities. The only index from object explorer looks like:
Create TABLE [dbo].[cities] ADD CONSTRAINT [xpkruianobec] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

Filtered non-clustered Index abnormal behaivior

I have a table that created some filtered non-clustered on some columns such:
CREATE UNIQUE NONCLUSTERED INDEX [IX_Sh_Esh] ON [dbo].[My_Tbl]
(
[City_Code] ASC,
[Sh_Esh] ASC
)
WHERE ([Sh_Bod]=(0) AND [Noe_Fa]=(0))
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
and:
CREATE UNIQUE NONCLUSTERED INDEX [IX_Kho] ON [dbo].[My_Tbl]
(
[City_Code] ASC,
[Kho] ASC
)
WHERE ([Sh_Bod]=(0) AND [Sh_Esh]=(0) AND [Noe_Fa]=(1))
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
I create this indexes on my table with no error but when I want to add a new column I get this error:
'My_Tbl' table
- Unable to create index 'IX_Sh_Esh'.
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'My_Tbl' and the index name 'IX_Sh_Esh'. The duplicate key value is (3, 0).
The statement has been terminated.
my table data is:
According to first Index because the rows No. 1 and No. 4 does not satisfy the where cluase should not create index on them.Why I get the above error?
thanks
EDIT 1) :
there is Interesting point.If I delete that index and add the Column(s) then ReCreate that Index,the index create with no error.STRANGE!!!!
The reason you can drop the index and then add the data, then recreate the index is because creating an index does not check existing data, just data that you are trying to insert/update.
You're not getting the error because its a filtered index, you're getting it because its a unique index and you're trying to add duplicate values to the table's column that the unique index is on. That's the beaty of them! If you need duplicate data don't make the index unique.

Resources