Mysql optimisation tool - mysql

Can anyone suggest a good MYSQL optimization tool which helps in finding the bottlenecks in a long query and hence help in optimization?? I am looking for a query profiler.
thanks...

Well, You mean Query Optimization? I guess EXPLAIN <query> is excellent in giving hits as to where the bottlenecks are. After which redefine you indexes & ...
UPDATE1: You could check out - MySQL optimization tools
UPDATE2: After digging up in my code, I see that I used to do 2 things for query optimization.
Turn on Slow Query Log - MySQL can record expensive SQL queries in the slow query log. You can define your expectations in seconds using parameter long_query_time.
mysqldumpslow command - After logging is turned on you can analyze the log contents using mysqldumpslow command. mysqldumpslow /path/to/your/mysql-slow-queries.log -t 10. This will show you top 10 performance killers. For each statement in the output you can see the number of identical calls, execution time in seconds, rows affected and the statement itself.

Related

SQL - Query Performance

How to find the inefficient queries in mysql database ? I want to do performance tuning on my queries , but i coudn't find where my queries are located ? Please suggest me where can i find mysql queries for my tables .
Thanks
Prabhakaran.R
You can enable the general log and slow query logs.
Enabling general query log will log all the queries and might be heavy if you have many reads/writes. In slow query log, you can mention a threshold and only queries taking time beyond some time will be logged. Post that, you can manually analyze it or you can use tools provided( Percona has great tools)
Have you analyzed your queries with explain plans? You may be able to find a query that will return the result set you wish that isn't as heavy a load on the query engine. Remember to only select the columns you actually need (try to avoid SELECT *), well planned indexing and to use inner/outer joins in favour of a huge list of WHERE clause filters.
Good luck.
RP
In addition to what the others said, use pt-query-digest (from percona.com) to summarize the slowlog. That will tell you the worst queries.
Performance tuning often involves knowing what indexes to put on tables. My index cookbook is one place to learn how to build an INDEX for a given SELECT.

Mysql Statspack Equivalent

Logging inefficient queries and query frequency in MySQL
We have a pour performing mysql DB. And I'm interested in logging queries and their frequency to assess which ones my time is best spent on. We have slow query logging but these queries aren't the ones hammering the DB. I've been told statspack for Oracle does exactly that with some sort of weighting on the queries.
Any ideas or suggestions would be appreciated.
The MySQL general query log will show all statements being executed. On a busy server, this will grow quickly, and will slow performance, so you likely want to enable it briefly, and then disable it again. That will give you every statement executed, as well as client connects and disconnects.
http://dev.mysql.com/doc/refman/5.5/en/query-log.html
The slow query log can also be a useful tool.
There are some third party tools that can assist with analyzing the output from the general log, the slow query log, and the output from continually running a SHOW FULL PROCESSLIST. One example is MONyog.

Any command in mysql equivalent to Oracle's autotrace for performance turning

In oracle sql plus, while doing performance testing, I would do
set autotrace traceonly:
which would display the query plan and statistics without printing the actual results. Is there anything equivalent in mysql?
No, there's no equivalent available in MySQL, at least not in the community edition.
MySQL does not implement the kind of "instrumentation" that Oracle has in its code; so there's no equivalent to an event 10046 trace.
You can preface your SELECT statement with the EXPLAIN keyword, and that will produce output with information about the execution plan that MySQL would use to run the statement, but that's just an estimate, and not a monitoring of the actual execution.
You can also enable the slow query log on the server, to capture SQL statements that take longer than long_query_time seconds to execute, but that really only identifies the long running queries. That would give you the SQL text, along with elapsed time and a count of rows examined.
To get the query plan, just add EXPLAIN to the beginning of a SELECT query.
EXPLAIN SELECT * FROM table
It also estimates the number of rows to be read if that's what statistics you're talking about.

how to determine slow queries?

My site is experiencing a really slow loading time. I have suspected that it might be javascript or php that causes longer loading time but i have tested my site in YSlow and its grade is B which i think is not bad.
Now i want to check my database if something is wrong with queries, database indexing that causes my site to load slower.
Is there some tutorials or tricks i might read or try to test database to figure out if there is slow queries? Any tips for database management?
I always fall back to 2 mantras for faster query execution -
Indexes, indexes and indexes.
Try to get rid of JOINS as much as possible.
There are some tried and tested methods to weed out slow queries. You need to turn on slow query log. This logs all those queries which take more than x seconds to execute. x is specified by you in mysql.conf.
Once the slow queries start logging in the log. You can analyse each query using EXPLAIN and appropriately add indexes to speedy the query execution.
I have a thin database abstraction layer on top of PDO (formerly on top of MySQL) that I've baked simple query logging into which I can switch on and off - it allows me to get a report of the queries and how long each one took. Thus rather than having a simple cut-off - something is either a long query or it's not - I get to see all my query times.
MySQL's slow query log is good, but its one-second resolution is not enough for my needs. To me, a lot of the time, a query that takes 200 milliseconds is an indication of something wrong.
I'm showing my age here. After a quick check of the MySQL manual, it turns out that MySQL's long_query_time can be specified down to microsecond resolution since MySQL 5.1.27! Nonetheless, my method is still handy.
Good tutorial for sql performance - focused on MySql. For joins, if you can't get rid of them - use the right one: LEFT JOIN, RIGHT, INNER, OUTER

how to check log in mysql

How would i monitor a MySQL to detect SELECTs which are running slowly? Having identified a poorly performing SELECT, how would i analyse it with a view to improving it?
You would enable slow query logging, e.g. add this to your my.cnf under the [mysqld] section
set-variable=slow_query_log=on
set-variable=long_query_time=20
This will log all queries taking 20 seconds or more to a "hostname"-slow.log, e.g. /var/lib/mysql/localhost-slow.log
You'd then inspect those queries, and at least run EXPLAIN on them, and figure out what makes it slow, e.g. add indexes, rewrite the SQL etc. - though optimizing queries is a whole other book.
MySQL keeps a 'slow query log' which will provide you with information about slow running queries.
I think it's enabled by default, and its location is set in your MySQL .ini file.
See here for more information on the slow query log.
You can use EXPLAIN to get information about how MySQL is executing your queries - just put EXPLAIN before your query. There is a good article here on how to interpret the results.