sql server - How does an index work/relate to FK? -


i know basic , given i'm @ databases should understand now, not comprehend index is.

to make concrete, following t-sql generated orm - creates fk , creates index it.

what having index (in posative sense) , excluding (in negative sense)?

-- creating foreign key on [item_id] in table 'cakestats' alter table [dbo].[cakestats] add constraint [fk_cakestat_item]     foreign key ([item_id])     references [dbo].[items]         ([id])     on delete no action on update no action;  -- creating non-clustered index foreign key 'fk_cakestat_item' create index [ix_fk_cakestat_item] on [dbo].[cakestats]     ([item_id]); go 

an index balanced tree allows faster searching specific data. indexing in general sense marking easy finding. visualize index in of reference book. it's alphabetically sorted allow quick finding of actual information @ cost of pages in of book making larger/thicker. more stuff index, larger is. there difference between clustered , nonclustered index. example gave above nonclustered index it's in addition normal book contents. clustered index actual content sorted keys of index.

in foreign key case, if foreign key something, way key enforced doing check on referenced table/column see if exists. having index on foreign key speeds (log(n) time) rather require looking @ every single row 1 @ time (n time).

in example, index helps if query searches cakestats based on item_id. important items(id) has index or check on foreign key going expensive every time item_id inserted/deleted/updated in cakestats.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -