Software Development

Debugging SQL query in MySQL

Recently I started writing SQL query for analyzing and debugging the production code. But I was surprised to see that some queries takes longer time to execute to achieve the same output. I did research and found some interesting stuff about how to debug the SQL query. I have a very simple table who’s definition is as following. In test environment, this table was populated with more then 1000K rows.
 
 

 
 
 

+-----------------------+--------------+------+-----+----------------+
| Field                 | Type         | Null | Key | Extra          |
+-----------------------+--------------+------+-----+----------------+
| id                    | bigint(20)   | NO   | PRI | auto_increment |
| dateCreated           | datetime     | NO   |     |                |
| dateModified          | datetime     | NO   |     |                |
| phoneNumber           | varchar(255) | YES  | MUL |                |
| version               | bigint(20)   | NO   |     |                |
| oldPhoneNumber        | varchar(255) | YES  |     |                |
+-----------------------+--------------+------+-----+----------------+

I executed a very simple query to find the tuple which contains 5107357058 as phoneNumber. It took almost 4 seconds to fetch the result.

select * from Device where phoneNumber = 5107357058;
takes 4 sec.

This simple query should have taken few milliseconds. I noticed that phoneNumber datatype is varchar but in query it is provided as number. When I modify the query to match the datatype, it took few milliseconds.

select * from Device where phoneNumber = '5107357058';
takes almost no time.

After googling and reading post on stackoverflow I found EXPLAIN SQL clouse which helps in debugging the query. The EXPLAIN statement provides information about the execution plan for a SELECTstatement. When I used it to get the information about the two queries I got the following results.

mysql> EXPLAIN select * from Device where phoneNumber = 5107357058;
+----+-------------+-----------+------+---------------------------------------+------+---------+------+---------+-------------+
| id | select_type | table     | type | possible_keys                         | key  | key_len | ref  | rows    | Extra       |
+----+-------------+-----------+------+---------------------------------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | Device    | ALL  | phoneNumber,idx_Device_phoneNumber    | NULL | NULL    | NULL | 6482116 | Using where |
+----+-------------+-----------+------+---------------------------------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

mysql> EXPLAIN select * from Device where phoneNumber = '5107357058';
+----+-------------+-----------+------+---------------------------------------+-------------+---------+-------+------+-------------+
| id | select_type | table     | type | possible_keys                         | key         | key_len | ref   | rows | Extra       |
+----+-------------+-----------+------+---------------------------------------+-------------+---------+-------+------+-------------+
|  1 | SIMPLE      |   Device  | ref  | phoneNumber,idx_Device_phoneNumber    | phoneNumber | 258     | const |    2 | Using where |
+----+-------------+-----------+------+---------------------------------------+-------------+---------+-------+------+-------------+
1 row in set (0.00 sec)

The EXPLAINgives you different query attribute. While analysing the query you should take care of the following attribute.

  • possible_keys : shows the indexes apply to the query
  • key : which key use to find the record. NULL values shows that there was no key used for the query and SQL search linearly, eventually takes long time.
  • rows : SQL query with less number of result-rows, is efficient. One should always try to improve the query and avoid using generic query clouse. The query performance is much evident when executed on large number of records.
  • type : is “The join type”. Ref means All rows with matching index values are read from the table; All means the full table scan.

The two outputs of EXPLAIN clearly indicate the subtle differences. The later query uses the string which is right datatype, results phoneNumber as key and checks only two rows. Whereas the former uses integer in the query which is different datatype and hence SQL converts to integer value to string value and compares with each record present in that table. This results NULL as key and 6482116 as row output. You can also see that the later query type value is ref and former query type value is All, which clearly indicates that the former is a bad query.
 

Reference: Debugging SQL query from our JCG partner Rakesh Cusat at the Code4Reference blog.

Subscribe
Notify of
guest

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

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Igor Madjeric
Igor Madjeric
12 years ago

Hi,

I like the topic of your blog. But i must admit that it is a little but strange that you having such big differences after running those two queries.

Can you explain how you measure the time of execution of those queries. I would say that you have those results as consequence of query caching.

I just can not believe that MySQL needs 4s for converting from an INT to VARCHAR.

Akansha
Akansha
10 years ago

Your Content is very good.

NIranjan
9 years ago

This article helped me solve my issues and provided me a great insight on Explain command and made me much more cautios before I write a query.

Back to top button