February 1, 2010 at 1:25 pm
REF: http://doc.ddart.net/mssql/sql70/create_2.htm
I have a small database where I have PK and FK.
The PK are already index due to there creation, some of the FK have not ( I assume just because they are FK they are not already indexed)
I want to index all fields that have been used in JOINS and WHERE clauses of the stored Procs.
I assume that I use NON CLUSTERED like this ...
Like :
CREATE NONCLUSTED INDEX <indexname> ON <table> (<column>)
Please confirm, advise, etc
February 1, 2010 at 1:39 pm
Best practice is to index your FK columns for join performance.
CREATE INDEX <index name> ON <table name>(<column name>)
February 1, 2010 at 1:42 pm
confirm.
Note that your <column> can be multiple columns, and the sort order can be ASC or DESC on each column.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
February 1, 2010 at 1:45 pm
February 1, 2010 at 3:12 pm
A sort order for an Index..
Is that wise ???
For example I have one column filled with BIT or -1 or 0
Shouldnt need to sort that !
When is it wise to sort or unsort..
thanks
February 1, 2010 at 5:11 pm
John Rowan (2/1/2010)
Best practice is to index your FK columns for join performance.
CREATE INDEX <index name> ON <table name>(<column name>)
The FK's should probably be on the PK of the "remote table" meaning that there should already be an index.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 1, 2010 at 5:12 pm
Digs (2/1/2010)
A sort order for an Index..Is that wise ???
For example I have one column filled with BIT or -1 or 0
Shouldnt need to sort that !
When is it wise to sort or unsort..
thanks
I probably wouldn't put an index on a BIT column. Heh... for that matter, I probably wouldn't have a BIT column to begin with. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
February 2, 2010 at 7:01 am
Digs (2/1/2010)
When is it wise to sort or unsort..
We'll frequently sort DESC on date columns for audit/historical tables - latest record first deal.
_____________________________________________________________________
- Nate
February 2, 2010 at 10:29 am
Jeff Moden (2/1/2010)
John Rowan (2/1/2010)
Best practice is to index your FK columns for join performance.
CREATE INDEX <index name> ON <table name>(<column name>)
The FK's should probably be on the PK of the "remote table" meaning that there should already be an index.
I guess I should have been more clear on this. Yes, the FK should be the PK in the 'remote' table and therefore be indexed already. I was referring to the table that you would build the FK on.
February 2, 2010 at 10:44 am
Digs (2/1/2010)
A sort order for an Index..Is that wise ???
Indexes are always sorted. By default ASC if no option is specified.
For example I have one column filled with BIT or -1 or 0
Shouldnt need to sort that !
It's uncommon to index a bit column alone, though it may be very useful to create a multi-column (composite) index with one of the columns in it being a bit.
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
February 2, 2010 at 12:53 pm
Ok check this..
Field1 Name: Smith,Brown,Green
Field2 Deleted:0,0,-1
NOTE:-1 means deleted.
So I do WHERE Name = 'Brown" AND Deleted = 0
A good INDEX would be <tablename>(Name,Deleted)
BUT what if WHERE clause is only
Name='Brown'
or
Deleted =0
Hows that fit with a multi column index
Cause at the moment I have just index each separare column on its own.
February 3, 2010 at 7:25 am
create table #tbl ([Name] varchar(10), [Deleted] int)
insert into #tbl values ('Smith', 0)
insert into #tbl values ('Brown', 0)
insert into #tbl values ('Green', -1)
create index ix_tbl on #tbl ([Name], [Deleted])
--create index ix_tbl_1 on #tbl ([Name])
--create index ix_tbl_2 on #tbl ([Deleted])
-- qry1
select [Name]
from #tbl
where [Name] = 'Brown'
and [Deleted] = 0
-- qry2
select [Name]
from #tbl
where [Name] = 'Brown'
-- qry3
select [Name]
from #tbl
where [Deleted] = 0
drop table #tbl
I think this is what you're trying to do.
Looking at the execution plan in this scenario using only index "ix_tbl": qry1 and qry2 are doing index seeks and qry3 is doing an index scan.
Using indexes "ix_tbl_1" and "ix_tbl_2": qry2 is doing an index seek while qry1 and qry3 are doing index scans.
Me, I'd go with the composite index (name, deleted).
_____________________________________________________________________
- Nate
February 3, 2010 at 7:31 am
Digs (2/2/2010)
BUT what if WHERE clause is onlyName='Brown'
That could easily use the composite index on Name, deleted
or
Deleted =0
That, however, cannot.
How often is deleted filtered on alone?
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
February 3, 2010 at 8:11 am
Could you explain why
Deleted = 0
cannot use the composite index on Name, Deleted?
Surely as Deleted is in the index, it can be tested in an index scan, without having to read the complete table data? Or does SQL Server only test for the first item in a composite index?
February 3, 2010 at 8:20 am
Sorry. It can not seek on that index because deleted is not a left-based subset of the index key. Yes, it can use that index for a scan operation.
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
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply