【问题标题】:Print console output of the run time of a pymysql query打印 pymysql 查询运行时的控制台输出
【发布时间】:2021-04-28 14:51:42
【问题描述】:

我正在使用pymysql 执行一些sql 插入查询。我正在使用LOAD DATA INFILE,它工作正常。

通常当您运行 SQL 查询时,它会在底部打印行数和时间。如果它是SELECT 查询,它将打印它之前的表。我知道一些有助于打印表格的第三方库。我正在寻找一种方法来打印包含行数和时间的最后一行。

这是一个示例 -

MySQL  localhost:33060+ ssl  world_x  JS > shell.options.set('resultFormat','table')
MySQL  localhost:33060+ ssl  world_x  JS > session.sql("select * from city where countrycode='AUT'")
+------+------------+-------------+---------------+-------------------------+
| ID   | Name       | CountryCode | District      | Info                    |
+------+------------+-------------+---------------+-------------------------+
| 1523 | Wien       | AUT         | Wien          | {"Population": 1608144} |
| 1524 | Graz       | AUT         | Steiermark    | {"Population": 240967}  |
| 1525 | Linz       | AUT         | North Austria | {"Population": 188022}  |
| 1526 | Salzburg   | AUT         | Salzburg      | {"Population": 144247}  |
| 1527 | Innsbruck  | AUT         | Tiroli        | {"Population": 111752}  |
| 1528 | Klagenfurt | AUT         | Kärnten       | {"Population": 91141}   |
+------+------------+-------------+---------------+-------------------------+
6 rows in set (0.0030 sec)

我想使用pymysql 打印6 rows in set (0.0030 sec)。我已经尝试过使用EXPLAIN,但这仅适用于SELECT 命令。我试过使用fetchone()fetchall()fetchmany(),但没有成功。

【问题讨论】:

标签: mysql pymysql


【解决方案1】:

据推测它已弃用,但从 MySql 8.0 开始,您仍然可以使用以下方式打开分析:

set profiling = 1;

但是,让我们向前思考吧;见Query Profiling Using Performance Schema

所以,如果connpymysql 连接:

cursor = conn.cursor()
# enable "profiling":
cursor.execute("UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%events_statements_%'")
cursor.execute("UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%events_stages_%'")

# execute your query
cursor.execute("select * from city where countrycode='AUT'")
rows = cursor.fetchall()

# get execution time for last query:
cursor.execute("""SELECT TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT
                        FROM performance_schema.events_statements_history_long
                        WHERE SQL_TEXT like '%select * from city where countrycode%'""")
total_time = cursor.fetchone()[0]

# code to print out the rows from your query
n_rows = len(rows) # number of rows returned
# other code to display rows omitted

# then finally:
print(f'{n_rows} row(s) in set ({total_time} sec)')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 2011-02-23
    • 2013-07-28
    • 1970-01-01
    • 2015-01-05
    • 2014-09-06
    相关资源
    最近更新 更多