Python sqlite3: Find Slow Queries

    Hello colleagues!
    When working with sqlite databases, I had the task of finding slow queries and logging them.
    Having asked the all-knowing Google, I unfortunately did not find a single solution (did I look badly?).
    Therefore, I want to offer my version of logging.
    UPD: Thanks for the help, the native solution is called APSW.


    We will calculate the execution time of the request at the cursor level.
    To do this, redefine this class by adding a query time count to it.

    import sqlite3
    import time
    class mycursor (sqlite3.Cursor):
        def execute (self, * args, ** kwargs):
            timestart = time.clock ()
            query = super (mycursor, self) .execute (* args, ** kwargs)
            idle = time.clock () - timestart
            if idle> = 0.1:
                file = open ("sqlite_slow.log", "a +")
                file.write (* args)
                file.write ("IDLE =" + str (idle) + "n")
                file.close ()
            return query

    In this example, all requests whose execution exceeds 0.1 second will fall into the log file. Accordingly, if necessary, you can change the criterion.
    The call of the received cursor must be done as follows:

    dbconnection = sqlite3.connect ("some_slqite_base.db)
    dbcursor = dbconnection.cursor (mycursor)
    dbcursor.execute (" SELECT * FROM sqlite_master ")

    Information enters the log file as follows:

    insert into objects ('comment', 'xmlns', 'name') values ​​('Patch number 125124', '', 'patch_object') IDLE = 1.5530665503253545
    insert into advisory_cpe ('advisory_id', 'cpe_id') values ​​('665', '158') IDLE = 0.19326974126357754

    Where the IDLE value is the execution time of the command.
    Thank you for your time. I hope you find this helpful too.

    Also popular now: