CREATE INDEX Statement

Creates an index on a specified table, or pair of tables. Once an index is created, it is never referenced in a SQL statement again except to delete it using the DROP INDEX statement.

Syntax

CREATEUNIQUE ] [ index-type ] INDEX [ IF NOT EXISTS ] index-nameONowner.]table-name
   … ( column-name [ , column-name ] …)
   …[ { IN | ON } dbspace-name ]
   …[ NOTIFY integer ]
   …[ DELIMITED BY  separators-string ‘ ]
   …[ LIMIT maxwordsize-integer ]

index-type:
   { CMP | HG | HNG | LF | WD | DATE | TIME | DTTM }

Parameters

Examples

  • Example 1 – creates a Compare index on the projected_earnings and current_earnings columns. These columns are decimal columns with identical precision and scale.
    CREATE CMP INDEX proj_curr_cmp
    ON sales_data
    ( projected_earnings, current_earnings )
  • Example 2 – creates a High_Group index on the ID column of the SalesOrderItems table. The data pages for this index are allocated from dbspace Dsp5.
    CREATE HG INDEX id_hg
    ON SalesOrderItems
    ( ID ) IN Dsp5
  • Example 3 – creates a High_Group index on the SalesOrderItems table for the ProductID column:
    CREATE HG INDEX item_prod_hg
    ON Sales_OrderItems
    ( ProductID)
  • Example 4 – creates a Low_Fast index on the SalesOrderItems table for the same ProductID column without any notification messages:
    CREATE LF INDEX item_prod
    ON SalesOrderItems
    ( ProductID)
     NOTIFY 0
  • Example 5 – creates a WD index on the earnings_report table. Specify that the delimiters of strings are space, colon, semicolon, and period. Limit the length of the strings to 25.
    CREATE WD INDEX earnings_wd
    ON earnings_report_table(varchar)
    DELIMITED BY ‘ :;.’
    LIMIT 25
  • Example 6 – creates a DTTM index on the SalesOrders table for the OrderDate column:
    CREATE DTTM INDEX order_dttm
    ON SalesOrders
    ( OrderDate )

Usage

  • Index ownership—There is no way to specify the index owner in the CREATE INDEX statement. Indexes are automatically owned by the owner of the table on which they are defined. The index name must be unique for each owner.

  • No indexes on views—Indexes cannot be created for views.

  • Index name—The name of each index must be unique for a given table.

  • Exclusive table use—CREATE INDEX is prevented whenever the statement affects a table currently being modified by another connection. However, queries are allowed on a table that is also adding an index.

  • CHAR columns—After a WD index is created, any insertions into its column are parsed using the separators, and maximum word size cannot be changed after the index is created.

    For CHAR columns, specify a space as at least one of the separators or use the default separator set. SAP Sybase IQ automatically pads CHAR columns to the maximum column width. If your column contains blanks in addition to the character data, queries on WD indexed data might return misleading results. For example, column CompanyName contains two words delimited by a separator, but the second word is blank padded:

    ‘Concord’ ‘Farms					       ’

    Suppose that a user entered this query:

    SELECT COUNT(*)FROM Customers WHERE CompanyName contains (‘Farms’)

    The parser determines that the string contains:

    ‘Farms           ’

    instead of:

    ‘Farms’

    and returns 0 instead of 1. You can avoid this problem by using VARCHAR instead of CHAR columns.

  • Data types:
    • You cannot use CREATE INDEX to create an index on a column with BIT data.
    • Only the default index, CMP index, or WD index can be created on CHAR and VARCHAR data with more than 255 bytes.
    • Only the default and WD index types can be created on LONG VARCHAR data.
    • Only the default index, CMP index, and TEXT index types can be created on BINARY and VARBINARY data with more than 255 bytes.
    • An HNG index or a CMP index cannot be created on a column with FLOAT, REAL, or DOUBLE data.
    • A TIME index can be created only on a column having the data type TIME.
    • A DATE index can be created only on a column having the data type DATE.
    • A DTTM index can be created only on a column having the data type DATETIME or TIMESTAMP.
  • Multicolumn indexes—You can create a unique or nonunique HG index with more than one column. SAP Sybase IQ implicitly creates a nonunique HG index on a set of columns that makes up a foreign key.

    HG and CMP are the only types of indexes that can have multiple columns. You cannot create a unique HNG or LF index with more than one column, and you cannot create a DATE, TIME, or DTTM index with more than one column.

    The maximum width of a multicolumn concatenated key is 5KB (5300 bytes). The number of columns allowed depends on how many columns can fit into 5KB. CHAR or VARCHAR data greater than 255 bytes are not allowed as part of a composite key in single-column HG, LF, HNG, DATE, TIME, or DTTM indexes.

    An INSERT on a multicolumn index must include all columns of the index.

    Queries with a single column in the ORDER BY clause run faster using multicolumn HG indexes. For example:

    SELECT abs (x) from t1
    ORDER BY x

    In the above example, the HG index vertically projects x in sorted order.

    To enhance query performance, use multicolumn HG indexes to run ORDER BY operations on more than one column (that can also include ROWID) in the SELECT or ORDER BY clause with these conditions:

    • All projected columns, plus all ordering columns (except ROWID), exist within the index

    • The ordering keys match the leading HG columns, in order

    If more than one multicolumn HG index satisfies these conditions, the index with the lowest distinct counts is used.

    If a query has an ORDER BY clause, and the ORDER BY column list is a prefix of a multicolumn index where all columns referenced in the SELECT list are present in a multicolumn index, then the multicolumn index performs vertical projection; for example:

    SELECT x,z,y FROM T 
    ORDER BY x,y

    If expressions exist on base columns in the SELECT list, and all the columns referenced in all the expressions are present in the multicolumn index, then the query will use a multicolumn index; for example:

    SELECT power(x,2), x+y, sin(z) FROM T 
    ORDER BY x,y

    In addition to the two previous examples, if the ROWID() function is in the SELECT list expressions, multicolumn indexes will be used. For example:

    SELECT rowid()+x, z FROM T 
    ORDER BY x,y,z

    In addition to the three previous examples, if ROWID() is present at the end of an ORDER BY list, and if the columns of that list—except for ROWID()—use multicolumn indexes in the exact order, multicolumn indexes will be used for the query. For example:

    SELECT z,y FROM T 
    ORDER BY x,y,z,ROWID()

    SAP Sybase IQ allows the use of NULL in data values on a user created unique multicolumn HG index, if the column definition allows for NULL values and a constraint (primary key or unique) is not being enforced. The rules for this feature are as follows:

    • A NULL is treated as an undefined value.

    • Multiple rows with NULL values in a unique index column or columns are allowed.
      1. In a single column index, multiple rows with a NULL value in an index column are allowed.

      2. In a multicolumn index, multiple rows with a NULL value in index column or columns are allowed, as long as non-NULL values in the rest of the columns guarantee uniqueness in that index.

      3. In a multicolumn index, multiple rows with NULL values in all columns participating in the index are allowed.

    These examples illustrate these rules. Given the table table1:

    CREATE TABLE table1
    (c1 INT NULL, c2 INT NULL, c3 INT NOT NULL);

    Create a unique single column HG index on a column that allows NULLs:

    CREATE UNIQUE HG INDEX c1_hg1 ON table1 (c1);

    According to rule 1 above, you can insert a NULL value into an index column in multiple rows:

    INSERT INTO table1(c1,c2,c3) VALUES (NULL,1,1);
    INSERT INTO table1(c1,c2,c3) VALUES (NULL,2,2);

    Create a unique multicolumn HG index on a columns that allows NULLs:

    CREATE UNIQUE HG INDEX c1c2_hg2 ON table1(c1,c2);

    According to rule 2 above, you must guarantee uniqueness in the index. The following INSERT does not succeed, since the multicolumn index c1c2_hg2 on row 1 and row 3 has the same value:

    INSERT INTO table1(c1,c2,c3) VALUES (NULL,1,3);

    These INSERT operations are successful, however, according to rules 1 and 3:

    INSERT INTO table1(c1,c2,c3) VALUES (NULL,NULL,3);
    INSERT INTO table1(c1,c2,c3) VALUES (NULL,NULL,4);

    Uniqueness is preserved in the multicolumn index.

    This UPDATE operation is successful, as rule 3 allows multiple rows with NULL values in all columns in the multicolumn index:

    UPDATE table1 SET c2=NULL WHERE c3=1

    When a multicolumn HG index is governed by a unique constraint, a NULL value is not allowed in any column participating in the index.

  • Parallel index creation—You can use the BEGIN PARALLEL IQ … END PARALLEL IQ statement to group CREATE INDEX statements on multiple IQ main store tables, so that they execute as though they are a single DDL statement. See BEGIN PARALLEL IQ … END PARALLEL IQ Statement for more information.

Warning!   Using the CREATE INDEX command on a local temporary table containing uncommitted data fails and generates the error message Local temporary table, <tablename>, must be committed in order to create an index. Commit the data in the local temporary table before creating an index.
Side Effects
  • Automatic commit

Standards

  • SQL—Vendor extension to ISO/ANSI SQL grammar.

  • Sybase—Adaptive Server Enterprise has a more complex CREATE INDEX statement than SAP Sybase IQ. While the Adaptive Server Enterprise syntax is permitted in SAP Sybase IQ, some clauses and keywords are ignored. For the full syntax of the Adaptive Server Enterprise CREATE INDEX statement, see the Adaptive Server Enterprise Reference Manual, Volume 2: Commands.

Adaptive Server Enterprise indexes can be either clustered or nonclustered. A clustered index almost always retrieves data faster than a nonclustered index. Only one clustered index is permitted per table.

SAP Sybase IQ does not support clustered indexes. The CLUSTERED and NONCLUSTERED keywords are allowed by SQL Anywhere, but are ignored by SAP Sybase IQ. If no index-type is specified, SAP Sybase IQ creates an HG index on the specified column(s).

SAP Sybase IQ does not permit the DESC keyword.

Index names must be unique on a given table for both SAP Sybase IQ and Adaptive Server Enterprise.

Permissions

Requires CREATE privilege on the dbspace where the index is being created. Also requires one of::
  • CREATE ANY INDEX system privilege.
  • CREATE ANY OBJECT system privilege.
  • REFERENCE privilege on the underlying table of the index.
  • You own the underlying table of the index.