Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 R2 Create index

Now I am looking for your help to create index on these.

Now this is my table structure

table description screen cap

This the query I need index for maximum performance.

select PageId 
    from tblPages 
    where UrlChecksumCode = @UrlChecksumCode 
        and PageUrl = @PageUrl

Now I am very bad with indexes. I plan like that when the query is executing it will find first that UrlChecksumCode rows then look pageurl columns. If you also explain me why to make such index I really appreciate that. Thank you.

like image 364
MonsterMMORPG Avatar asked Jan 11 '12 19:01

MonsterMMORPG


People also ask

How do I create an index in SQL Server?

In Object Explorer, expand the database that contains the table on which you want to create a nonclustered index. Expand the Tables folder. Expand the table on which you want to create a nonclustered index. Right-click the Indexes folder, point to New Index, and select Non-Clustered Index....

How do I add an index to an existing table?

ALTER command to add and drop INDEXALTER TABLE tbl_name ADD INDEX index_name (column_list) − This adds an ordinary index in which any value may appear more than once. ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list) − This creates a special FULLTEXT index that is used for text-searching purposes.

Is CREATE INDEX DML or DDL?

DDL - CREATE INDEX. Used to create an index on an existing table. The ALTER TABLE statement can also be used to create (or drop) an index on a table. The syntax for this command varies across systems.

Does primary key automatically CREATE INDEX?

A primary index is automatically created for the primary key and ensures that the primary key is unique. You can use the primary index to retrieve and access objects from the database. The unique index is a column, or an ordered collection of columns, for which each value identifies a unique row.


2 Answers

one way, since your pageURL is long(nvarchar(1000) and an index can only be 900 bytes if you don't use included columns, I have created this index with included columns

create index ix_IndexName on tblPages (UrlChecksumCode) INCLUDE(PageUrl)

or

create index ix_IndexName on tblPages (UrlChecksumCode) INCLUDE(PageUrl, PageID)

See also SQL Server covering indexes

why is URL nvarchar instead of varchar...can they be unicode? if not make it varchar and save 1/2 the size

like image 118
SQLMenace Avatar answered Oct 13 '22 14:10

SQLMenace


Create Nonclustered Indexes

       create nonclustered index indexname on tablename(columnname)

Drop Index

       Drop Index index name on tablename

See all Indexes in a database

       select * from sys.indexes
like image 32
kavitha Reddy Avatar answered Oct 13 '22 12:10

kavitha Reddy