减少给定数据库处理Python 2.7程序的执行时间。

时间:2012-06-08 06:27:29

标签: mysql performance python-2.7

以下函数从数据库中提取数据:

def findid(name,parent):
    conn = MySQLdb.connect (host = "localhost",
                       user = "arunkamaly",
                        passwd = "code",
                       db = "se")
    cursor=conn.cursor()

    cursor.execute(" select xbrl_id from se_xbrl_concepts where xbrl_name=%s;",name)
    name=cursor.fetchone()
    cursor.execute(" select xbrl_id from se_xbrl_concepts where xbrl_name=%s;",parent)
    pname=cursor.fetchone()
    cursor.close()
    if pname==None:
            return name[0],0
    return name[0],pname[0]

这里使用了上面的函数,但是引入了insert方法:

def prsentparse():
    conn = MySQLdb.connect (host = "localhost",\
                       user = "arunkamaly",\
                        passwd = "code",\
                       db = "se")
    cursor=conn.cursor()

    f = open(csvfile, 'rb')
    spamReader = csv.reader(f, delimiter=',', quotechar='"')
    for clist in spamReader:
            if clist[0]=='LinkRole' or clist[0] =='' or clist[0]=='prefix':
                    continue
            name=clist[0]+':'+clist[1]

            parent=clist[6].strip()
            xid,pid=findid(name,parent)
            prio=0 if clist[5].strip()=='' else clist[5]
            order=0 if clist[4].strip()=='' else clist[4]
            depth=0 if clist[3].strip()=='' else clist[3]
    #print clist
            #cursor.execute("INSERT INTO se_xbrl_presentation (xbrl_id,xbrl_parent_id,priority,order,depth) VALUES (%s,%s,%s,%s,%s);",(xid,pid,prio,order,depth) )
    #sql = "insert into se_xbrl_presentation (xbrl_id, xbrl_parent_id, priority, order, depth) values (" + xid + ", " + pid + ", " + prio + ", " + order + ", " + depth + ");"
    try:
        cursor.execute("INSERT INTO `se_xbrl_presentation`(`xbrl_id`, `xbrl_parent_id`, `priority`, `order`, `depth`) VALUES (%s,%s,%s,%s,%s);",(xid,pid,prio,order,depth) )
    except MySQLdb.Error,e:
        print "mysql Error %d:%s"%(e.args[0],e.args[1])
conn.commit
cursor.close()

这种方法似乎太慢了。你能否提出任何提高绩效的建议?

1 个答案:

答案 0 :(得分:1)

您正在findid函数中打开一个连接。这意味着您在for循环的每次迭代中都打开了一个连接。相反,将已有的连接传递给findid函数,您不必每次都打开它。

另一件可能很重要的事情是:se_xbrl_concepts表的xbrl_name字段中是否有索引?你在for循环的每次迭代中都做了两次选择。