Magento 2 : How To Create UNIQUE or Normal KEY Index

how to create unique or normal key index in magento 2

We are having a lot of learning and fun working on the complex project for one of our prestige clients. As we develop we learn new things everyday on Magento 2 and always feel to share our learning with the community because without giving you don’t get anything back as we all know sharing is caring 🙂

Today we are going to share how to create indexes on the existing Magento 2 or custom tables. Table indexes are good in many ways if you know what you are doing for example you can improve the performance of your queries by creating indexes on the fields which are getting used all the time in your where clauses. Other example is unique index which can guarantee uniquely identifiable records in the table. But make sure you are not creating too many indexes because they could also slow down your insert queries and could have bigger impact in performance instead of any gain.

Anyways lets crack on with creating indexes in Magento 2, we are assuming you already have your module with tables in Magento 2 DB.

We will start with creating unique index on custom table with multiple fields by using our ScommerceMage\Custom\Setup\UpgradeSchema.php file

namespace ScommerceMage\Custom\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
   
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
   
        if(version_compare($context->getVersion(), '1.0.1', '<')) { $setup->getConnection()->addIndex(
                $setup->getTable('custom_table'),
                'unique_index_name',
                array('custom_field_1', 'custom_field_2', 'custom_field_2'),
                \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
            );
        }

        $setup->endSetup();
    }
}

The following code will create unique index on custom table with single field

namespace ScommerceMage\Custom\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
   
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
   
        if(version_compare($context->getVersion(), '1.0.1', '<')) { $setup->getConnection()->addIndex(
                $setup->getTable('custom_table'),
                'unique_index_name',
                'custom_field_1',
                \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
            );
        }

        $setup->endSetup();
    }
}

The following code will create an index on custom table with multiple fields

namespace ScommerceMage\Custom\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
   
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
   
        if(version_compare($context->getVersion(), '1.0.1', '<')) { $setup->getConnection()->addIndex(
                $setup->getTable('custom_table'),
                'index_name',
                array('custom_field_1', 'custom_field_2', 'custom_field_2')
            );
        }

        $setup->endSetup();
    }
}

The following code will create an index on custom table with single field

namespace ScommerceMage\Custom\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
   
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
   
        if(version_compare($context->getVersion(), '1.0.1', '<')) { $setup->getConnection()->addIndex(
                $setup->getTable('custom_table'),
                'index_name',
                'custom_field_1'
            );
        }

        $setup->endSetup();
    }
}

That’s it, Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Scommerce

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.