I need to alter a table to modify the order of the indexes created from the composite key for the below mentioned changeset.
<changeSet author="demo (generated)" id="demo-11">
<createTable tableName="customersalesdata">
<column name="id" type="BIGINT">
<constraints unique="true" primaryKey="true" primaryKeyName="customersalesdata_pkey"/>
</column>
<column name="customerid" type="NVARCHAR(255)">
<constraints primaryKey="true" unique="true" primaryKeyName="customersalesdata_pkey"/>
</column> </createTable>
</changeSet>
The reason for altering is that ordering of the columns in an index makes a big difference. Since the customerid is the second column, it will not be used. The query is performing an index scan because of this. Since the table has two indexes that start with id, having the id
, customerid
in this order is a waste (in most cases).
So I need to change the column order to customerid
and id
. And the another problem is customerid
which is the composite key, is referred as the foreign key in the another table.
My question is should I need to drop FK
first and then drop Composite Keys and then form the composite key in the order as shown below
<changeSet id="2">
<addPrimaryKey columnNames="customerid, id"
constraintName="customersalesdata_pkey"
tableName="customersalesdata"
validate="true"/>
</changeSet>
Or just create a another index on top of composite key by combining both of the fields as shown below
<changeSet author="demo" id="demo-id">
<createIndex tableName="customersalesdata" indexName="idxn_customer_id_id">
<column name="customer_id"/>
<column name="id"/>
</createIndex>
</changeSet>
Also in both the cases will there be any chances of data loss? Can you please suggest the best approach here.