question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

can i get how long one sql execute time

See original GitHub issue

I want to known sql execute time, but logging not show.

just like :

10.12 ms SELECT * FROM `person` WHERE `name` = 'Tom' LIMIT 1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
moudaycommented, Mar 10, 2021

You can try monkey-patching Database.execute_sql() or subclass your database implementation and override the execute_sql() method to add timing information.

thank you, i impl code as :

# -*- coding: utf-8 -*-

import math
import time
from functools import wraps

from peewee import MySQLDatabase

import logging


logger = logging.getLogger('peewee')

def timer(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time() * 1000
        ret = func(*args, **kwargs)
        end_time = time.time() * 1000
        logger.debug("time: %.2f ms" % math.ceil(end_time - start_time))
        return ret

    return wrapper


class CustomMySQLDatabase(MySQLDatabase):
    @timer
    def execute_sql(self, sql, params=None, commit=SENTINEL):
        return super().execute_sql(sql, params, commit)


0reactions
moudaycommented, Mar 10, 2021

Just a suggestion: you might try using something like this for the start/end timings:

https://docs.python.org/3.9/library/time.html#time.perf_counter

thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Measure the time it takes to execute a t-sql query
I usually stick SET @t1 = GETDATE(); at the top of my query and then paste SET @t2 = GETDATE();SELECT 'NOTE 1',DATEDIFF(millisecond,@t1,@t2) AS...
Read more >
How to Make Query Execution Faster in SQL Server
The answer is that SQL Server offers a method to get execution times measured at the millisecond level which is 1000x more precise...
Read more >
Usage details of the SET STATISTICS TIME ON statement in ...
In the Execution Times section of the SET STATISTICS TIME ON statement output, we can find out the time taken by the SQL...
Read more >
Collect SQL Server Query Execution time in seconds
Finally, if you know the exact query, you can run it in SSMS and look at the actual execution plan. Highlight the select,...
Read more >
How to determine the execution time of a query through ... - IBM
Answer · 1. Create a sql file, query.sql, containing the list of SQL statements, separated by semicolon (';'), that you want to execute...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found