ljjm 发表于 2021-5-19 17:38:26

数据操作执行很慢

做了一个填充数据库的程序,先读取两个个包含男女常用名字的文本,一个常见姓氏文本,随机生成一个名字,根据读取的文本决定男女,之后就是冲数据库中取出地址,根据地址的行政区划编码,随机生成身份证号,
但是上机执行的时候发现执行很慢,每秒向数据库中填充的数据只有20-30条,我预计会有几百条,这还是放在服务器上的结果,如果放在本地,执行效率就只有每秒两三条了,求解如何提高执行速度?
import pymysql
import random
import time
def xmxb():
    with open("姓.txt","r") as f:
      xing = f.read()
    with open("男名.txt","r") as f:
      nanming = f.read()
    with open("女名.txt","r") as f:
      nvming = f.read()
   

    len_mz = 0
    xb = random.randint(0,100)
    sjs = random.randint(0,100)
    if sjs < 60:
      len_mz = 3
    else:
      len_mz = 2
    ##############################
    if len_mz == 3:
      if xb < 50:
            xingbie = "男"
            xingming = random.choice(xing)+random.choice(nanming)+random.choice(nanming)
      else:
            xingbie = "女"
            xingming = random.choice(xing)+random.choice(nvming)+random.choice(nvming)
    else:
      if xb < 50:
            xingbie = "男"
            xingming = random.choice(xing)+random.choice(nanming)
      else:
            xingbie = "女"
            xingming = random.choice(xing)+random.choice(nvming)
    return (xingming , xingbie)

def xzbm():
    db = pymysql.connect(host="www.dfs***.cn", user="*****", password="*****", db="dfzxdb")
    cursor = db.cursor()
    #定义SQL语句
    limit = random.randint(0,614559)
    sql = "select * from `xzqh-cun` limit " + str(limit) + ",1"

    #执行语句
    try:
       #print("正在从远端服务器获取数据。。。")
       cursor.execute(sql)
       results = cursor.fetchall()
       #print(results)
       #for i in results:
            #print(i)
            #print(results)
    except:
      print("连接数据库错误。")

    db.close()
    return(results,results+results+results+results+results)

def csrq():
    jn = random.randint(1,10000)
    if jn == 1:
      nian = random.randint(1890,1900)
    if jn > 1 and jn<30:
      nian = random.randint(1911,1920)
    if jn >= 30 and jn < 100:
      nian = random.randint(1921,1940)
    if jn >= 100 and jn < 2000:
      nian = random.randint(1941,1950)
    if jn >= 2000 and jn < 5000:
      nian = random.randint(1951,1970)
    if jn >= 5000:
      nian = random.randint(1971,2020)
    #nian = random.randint(1890,2020)
    yue = random.randint(1,12)
    if yue == 2:
      tian = random.randint(1,28)
    if yue in :
      tian = random.randint(1,31)
    if yue in :
      tian = random.randint(1,30)
    if yue < 10:
      s_month = "0" + str(yue)
    else:
      s_month = str(yue)
    if tian < 10:
      s_day = "0" + str(tian)
    else:
      s_day = str(tian)
    #print(nian,yue,tian)
    #print(str(nian),s_month,s_day)
    birthday = str(nian) + s_month + s_day
    return(birthday)

def sfzh(xb , birthday , xzbm):
    oum , jim = [] , []
    for i in range(111,995):
      if i % 2 == 1:
            jim.append(str(i))
      else:
            oum.append(str(i))
    dqm = xzbm[:6]
    srm = birthday
    if xb == "男":
      sxm = random.choice(jim)
    else:
      sxm = random.choice(oum)
    if xb == "男" and int(birthday[:4]) < 1920:
      sxm = random.choice()
    if xb == "女" and int(birthday[:4]) < 1920:
      sxm = random.choice()
    sxm1 = str(sxm)
    s17 = dqm + srm + sxm1
    #7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2

    #1-0-X -9-8-7-6-5-4-3-2
    s18list=(1,0,10,9,8,7,6,5,4,3,2)
    yzm = int(s17) * 7 + int(s17) * 9 + int(s17) * 10 + int(s17) * 5 + int(s17) * 8 + int(s17) * 4 + int(s17) * 2 + int(s17) + int(s17) * 6 + int(s17) * 3 + int(s17) * 7 + int(s17) * 9 + int(s17) * 10 + int(s17) * 5 + int(s17) * 8 + int(s17) * 4 +int(s17) * 2
    ys = yzm % 11
    s18 = s18list
    if s18 == 10:
      s_18 = "X"
    else:
      s_18 = str(s18)
    sfzh = s17+s_18
    return(sfzh)
while True:
    xm = xmxb()
    xb = xmxb()
    srm = csrq()
    dqm = xzbm()

    #print(dqm)
    #csrq1 = csrq()
    sfz = sfzh(xb,srm,str(dqm))
    print(xm,xb,sfz,dqm)
    db = pymysql.connect(host = "www.dfs***.cn",user = "*****",password = "*****",db = "dfzxdb")
    cursor = db.cursor()
    sql = "INSERT INTO `rwxx`(xingming , xingbie , shenfenzheng , dizhi) VALUES('"+ xm +"','"+ xb +"','"+ sfz +"','"+ dqm +"')"
    try:
      cursor.execute(sql)
      db.commit()
    except:
      db.rollback()
      print("数据库错误。")
    db.close()

nahongyan1997 发表于 2021-6-23 15:52:50

多线程啊,多创建几个 mysql的用户,一个线程用一个账号

z5560636 发表于 2021-6-25 13:35:02

你先测试,是操作数据库慢了,还是你生成算法慢了。
页: [1]
查看完整版本: 数据操作执行很慢