MySQL INDEXES

Hi all, from this post I’m going  to talk about another optimization technique used in MySQL, indexing. We can use indexing with tables to make our queries more efficient. Let’s see what is indexing? Practically, indexes are also type of tables, which keep primary key or index field and a pointer to each record into the actual table. Indexes are not visible to the users. But they will increase the searching speed of a table. But the downside of indexing is it will increase the inserting, updating, deleting times as well. Because it has to update the indexes as well.

Let’s see actually it behaves like that or not.

  • First I’m creating a table without indexing.

1

  • Then I’m inserting data to that table. It took 0.04 seconds to complete the query.

2

  • Next I want to retrieve data from the table. It took 0.02 seconds to complete the query.

3

  • Finally I’m updating some records. It took 0.05 seconds to complete the query.

4

  • Now let’s create another table with same fields, but this time with indexing.

6

  • Then I’m inserting same set of data to the table. It took 0.04 seconds to complete the query.

7

  • Retrieving the same data set. It took 0.00 seconds to complete the query.

8

  • Updating the same record. It took 0.06 seconds to complete the query.

9

  • So you can see that there is a clear time reduction in the select query and time increase in the update query as well.

If you are using data base tables in your application for searching more than other things, then indexing can help you. There are 4 types of indexing techniques are used,

  1. Primary Key
  2. Unique
  3. Index
  4. Full Text

PRIMARY KEY

This is the use of a PRIMARY KEY in a table as indexing. This is what we talked earlier.

  • How can we make sure that indexes has been created.
  • You can use the following query. Here data is not readable.

10

  • So I’m using the ‘\G‘, vertical format output, to make it more readable.

11

  • Let’s check it for the table without the indexing. It’s an empty set.

100.png

UNIQUE

This is the use of a UNIQUE key field. If we use UNIQUE, then there can’t be any repeating values.

  • Let’s see how to add a UNIQUE index to the previously created table.

18

  • Let’s check for the index.

19

  • If we want we can use two columns as the index field as well.

22

  • Then it will show both columns as key indexes.

23

INDEX

Here we are using the INDEX. Here we can use repeating values as the index as well.

  • Let’s create a new table with INDEX.

12

  • Now let’s check the index.

17

  • Next try to add data.

13

  • Then we can retrieve data.

14

  • Let’s update some data.

15

FULL TEXT

We can use the FULL TEXT with fields with text values such as VAR CHAR, CHAR, TEXT.

  • Before adding the new index I want to delete the previously created index. You can simple DROP it.

20

  • Let’s check whether it has been dropped or not.

21.png

  • Let’s try to create an index for a field with INT value.

24.png

  • So now create it for a text field.

25.png

  • Let’s check it.

27.png

  • If you want you can create the indexes in the descending order of the column which we are using. Just add DESC at the end of the column name.

28

29

That’s how to create, update, delete indexes to a table. There is another way to check the use of INDEXES. We can use the EXPLAIN with a query as well.

30

31

32

That’s all about indexing. Hope now you have a clear idea about indexing. See you soon with another interesting topic. Thank You!

Advertisements

6 thoughts on “MySQL INDEXES

    1. NO, What auto increment do is when “No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically”. It is not a indexing technique. It is using to assign values to a field automatically.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s