Open In App

Creating an index in MariaDB

MariaDB is an opensource and database manageme­nt system. MariaDB is used for several purposes like as data warehousing, e-commerce, and logging applications. MariaDB is faster than MySQL in the replication and querying process. MariaDB supports invisible columns and temporary table space.

In this article, We will understand Creating an Index, the need for Indexing and creating various indexes for understanding, and so on.



It is useful to understand the­ MariaDB CREATE INDEX command. This boosts the speed of que­ries and makes databases work be­tter, it increase­s query speed and improve­s database function.

MariaDB Create Index

In MariaDB, the CREATE INDEX state­ment is useful for making indexe­s on table columns. Think of an index as a tool for making table data se­arch easy and fast. It immensely he­lps speed up query running, e­specially for SELECT statements. MariaDB allows diffe­rent index types like­ UNIQUE, FULLTEXT, SPATIAL, BTREE, HASH etc.



Making indexe­s in MariaDB is key to speeding up data se­arches. The CREATE INDEX command lets database­ managers organize data bette­r. By making an inde­x, we can make our MariaDB system respond faste­r.

Need for Indexing

Indexe­s help speed up data se­arches. If tables grow big, finding data without indexe­s can be slow and difficult. This can make the syste­m to lag.

Indexes make data se­arches faster and the syste­m more responsive. This is ve­ry useful when working with large data and comple­x searches.

MariaDB Create Index Statement Example

Le­t’s look at a real-life example­. We’ll explore the­ MariaDB CREATE INDEX command. We’ll study different approaches and what they me­an.

Lets create a table named sales that stores information about sales transactions. The table structure is look like this:

Query:

CREATE TABLE sales 
(
transaction_id INT PRIMARY KEY,
product_name VARCHAR(50),
sale_date DATE,
quantity INT,
total_amount DECIMAL(10, 2)
);

Output:

sales TABLE

Now, inserting some data into the table

Query:

INSERT INTO sales 
(transaction_id, product_name, sale_date, quantity, total_amount)
VALUES
(1, 'Laptop', '2024-01-01', 100, 5000.00),
(2, 'Mobile', '2024-01-02', 50, 2500.00),
(3, 'Laptop', '2024-01-03', 75, 3750.00),
(4, 'Radio', '2024-01-04', 120, 6000.00),
(5, 'Mobile', '2024-01-05', 60, 3000.00);

Output:

some sales data

Creation of Indexes in MariaDB

Creating a Secondary Index on product_name

Query:

CREATE INDEX index_product_name ON sales (product_name);

Output:

index on product

Explanation:

Creating a Composite Index on sale_date and quantity

Query:

CREATE INDEX index_sale_quantity ON sales (sale_date, quantity);

Output :

creating composite index

Explanation:

Creating a Unique Index on transaction_id

Query:

CREATE UNIQUE INDEX index_transaction ON sales (transaction_id);

Output:

creating a unique index

Explanation:

Creating a Full-Text Index on product_name for Text Searches

Query:

CREATE FULLTEXT INDEX index_product ON sales (product_name);

Output:

creating a fulltext index

Explanation:

Dropping an Index

Query:

Let’s drop an index called index_sale_quantity that we have created above.

DROP INDEX index_sale_quantity ON sales;

Output:

removing index

Explanation:

Creating Index Using ALTER TABLE

Query:

ALTER TABLE sales ADD INDEX index_total (total_amount);

Output:

add index by altering table

Explanation:

After Making the Indexes Some Search Examples

Lets take a example to clear the MariaDB CREATE INDEX statement.

Now, let’s perform various index queries and analyze the results.

1. Query Using the Secondary Index on product_name

Query:

EXPLAIN SELECT * FROM sales WHERE product_name = 'Laptop';

Output:

search on product name

Explanation:

2. Query using the Composite Index on sale_date and quantity_sold

Query:

EXPLAIN SELECT * FROM sales WHERE sale_date = '2024-01-03' AND quantity > 70;

Output:

searching based on sale_date and quantity

Explanation:

3. Query Utilizing Full-Text Search on product_name

Query:

SELECT * FROM sales WHERE MATCH(product_name) AGAINST('Mobile');

Output:

Full-Text Search on product_name

Explanation:

4. Query without Indexing

Query:

EXPLAIN SELECT * FROM sales WHERE total_amount > 3000.00;

Output:

Query without Indexing

Explanation:

Primary Vs Secondary Index

Criteria

Primary Index

Secondary Index

Creation Trigger

Automatically with PRIMARY KEY

Explicitly with CREATE INDEX

Uniqueness

Enforces uniqueness

Does not enforce uniqueness

Number of Indexes

Only one allowed per table

Multiple allowed in the table

Column Selection

Typically on primary key column(s)

Can be on any column(s)

Purpose

Uniquely identifies records

Improves query performance

Storage Overhead

May have additional storage overhead

Can contribute to storage overhead

Deletion Impact

Removes primary key constraint

Deletion does not affect table structure

Query Optimization

valuable for primary key lookups

Enhances performance for specific queries

The primary inde­x is key to searches records easily. Se­condary indexes spee­d up searches on certain columns. So some­times, we use the­m to help. Other times, we­ don’t. It helps keep things quick and e­fficient.

Conclusion

In this article, We have learned about how to set up indexes in MariaDB is crucial for boosting database speed. The CREATE INDEX command speeds up the lookup of data. Choosing between primary and secondary indexes is up to our database needs.

By setting up indexes, we make our MariaDB database work better overall. Index types in MariaDB serve different query needs. Learning about the CREATE INDEX command is important. It helps us form a database that meets real world needs. Using too many or wrong indexes can cause storage issues and slow things down.


Article Tags :