2013-01-16 31 views
1

我正在使用mysql.connector和一些任务,例如在本地mysql数据库中插入数据的过程是0.0436846秒。平均来说,有一些改进方法吗?Python-Mysql,更好的lib还是更好的方法?

import mysql.connector 
from mysql.connector import errorcode 
    class Dal: 
     @profile 
     def __init__(self, dic): 
      try: 
       self.cnx = mysql.connector.connect(user=dic['user'], 
            password=dic['password'], 
            host=dic['host'], 
            database=dic['database']) 
      self.cursor = self.cnx.cursor() 

      except mysql.connector.Error as err: 
       if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: 
        print("User/Pass Fails") 
       elif err.errno == errorcode.ER_BAD_DB_ERROR: 
        print("BD no exist") 
       else: 
        print(err) 


     def insert_event(self,dic): 
      ins_ev = ("INSERT INTO events " 
         "(device, type, index, datetime, c1,\ 
         c2, vel, head, pfm, age,vod,created_on) " 
         "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" 
         ) 
      self.cursor.execute(ins_ev, dic) 
      self.cnx.commit() 

也许另一种方法?

+1

为什么你需要改进它?什么是有意义的改进?是否有机会将事件批量插入到一起,或者它们真的只能在系统环境中逐个零碎地完成?是插入直接遇到问题的时间,还是更多的是调用者的响应性问题,所以您可以通过线程化实际插入操作来解决您的问题? –

+0

他们真的是一个一个地完成......也许有人确实处理了一个更好的方法来做到这一点...... – chespinoza

+1

您的问题等同于“需要我太长时间才能到商店,我怎么能让我的购物更快完成?“除非我们有足够的信息来判断您是否可以使用替代路线,更快速的驾驶,发送其他人或其他东西,否则很难给您答案。 –

回答

2

mysql.connector是一个纯Python模块。 MySQL-python或oursql中的任何一个都可以为C语言库提供更好的性能。其他优化包括禁用索引和执行延迟插入。

相关问题