I am executing the following code 25 times before calling commit (very abbreviated).
def query_and_insert(conn):
c = conn.cursor()
result = c.execute("SELECT ...")
result = modify_result(result)
c.execute("INSERT ...".format(result))
result.close()
if __name__ == "__main__":
conn = MySQLDB.connect(...)
loop 25 times:
query_and_insert(conn)
conn.commit()
conn.close()
After the first 15 or so queries, the speed slows down significantly (goes from < 1 second per query/insert combo to > 20 seconds). The problem definitely isn’t the complexity of the queries I’m executing; moving the last 10 queries to the beginning has them running just as fast, and then the last queries again slow down significantly.
Is there some sort of throttling or decreased priority for processes that take too long on AWS? Is there any way around this?
Edit: We are using a MicroInstance on Amazon EC2. There is no issue with building table size because we are doing the inserts on an empty table.
What you’re describing is normal behavior for micro instances. See more in the Micro Instances documentation. In short, micro instances have access to a fair amount of CPU — but once you’ve used it for more than a very brief period, you get throttled hard. If this is adversely affecting your application, then it’s simply not a good fit for micro instances, which are intended for low-CPU applications and/or applications where users will not notice added latency.
Micro instances are the only instances that are subject to this type of throttling, so upgrading to a
m1.smallwould be one way out. Since everyt1.microinstance is EBS-backed, can upgrade from the EC2 console by stopping the instance (not terminating), selecting a larger instance using “Change Instance Type”, and starting up again. This will result in a new internal IP/hostname, as well as a new public IP/hostname unless you’re using elastic IPs, but you’ll get consistent amounts of CPU.