Creates an index on one or more computed or noncomputed columns in a table. Creates partitioned indexes. Allows computed columns, like ordinary columns, to be index keys, and creates function-based indexes. A function-based index has one or more expressions as its index key.
The existing create index syntax can create indexes on computed columns, but function-based indexes require additional syntax.
create [unique] [clustered | nonclustered] index index_name on [[database.][owner].]table_name (column_expression [asc | desc] [, column_expression [asc | desc]]...) [with {fillfactor = pct, online, max_rows_per_page = num_rows, reservepagegap = num_pages, consumers = x, ignore_dup_key, sorted_data, [ignore_dup_row | allow_dup_row], [statistics using num_steps values ] [, statistics hash_option] [, statistics max_resource_granularity = int] [, statistics histogram_tuning_factor = int] [, statistics print_progress = int] } ] [on segment_name] [index_partition_clause] [with index_compression = { NONE | PAGE }]...]]]
index_partition_clause::= [local index [partition_name [with index_compression = { NONE | PAGE }][on segment_name] [, partition_name [on segment_name] [with index_compression = { NONE | PAGE }][on segment_name]...]]
create [unique | nonclustered] index index_name on [[database.] [owner].] table_name (column_expression [asc | desc] [, column_expression [asc | desc]]...
insert and update commands, which generate duplicate key values, can succeed if you create your index using the allow_dup_row option.
Composite indexes (indexes in which the key value is composed of more than one column) can also be unique.
The default is nonunique. To create a nonunique clustered index on a table that contains duplicate rows, specify allow_dup_row or ignore_dup_row.
When you create a unique local index on range-, list-, and hash-partitioned tables, the index key list is a superset of the partition-key list.
If clustered is not specified, nonclustered is assumed.
The value you specify is not saved in sysindexes. Use sp_chgattribute to create stored fillfactor values.
The default for fillfactor is 0; this is used when you do not include with fillfactor in the create index statement (unless the value has been changed with sp_configure). When specifying a fillfactor, use a value between 1 and 100.
A fillfactor of 0 creates clustered indexes with completely full pages and nonclustered indexes with completely full leaf pages. It leaves a comfortable amount of space within the index B-tree in both the clustered and nonclustered indexes. There is seldom a reason to change the fillfactor.
If the fillfactor is set to 100, the SAP ASE server creates both clustered and nonclustered indexes with each page 100 percent full. A fillfactor of 100 makes sense only for read-only, to which no data is ever added.
fillfactor values smaller than 100 (except 0, which is a special case) cause the SAP ASE server to create new indexes with pages that are not completely full. A fillfactor of 10 might be a reasonable choice if you are creating an index on a table that eventually holds a great deal more data, but small fillfactor values cause each index (or index and data) to occupy more storage space.
If you do not specify a value for max_rows_per_page, the SAP ASE server uses a value of 0 when creating the table. Values for tables and clustered indexes range from 0 to 183K on a 2K page, to 0 to 1486 on a 16K page.
The maximum number of rows per page for nonclustered indexes depends on the size of the index key. The SAP ASE server returns an error message if the specified value is too high.
A max_rows_per_page value of 0 creates clustered indexes with full pages and nonclustered indexes with full leaf pages. It leaves a comfortable amount of space within the index B-tree in both clustered and nonclustered indexes.
If max_rows_per_page is set to 1, the SAP ASE server creates both clustered and nonclustered indexes with one row per page at the leaf level. Use low values to reduce lock contention on frequently accessed data. However, low max_rows_per_page values cause the SAP ASE server to create new indexes with pages that are not completely full, uses storage space, and may cause more page splits.
If CIS is enabled, you cannot use max_rows_per_page for remote servers.
You cannot create a unique index on a column that includes duplicate values or more than one null value, whether or not ignore_dup_key is set. If you attempt to do so, the SAP ASE server prints an error message that displays the first of the duplicate values. You must eliminate duplicates before the SAP ASE server can create a unique index on the column.
The default value is 20, if no histogram is currently stored for the leading index column.
The current number of steps is used, if a histogram for the leading column of the index column already exists.
If you specify 0 for num_steps, the index is re-created, but the statistics for the index are not overwritten in the system tables.
The actual number of steps may differ from the one you specify; if the histogram steps specified with num_steps is M, and the histogram_tuning_factor parameter is N, then the actual steps are between M and M*N, depending on the number of frequency cells that exist in the distribution.
Immediately before the index_partition_clause – defines a global default which is used for all partitions where the segment is not explicitly defined in the index_partition_clause.
Within that clause itself – allows you to specify a segment for each individual partition
See the examples section for an example that uses on segment_name in both locations.
hashing – statistics are gathered with hashing.
new_ hashing – enables hash-based gathering for minor attributed columns that have not had statistics previously gathered.
no_ hashing – no hash-based statistics are gathered.
max_resource_granularity – sets the maximum percentage of system resources a query can use.
histogram_tuning_factor – controls the number of steps that SAP ASE analyzes per histogram.
print_progress – shows progress messages during gathering of statictics with hashing.
For both semantically partitioned tables and round-robin-partitioned tables, each table partition has only one corresponding index partition.
If partition_name is omitted, the SAP ASE server creates a name in the form table_name_partition_id. The SAP ASE server truncates partition names that exceed the allowed maximum length.
create index au_id_ind on authors (au_id)
create unique clustered index au_id_ind on authors (au_id)
create index ind1 on titleauthor (au_id, title_id)
create nonclustered index zip_ind on authors (postalcode) with fillfactor = 25, consumers = 4
create index pub_dates_ix on titles (pub_id asc, pubdate desc)
create index title_id_ix on titles (title_id) with reservepagegap = 40, statistics using 50 values
create clustered index clust_idx on salesdetail (ord_num) local index
create nonclustered index global_idx on sales (order_num)
create table pback_sales (c1 int, c2 int, c3 varchar (20)) partition range (c1) (p1 c1 values <= (10), p2 c1 values <= (20), p3 c1 values <= (MAX))
create index fc_idx on pback_sales (c1*c2) local index p1
create index i1 on t1(c1) with sorted_data, consumers = N create index i1 on t1(c1) with sorted_data
create index sum_sales on mytitles (price * total_sales)
use tempdb go if not exists(select 1 from tempdb..syssegments where name = 'seg1') exec sp_addsegment seg1,tempdb,master go if not exists(select 1 from tempdb..syssegments where name = 'seg2') exec sp_addsegment seg2,tempdb,master go if not exists(select 1 from tempdb..syssegments where name = 'seg3') exec sp_addsegment seg3,tempdb,master go if not exists(select 1 from tempdb..syssegments where name = 'seg4') exec sp_addsegment seg4,tempdb,master go if exists(select 1 from sysobjects where name = 't1') drop table t1 go create table t1 (a int, b varchar(30)) partition by roundrobin (p1 on seg1, p2 on seg2) go create index t1_i1 on t1 (a) local index go create index t1_i2 on t1 (a) on seg3 local index ip1 on seg4 go sp_help t1 go
Name Owner Object_type Create_date ---- ----- ----------- ------------------- t1 dbo user table Aug 7 2008 11:14AM (1 row affected) Column_name Type Length Prec Scale Nulls Default_name Rule_name Access_Rule_name Computed_Column_object Identity ----------- ------- ------ ---- ----- ----- ------------ --------- ---------------- ---------------------- ---------- a int 4 NULL NULL 0 NULL NULL NULL NULL 0 b varchar 30 NULL NULL 0 NULL NULL NULL NULL 0 Object has the following indexes index_name index_keys index_description index_max_rows_per_page index_fillfactor index_reservepagegap index_created index_local ---------- ---------- ----------------- ----------------------- ---------------- -------------------- ------------------- ----------- t1_i1 a nonclustered 0 0 0 Aug 7 2008 11:14AM Local Index t1_i2 a nonclustered 0 0 0 Aug 7 2008 11:14AM Local Index (2 rows affected) index_ptn_name index_ptn_seg ---------------- ------------- t1_i1_952063116 default t1_i1_968063173 default ip1 seg4 t1_i2_1000063287 seg3 (4 rows affected) No defined keys for this object. name type partition_type partitions partition_keys ---- ---------- -------------- ---------- -------------- t1 base table roundrobin 2 NULL (1 row affected) partition_name partition_id pages row_count segment create_date -------------- ------------ ----- --------- ------- ------------------- p1 920063002 1 0 seg1 Aug 7 2008 11:14AM p2 936063059 1 0 seg2 Aug 7 2008 11:14AM Partition_Conditions -------------------- NULL Avg_pages Max_pages Min_pages Ratio(Max/Avg) Ratio(Min/Avg) ----------- ----------- ----------- -------------- ---------------- 1 1 1 1.000000 1.000000 Lock scheme Allpages The attribute 'exp_row_size' is not applicable to tables with allpages lock scheme. The attribute 'concurrency_opt_threshold' is not applicable to tables with allpages lock scheme. exp_row_size reservepagegap fillfactor max_rows_per_page identity_gap ascinserts ------------ -------------- ---------- ----------------- ------------ ----------- 0 0 0 0 0 0 (1 row affected) concurrency_opt_threshold optimistic_index_lock dealloc_first_txtpg ------------------------- --------------------- ------------------- 0 0 0 (1 row affected) (return status = 0)
create index idx_order_line on order_line (ol_delivery_d, ol_dist_info) with index_compression = page
If the index has an index row length that is too short to benefit from compression, a warning is raised indicating the index will not be compressed.
create index idx_sales on Sales(store_id, order_num) local index ip1 with index_compression = PAGE, ip2 with index_compression = PAGE, ip3
create unique clustered index au_id_ind on authors(au_id, title_id) with statistics hashing, statistics using 50 values, statistics print_progress = 1
create index ind1 on titleauthor (au_id, title_id) with statistics using 50 values, statistics hashing, statistics max_resource_granularity = 80
create nonclustered index zip_ind on authors(postalcode, au_id) with fillfactor = 25, consumers = 4, statistics using 50 values, statistics hashing, statistics histogram_tuning_factor = 40
create unique clustered index au_id_ind on authors(au_id, title_id) with statistics new_hashing
Periodically run update statistics if you add data to the table that changes the distribution of keys in the index. The query optimizer uses the information created by update statistics to select the best plan for running queries on the table.
You can create non-clustered local index in parallel for partitioned tables that includes empty partitions.
If the table contains data when you create a nonclustered index, the SAP ASE server runs update statistics on the new index. If the table contains data when you create a clustered index, the SAP ASE server runs update statistics on all the table’s indexes.
Index all columns that are regularly used in joins.
When CIS is enabled, the create index command is reconstructed and passed directly to the SAP ASE associated with the table.
You cannot use create index (clustered or unclustered) on the segment that includes the virtually hashed table, since a virtually hashed table must take only one exclusive segment, which cannot be shared by other tables or databases
You can run writetext concurrently with the online parameter.
sp_addsegment, sp_chgattribute, sp_helpcomputedcolumn, sp_helpindex, sp_helpsegment, sp_spaceused in Reference Manual: Procedures
optdiag in the Utility Guide
ANSI SQL – Compliance level: Transact-SQL extension.
The permission checks for create index differ based on your granular permissions settings.
Setting | Description |
---|---|
Enabled | With granular permissions enabled, you must be the table owner, or a user with the create any index privilege. |
Disabled | With granular permissions disabled, you must be the table owner or a user with sa_role. create index permission defaults to the table owner and is not transferable. |
Values in event and extrainfo columns of sysaudits are:
Information | Values |
---|---|
Event | 104 |
Audit option | create |
Command or access audited | create index |
Information in extrainfo |
|