Development, Analysis And Research


MySQL & PHP Performance Optimization Tips

Posted in Db, General by Andrew Johnstone on the July 25th, 2007

In high performance web applications you will always have bottlenecks within your application. Identifying these bottlenecks and optimizing is a tedious task and typically show themselves underload. A single bad/unindexed query can bring a server to its knees. A large number of rows will also help to highlight any poor queries, and on very large datasets you may come to the point where you may have to make decisions whether to denormilize database schema.

Explain each page

Whilst I develope sites, I typically print out all queries, EXPLAIN each select statement at the bottom of each page, and highlight it red if its doing a full table scan, temp tables or a filesort. As well as displaying SHOWS INDEXES FROM TABLE…

Not only will it help you to optimize sites, you can also see bad logic and areas to optimize such as a query for each loop when looking through a users table for example.

MySQL indexing optimization

How do you identify where bottlenecks occur?

One of my favourite linux commands lately is the watch command. For Mac users you can get this from macports via “sudo port install watch”. Also a few other handy applications are mysqlreport, mytop.

# Appends file with processlist
watch -n1 "mysqladmin -uroot processlist >>watch.processlist.txt"

# Count the number of locked processes
watch -n1 "mysqladmin -uroot processlist | grep -i 'lock' | wc -l ";

# Count the number of processes sleep
watch -n1 "mysqladmin -uroot processlist | grep -i 'sleep' | wc -l ";

# Run a specific query every second
watch -n1 "mysql -uadmin -p`cat /etc/psa/.psa.shadow` trade_engine --execute "SELECT NOW(),date_quote FROM sampleData WHERE 1=1 AND permission = '755' AND  symbol='IBZL' GROUP BY date_quote;" "

# Emails mysqlreport every 60 seconds
watch -n60 mysqlreport --all --email andrew@email.com

# Displays process list as well as appending the contents to a file
watch -n1 "mysqladmin -uadmin -p`cat /etc/psa/.psa.shadow` processlist | tee -a process.list.txt"

Watching the processlist is very handy in identifying locked, sleeping or sorting process states. If you have a large number of locked processes you typically should change the table type to INNODB, which supports row level locking. if you have a large number of sleeping connections, and you have persistent connections enabled, most likely indicates that connections are not being reused.

Running a specific query every second is exceptionally handy, the example I gave indicates whether one of our crons is correctly functioning and as each row is inserted you can watch something being either inserted or updated. mysqlreport gives numerous peices of information, extremely helpful in identifying issues, you can see more indepth at hackmysql.com/mysqlreportguide.

Look at the mysql slow query log and optimize each query starting with the most common, think whether you have to execute that query at all and use a cache such as memcached.

I also typically tend to look at the following:

  • vmstat -S M
  • ps axl | grep -i ‘mysql’
  • pstree –G
  • free –m

Reference:
http://dev.mysql.com/tech-resources/presentations/presentation-oscon2000-20000719/index.html

90 Responses to 'MySQL & PHP Performance Optimization Tips'

Subscribe to comments with RSS or TrackBack to 'MySQL & PHP Performance Optimization Tips'.


  1. on July 25th, 2007 at 8:26 am

    [...] Johnstone has posted a few handy tips to help you find out where in your application a certain kind of issue might lie - MySQL/PHP [...]


  2. on July 28th, 2007 at 7:45 am

    Test

  3. omry said,

    on July 28th, 2007 at 7:52 am

    Nice post, would be nice if you did some SQL profiling to FireStats and let me know what you find. :).


  4. on July 30th, 2007 at 9:53 am

    [...]  mysql and php performance and optimization tips [...]

  5. Xaprb said,

    on July 30th, 2007 at 10:16 am

    innotop is similar to mytop, but much more capable:
    http://innotop.sourceforge.net/


  6. on August 15th, 2007 at 6:59 am

    [...] MySQL & PHP Performance Optimization Tips [...]

  7. kvz said,

    on October 6th, 2007 at 11:07 am

    Another article about optimizing the performance of apache & php for high traffic loads:
    http://kevin.vanzonneveld.net/techblog/article/survive_heavy_traffic_with_your_webserver


  8. on June 12th, 2008 at 5:32 pm

    [...] MySQL & PHP Performance Optimization Tips [...]


  9. on June 15th, 2008 at 1:42 pm

    [...] MySQL & PHP Performance Optimization Tips [...]


  10. on June 16th, 2008 at 3:52 pm

    [...] MySQL & PHP Performance Optimization Tips [...]


  11. on June 19th, 2008 at 1:38 pm

    [...] MySQL & PHP Performance Optimization Tips [...]


  12. on June 22nd, 2008 at 8:41 am

    [...] MySQL & PHP Performance Optimization Tips [...]


  13. on June 25th, 2008 at 5:19 am

    [...] MySQL & PHP Performance Optimization Tips [...]


  14. on July 2nd, 2008 at 11:18 pm

    [...] MySQL & PHP Performance Optimization Tips [...]


  15. on August 25th, 2008 at 2:01 pm

    Amazine site
    Thanks, webmaster.


  16. on August 25th, 2008 at 11:44 pm

    Nice site
    Thanks, webmaster.


  17. on August 26th, 2008 at 7:39 am

    Beautifull design
    Thanks, webmaster.


  18. on August 26th, 2008 at 7:47 am

    Nice site
    Thanks, webmaster.


  19. on August 26th, 2008 at 10:48 am

    Cool blog
    Thanks, webmaster.


  20. on August 26th, 2008 at 10:56 am

    Amazine site
    Thanks, webmaster.


  21. on August 26th, 2008 at 4:52 pm

    Beautifull design
    Thanks, webmaster.


  22. on August 26th, 2008 at 5:08 pm

    Nice site
    Thanks, webmaster.


  23. on August 26th, 2008 at 11:01 pm

    Nice site
    Thanks, webmaster.


  24. on August 26th, 2008 at 11:10 pm

    Cool blog
    Thanks, webmaster.


  25. on August 27th, 2008 at 8:13 am

    Cool blog
    Thanks, webmaster.


  26. on September 3rd, 2008 at 8:03 am

    Amazing site.
    Thanks, admin.

  27. kdrbo xoql said,

    on September 5th, 2008 at 12:32 am

    dtjie fguhdxpy eszvlcrnh qlpfduho fiblg tbwsryq iwpndk

  28. werewolf said,

    on September 5th, 2008 at 7:13 am

    Cool blog
    Thanks, webmaster.

  29. werewolf said,

    on September 5th, 2008 at 7:22 am

    Nice site
    Thanks, webmaster.

  30. Svetlana said,

    on September 5th, 2008 at 9:42 pm

    Cool blog
    Thanks, webmaster.

  31. Svetlana said,

    on September 5th, 2008 at 10:05 pm

    Beautifull design
    Thanks, webmaster.

  32. pokemon said,

    on September 7th, 2008 at 8:15 am

    Amazing site.
    Thanks, webmaster.

  33. pokemon said,

    on September 7th, 2008 at 12:09 pm

    Nice site.
    Thanks, webmaster.

  34. helmet said,

    on September 8th, 2008 at 5:37 am

    Beautiful design.
    Thanks, webmaster.

  35. helmet said,

    on September 8th, 2008 at 1:33 pm

    Nice site.
    Thanks, admin.

  36. helmet said,

    on September 8th, 2008 at 6:42 pm

    Cool blog.
    Thanks, admin.

  37. helmet said,

    on September 8th, 2008 at 6:47 pm

    Amazing site.
    Thanks, webmaster.

  38. were to buy said,

    on September 11th, 2008 at 9:19 am

    Amazing site.
    Thanks, webmaster.

  39. halloween said,

    on September 11th, 2008 at 1:18 pm

    Beautiful design.
    Thanks, admin.

  40. halloween said,

    on September 11th, 2008 at 1:25 pm

    Cool blog.
    Thanks, webmaster.

  41. halloween said,

    on September 11th, 2008 at 3:52 pm

    Amazing site.
    Thanks, webmaster.


  42. on September 11th, 2008 at 7:52 pm

    Cool blog.
    Thanks, webmaster.


  43. on September 11th, 2008 at 7:57 pm

    Amazing site.
    Thanks, webmaster.


  44. on September 11th, 2008 at 11:43 pm

    Amazing site.
    Thanks, admin.

  45. Elena said,

    on September 12th, 2008 at 2:58 pm

    Amazing site.
    Thanks, webmaster.

  46. Pavel said,

    on September 12th, 2008 at 5:57 pm

    Nice site.
    Thanks, webmaster.

  47. Sveta said,

    on September 12th, 2008 at 10:32 pm

    Nice site.
    Thanks, admin.

  48. Marina said,

    on September 13th, 2008 at 6:21 am

    Beautiful design.
    Thanks, webmaster.


  49. on September 14th, 2008 at 12:24 am

    Nice site
    Thanks.

  50. Marina said,

    on September 14th, 2008 at 5:13 pm

    Amazing site.
    Thanks, webmaster.

  51. Elena said,

    on September 14th, 2008 at 7:32 pm

    Beautiful design.
    Thanks, admin.

  52. Maria said,

    on September 14th, 2008 at 7:37 pm

    Nice site.
    Thanks, webmaster.

  53. Pavel said,

    on September 14th, 2008 at 10:28 pm

    Beautiful design.
    Thanks, admin.

  54. Sveta said,

    on September 14th, 2008 at 10:32 pm

    Cool blog.
    Thanks, admin.

  55. Sveta said,

    on September 15th, 2008 at 1:24 am

    Beautiful design.
    Thanks, webmaster.

  56. Maria said,

    on September 15th, 2008 at 1:28 am

    Cool blog.
    Thanks, webmaster.

  57. Maria said,

    on September 15th, 2008 at 9:08 am

    Nice site.
    Thanks, admin.

  58. Maria said,

    on September 15th, 2008 at 12:06 pm

    Amazing site.
    Thanks, webmaster.

  59. Pavel said,

    on September 15th, 2008 at 12:10 pm

    Amazing site.
    Thanks, admin.


  60. on November 24th, 2008 at 4:05 am

    [...] MySQL & PHP Performance Optimization Tips [...]


  61. on December 1st, 2008 at 6:22 am

    [...] it. Learn how to use explain. A primer for this can be found here. Tune MySQL server settings (1,2,3,and more). Eventually throw in MySQL Proxy and read-write split (read from slave, write to [...]


  62. on December 2nd, 2008 at 9:26 am

    application acceleration…I’ ve had occasion to try out taksi, it worked well for GDI capture, but for Direct3D capture on the engine I used it failed in CTaksiDX9:: GetFrame during GetRenderTargetData. I’ ve found a solution by disabling the avi feature (I didn’ t need i…

  63. andrew said,

    on December 3rd, 2008 at 12:51 am

    1iaDf9 hko9E3dnVxga1jOps


  64. on December 6th, 2008 at 9:17 pm

    Health Insurance Aetna Health Insurance Universal Life Insurance…I didn’t agree with you first, but last paragraph makes sense for me…

  65. Shy_Guy said,

    on December 12th, 2008 at 9:43 am

    Enjoyed your site very much. Thank you! 8-)) Keep up a good work!

  66. Shy_Guy said,

    on December 12th, 2008 at 11:04 am

    Excellent time. And nice site! 8-)) Keep up a good work!

  67. Shy_Guy said,

    on December 12th, 2008 at 12:35 pm

    Perfect time. And nice site! 8-)) Keep up a good work!


  68. on December 20th, 2008 at 5:52 pm

    Good and nice site! ;-) Keep up a good job!


  69. on December 21st, 2008 at 1:10 pm

    I’ll bookmark your site! Keep it up.


  70. on December 22nd, 2008 at 3:40 am

    Excellent and nice site!


  71. on December 24th, 2008 at 6:20 pm

    Enjoyed your site very much. Thank you! ;-) Keep up your site!


  72. on December 24th, 2008 at 6:21 pm

    It is pretty! ;-) Keep up your site!


  73. on December 24th, 2008 at 9:05 pm

    I’ll bookmark your site! ;-) Keep up your site!


  74. on December 24th, 2008 at 9:07 pm

    ;-) Keep up your site!


  75. on December 24th, 2008 at 11:11 pm

    Good and nice site! ;-)) Keep up your site!


  76. on December 25th, 2008 at 12:16 am

    ;-)) Keep up your site!


  77. on December 25th, 2008 at 12:18 am

    Excellent and nice site! ;-)) Keep up your site!


  78. on December 25th, 2008 at 12:24 am

    It is pretty! ;-)) Keep up your site!


  79. on December 25th, 2008 at 3:28 am

    I’ll bookmark your site! ;-)) Keep up your site!


  80. on December 25th, 2008 at 3:43 am

    Marvelous and nice site! ;-)) Keep up your site!


  81. on December 25th, 2008 at 9:23 am

    I love this post and the comparisons you have made.


  82. on December 26th, 2008 at 10:10 am

    Enjoyed your site very much. Thank you! Keep up a good job!


  83. on December 26th, 2008 at 11:13 am

    Keep up a good job!


  84. on December 26th, 2008 at 3:19 pm

    It is pretty! Keep up a good job!


  85. on December 26th, 2008 at 3:41 pm

    Good and nice site! Keep up a good job!


  86. on December 27th, 2008 at 12:04 pm

    I love this post and the comparisons you have made. Totally true and I guess some of this can be used in many applications in life!


  87. on December 27th, 2008 at 7:36 pm

    I like this post and the comparisons you have made. Totally true and I guess some of this can be used in many applications in life!


  88. on December 29th, 2008 at 2:40 am

    I like this site. Totally awesome and I guess this can be very usefull in life!


  89. on December 29th, 2008 at 4:50 am

    I enjoyed this site. Totally awesome and I guess this can be very usefull in life!


  90. on December 29th, 2008 at 4:54 am

    I love this site. Totally awesome and I guess this can be very usefull in life!

Leave a Reply


FireStats iconPowered by FireStats