{"id":2679,"date":"2010-05-11T15:31:20","date_gmt":"2010-05-11T13:31:20","guid":{"rendered":"http:\/\/localhost\/helpcentre\/?p=2679"},"modified":"2022-11-15T13:26:30","modified_gmt":"2022-11-15T11:26:30","slug":"index-database-optimization","status":"publish","type":"post","link":"https:\/\/xneelo.co.za\/help-centre\/website\/index-database-optimization\/","title":{"rendered":"Database Indexes for database optimization"},"content":{"rendered":"<p>If you are looking into<strong> troubleshooting the performance of a database<\/strong> and the queries that run on it, you can make use of the database feature, <strong>database indexes<\/strong>. This feature if used efficiently, by ensuring indexes are configured and being used correctly in your queries, may help in improving query response times and avoid any unnecessary use of more expensive newer hardware.<\/p>\n<p><b>Database table example:<\/b><\/p>\n<p><u>Table name: customers<\/u><\/p>\n<table style=\"border: 1px solid #c0c0c0; width: 100%; border-collapse: separate; background-color: #ffffff;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">Name<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">Type<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">Size<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">Description<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">customer_id<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">char<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">10<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">NOT NULL<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">name<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">varchar<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">40<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">surname<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">varchar<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">40<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">phone_number<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">int<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">11<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>To find the customer Dave\u2019s phone number with the customer_id = \u2018274178\u2019, you would need to:<\/p>\n<div id=\"action_points\">\n<ol>\n<li>Run a SQL query<\/li>\n<li>SELECT phone_number FROM customers WHERE customer_id = \u2018274178\u2019<\/li>\n<li>To find the correct record MySQL would have to search through all the records in the entire table i.e. possibly searching through thousands of entries.<\/li>\n<\/ol>\n<\/div>\n<p><strong>Database Indexes <\/strong>work in the same way as a book index. The index helps the reader find the relevant information quickly by searching the index i.e. instead of going through each page of the book until the correct information is found. Tables may have multiple indexes applied to them. Each index may reference one or more columns. The best way to see where indexes will provide benefit is to see which columns are referenced in the \u201cWHERE\u201d portion of the query.<\/p>\n<p><b>Explain command:<\/b><\/p>\n<p>EXPLAIN is a command that provides some insight into the methods MySQL will use to optimise a query and its output can be a useful tool to identify and remove obstacles. Using EXPLAIN is as simple as adding the keyword EXPLAIN at the start of a query.<\/p>\n<p><b>Example:<\/b> The following will demonstrate the \u2018explain command\u2019 by investigating the table above:<\/p>\n<p>EXPLAIN SELECT customer_id,name,surname FROM customers WHERE customer_id = \u2018274178\u2019;<\/p>\n<table style=\"border: 1px solid #c0c0c0; width: 100%; border-collapse: separate; background-color: #ffffff;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">table<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">type<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">possible_keys<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">key<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">key_len<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">ref<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">rows<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">Extra<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">CUSTOMERS<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">ALL<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">NULL<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">NULL<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">NULL<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">283211<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">2<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">where used<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Looking at the <b>possible_keys<\/b> the output is <b>null<\/b>, which indicates there are no Indexes currently and that MySQL has to search 283211 records to return the result.<\/p>\n<p><b>Create index example:<\/b><\/p>\n<p>CREATE INDEX indx_customer_id ON customer (customer_id);<\/p>\n<p>If we re-run the EXPLAIN the output changes to:<\/p>\n<table style=\"border: 1px solid #c0c0c0; width: 100%; border-collapse: separate; background-color: #ffffff;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">table<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">type<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">possible_keys<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">key<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">key_len<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">ref<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">rows<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">Extra<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">customers<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">const<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">PRIMARY<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">PRIMARY<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">10<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">const<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\">1<\/td>\n<td style=\"border: 1px solid #c0c0c0; padding: 5px;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Comparing the output to the previous explain output, you\u2019ll notice that the <b>possible_keys<\/b> field now says <b>PRIMARY<\/b> and only had to search 1 row to find the record.<\/p>\n<p><b>Note:<\/b> Indices can be on any column on the table<\/p>\n<p>For additional information about database indexing and database optimizing refer to the following:<\/p>\n<ul>\n<li><a title=\"Optimizing Database Structure\" href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/optimizing-database-structure.html\" target=\"new\" rel=\"noopener noreferrer\">Optimizing Database Structure<\/a><\/li>\n<li><a title=\"MySql Indexes\" href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/mysql-indexes.html\" target=\"new\" rel=\"noopener noreferrer\">MySql Indexes<\/a><\/li>\n<\/ul>\n","protected":false,"plain":"If you are looking into<strong> troubleshooting the performance of a database<\/strong> and the queries that run on it, you can make use of the database feature, <strong>database indexes<\/strong>. This feature if used efficiently, by ensuring indexes are configured and being used correctly in your queries, may help in improving query response times and avoid any unnecessary use of more expensive newer hardware.\r\n\r\n<b>Database table example:<\/b>\r\n\r\n<u>Table name: customers<\/u>\r\n<table  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\r\n<tbody>\r\n<tr>\r\n<td >Name<\/td>\r\n<td >Type<\/td>\r\n<td >Size<\/td>\r\n<td >Description<\/td>\r\n<\/tr>\r\n<tr>\r\n<td >customer_id<\/td>\r\n<td >char<\/td>\r\n<td >10<\/td>\r\n<td >NOT NULL<\/td>\r\n<\/tr>\r\n<tr>\r\n<td >name<\/td>\r\n<td >varchar<\/td>\r\n<td >40<\/td>\r\n<td ><\/td>\r\n<\/tr>\r\n<tr>\r\n<td >surname<\/td>\r\n<td >varchar<\/td>\r\n<td >40<\/td>\r\n<td ><\/td>\r\n<\/tr>\r\n<tr>\r\n<td >phone_number<\/td>\r\n<td >int<\/td>\r\n<td >11<\/td>\r\n<td ><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nTo find the customer Dave\u2019s phone number with the customer_id = \u2018274178\u2019, you would need to:\r\n<div id=\"action_points\">\r\n<ol>\r\n \t<li>Run a SQL query<\/li>\r\n \t<li>SELECT phone_number FROM customers WHERE customer_id = \u2018274178\u2019<\/li>\r\n \t<li>To find the correct record MySQL would have to search through all the records in the entire table i.e. possibly searching through thousands of entries.<\/li>\r\n<\/ol>\r\n<\/div>\r\n<strong>Database Indexes <\/strong>work in the same way as a book index. The index helps the reader find the relevant information quickly by searching the index i.e. instead of going through each page of the book until the correct information is found. Tables may have multiple indexes applied to them. Each index may reference one or more columns. The best way to see where indexes will provide benefit is to see which columns are referenced in the \u201cWHERE\u201d portion of the query.\r\n\r\n<b>Explain command:<\/b>\r\n\r\nEXPLAIN is a command that provides some insight into the methods MySQL will use to optimise a query and its output can be a useful tool to identify and remove obstacles. Using EXPLAIN is as simple as adding the keyword EXPLAIN at the start of a query.\r\n\r\n<b>Example:<\/b> The following will demonstrate the \u2018explain command\u2019 by investigating the table above:\r\n\r\nEXPLAIN SELECT customer_id,name,surname FROM customers WHERE customer_id = \u2018274178\u2019;\r\n<table  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\r\n<tbody>\r\n<tr>\r\n<td >table<\/td>\r\n<td >type<\/td>\r\n<td >possible_keys<\/td>\r\n<td >key<\/td>\r\n<td >key_len<\/td>\r\n<td >ref<\/td>\r\n<td >rows<\/td>\r\n<td >Extra<\/td>\r\n<\/tr>\r\n<tr>\r\n<td >CUSTOMERS<\/td>\r\n<td >ALL<\/td>\r\n<td >NULL<\/td>\r\n<td >NULL<\/td>\r\n<td >NULL<\/td>\r\n<td >283211<\/td>\r\n<td >2<\/td>\r\n<td >where used<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nLooking at the <b>possible_keys<\/b> the output is <b>null<\/b>, which indicates there are no Indexes currently and that MySQL has to search 283211 records to return the result.\r\n\r\n<b>Create index example:<\/b>\r\n\r\nCREATE INDEX indx_customer_id ON customer (customer_id);\r\n\r\nIf we re-run the EXPLAIN the output changes to:\r\n<table  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\r\n<tbody>\r\n<tr>\r\n<td >table<\/td>\r\n<td >type<\/td>\r\n<td >possible_keys<\/td>\r\n<td >key<\/td>\r\n<td >key_len<\/td>\r\n<td >ref<\/td>\r\n<td >rows<\/td>\r\n<td >Extra<\/td>\r\n<\/tr>\r\n<tr>\r\n<td >customers<\/td>\r\n<td >const<\/td>\r\n<td >PRIMARY<\/td>\r\n<td >PRIMARY<\/td>\r\n<td >10<\/td>\r\n<td >const<\/td>\r\n<td >1<\/td>\r\n<td ><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nComparing the output to the previous explain output, you\u2019ll notice that the <b>possible_keys<\/b> field now says <b>PRIMARY<\/b> and only had to search 1 row to find the record.\r\n\r\n<b>Note:<\/b> Indices can be on any column on the table\r\n\r\nFor additional information about database indexing and database optimizing refer to the following:\r\n<ul>\r\n \t<li><a title=\"Optimizing Database Structure\" href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/optimizing-database-structure.html\" target=\"new\" rel=\"noopener noreferrer\">Optimizing Database Structure<\/a><\/li>\r\n \t<li><a title=\"MySql Indexes\" href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/mysql-indexes.html\" target=\"new\" rel=\"noopener noreferrer\">MySql Indexes<\/a><\/li>\r\n<\/ul>"},"excerpt":{"rendered":"<p>If you are looking into troubleshooting the performance of a database and the queries that run on it, you can make use of the database feature, Database Indexes.<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"lsx_disable_title":"0","_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","footnotes":""},"categories":[180,166],"tags":[755,757,759],"topics":[10405],"class_list":["post-2679","post","type-post","status-publish","format-standard","hentry","category-mysql","category-website","tag-optimise-database","tag-index","tag-mysql-indexes","topics-databases"],"acf":[],"additional_meta":{"category_title":[{"term_id":180,"name":"MySQL","slug":"mysql","term_group":0,"term_taxonomy_id":180,"taxonomy":"category","description":"Using MySQL for web applications ","parent":168,"count":9,"filter":"raw","term_order":"92","cat_ID":180,"category_count":9,"category_description":"Using MySQL for web applications ","cat_name":"MySQL","category_nicename":"mysql","category_parent":168},{"term_id":166,"name":"Website","slug":"website","term_group":0,"term_taxonomy_id":166,"taxonomy":"category","description":"About your Website(s)","parent":0,"count":169,"filter":"raw","term_order":"120","cat_ID":166,"category_count":169,"category_description":"About your Website(s)","cat_name":"Website","category_nicename":"website","category_parent":0}],"tag_title":[{"term_id":755,"name":"optimise database","slug":"optimise-database","term_group":0,"term_taxonomy_id":755,"taxonomy":"post_tag","description":"","parent":0,"count":4,"filter":"raw","term_order":"2779"},{"term_id":757,"name":"index","slug":"index","term_group":0,"term_taxonomy_id":757,"taxonomy":"post_tag","description":"","parent":0,"count":2,"filter":"raw","term_order":"2802"},{"term_id":759,"name":"mysql indexes","slug":"mysql-indexes","term_group":0,"term_taxonomy_id":759,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw","term_order":"2824"}]},"featured_image_src":null,"author_info":{"display_name":"marketing","author_link":"https:\/\/xneelo.co.za\/help-centre\/author\/marketing\/","author_avatar":"https:\/\/secure.gravatar.com\/avatar\/a6ea315e112423b2b955cb020fbce2b0835956c6ad85ff0f13f1db298977eaaa?s=96&d=mm&r=g"},"_links":{"self":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts\/2679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/comments?post=2679"}],"version-history":[{"count":0,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts\/2679\/revisions"}],"wp:attachment":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/media?parent=2679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/categories?post=2679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/tags?post=2679"},{"taxonomy":"topics","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/topics?post=2679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}