-2

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

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()

这种方法似乎太慢了。您能否建议任何提高性能的改进?

4

1 回答 1

1

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

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

于 2012-06-08T06:34:44.563 回答