【问题标题】:Why query taking more time to execute.为什么查询需要更多时间来执行。
【发布时间】:2017-03-24 10:32:04
【问题描述】:

当我执行删除查询时,执行需要更多时间。 我正在从 csv 文件中读取参数,并使用参数从数据库中删除数据。 我的删除查询是,

delete_query = 'delete from app_outage_dfr_correlation_report_exclusions where start_time= %s AND stop_time= %s AND gil_site_id = %s AND total_time_in_mins = %s;'
args = (start_time,stop_time,gil_site_id,total_time_in_mins,)
cursor.execute(delete_query,args)

当我签入 cProfile 时,它​​显示以下结果。

1/8    0.000    0.000    0.000    0.000 {method 'join' of 'str' objects}
       46    0.000    0.000    0.000    0.000 {method 'lower' of 'str' objects}
        1    0.000    0.000    0.000    0.000 {method 'lstrip' of 'str' objects}
       13    0.000    0.000    0.000    0.000 {method 'match' of '_sre.SRE_Pattern' objects}
        4    0.000    0.000    0.000    0.000 {method 'pop' of 'dict' objects}
        1   11.135   11.135   11.135   11.135 {method 'query' of '_mysql.connection' objects}
        1    0.000    0.000    0.000    0.000 {method 'release' of 'thread.lock' objects}
       27    0.000    0.000    0.000    0.000 {method 'remove' of 'list' objects}
        9    0.000    0.000    0.000    0.000 {method 'remove' of 'set' objects}
       66    0.000    0.000    0.000    0.000 {method 'replace' of 'str' objects}
        3    0.000    0.000    0.000    0.000 {method 'setter' of 'property' objects}

谁能告诉我为什么执行需要更多时间。谢谢。

【问题讨论】:

    标签: python mysql cprofile


    【解决方案1】:

    尝试使用 EXPLAIN SQL 语句检查您的查询,可能您的表确实很大,并且查询需要很长时间才能执行。

    【讨论】:

    • 在我的表中有 4Lacs+ 记录可用。 @Oleg Butuzov
    • 因此(对于包括我在内的所有非印度人)您的表中有超过 400000 条记录。而你运行 delete 需要 11 秒,这已经是很多了。我建议优化你的表和 sql。 python中没有问题,数据结构和sql语句中的问题。在查询时对它进行成像检查每一行 - 这显然很慢..
    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多