Git Product home page Git Product logo

queries-not-using-indexes-find's Introduction

Blog Post link: https://pavanyogi.wordpress.com/2019/12/11/find-slow-queries-in-your-mysql-backed-web-application-script-automated-way/

queries-not-using-indexes-find

These python and bash scripts are useful to find missing indexes(slow queries) in a automated way.

  • Assumed python3 is already installed.

  • Assumed Linux, Apache, MySQL is already working perfectly.

  • The application may have written in any programming langauage(PHP, Python, node-js, vue-js etc.). Only pre-condition is- it's database should be mysql

  • Assumed that you have already enabled the slow-query and general-query log in the mysql. If not then enable these queries in the local machine(not on the production):

  • mysql conf file localtion: /etc/mysql/my.cnf
    sudo nano /etc/mysql/my.cnf

  • append the following content to my.cnf file (Below the [mysqld] section)

slow_query_log = 1
slow_query_log_file = /var/log/mysql/localhost-slow.log
long_query_time = 0
log_queries_not_using_indexes
general_log_file = /var/log/mysql/mysql.log
general_log = 1

  • restart the mysql:
    sudo service mysql restart

  • go to required script file locations, from terminal
    cd queries-not-using-indexes-find

  • change permission of localhost-slow.log, mysql.log file
    sudo chmod -v 777 /var/log/mysql/localhost-slow.log /var/log/mysql/mysql.log

  • empty the file localhost-slow.log, mysql.log
    > /var/log/mysql/mysql.log && > /var/log/mysql/localhost-slow.log && echo "clear-all-db-logs"

  • load your web application (eg http://local.myapplication.com) from browser(or postman).

  • do login.

  • do all kind of oprations in your appliation(like all button clicks, form-submission, file-upload, all pages reloads, all api-calls etc.) so that all kind of possible queries gets run on the database and logged into localhost-slow.log (Since long_query_time = 0).

  • run the following command from the terminal
    python3 generateMySqlProfilerScript.py // it will take /var/log/mysql/localhost-slow.log as input. It will extract USE, SELECT, UPDATE, DELETE, INSERT, REPLACE statements. It will prepends EXPLAIN with the query and will output explain-query.txt.

  • run the following command from the terminal
    mysql -vvv -hlocalhost -uadmin -p12345678 mydatabasename < explain-query.txt > explain-output.txt // it will take explain-query.txt as input sql file and will output explain-output.txt with explain statements here admin is the mysql username. 12345678 is the mysql password.

  • run the following command from the terminal
    python3 indexes-mismatching.py > final-output.txt // it will take explain-output.txt as input , will find 'NULL condition true' and 'Not using all keys' statements and will output final-output.txt file.

queries-not-using-indexes-find's People

Contributors

pavanyogi avatar

Watchers

Amitabh Patnaik avatar James Cloos avatar  avatar Karra Raghu Ram avatar

Forkers

duynguyen1879

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.