optimizing mysql_ log slow queries - web development stuff
DESCRIPTION
Optimizing MySQL_ Log Slow QueriesTRANSCRIPT
-
1/29/2015 Optimizing MySQL: Log Slow Queries - Web Development Stuff
http://www.webdevstuff.com/112/optimizing-mysql-log-slow-queries.html 1/4
Home
AboutSubmit
Search
//webdevelopmentstuff
ready-to-use resources, tutorials, tips and many other web development related stuff
Knowledges Licences
Resources
www.webdevelopmentstuff.com changed to www.webdevstuff.com
Optimizing MySQL: Log Slow Queries
Posted in Tutorials on Jun 22nd, 2009 | 8 responses
Tagged as mysql, performance
This is a second part of MySQL optimizing tutorial mini series. As the title indicates, were going focus on MySQL slow queries and their logging. Well use
the official MySQL manual for version 5.0 as a refernce. After read of this post you should be able to set up logging of MySQL slow queries for further useand be familiar with its basic output.
What Is It?
As mentioned earlier, in the first Optimizing MySQL part, the logging and evaluation of MySQL slow queries is one of the most important optimization
techniques. The slow query log consists of all SQL statements that took more than long_query_time seconds to execute. The time to acquire the initialtable locks is not counted as execution time. By exploring a slow query log we can get very interesting data to analyze. Results of such analysis may have
positive consequences on MySQL performance. But before we start check something we must have it available.
Turn it ON
Logging of slow queries is builtin capability of mysqld (MySQL deamon) which physically makes these entries. To enable mysql log slow queries, we havetwo choices how to do that:
Start mysqld with option --log-slow-queries[=file_name] from the command line
Start mysqld with option log-slow-queries=1 in the MySQL configuration file (usually /usr/local/etc/my.cnf)
If you do not specify the optional parameter file_name in the first case, MySQL creates a new file with default name host_name-slow.log and startswrite slow queries in there. The second case does not allow you to specify the filename and slow queries are written into the slow_log files and are very
friendly checkable as a classic MySQL table of default mysql database. Actually, you can find slow queries files in the /var/db/mysql/mysql/ directory
in both cases. Before well move forward, it would be usefull to mention the fields of slow queries log.
Slow Log Table Fields
start_time: datetime when a query starts
-
1/29/2015 Optimizing MySQL: Log Slow Queries - Web Development Stuff
http://www.webdevstuff.com/112/optimizing-mysql-log-slow-queries.html 2/4
user_host: host on which a query was executedquery_time: how long a query took
lock_time: how long a table lock took
row_sent: number of rows sent back to caller
row_examined: number of rows scanneddb: name of database on which a query was executed
last_insert_id: not applicable, used if AUTO_INCREMENT set up
insert_id: not applicable, used if AUTO_INCREMENT set up
server_id: mysql server identificationsql_text: text of sql statement (which actually causes the slow query)
As you can see from the fields above, an information about slow query is good enough to use it for optimization. Maybe I forgot explain one important detail
mentioned in the second paragraph of this post: the selection criteria for slow query entry is long_query_time option set in the mysql config file. I have this
option set to 5 seconds (long_query_time=5 right before the log_slow_queries in the my.cnf file) as shown below.
Summary
The slow query log can be used to find queries that take a long time to execute and are therefore candidates for optimization. However, examining a long
slow query log can become a difficult task. To make this easier, you can process the slow query log using the mysqldumpslow command to summarize thequeries that appear in the log or explore the slow_log table entries by means of classic phpMyAdmin web interface. Theres also CSV file in the/var/db/mysql/mysql/ directory available for data export and further manipulation.
8 Responses to Optimizing MySQL: Log Slow Queries
1. David says:
June 25, 2009 at 9:05 am
Could we use waits here? I forgot what the command is in PHP to initiate a wait. You could also sort the slow queries by which took the longest,
giving you a priority list.
2. Teddy says:June 25, 2009 at 9:23 pm
I dont know what exactly do you think about, but PHP uses the sleep() function to delay execution.
3. Shahriat Hossain says:July 5, 2009 at 4:27 pm
Nice post but you can also optimize your query using profiling system which is now a days most popular technique.
4. Teddy says:
July 5, 2009 at 11:16 pm
Yes Shahriat, I agree with you but another optimizing techniques are getting ready, so stay tuned! Nevertheless, any suggestion for new parts of this
MySQL optimization mini series is appreciated as well!
5. web design sussex says:July 6, 2009 at 1:18 am
-
1/29/2015 Optimizing MySQL: Log Slow Queries - Web Development Stuff
http://www.webdevstuff.com/112/optimizing-mysql-log-slow-queries.html 3/4
I am new to PHP having moved over from .NET. Im glad I read this article as I have many queries that seem to run slow in our CMS package which
have been identified using the slow log process. I just need to know how to optimize them now! Great post thanks!
6. sonny says:May 27, 2010 at 1:52 pm
How come insert the slow log queries info in slow log table as shown above?
7. Luke says:
August 31, 2010 at 6:30 pm
I have the same query in multiple places, I would like to find out what file the query is in in the log. Is this possible?
8. Teddy says:
August 31, 2010 at 6:40 pm
Yes, of course its possible. Use some file searching utility grep for an instance
Categories
Resources
CMS SoftwareCode BytesFrameworks
FunnyGraphics
LibrariesTemplatesWeb Applications
KnowledgesHow-ToNews
ReferencesStandards
ToolsTutorials
Licences
Academic FreeApache Licence
BSD LicenceCC Licence
Commercial StuffFree StuffGPLLGPLMIT Licence
Open SourceOther License
Uncategorized
Tag Cloud
ajax asp browsers charts colors css dhtml dom drupal e-commerce ecma editors effects extension flash fonts graphs html icons javascriptjoomla jquery layout mambo mysql online oscommerce performance photo photoshop php plugin programming prototype ruby securitysocial sql video w3c wordpress xhtml xml youtube zencartRecent Posts
Best Particles for Websites Landing PagesServlet 3.0: Java Web ApplicationRuby on Rails 1.0 Released
ASP.NET MVC At A GlanceZimbra: Takes Web E-mail Offline
Recent Comments
Hao on MySQL DROP DATABASE RecoveryRencontre on How to Center Layers in Photoshop
-
1/29/2015 Optimizing MySQL: Log Slow Queries - Web Development Stuff
http://www.webdevstuff.com/112/optimizing-mysql-log-slow-queries.html 4/4
2008 - 2015, webdevstuff.com | Powered by Wordpress | Theme by Elegant Themes | Valid XHTML and CSS | Subscribe to RSS
Guo on How to Center Layers in PhotoshopPiemol on Optimizing MySQL: Importance of JOIN OrderJarred on Optimizing MySQL: Importance of JOIN Order