0

I am quite new to the both MySQL and AWS RDS service. I just find my query to the MySQL on a small RDS instance takes roughly more than 5 seconds. Besides, I notice with the CloudWatch that the CPU of the rds is always 100% consumed. The size of my database is around 1 million now. Can any tell me what I can do to fix this?

One query is like this:

mysql> EXPLAIN SELECT * FROM iApps;
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows    | Extra |
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------+
|  1 | SIMPLE      | iApps | ALL  | NULL          | NULL | NULL    | NULL | 1165255 |       |
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------+
1 row in set (0.04 sec)

Here is another query:

mysql> EXPLAIN SELECT * FROM iApps WHERE familyName='Thompson' AND firstName='David';
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | iApps | ALL  | NULL          | NULL | NULL    | NULL | 1166070 | Using where |
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.08 sec)

The time here is quite small, but it's quite slow with my code. I use python package mysqldb for all the operations, could that be the problem?

I did search online for this issue. But I don't really get the solutions from the pages. I'm entry level for the database.

1 Answers1

3

The second query's EXPLAIN plan give it away. You need an index.

The clause WHERE familyName='Thompson' AND firstName='David' is the clue.

Simply run the following:

ALTER TABLE iApps ADD INDEX familyName_firstName_ndx (familyName,firstName);

I can assure you this will speed up this particular query. Other queries that have different WHERE clauses will require different indexes.

As for the CPU spike, that should not be a surprise. A full table scan on a 1 million row table will push data in and out of the InnoDB Buffer Pool. Keep in mind that the Transaction Logs in Amazon RDS are always the same size (innodb_log_file_size = 128M) for all seven RDS models. Transacitons that are passing through the same rows you are selecting would also have bearing on CPU usage as it manipulate the Transaction Logs as well.

RolandoMySQLDBA
  • 16,544
  • 3
  • 48
  • 84