简体   繁体   中英

Slow Mysql stored procedure on RDS instance

I have a problem with a Mysql stored procedure running slow on an Amazon RDS instance.

I'm currently migrating the Mysql database of a client to our own Mysql database using stored procedures. Both databases are on the same RDS instance. The instance class is 'db.m1.medium'.

One stored procedures copies approximately 2.600.000 records from one table in the client's database to 2 tables in our database.

When running this stored procedure on a local copy of our database on my laptop, it takes 1 hour 15 minutes. When running this stored procedure on the RDS instance it would take approximately 19 hours. I'm calling the stored procedure from an EC2 instance in the same availability zone.

Can I make the stored procedure running faster on the RDS instance?

EDIT 27-01-2015: Last night the stored procedure crashed because of an out of memory exception.

I now changed the stored procedure. I wrapped all the inserts in one transaction for every row in the original table. I also made a work around for some select queries I did in the stored procedure so I don't have to do them anymore.

I also gave the RDS instance an SSD drive of 30 GB.

Now the stored procedures runs in 12 minutes on my laptop. But I estimate it will still need 8 hours on the Amazon RDS. How is this big difference possible?

EDIT 28-01-2015: The procedure is still running. Estimated time has now increased to 31 hours and 15 minutes.

When I monitor my RDS instance, the amount Freeable Memory keeps going down. Is it possible that there is a memory leak in a stored procedure?

Your average laptop made in the last couple years will be vastly more powerful than a m1.medium instance. It gives you 1 vCPU and 1.7 GB of RAM, which on the m1 line is "the equivalent CPU capacity of a 1.0-1.2 GHz 2007 Opteron or 2007 Xeon processor".

Consider migrating to a m3 instance - they have much more modern processors - and going up to a larger instance style with more RAM.

How to write store procedures in RDS MySQL DB?

Here is the example:

USE `your_db_name`;
DROP procedure IF EXISTS `procedure_method_name_here`;

DELIMITER $$
USE `your_db_name`$$
CREATE PROCEDURE `procedure_method_name_here` ()
BEGIN
  // sql statement
  SELECT * 
    FROM db_name.table_name
    WHERE id = match_ids;
END$$

DELIMITER ;

Run above command. Your procedure will be created for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM