Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Table Structure Expand / Collapse
Author
Message
Posted Tuesday, December 24, 2013 12:23 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,891, Visits: 2,329
Hi all,

I have tables with structures like this:
/****** Object:  Table [dbo].[ServiceCallJobPhoto]    Script Date: 12/24/2013 12:48:50 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ServiceCallJobPhoto](
[ServiceCallJobPhotoId] [int] IDENTITY(1,1) NOT NULL,
[InstanceId] [nchar](3) NOT NULL,
[SiteId] [nchar](3) NOT NULL,
[ServiceCallJobId] [int] NULL,
[WorkDate] [datetime] NOT NULL,
[MechanicId] [int] NOT NULL,
[PhotoDescription] [nvarchar](75) NOT NULL,
[PhotoData] [nvarchar](max) NOT NULL,
[StatusFlag] [bit] NOT NULL,
[DateAdded] [datetime] NOT NULL,
[AddedBy] [nvarchar](75) NOT NULL,
[DateChanged] [datetime] NULL,
[ChangedBy] [nvarchar](75) NULL,
[LocalDateChanged] [datetime] NOT NULL,
[LocalChangedBy] [nvarchar](75) NOT NULL,
[SyncTimeStamp] [timestamp] NOT NULL,
CONSTRAINT [PK_ServiceCallJobPhoto] PRIMARY KEY CLUSTERED
(
[ServiceCallJobPhotoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY],
CONSTRAINT [uq1_ServiceCallJobPhoto] UNIQUE NONCLUSTERED
(
[InstanceId] ASC,
[SiteId] ASC,
[ServiceCallJobId] ASC,
[MechanicId] ASC,
[WorkDate] ASC,
[PhotoDescription] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[ServiceCallJobPhoto] WITH CHECK ADD CONSTRAINT [FK_ServiceCallJobPhotoJobId] FOREIGN KEY([ServiceCallJobId])
REFERENCES [dbo].[ServiceCallJob] ([ServiceCallJobId])
GO

ALTER TABLE [dbo].[ServiceCallJobPhoto] CHECK CONSTRAINT [FK_ServiceCallJobPhotoJobId]
GO

ALTER TABLE [dbo].[ServiceCallJobPhoto] WITH CHECK ADD CONSTRAINT [FK_ServiceCallJobPhotoMechId] FOREIGN KEY([MechanicId])
REFERENCES [dbo].[Employee] ([EmployeeId])
GO

ALTER TABLE [dbo].[ServiceCallJobPhoto] CHECK CONSTRAINT [FK_ServiceCallJobPhotoMechId]
GO

As, the PK is created on an Identity Key column which is not going to be used in joins of stored prcedures.
Also, unique key is created on columns having datatype Datetime, nvarchar.
So, my concern is that as while performing joins clustered index will never be used and this leads to table scan
and also there may be performance impact due to column having datatype datetime used in keys.

What can be done to improve this?

Thanks in advance...



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1525694
Posted Tuesday, December 24, 2013 3:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:56 AM
Points: 7,010, Visits: 8,462
all NCI access that needs extra data will use the clustered index to fetch that data.


what columns do you expect to get used ?
Provide indexes for those columns and try to figure out if you can use included columns for index only access.


As a general guideline I suggest to provide FK indexes, unless proven they hurt your data systems performance.

As a status column datatyped bit, maybe a filtered index can reduce IO quite a bit depending on the distribution ratio .


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #1525718
Posted Tuesday, December 24, 2013 5:47 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,891, Visits: 2,329
ALZDBA (12/24/2013)
all NCI access that needs extra data will use the clustered index to fetch that data.


what columns do you expect to get used ?
Provide indexes for those columns and try to figure out if you can use included columns for index only access.


As a general guideline I suggest to provide FK indexes, unless proven they hurt your data systems performance.

As a status column datatyped bit, maybe a filtered index can reduce IO quite a bit depending on the distribution ratio .


Should I drop unique constraint and make non clustered index only or add more column in unique constraint instead of creating another nonclustered index?

Also columns included in unique constraint if appear in non clustered index then will it make effect?



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1525734
Posted Tuesday, December 24, 2013 7:21 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:19 AM
Points: 43,026, Visits: 36,192
kapil_kk (12/24/2013)
Should I drop unique constraint and make non clustered index only or add more column in unique constraint instead of creating another nonclustered index?


If the index is there to enforce uniqueness, then it needs to be there, as-is to enforce uniqueness. Otherwise you're reducing the integrity of the database by potentially allowing duplicates.

Have you done performance tests? Is the performance of queries using this table inadequate?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1525749
Posted Tuesday, December 24, 2013 11:27 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,891, Visits: 2,329
GilaMonster (12/24/2013)
kapil_kk (12/24/2013)
Should I drop unique constraint and make non clustered index only or add more column in unique constraint instead of creating another nonclustered index?


If the index is there to enforce uniqueness, then it needs to be there, as-is to enforce uniqueness. Otherwise you're reducing the integrity of the database by potentially allowing duplicates.

Have you done performance tests? Is the performance of queries using this table inadequate?

Yes, uniqueness are required.....

While running my stored procedure I see bookmark lookup so I thought to avoid that if I add my non clustered index columns to the unique non clustered only... due to that index also get reduced



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1525859
Posted Thursday, December 26, 2013 1:43 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:19 AM
Points: 43,026, Visits: 36,192
kapil_kk (12/24/2013)
GilaMonster (12/24/2013)
kapil_kk (12/24/2013)
Should I drop unique constraint and make non clustered index only or add more column in unique constraint instead of creating another nonclustered index?


If the index is there to enforce uniqueness, then it needs to be there, as-is to enforce uniqueness. Otherwise you're reducing the integrity of the database by potentially allowing duplicates.

Have you done performance tests? Is the performance of queries using this table inadequate?

Yes, uniqueness are required.....


Then you can't add more columns to the unique constraint

While running my stored procedure I see bookmark lookup so I thought to avoid that if I add my non clustered index columns to the unique non clustered only... due to that index also get reduced


So there are key lookups. Are they a problem? Is the query not meeting it's performance requirements?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1526042
Posted Friday, December 27, 2013 3:00 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,891, Visits: 2,329
GilaMonster (12/26/2013)
kapil_kk (12/24/2013)
GilaMonster (12/24/2013)
kapil_kk (12/24/2013)
Should I drop unique constraint and make non clustered index only or add more column in unique constraint instead of creating another nonclustered index?


If the index is there to enforce uniqueness, then it needs to be there, as-is to enforce uniqueness. Otherwise you're reducing the integrity of the database by potentially allowing duplicates.

Have you done performance tests? Is the performance of queries using this table inadequate?

Yes, uniqueness are required.....


Then you can't add more columns to the unique constraint

While running my stored procedure I see bookmark lookup so I thought to avoid that if I add my non clustered index columns to the unique non clustered only... due to that index also get reduced


So there are key lookups. Are they a problem? Is the query not meeting it's performance requirements?

But if I add few column of non clustered index to unique constraint columns then also uniqueness will be retained.... so in that case can I go with this



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1526102
Posted Friday, December 27, 2013 4:28 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:19 AM
Points: 43,026, Visits: 36,192
kapil_kk (12/27/2013)
But if I add few column of non clustered index to unique constraint columns then also uniqueness will be retained.... so in that case can I go with this


Yes, and you'll end up with a redundant index and additional write overhead.

Once more with feeling....
Have you done performance tests? Is the performance of queries using this table inadequate? Is there a problem here at all?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1526107
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse