SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Table Structure


Table Structure

Author
Message
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5446 Visits: 2768
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/
ALZDBA
ALZDBA
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30721 Visits: 8986
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


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- 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 :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5446 Visits: 2768
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/
GilaMonster
GilaMonster
SSC Guru
SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)

Group: General Forum Members
Points: 231431 Visits: 46350
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, MVP, M.Sc (Comp Sci)
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


kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5446 Visits: 2768
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/
GilaMonster
GilaMonster
SSC Guru
SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)

Group: General Forum Members
Points: 231431 Visits: 46350
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, MVP, M.Sc (Comp Sci)
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


kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5446 Visits: 2768
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/
GilaMonster
GilaMonster
SSC Guru
SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)SSC Guru (231K reputation)

Group: General Forum Members
Points: 231431 Visits: 46350
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, MVP, M.Sc (Comp Sci)
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search