How do I index a database column

后端 未结 9 1844
难免孤独
难免孤独 2020-12-12 15:51

Hopefully, I can get answers for each database server.

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

相关标签:
9条回答
  • 2020-12-12 16:00

    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 using mongo shell.

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

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

    In the above example an single key descending index is created on the name field.

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

    There are multiple types of indexes we can create in mongodb, for more information refer to below link - https://docs.mongodb.com/manual/indexes/

    0 讨论(0)
  • 2020-12-12 16:00

    We can use following syntax to create index.

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

    If we do not want duplicate value to be allowed then we can add UNIQUE while creating index as follow

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

    We can create index on multiple column by giving multiple column name separated by ','

    0 讨论(0)
  • 2020-12-12 16:05

    The following is SQL92 standard so should be supported by the majority of RDMBS that use SQL:

    CREATE INDEX [index name] ON [table name] ( [column name] )
    
    0 讨论(0)
  • 2020-12-12 16:06

    An index is not always needed for all the databases. For eg: Kognitio aka WX2 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 disk in the simplest possible way.

    0 讨论(0)
  • 2020-12-12 16:09

    In SQL Server, you can do the following: (MSDN Link to 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.

    0 讨论(0)
  • 2020-12-12 16:10
    1. CREATE INDEX name_index ON Employee (Employee_Name)

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

    0 讨论(0)
提交回复
热议问题