Showing posts with label ado. Show all posts
Showing posts with label ado. Show all posts

Wednesday, March 7, 2012

indexes on foreign key columns?

I read recently in an ADO.NET book that I should create indexes on foreign
key constraints if they will commonly be used when joining. I can't
remember where, but I could swear that I had read somewhere else that
indexes were automatically created when you assign a foreign key constraint
to a column. What's the deal?
Joel Lyons
Hi Joel
You should create (yourself) indexes on referencing columns in a foreign key
constraint as it will improve things like the checking of the column when you
change a referenced value (for instance when you delete a row in the
referenced table). The referenced table will already have a primary key or a
unique index on the column being referenced.
John
"Joel Lyons" wrote:

> I read recently in an ADO.NET book that I should create indexes on foreign
> key constraints if they will commonly be used when joining. I can't
> remember where, but I could swear that I had read somewhere else that
> indexes were automatically created when you assign a foreign key constraint
> to a column. What's the deal?
> Joel Lyons
>
>
|||Hi Joel
Indexes are NOT automatically created on foreign keys, and it is usually a
good idea to create them. Even if you don't specifically write joins between
foreign and primary keys, if you ever allow deletes or updates to the
primary keys, SQL Server does an internal join to find matching FK values,
and an index can help the performance of the data modification.
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://sqlblog.com
"Joel Lyons" <joell_REMOVE_@.novarad.net> wrote in message
news:eTWbqWyIIHA.1212@.TK2MSFTNGP05.phx.gbl...
>I read recently in an ADO.NET book that I should create indexes on foreign
> key constraints if they will commonly be used when joining. I can't
> remember where, but I could swear that I had read somewhere else that
> indexes were automatically created when you assign a foreign key
> constraint
> to a column. What's the deal?
> Joel Lyons
>

indexes on foreign key columns?

I read recently in an ADO.NET book that I should create indexes on foreign
key constraints if they will commonly be used when joining. I can't
remember where, but I could swear that I had read somewhere else that
indexes were automatically created when you assign a foreign key constraint
to a column. What's the deal?
Joel LyonsHi Joel
You should create (yourself) indexes on referencing columns in a foreign key
constraint as it will improve things like the checking of the column when yo
u
change a referenced value (for instance when you delete a row in the
referenced table). The referenced table will already have a primary key or a
unique index on the column being referenced.
John
"Joel Lyons" wrote:

> I read recently in an ADO.NET book that I should create indexes on foreign
> key constraints if they will commonly be used when joining. I can't
> remember where, but I could swear that I had read somewhere else that
> indexes were automatically created when you assign a foreign key constrain
t
> to a column. What's the deal?
> Joel Lyons
>
>|||Hi Joel
Indexes are NOT automatically created on foreign keys, and it is usually a
good idea to create them. Even if you don't specifically write joins between
foreign and primary keys, if you ever allow deletes or updates to the
primary keys, SQL Server does an internal join to find matching FK values,
and an index can help the performance of the data modification.
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://sqlblog.com
"Joel Lyons" <joell_REMOVE_@.novarad.net> wrote in message
news:eTWbqWyIIHA.1212@.TK2MSFTNGP05.phx.gbl...
>I read recently in an ADO.NET book that I should create indexes on foreign
> key constraints if they will commonly be used when joining. I can't
> remember where, but I could swear that I had read somewhere else that
> indexes were automatically created when you assign a foreign key
> constraint
> to a column. What's the deal?
> Joel Lyons
>

indexes on foreign key columns?

I read recently in an ADO.NET book that I should create indexes on foreign
key constraints if they will commonly be used when joining. I can't
remember where, but I could swear that I had read somewhere else that
indexes were automatically created when you assign a foreign key constraint
to a column. What's the deal?
Joel LyonsHi Joel
You should create (yourself) indexes on referencing columns in a foreign key
constraint as it will improve things like the checking of the column when you
change a referenced value (for instance when you delete a row in the
referenced table). The referenced table will already have a primary key or a
unique index on the column being referenced.
John
"Joel Lyons" wrote:
> I read recently in an ADO.NET book that I should create indexes on foreign
> key constraints if they will commonly be used when joining. I can't
> remember where, but I could swear that I had read somewhere else that
> indexes were automatically created when you assign a foreign key constraint
> to a column. What's the deal?
> Joel Lyons
>
>|||Hi Joel
Indexes are NOT automatically created on foreign keys, and it is usually a
good idea to create them. Even if you don't specifically write joins between
foreign and primary keys, if you ever allow deletes or updates to the
primary keys, SQL Server does an internal join to find matching FK values,
and an index can help the performance of the data modification.
--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://sqlblog.com
"Joel Lyons" <joell_REMOVE_@.novarad.net> wrote in message
news:eTWbqWyIIHA.1212@.TK2MSFTNGP05.phx.gbl...
>I read recently in an ADO.NET book that I should create indexes on foreign
> key constraints if they will commonly be used when joining. I can't
> remember where, but I could swear that I had read somewhere else that
> indexes were automatically created when you assign a foreign key
> constraint
> to a column. What's the deal?
> Joel Lyons
>

Indexes on Computed columns and ADO

OK maybe this crosses the SQLServer forum (apologies and dlet me know if so) BUT:

I have a composite PK/index with a computed column (basically CASTing a bit value). The index is fine in SQL2K however when I try to access the table through an ADO recordset (to a VBA form) the Datasheet control is locked.

Seems like a behind the scene ADO cursor-type/lock-type issue. ANY input is welcome I AM UNDER THE PRODUCTION GUN.

TIAYou can try to set the "arithmetic abort" property to on (in SQlServer>properties>connection tab).

I had the same problem, in one of my application, except there I was logging on to the server via ODBC drivers, not OLEDB (as ADO usualy does).

I hope it works. Good luck!

IONUT|||Originally posted by zarathustra
OK maybe this crosses the SQLServer forum (apologies and dlet me know if so) BUT:

I have a composite PK/index with a computed column (basically CASTing a bit value). The index is fine in SQL2K however when I try to access the table through an ADO recordset (to a VBA form) the Datasheet control is locked.

Seems like a behind the scene ADO cursor-type/lock-type issue. ANY input is welcome I AM UNDER THE PRODUCTION GUN.

TIA

Try what happens if you lose the computed column in the primary key and replace it by the column(s) that the computed column was based on. There should be no need to have a computed column in the primary key.

I must admit that I also had trouble with ADO when using computed columns and I quickly ended up avoiding computed columns, not wanting to spend the time to find how to solve the problems.|||Originally posted by Ad Dieleman
Try what happens if you lose the computed column in the primary key and replace it by the column(s) that the computed column was based on. There should be no need to have a computed column in the primary key.

I must admit that I also had trouble with ADO when using computed columns and I quickly ended up avoiding computed columns, not wanting to spend the time to find how to solve the problems.

I agree about not having the PK contain a computed val but IXs can't have bit values. Hence the comp'd field. I've got it working now. I've seen issues as well with computed fields/ADO and connection settings like ARITHABORT etc... joy.

thanks to you both.

Friday, February 24, 2012

Indexes and ADO.NET

I have an app that uses both SQL 2005 and SQL 2000 as the DB. My
question is when I run a query with ADO.NET throught VB.NET without
specifing the "WITH INDEX" in the query, does SQL automatically use any
indexes available?
Also, I tried running the SQL Tunning Wizard w/ SQL2005 and after I get
the results on which indexes to create How do I find out which
field/columns are being used in that index without first creating the
index and going to the table view?
Thanks in advance
Mike>I have an app that uses both SQL 2005 and SQL 2000 as the DB. My
> question is when I run a query with ADO.NET throught VB.NET without
> specifing the "WITH INDEX" in the query, does SQL automatically use any
> indexes available?
It will use an index if it makes sense to do so in the query. The mere
presence of an index does not guarantee that it is a useful index (either
overall or for a specific query).
If you can provide more specific details, we may be able to help. Index
consideration, in general, is a very broad topic and there is no brief,
unilateral, "do this and don't do that" answer.

> Also, I tried running the SQL Tunning Wizard w/ SQL2005 and after I get
> the results on which indexes to create How do I find out which
> field/columns are being used in that index
Can't you view the CREATE INDEX script? The table(s)/column(s) will be
specified there.|||ok. Here is a sample of the tables / Querys I am performing.
[--TicketsDetails TABLE --]
Ticket_Detail_ID int 4 0
lLocationID int 4 1
Ticket_ID int 4 1
dtCreated datetime 8 1
dtUpdated datetime 8 1
sTicket_Number nvarchar 10 1
sDescription nvarchar 50 1
Reference_ID int 4 1
PK_Ticket_Detail_ID on Ticket_Detail_ID
NDX_Ticket_ID on Ticket_ID
NDX_Description on sDescription
Querys I run
1- Select * from TicketsDetails where Ticket_Detail_ID = 231
2- Select count(*) as TotalSold from TicketsDetails where Ticket_ID =
12345 and Detail_Type_ID = 9222
3- Select * from TicketsDetails where sDescription = 'ABC123'
4- Select * from TicketsDetails where sDescription like 'ABC1%'
As far as vieweing the CREATE INDEX script I have not been able to find
anywhere in the tunning wizard where it will show me the script. It
tells me that I will get 60% improvement over my currrent configuration
but all I can see if the results report on which index names is going
to add but thats about it.
Thanks in advance
Mike