Surrerstry.pl

Entries Projects About

 Queries Tester in Psycopg2

16 September 2017

Today, I'm putting up a Python script to test how fast our queries will be finished depending on the way of execution.
The script is adapted for postgres by Psycopg2 library.

Please let me show you an example of how it works.

First, we need to have the database where our script will be making queries.
I have one here with one small table inside.

Our simple table
Simple table


On the next screen below, we can see the script in action.

Queries testing
Queries testing

To run the script correctly, we should pass its parameters connection string into our database.
Afterwards, the script will ask us about any necessary information.

First, we must pass our query to execute.
Secondly is about how many times we want to our query to be executed.
The third step needs a little more explanation. Query multiplication for a defined query means the folowing: if there is query defined as: "select count(1) from test", and we multiply it by 2, our query becomes "select count(1) from test;select count(1) from test".
The last step means, for example, that if there are 15 queries and we set commits on 3, then commits will be done after queries 3,6,9,12,15. If we set our commits value to more then the number of queries, there will be no commit.

As a result, we get the time in seconds indicating how long this sequence of queries took.
Additionaly, script saves detailed information in log file.

Source code:
Github Queries Tester in Psycopg2
Onsite queries_tester.py.zip


Posts



Miscellaneous


Surrerstry.pl © 2014-2017-2018