66

How do I index a database column in different database servers?

For an outline of how indexing works check out: How does database indexing work?

3
  • 1
    This seems like a question that would be well answered by the docs for the database server you might be using. For Oracle: [download.oracle.com/docs/cd/B28359_01/server.111/b28310/… Commented Oct 3, 2008 at 12:45
  • I didn't get the correct page when I clicked the above link for Oracle. Posting the correct link: docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm Commented Jul 8, 2013 at 3:19
  • 8
    One of SO's stated goals is to show up first in a google search. Often times, a SO answer is better than the docs themselves. Would it not be valuable to set up an answer here as a community wiki to collate all of the answers? Commented Jun 5, 2014 at 20:35

9 Answers 9

71

The following is in the SQL-92 standard, so it should be supported by the majority of RDBMSes that use SQL:

CREATE INDEX [index name] ON [table name] ( [column name] )
Sign up to request clarification or add additional context in comments.

1 Comment

In most systems, this can be used to have multiple columns as well, just by adding a comma separated list of column names instead of the single column.
7

SQL Server 2005 gives you the ability to specify a covering index. This is an index that includes data from other columns at the leaf level, so you don't have to go back to the table to get columns that aren't included in the index keys.

create nonclustered index my_idx on my_table (my_col1 asc, my_col2 asc) include (my_col3);

This is invaluable for a query that has my_col3 in the select list, and my_col1 and my_col2 in the where clause.

Comments

4

For Python PyTables, indexes don't have names, and they are bound to single columns:

tables.columns.column_name.createIndex()

Comments

4

In SQL Server, you can do the following (see CREATE INDEX (Transact-SQL) for the full list of options):

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
    ON <object> ( column [ ASC | DESC ] [ ,...n ] )
    [ INCLUDE ( column_name [ ,...n ] ) ]
    [ WHERE <filter_predicate> ]

(ignoring some more advanced options...)

The name of each index must be unique database-wide.

All indexes can have multiple columns, and each column can be ordered in whatever order you want.

Clustered indexes are unique - one per table. They can't have INCLUDEd columns.

Nonclustered indexes are not unique and can have up to 999 per table. They can have included columns and where clauses.

Comments

2

To create indexes, the following stuff can be used:

  1. Creates an index on a table. Duplicate values are allowed: CREATE INDEX index_name ON table_name (column_name)

  2. Creates a unique index on a table. Duplicate values are not allowed: CREATE UNIQUE INDEX index_name ON table_name (column_name)

  3. Clustered Index: CREATE CLUSTERED INDEX CL_ID ON SALES(ID);

  4. Non-clustered index: CREATE NONCLUSTERED INDEX NONCI_PC ON SALES(ProductCode);

Refer to http://www.codeproject.com/Articles/190263/Indexes-in-MS-SQL-Server for details.

1 Comment

The www.codeproject.com link is broken: "Hmm. We’re having trouble finding that site. We can’t connect to the server at www.codeproject.com.". All the original content on The Code Project was removed (and relaunched as a forum).
0
  1. CREATE INDEX name_index ON Employee (Employee_Name)

  2. On a multi column: CREATE INDEX name_index ON Employee (Employee_Name, Employee_Age)

1 Comment

0

We can use the following syntax to create an index.

CREATE INDEX <index_name> ON <table_name>(<column_name>)

If we do not want duplicate values to be allowed then we can add UNIQUE while creating an index as follows.

CREATE UNIQUE INDEX <index_name> ON <table_name>(<column_name>)

We can create an index on multiple columns by giving multiple column names separated by ','.

Comments

0

An index is not always needed for all the databases. For example: The Kognitio, AKA WX2, database engine, doesn't offer a syntax for indexing, as the database engine takes care of it implicitly.

Data goes on via round-robin partitioning and Kognitio WX2 gets data on and off the disk in the simplest possible way.

Comments

0

Since most of the answers are given for SQL databases, I am writing this for NoSQL databases, specifically for MongoDB.

Below is the syntax to create an index in the MongoDB database using the MongoDB shell.

db.collection.createIndex(<key and index type specification>, <options>)

Example - db.collection.createIndex( { name: -1 } )

In the above example, a single key descending index is created on the "name" field.

Keep in mind MongoDB indexes uses a B-tree data structure.

There are multiple types of indexes we can create in MongoDB. For more information, refer to Indexes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.