What is the use of index in table creation in magento2

->addIndex($installer->getIdxName('blog_comment', ['parent_id']), ['parent_id'])


2 Answers 2


You can create your column as index key for better performance and speedy retrieval of data from a table.

An index is a performance optimization feature that enables data to be accessed faster compare to other nonindex columns. An index is used to more quickly find rows in the table based on the values which are part of the index and they don’t define the uniqueness of a column.

You can add your column as Index key using Magento 2 by below way using InstallSchema or UpgradeSchema.php file,

    $installer->getIdxName('table_name', ['table_field']),

Here table_name is your table name of database and table_field is your field which you want to create index key. You can check blog for Add foreign key to table, Add Foreign key to table using Magento 2

  • Thanks. Its work for me.... Oct 23, 2019 at 12:47

This worked for me on Magento version 2.3.3:

    $setup->getIdxName('table_name', ['column_name']),

I think the syntax has changed a bit in the last few versions.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.