📜 ⬆️ ⬇️

Python sqlite3: Finding slow queries

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

image


Counting the time of the request, we will keep at the cursor level.
To do this, override this class by adding to it the count of the query execution time.
')
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 that exceed 0.1 seconds will get 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 ")


The log file information is as follows:

insert into objects ('comment', 'xmlns', 'name') values ​​('Patch number 125124', 'http://oval.mitre.org/XMLSchema/oval-definitions-5#solaris', '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 command execution time.
Thank you for your time. I hope you find this useful too.

Source: https://habr.com/ru/post/137677/


All Articles