Python中的MySQL查询速度慢,但elsewh速度快

2024-10-01 15:33:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python处理缓慢的MySQL查询。在我的应用程序中,“加载数据填充”的速度很快。另一方面,select查询非常慢。在

在PhpMyAdmin和Navicat中执行相同的查询(作为第二个测试),得到的响应比Python快5倍。在

几句话。。。在

  • 我切换到MySQLdb作为连接器,并且使用SSCursor。没有性能提升。在
  • 数据库被优化,索引等。我将这个应用程序从PHP/Codeigniter移植到Python,在那里它运行得很好(我愚蠢地认为脱离PHP将有助于加快速度)
  • PHP/Codeigniter快速执行select查询。例如,应用程序的一个关键方面在PHP/Codeigniter中需要大约2秒,但是在Python中需要10秒才能完成任何数据分析。在

我的数据库链接相当标准。。。在

dbconn=MySQLdb.connect(host="127.0.0.1",user="*",passwd="*",db="*", cursorclass = MySQLdb.cursors.SSCursor)

如有任何见解/帮助/建议,我们将不胜感激!在

更新

在获取/处理结果方面,我尝试了几种方法。初始查询相当标准。。。在

^{pr2}$

我删除了这个循环中的所有代码,只是为了确保它不是这样的,而且不是。我把伪代码放在它的位置上。整个过程一点也没有加快。在

db_results = "test"

# Loop Results
for row in cursor:

    a = 0 (this was the dummy code I put in to test)

return db_results

查询结果本身只有501行(大量列)。。。在Python之外花了0.029秒。比在Python中花费的时间要长得多。在

这个项目与赛马有关。查询在这个函数中完成。查询本身很长,但是它在Python之外运行得很好。我注释掉了循环中的代码,目的是为了测试。。。还有打印(查询)希望能搞清楚。在

# Get PPs
def get_pps(race_ids):

# Comma Race List
race_list = ','.join(map(str, race_ids))

# PPs Query
query = ("SELECT raceindex.race_id, entries.entry_id, entries.prognum, runlines.line_id, runlines.track_code, runlines.race_date, runlines.race_number, runlines.horse_name, runlines.line_date, runlines.line_track, runlines.line_race, runlines.surface, runlines.distance, runlines.starters, runlines.race_grade, runlines.post_position, runlines.c1pos, runlines.c1posn, runlines.c1len, runlines.c2pos, runlines.c2posn, runlines.c2len, runlines.c3pos, runlines.c3posn, runlines.c3len, runlines.c4pos, runlines.c4posn, runlines.c4len, runlines.c5pos, runlines.c5posn, runlines.c5len, runlines.finpos, runlines.finposn, runlines.finlen, runlines.dq, runlines.dh, runlines.dqplace, runlines.beyer, runlines.weight, runlines.comment, runlines.long_comment, runlines.odds, runlines.odds_position, runlines.entries, runlines.track_variant, runlines.speed_rating, runlines.sealed_track, runlines.frac1, runlines.frac2, runlines.frac3, runlines.frac4, runlines.frac5, runlines.frac6, runlines.final_time, charts.raceshape "
         "FROM hrdb_raceindex raceindex "
         "INNER JOIN hrdb_runlines runlines ON runlines.race_date = raceindex.race_date AND runlines.track_code = raceindex.track_code AND runlines.race_number = raceindex.race_number "
         "INNER JOIN hrdb_entries entries ON entries.race_date=runlines.race_date AND entries.track_code=runlines.track_code AND  entries.race_number=runlines.race_number AND entries.horse_name=runlines.horse_name "
         "LEFT JOIN hrdb_charts charts ON runlines.line_date = charts.race_date AND runlines.line_track = charts.track_code AND runlines.line_race = charts.race_number "
         "WHERE raceindex.race_id IN (" + race_list  + ") "
         "ORDER BY runlines.line_date DESC;")

print(query)

# Run Query
cursor.execute(query)

# Query Fields
fields = [i[0] for i in cursor.description]

# PPs List
pps = []

# Loop Results
for row in cursor:

    a = 0
    #this_pp = {}

    #for i, value in enumerate(row):
    #    this_pp[fields[i]] = value            

    #pps.append(this_pp)

return pps

最后一个音符。。。我还没有考虑处理这个结果的理想方法。我相信一个光标可以让结果作为一组字典返回。我甚至还没有做到这一点,因为查询和返回本身是如此缓慢。在


Tags: andinnumberfordatelinecodetrack
2条回答

虽然你只有501行,但看起来你有超过50列。总共有多少数据从MySQL传递到Python?在

501行x 55列=返回27555个单元格。在

如果每个单元平均“仅”1K,则返回的数据将接近27MB。在

要了解mysql推送的数据量,可以在查询中添加以下内容:

SHOW SESSION STATUS LIKE "bytes_sent"

你的服务器资源充足吗?内存分配是否配置良好?在

我的猜测是,当您使用PHPMyAdmin时,您将得到分页的结果。这就掩盖了MySQL返回的数据超出了服务器的处理能力(我不使用Navicat,不知道如何返回结果)。在

也许Python进程是内存受限的,当面对如此大的结果集时,它必须将页面输出到磁盘以处理结果集。在

如果您减少了调用和/或约束到的列数,比如在查询中使用LIMIT 10,您的查询速度会得到提高吗?在

当你调用Python查询服务器的时候,你能看到页面服务器在运行吗?你能看看Python被分配了多少内存,在这个过程中使用了多少内存,以及这个分配和使用情况与PHP版本中的相同值相比如何吗?在

你能为你有限的资源分配更多的内存吗?在

您能否减少通过分页或异步加载调用的列或行的数量?在

我知道这已经晚了,但是,我在mysql和python中遇到了类似的问题。我的解决方案是使用另一种语言进行查询……我使用R进行盲目快速的查询,在R中尽我所能,然后将数据发送到python,如果需要的话,可以进行更一般的编程,尽管R也有许多通用库。只是想发布一些东西,可能有助于有类似问题的人,我知道这方面的步骤的核心问题。在

相关问题 更多 >

    热门问题