
You can clearly see that the number of rows to scan will be 1. | 1 | SIMPLE | customers | NULL | ref | customer_id | customer_id | 13 | const | 1 | 100.00 | NULL |įrom the above explain output, it's clear that MySQL server will use our index (customer_Id) to search the table. If we run the explain statement one more time, we will get the below results: mysql> Explain select customer_id, customer_name from customers where customer_id='140385' To optimize the above query, we can just add an index to the ' customer_id' field using the below syntax: mysql> Create index customer_id ON customers (customer_Id)
MYSQL OPTIMIZER SCRIPT FULL
Second, MySQL server has clearly indicated that it's going to conduct a full scan on the 500 rows in our database. First, it is clear that MySQL will conduct a full table scan because key column is ' NULL'. | 1 | SIMPLE | customers | NULL | ALL | NULL | NULL | NULL | NULL | 500 | 10.00 | Using where |Īs you can see, the optimizer has displayed very important information that can help us to fine-tune our database table. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | If we run the above SQL one more time with the explain statement, we will get a full picture of what MySQL will do to execute the query: mysql> explain select customer_id, customer_name from customers where customer_id='140385' Once you append the query before an SQL statement, MySQL displays information from the optimizer about the intended execution plan.
MYSQL OPTIMIZER SCRIPT UPDATE
Luckily, MySQL has a special ' EXPLAIN' statement that you can use alongside select, delete, insert, replace and update statements to analyze your queries. The above query will force MySQL server to conduct a full table scan (start to finish) to retrieve the record that we are searching. Mysql> select customer_id, customer_name from customers where customer_id='140385' It is always advisable to test MySQL queries with a 'worst case scenario' sample amount of data to get a clearer picture of how the query will behave on production.Ĭonsider a case where you are running the following SQL query from a database with 500 rows without an index: However, if your table has more than 10 rows, they can considerably reduce select query execution time.


MySQL indexes may take up more space and decrease performance on inserts, deletes and updates.

An index is also very useful when it comes to sorting records. Tip #1: Index All Columns Used in 'where', 'order by' and 'group by' ClausesĪpart from guaranteeing uniquely identifiable records, an index allows MySQL server to fetch results faster from a database.
MYSQL OPTIMIZER SCRIPT FREE
If you don't have one already, you can sign up for an Alibaba Cloud and enjoy $300 worth in Free Trial. This will guarantee stability, scalability, reliability and speed of applications and websites running on your Alibaba Cloud instance.

In this guide, we will take you through the steps of optimizing SQL queries and databases on your Alibaba Cloud Elastic Compute Service (ECS) instance. Alibaba Cloud was the winner of the prestigious 2018 MySQL Corporate Contributor Award and is also a platinum sponsor of the MariaDB foundation. Alibaba has an advanced network of cloud based technologies and their breaking performance and flexible billing has enabled cloud without borders for its over one million paid customers.Īlibaba Cloud has continued to show enormous contribution to the open source communities and has empowered developers worldwide. You can deploy fast, secure and trusted MySQL database instances from Alibaba Cloud. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community. By Francis Ndungu, Alibaba Cloud Tech Share Author.
