Skip to main content

When To Use Indexes In MySQL

When deciding when and how to create an index in your MySQL database, it's important to consider how the data is being used.

Let's say you have a database of students. We will create it like this:

CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) DEFAULT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  `class` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
Indexes are best used on columns that are frequently used in where clauses, and in any kind of sorting, such as "order by".

You should also pay attention to whether or not this information will change frequently, because it will slow down your updates and inserts. Since you wont frequently be adding students, you don't have to worry about the inserts


Let's say that you will be looking up the students with a web interface and the end user will be typing in the students name to find them, since remembering the students ID's would be cumbersome.

It sounds like this situation would be good to use an index.
  • You won't be updating the student's name very often, so you don't have to worry about a performance hit there.
  • you will be using the student in where clauses like this:
SELECT * FROM students WHERE first_name ='kawshi';
  • You WILL be generating reports, which will probably be alphabetic, like this:
SELECT * FROM students ORDER BY first_name ASC;
           So in this simple example it's easy to see when it would be important to use indexes.

           So, you could do it like this:
CREATE INDEX name_index ON students (first_name);
You might be working on a more complex database, so it's good to remember a few simple rules.
  • Indexes slow down inserts and updates, so you want to use them carefully on columns that are FREQUENTLY updated.
  • Indexes speed up where clauses and order by.
  • It is always wise to start by indexing columns that join with other tables 

Summary


If you find your SELECT queries are taking a long time to run, you are most likely missing an index on your table. Look at which columns are searching on, and create an index on each one using the following syntax:

CREATE INDEX by_student_id ON grades (student_id);
It is always wise to start by indexing columns that join with other tables. After that, look for columns you know you will commonly search on. Focus on your largest tables.


References





Comments

Popular posts from this blog

Understanding C1 and C2 Compilers in Java

Understanding C1 and C2 Compilers in Java Understanding C1 and C2 Compilers in Java In Java, the Just-In-Time (JIT) compiler is a part of the Java Virtual Machine (JVM) that improves the performance of Java applications by compiling bytecode into native machine code at runtime. The JIT compiler includes two different compilers, known as the C1 and C2 compilers, each with distinct optimization strategies and purposes. C1 Compiler (Client Compiler) The C1 compiler, also known as the client compiler, is designed for fast startup times and lower memory consumption. It performs lighter and quicker optimizations, which makes it suitable for applications that require quick startup and responsiveness. Key characteristics of the C1 compiler include: Quick Compilation: Prioritizes fast compilation times over deep optimizations. Low Overhead: Consumes less memory and resources during compilation. Profile-Guided Optimization: Ca...

Understanding -XX:+PrintCompilation Output in Java

Understanding -XX:+PrintCompilation Output in Java Understanding -XX:+PrintCompilation Output in Java The -XX:+PrintCompilation flag in the Java Virtual Machine (JVM) prints information about the methods being compiled by the Just-In-Time (JIT) compiler. When you enable this flag, the JVM will output a log of compilation events to the standard output. Each line of the output provides information about a specific method being compiled. Here, I'll explain the meaning of the different columns and markers, specifically focusing on the n , s , and % markers as seen in your example. Explanation of Output Columns and Markers Here's a breakdown of what each column and marker means: Timestamp : The time (in milliseconds) since the JVM started when the compilation event occurred. Compilation ID : A unique identifier for each compilation task within the JVM's lifecycle. Optimization Level : The lev...

What is L1, L2 and L3 Support Engineering

In this blog article, I'm going to explain about the Software support engineering role with my experience. I was a Level 2 and 3 support Engineer during my career. L1 - Level 1 L2 - Level 2 L3 - Level 3 Ticket - Incident L1 support includes interacting with customers, understand their issue and create tickets against it. The ticket then routed to the relevant L2 support ( Integration support, Server & Storage support, etc ...). L1 support Engineers have basic knowledge of product/service and skill to troubleshoot a very basic issue like password reset, software installation/uninstallation/reinstallation. L2 support manages the tickets which routed to them by L1( L2 support also can create tickets against any issue notice by them). They have more knowledge, more experience in solving related complex issues and can guide/help L1 support folks job in troubleshooting. If the solution not provided at this level then escalate to the L3. L3 is ...