Showing posts with label index. Show all posts
Showing posts with label index. Show all posts

Thursday, March 29, 2012

Foreign key and indexes

I have a question about foreign keys and indexes that I am unsure
about. If a table has a foreign key does this column have a non
clustered index assigned to it as default (that is hidden)? or does it
make sense to add a non clustered index to the foreign key column in
the foreign key table?
I am thinking this as I am unsure how SQL server handles foreign keys.
EXAMPLE BELOW...
DOES TABLE2.Table1ID have a non clustered index that SQL server uses?
CREATE TABLE dbo.Table1
(
Table1Id int NOT NULL,
Foo varchar(10) NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE dbo.Table1 ADD CONSTRAINT
PK_Table1_1 PRIMARY KEY CLUSTERED
(
Table1Id
) ON [PRIMARY]
GO
CREATE TABLE dbo.Table2
(
Table2ID int NOT NULL,
Table1ID int NULL,
barr varchar(10) NULL
) ON [PRIMARY]
GO
ALTER TABLE dbo.Table2 ADD CONSTRAINT
PK_Table2 PRIMARY KEY CLUSTERED
(
Table2ID
) ON [PRIMARY]
GO
ALTER TABLE dbo.Table2 WITH NOCHECK ADD CONSTRAINT
FK_Table2_Table1 FOREIGN KEY
(
Table1ID
) REFERENCES dbo.Table1
(
Table1Id
) NOT FOR REPLICATION
GO
ALTER TABLE dbo.Table2
NOCHECK CONSTRAINT FK_Table2_Table1
GOSQL Server does not index foreign keys by default. Usually it does make
sense to create an index on a foreign key.
David Portas
SQL Server MVP
--sql

Foreign Key + Index

Imagine 2 tables:
AUTHORS
author_id (int) (PK)
author_name (varchar)
BOOKS
book_id (int) (PK)
book_author_id (int) (FK from AUTHOR)
book_title (varchar)
book_author_id is already declared as a foreign key.
If I want better performance when querying SELECT * FROM BOOKS WHERE
book_author_id = 1234
do I have to set a index on book_author_id,
or is it unecessary as a FK is already set?
I think that as a FK is a constraint and not an index, it's still necessary
but I want to be sure.
Can you answer my question?
Thanks
Henria Foreign Key is NOT automatically indexed in SQL Server.
you'll need to index it.
Greg Jackson
Portland, OR|||Thanks for your answer Greg :-)
"pdxJaxon" <GregoryAJackson@.Hotmail.com> a écrit dans le message de
news:OMI0r%234EFHA.3200@.TK2MSFTNGP10.phx.gbl...
> a Foreign Key is NOT automatically indexed in SQL Server.
> you'll need to index it.
>
> Greg Jackson
> Portland, OR
>
>

Foreign Key + Index

Imagine 2 tables:
AUTHORS
author_id (int) (PK)
author_name (varchar)
BOOKS
book_id (int) (PK)
book_author_id (int) (FK from AUTHOR)
book_title (varchar)
book_author_id is already declared as a foreign key.
If I want better performance when querying SELECT * FROM BOOKS WHERE
book_author_id = 1234
do I have to set a index on book_author_id,
or is it unecessary as a FK is already set?
I think that as a FK is a constraint and not an index, it's still necessary
but I want to be sure.
Can you answer my question?
Thanks
Henri
a Foreign Key is NOT automatically indexed in SQL Server.
you'll need to index it.
Greg Jackson
Portland, OR
|||Thanks for your answer Greg :-)
"pdxJaxon" <GregoryAJackson@.Hotmail.com> a crit dans le message de
news:OMI0r%234EFHA.3200@.TK2MSFTNGP10.phx.gbl...
> a Foreign Key is NOT automatically indexed in SQL Server.
> you'll need to index it.
>
> Greg Jackson
> Portland, OR
>
>

Foreign Key + Index

Imagine 2 tables:
AUTHORS
author_id (int) (PK)
author_name (varchar)
BOOKS
book_id (int) (PK)
book_author_id (int) (FK from AUTHOR)
book_title (varchar)
book_author_id is already declared as a foreign key.
If I want better performance when querying SELECT * FROM BOOKS WHERE
book_author_id = 1234
do I have to set a index on book_author_id,
or is it unecessary as a FK is already set?
I think that as a FK is a constraint and not an index, it's still necessary
but I want to be sure.
Can you answer my question?
Thanks
Henria Foreign Key is NOT automatically indexed in SQL Server.
you'll need to index it.
Greg Jackson
Portland, OR|||Thanks for your answer Greg :-)
"pdxJaxon" <GregoryAJackson@.Hotmail.com> a crit dans le message de
news:OMI0r%234EFHA.3200@.TK2MSFTNGP10.phx.gbl...
> a Foreign Key is NOT automatically indexed in SQL Server.
> you'll need to index it.
>
> Greg Jackson
> Portland, OR
>
>

Monday, March 26, 2012

Forcing use of an index

Hi
Can you force a query to use a particular index. IF so please let me
know the sql statement syntax.
thanks
Newish
Hi
Take a look at "index hints" topic in the BOL
"Newish" <ahussain3@.gmail.com> wrote in message
news:1164198675.592286.139920@.m7g2000cwm.googlegro ups.com...
> Hi
> Can you force a query to use a particular index. IF so please let me
> know the sql statement syntax.
> thanks
> Newish
>

Forcing use of an index

Hi
Can you force a query to use a particular index. IF so please let me
know the sql statement syntax.
thanks
NewishHi
Take a look at "index hints" topic in the BOL
"Newish" <ahussain3@.gmail.com> wrote in message
news:1164198675.592286.139920@.m7g2000cwm.googlegroups.com...
> Hi
> Can you force a query to use a particular index. IF so please let me
> know the sql statement syntax.
> thanks
> Newish
>

Forcing use of an index

Hi
Can you force a query to use a particular index. IF so please let me
know the sql statement syntax.
thanks
NewishHi
Take a look at "index hints" topic in the BOL
"Newish" <ahussain3@.gmail.com> wrote in message
news:1164198675.592286.139920@.m7g2000cwm.googlegroups.com...
> Hi
> Can you force a query to use a particular index. IF so please let me
> know the sql statement syntax.
> thanks
> Newish
>

Monday, March 19, 2012

Force Index Use

Hi

I have a reporting procedure that fetches a great deal of data
and is currently taking too long to retrieve the results (40 sec).
Looking at the execution plan, I see SQL is not using some of the indexes I added especially for the queries benefit (ie.time column), usually it uses the clustered composite PK.
Does anyone know how I can force SQL to use a particular index on
a query?

advice appreciated...

Thanks
Desmondno worries.., got it...(BOL)
Desmond|||after table name or table alias in SELECT clause and/or in JOIN clause put (INDEX=index_name)|||thanks...still needed the syntax!
des|||Originally posted by DesmondX
thanks...still needed the syntax!
des

Check this:

drop table test13
go
create table test13(id int identity primary key, code varchar(10))
go
create index i_test13 on test13 (code)
go
select * from test13 with(index(i_test13))
select * from test13 (index=i_test13)

Monday, March 12, 2012

force a specific order of record in a table

I want the record in table is sorted, I try to use index but not succesful,
why?
I am using SQLServer 2000Use ORDER BY clause
"kei" <kei@.discussions.microsoft.com> wrote in message
news:0DB8A9B7-CFD0-4671-B585-9B8E948E4C9E@.microsoft.com...
>I want the record in table is sorted, I try to use index but not succesful,
> why?
> I am using SQLServer 2000|||YOu could create a clustered index which defines the rows / data to be
physically sorted after the clustered key. but if you want to get the
data in an ordered way, the best thing is to use the ORDER BY clause.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--|||No, I means the physical order, not using sql statement, anyway, I think the
answer is clustered index, thx!!
"Uri Dimant" wrote:

> Use ORDER BY clause
>
>
> "kei" <kei@.discussions.microsoft.com> wrote in message
> news:0DB8A9B7-CFD0-4671-B585-9B8E948E4C9E@.microsoft.com...
>
>|||kei
No, don't relay on phisycal order .If you wanted to get out a sorted result
use ORDER BY clause for your safety
"kei" <kei@.discussions.microsoft.com> wrote in message
news:47E2427C-C55B-4970-808F-8AF193920F36@.microsoft.com...[vbcol=seagreen]
> No, I means the physical order, not using sql statement, anyway, I think
> the
> answer is clustered index, thx!!
> "Uri Dimant" wrote:
>|||No that wouldn't actually solve it as Uri already explained.
The why is due to the definition of a table in relational
database systems. A table has unordered rows and columns.
The physical order of the columns and rows does not, should
not matter. You manage ordering through SQL statements and
order by clauses.
-Sue
On Wed, 26 Apr 2006 02:40:01 -0700, kei
<kei@.discussions.microsoft.com> wrote:
[vbcol=seagreen]
>No, I means the physical order, not using sql statement, anyway, I think th
e
>answer is clustered index, thx!!
>"Uri Dimant" wrote:
>

force a specific order of record in a table

I want the record in table is sorted, I try to use index but not succesful,
why?
I am using SQLServer 2000YOu could create a clustered index which defines the rows / data to be
physically sorted after the clustered key. but if you want to get the
data in an ordered way, the best thing is to use the ORDER BY clause.
HTH, Jens Suessmeyer.
--
http://www.sqlserver2005.de
--|||Use ORDER BY clause
"kei" <kei@.discussions.microsoft.com> wrote in message
news:0DB8A9B7-CFD0-4671-B585-9B8E948E4C9E@.microsoft.com...
>I want the record in table is sorted, I try to use index but not succesful,
> why?
> I am using SQLServer 2000|||kei
No, don't relay on phisycal order .If you wanted to get out a sorted result
use ORDER BY clause for your safety
"kei" <kei@.discussions.microsoft.com> wrote in message
news:47E2427C-C55B-4970-808F-8AF193920F36@.microsoft.com...
> No, I means the physical order, not using sql statement, anyway, I think
> the
> answer is clustered index, thx!!
> "Uri Dimant" wrote:
>> Use ORDER BY clause
>>
>>
>> "kei" <kei@.discussions.microsoft.com> wrote in message
>> news:0DB8A9B7-CFD0-4671-B585-9B8E948E4C9E@.microsoft.com...
>> >I want the record in table is sorted, I try to use index but not
>> >succesful,
>> > why?
>> > I am using SQLServer 2000
>>|||No, I means the physical order, not using sql statement, anyway, I think the
answer is clustered index, thx!!
"Uri Dimant" wrote:
> Use ORDER BY clause
>
>
> "kei" <kei@.discussions.microsoft.com> wrote in message
> news:0DB8A9B7-CFD0-4671-B585-9B8E948E4C9E@.microsoft.com...
> >I want the record in table is sorted, I try to use index but not succesful,
> > why?
> > I am using SQLServer 2000
>
>|||No that wouldn't actually solve it as Uri already explained.
The why is due to the definition of a table in relational
database systems. A table has unordered rows and columns.
The physical order of the columns and rows does not, should
not matter. You manage ordering through SQL statements and
order by clauses.
-Sue
On Wed, 26 Apr 2006 02:40:01 -0700, kei
<kei@.discussions.microsoft.com> wrote:
>No, I means the physical order, not using sql statement, anyway, I think the
>answer is clustered index, thx!!
>"Uri Dimant" wrote:
>> Use ORDER BY clause
>>
>>
>> "kei" <kei@.discussions.microsoft.com> wrote in message
>> news:0DB8A9B7-CFD0-4671-B585-9B8E948E4C9E@.microsoft.com...
>> >I want the record in table is sorted, I try to use index but not succesful,
>> > why?
>> > I am using SQLServer 2000
>>

Friday, February 24, 2012

For non-serialized does key lock lock more than one row?

For Sql 2000, for isolation levels not serialized, can a key lock
(especially created by an inserted row), involving a nonprimary key index
end up locking more than one row?
Thanks,
Randy Neall"Randolph Neall" <randolphneall@.veracitycomputing.com> wrote in message
news:#GWEwT8EHHA.3780@.TK2MSFTNGP02.phx.gbl...
> For Sql 2000, for isolation levels not serialized, can a key lock
> (especially created by an inserted row), involving a nonprimary key index
> end up locking more than one row?
>
Since each key in a non-unique index may relate to multiple rows, a key lock
on a non-unique index typically impacts multiple rows, since The rows
themselves are not locked, but the key lock will be inconsistent with any
other transaction reading or locking that index key. So it may well block
other operations on other rows that share that index key.
David|||Thanks, David.
Randy
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:OsJQ508EHHA.3780@.TK2MSFTNGP02.phx.gbl...
>
> "Randolph Neall" <randolphneall@.veracitycomputing.com> wrote in message
> news:#GWEwT8EHHA.3780@.TK2MSFTNGP02.phx.gbl...
> > For Sql 2000, for isolation levels not serialized, can a key lock
> > (especially created by an inserted row), involving a nonprimary key
index
> > end up locking more than one row?
> >
> Since each key in a non-unique index may relate to multiple rows, a key
lock
> on a non-unique index typically impacts multiple rows, since The rows
> themselves are not locked, but the key lock will be inconsistent with any
> other transaction reading or locking that index key. So it may well block
> other operations on other rows that share that index key.
> David
>

For index

Is there any way I can findout all the columns and it's table which used in
stored procedure in the where clause and don't have indexes on it ? If yes
then how can I finout out.
Thanks
Look at the Index Tuning Wizard. That would be a great tool for you to
learn how to use.
"Rogers" <Rogers@.discussions.microsoft.com> wrote in message
news:188538E6-A0AA-4750-98DA-AE4DE36ECB81@.microsoft.com...
> Is there any way I can findout all the columns and it's table which used
in
> stored procedure in the where clause and don't have indexes on it ? If yes
> then how can I finout out.
> Thanks