{"id":227,"date":"2007-01-29T02:27:58","date_gmt":"2007-01-29T07:27:58","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2007\/01\/29\/mysql-optimization-tutorial\/"},"modified":"2007-01-29T02:27:58","modified_gmt":"2007-01-29T07:27:58","slug":"mysql-optimization-tutorial","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2007\/01\/29\/mysql-optimization-tutorial\/","title":{"rendered":"MySQL Optimization Tutorial"},"content":{"rendered":"<p>A while back I wrote a little introductory <a href=\"http:\/\/www.juixe.com\/techknow\/index.php\/2005\/10\/23\/mysql-admin\/\">MySQL Administration<\/a> tutorial.  The tutorial had your basic information about starting the database server and creating databases.  Now, I would like to jot down some commands that are useful when trying to optimize a SQL query on MySQL.<\/p>\n<p>The first thing you need is to have a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Database_normalization\">normalized database<\/a>.  This tutorial assumed you do and will not go into how to normalized you database schema.<\/p>\n<p>In my limited experience with MySQL <b>optimization techniques<\/b> I have found that the single most important thing you can do is to create indices for <b>foreign key<\/b> columns and other columns used in the where clause.  Here is the command to create an index.<\/p>\n<p>[source:sql]<br \/>\ncreate index &lt;index_name&gt; on &lt;table_name&gt;(&lt;column_name&gt;);<br \/>\n[\/source]<br \/>\n<!--more--><br \/>\nYou list more than one column names to create an index for more than one column.  To list all the indices on a table you can execute the following command.<\/p>\n<p>[source:sql]<br \/>\nshow index from &lt;table_name&gt;;<br \/>\n[\/source]<\/p>\n<p>And finally, if you would like to delete a index you use the drop command as in the following SQL statement.<\/p>\n<p>[source:sql]<br \/>\ndrop index &lt;index_name&gt; on &lt;table_name&gt;;<br \/>\n[\/source]<\/p>\n<p>Again, as a general rule of thumb all foreign keys should be indexed.  If you feel that the index did not help I recommend you use the <b>explain<\/b> command to view the execution plan of the database engine.  The <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/explain.html\">explain<\/a> command probably deserves a tutorial of its own.  The explain command will return a table detailing the execution plan and the most important column on that table is the type.  If you have a value of ALL on that column your SQL statement will do a full table scan, which you should avoid.  Here is how to use the explain command.<\/p>\n<p>[source:sql]<br \/>\nexplain &lt;sql_statement&gt;;<br \/>\n[\/source]<\/p>\n<p>I also have found that after you delete a lot of rows for a particular table you should <b>optimize<\/b> it.<\/p>\n<p>[source:sql]<br \/>\noptimize table &lt;table_name&gt;;<br \/>\n[\/source]<\/p>\n<p>If you have inserted a great number of rows I would <b>analyze<\/b> the table with SQL similar to the following.<\/p>\n<p>[source:sql]<br \/>\nanalyze table &lt;table_name&gt;;<br \/>\n[\/source]<\/p>\n<p>If this does not help, you can always throw more memory at the problem.  MySQL also has a ton of start up options to control the size of the index cache and other startup parameters with performance issues.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/database\" rel=\"tag\">database<\/a>, <a href=\"http:\/\/technorati.com\/tag\/mysql\" rel=\"tag\"> mysql<\/a>, <a href=\"http:\/\/technorati.com\/tag\/optimization\" rel=\"tag\"> optimization<\/a>, <a href=\"http:\/\/technorati.com\/tag\/query+optimization\" rel=\"tag\"> query optimization<\/a>, <a href=\"http:\/\/technorati.com\/tag\/sql\" rel=\"tag\"> sql<\/a>, <a href=\"http:\/\/technorati.com\/tag\/explain+sql\" rel=\"tag\"> explain sql<\/a>, <a href=\"http:\/\/technorati.com\/tag\/performance\" rel=\"tag\"> performance<\/a>, <a href=\"http:\/\/technorati.com\/tag\/database+normalization\" rel=\"tag\"> database normalization<\/a>, <a href=\"http:\/\/technorati.com\/tag\/foreign+keys\" rel=\"tag\"> foreign keys<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while back I wrote a little introductory MySQL Administration tutorial. The tutorial had your basic information about starting the database server and creating databases. Now, I would like to jot down some commands that are useful when trying to optimize a SQL query on MySQL. The first thing you need is to have a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[19,23,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-3F","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/227"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}