Showing posts with label keys. Show all posts
Showing posts with label keys. Show all posts

Tuesday, March 27, 2012

Do I need to verify that NewID() returns a unique GUID?

I'm migrating a web based system to SQL server. I'm planning on using the SQL server function NewID() to create unique keys for many of my records in many different tables. I'm just wondering if NewID() is guaranteed to return a value that does not already exist in my database. I mean obviously once you have a certain number of records (a hell of a lot) you'd be breaking the odds to never come up with a duplicate.

Do I need to make sure the result of NEWID() doesn't already exist?

Thanks

No this algorithm is guaranteed to always return a unique value. At least for a really, really long time,

Hope this helps,

Thursday, March 22, 2012

Do foreign keys generate implicit indexes?

If i create a simple table with a foreign key constraint, does it
create an implicit index on that given ID? I've been told this is
done in some databases, but i need to know for sure if SQL Server does
it. Has anyone heard of this before, on any other databses perhaps?

Heres an example of how the foreign key constraint is being added:

ALTER TABLE [dbo].[administrators] WITH CHECK ADD CONSTRAINT
[FPSLUFSUOXZGAJOJ] FOREIGN KEY([AdministratorRoleID])
REFERENCES [dbo].[administratorroles] ([AdministratorRoleID])

My initial testing seems to indicate adding an index on the foreign
key column helps, but i need to know for sure. Any insight would be
greatly appreciated!

Bobbobdurie@.gmail.com (bobdurie@.gmail.com) writes:

Quote:

Originally Posted by

If i create a simple table with a foreign key constraint, does it
create an implicit index on that given ID?


In SQL Server, no.

Quote:

Originally Posted by

I've been told this is done in some databases, but i need to know for
sure if SQL Server does it. Has anyone heard of this before, on any
other databses perhaps?


I seem to recall having heard this about Sybase Anywhere.

Quote:

Originally Posted by

Heres an example of how the foreign key constraint is being added:
>
ALTER TABLE [dbo].[administrators] WITH CHECK ADD CONSTRAINT
[FPSLUFSUOXZGAJOJ] FOREIGN KEY([AdministratorRoleID])
REFERENCES [dbo].[administratorroles] ([AdministratorRoleID])
>
My initial testing seems to indicate adding an index on the foreign
key column helps, but i need to know for sure. Any insight would be
greatly appreciated!


Indeed, it is often a good idea to add indexes on foreign keys, as it
can speed up deletions considerably. And it is not uncommon to search
for data in a table on a foreign key. However, as always, you should
think twice, and not add indexes blindly. For instance, if you have a
country-code column in a address table, there is little reason to add
an index on that column, since you don't delete countries very often.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||>I've been told this is done in some databases, .. <<

Yes, but better. Sybase SQL Anywhere (nee Watcom SQL) builds links
from all the FK references to the single PRIMARY KET/UNIQUE occurence
in the referenced table. Saves space, pre-joins tables for speed and
makes DRI actions both easy and fast.

SQL Server is still thinking in terms of "table = file" instead of
"table is part of a whole schema" and that "record =row" instead of
"row is made up of columns". Stonebreaker had a recent blog on column-
oriented design over contigous storage model.|||On Fri, 21 Sep 2007 19:51:14 -0000, "bobdurie@.gmail.com"
<bobdurie@.gmail.comwrote:

Microsoft Access does this, when you create a relationship between two
tables.
Check with sysindexes to see if SQL Server does this too.

-Tom.

Quote:

Originally Posted by

>If i create a simple table with a foreign key constraint, does it
>create an implicit index on that given ID? I've been told this is
>done in some databases, but i need to know for sure if SQL Server does
>it. Has anyone heard of this before, on any other databses perhaps?
>
>Heres an example of how the foreign key constraint is being added:
>
>ALTER TABLE [dbo].[administrators] WITH CHECK ADD CONSTRAINT
>[FPSLUFSUOXZGAJOJ] FOREIGN KEY([AdministratorRoleID])
>REFERENCES [dbo].[administratorroles] ([AdministratorRoleID])
>
>My initial testing seems to indicate adding an index on the foreign
>key column helps, but i need to know for sure. Any insight would be
>greatly appreciated!
>
>Bob

|||Check with sysindexes to see if SQL Server does this too.

As Erland mentioned, SQL Server does not automatically index foreign key
columns. That task is left to the discretion of the DBA, who might choose
not to index the foreign column(s) due to low cardinality and static data.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Tom van Stiphout" <no.spam.tom7744@.cox.netwrote in message
news:8qqbf3190hk4cis52502s9th2ebsjjfpd5@.4ax.com...

Quote:

Originally Posted by

On Fri, 21 Sep 2007 19:51:14 -0000, "bobdurie@.gmail.com"
<bobdurie@.gmail.comwrote:
>
Microsoft Access does this, when you create a relationship between two
tables.
Check with sysindexes to see if SQL Server does this too.
>
-Tom.
>
>

Quote:

Originally Posted by

>>If i create a simple table with a foreign key constraint, does it
>>create an implicit index on that given ID? I've been told this is
>>done in some databases, but i need to know for sure if SQL Server does
>>it. Has anyone heard of this before, on any other databses perhaps?
>>
>>Heres an example of how the foreign key constraint is being added:
>>
>>ALTER TABLE [dbo].[administrators] WITH CHECK ADD CONSTRAINT
>>[FPSLUFSUOXZGAJOJ] FOREIGN KEY([AdministratorRoleID])
>>REFERENCES [dbo].[administratorroles] ([AdministratorRoleID])
>>
>>My initial testing seems to indicate adding an index on the foreign
>>key column helps, but i need to know for sure. Any insight would be
>>greatly appreciated!
>>
>>Bob

|||Also, analyse your query requirements then apply an Indexing Strategy

--

Jack Vamvas
___________________________________
Need an IT job? http://www.ITjobfeed.com/SQL
<bobdurie@.gmail.comwrote in message
news:1190404274.471197.197240@.n39g2000hsh.googlegr oups.com...

Quote:

Originally Posted by

If i create a simple table with a foreign key constraint, does it
create an implicit index on that given ID? I've been told this is
done in some databases, but i need to know for sure if SQL Server does
it. Has anyone heard of this before, on any other databses perhaps?
>
Heres an example of how the foreign key constraint is being added:
>
ALTER TABLE [dbo].[administrators] WITH CHECK ADD CONSTRAINT
[FPSLUFSUOXZGAJOJ] FOREIGN KEY([AdministratorRoleID])
REFERENCES [dbo].[administratorroles] ([AdministratorRoleID])
>
My initial testing seems to indicate adding an index on the foreign
key column helps, but i need to know for sure. Any insight would be
greatly appreciated!
>
Bob
>

|||On Sep 24, 3:25 am, "Jack Vamvas" <DEL_TO_RE...@.del.comwrote:

Quote:

Originally Posted by

Also, analyse your query requirements then apply an Indexing Strategy
>
--
>
Jack Vamvas
___________________________________
Need an IT job? http://www.ITjobfeed.com/SQL
>
<bobdu...@.gmail.comwrote in message
>
news:1190404274.471197.197240@.n39g2000hsh.googlegr oups.com...
>

Quote:

Originally Posted by

If i create a simple table with a foreign key constraint, does it
create an implicit index on that given ID? I've been told this is
done in some databases, but i need to know for sure if SQL Server does
it. Has anyone heard of this before, on any other databses perhaps?


>

Quote:

Originally Posted by

Heres an example of how the foreign key constraint is being added:


>

Quote:

Originally Posted by

ALTER TABLE [dbo].[administrators] WITH CHECK ADD CONSTRAINT
[FPSLUFSUOXZGAJOJ] FOREIGN KEY([AdministratorRoleID])
REFERENCES [dbo].[administratorroles] ([AdministratorRoleID])


>

Quote:

Originally Posted by

My initial testing seems to indicate adding an index on the foreign
key column helps, but i need to know for sure. Any insight would be
greatly appreciated!


>

Quote:

Originally Posted by

Bob


Thanks for all the responses on this, its much appreciated!!! I also
found this article which makes me realize other people have had the
same misconceptions as me :)
http://www.sqlskills.com/blogs/kimb...yColu mns.aspx

Do Foreign Key Boost Performance?

Hello,
can Foreign Keys boost performance resp. Select or Where Statement in combination with a join?
Silasnope. but indexes do and I index all of my FKs. FKs are all about maintaining data integrity and they are very important.|||Do brakes boost your car's performance?|||In a very few incredibly specific circumstances foreign keys can (in an MS SQL Server DB) boost performance. I never consider them in terms of a performance boost. I would advise you don't either - there are an incalcable number of things to consider first.|||gotta link?|||One caveat to my previous post. I have read that using cascading deletes in conjunction with foreign keys requires less overhead than maintaining the data integrity on your own. But as far as I know plain vanilla foreign keys on their own do not impact performance. If I am wrong, please let me know.|||Yeah - a bit like Sean I'd like to stress I only addressed a small part of the performance aspect of your question. Their purpose in maintaining integrity is far more important than any performance overhead.gotta link?Yuppers:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74552&SearchTerms=foreign,key,pootle,kristen|||does youse guys ever link anyplace other than sqlteam?

buncha wannabe (http://en.wiktionary.org/wiki/wannabe)s

:p|||does youse guys ever link anyplace other than sqlteam?

buncha wannabe (http://en.wiktionary.org/wiki/wannabe)s

:p

whenever my boss introduces me as the resident guru I do feel like a wannabe and a fraud.

but back to my point here, I went to the msdn link and am I right that FK's on their own without setting db options or setting cascading deletes to on do not improve performance in a perceptible way?|||"setting cascading deletes to on" is a bit of misnomer

as soon as you define the foreign key, it gets an ON DELETE action

if you specify nothing, it defaults to NO ACTION

SQL Server 2000 offers only NO ACTION and DELETE as options

SQL Server 2005 offers NO ACTION, CASCADE, SET NULL, and SET DEFAULT

it's safe to say that all of these actions are more efficiently done by the database than by application code

but as far as SELECT queries are concerned, no, foreign keys don't speed those up (except insofar as the columns are indexed, which they usually are when someone defines foreign keys)|||Do brakes boost your car's performance?

Yes....in terms of stopping performance! ;)|||Yes....in terms of stopping performance! ;)If you are at least marginally sane, you won't operate a car that you can't stop safely. A car without brakes is crippled, its top performance is negligable because I can walk faster than I'll try to drive it.

Give me a 1970 Volvo that is rusted out, with one bad carburator, and good brakes and it will easily reach the speed limit... It might be smelly, ugly, and loud, but it will go as fast as the law allows.

Give me a 2007 Lamborghini, fresh off the sales floor with bad brakes, and while the engine is fantastic and the transmission is good enough, the amount of torque that can reach the pavement is limited (by law) to what will get me to ten kph.

Since the Volvo with brakes can travel at more than ten times the top legal speed of the Lamborghini, I feel pretty comfortable with the assertion that good brakes will significantly increase performance!

-PatP|||For those of you that are slow studies, the above analogy applies just as well to databases as it applies to vehicles... Foreign keys are the "safety gear" that is needed to keep a database from becoming corrupt.

Getting wrong answers fast is the fetish of amatuer programmers and DBAs. Wrong answers are still wrong, it doesn't matter how fast you get them.

Having foreign key constraints that you don't need will not hurt you (or query performance). Having them may help reduce elapsed time for queries because of how the engine enforces the foreign key and runs queries, but it will not hurt queries. There is some (time) cost to loading data with foreign key constraints, but that is a tiny cost compared to either a wrong answer that it caught and corrected by the business and trivial compared to the cost of a wrong answer that is actually used by the business!

-PatP|||oops, you posted first, and a nice lesson there about FKs

i wanted to mention that good performance without reliable brakes is a huge risk

last year i drove my car (a '69 beetle) 20 miles to the shop using only the handbrake, as the brake cable had snapped off, and i can tell you, i drove real careful (note to the pedants: yes, i am aware i used an adjective where i should've used an adverb, but that's just my style, innit :))

this year the clutch pedal rusted half off, and i had to drive it to the same shop without being able to get it into first gear, and let me tell you, that was no picnic, you just cannot let yourself come to a complete stop because then you can't get it into second, either, and in third you'll just stall when you try to go (luckily i only had to push it while in traffic once)

anyhow, my point to databases was that speed is irrelevant if something else is wrong

then i was going to quote that familiar "to err is human, but to really screw things up, let your programmers put apps into production without data reliability"|||THAT is why I love old vehicles... Especially Beetles :D|||The question was: Do foreign keys boost performance?
I'm the only one that answered the question and provided evidence (from the horses mouth too - and I don't mean the wannabees).
ergo:
Poots - 1
The rest - 0

Besides - Silas gave up on us ages ago.|||Isn't that always the score? ;)|||Isn't that always the score? ;)Spoken like a true protege ;)|||...I feel pretty comfortable with the assertion that good brakes will significantly increase performance!

-PatP

When I wrote 'stopping performance' I meant the ability to be able to stop, not reducing the ability to go. There is a difference I believe!|||Guys, can we just drop this topic now and move on to evaluating the importance of regularly scheduled oil changes?