{"id":1527,"date":"2018-01-19T19:22:56","date_gmt":"2018-01-19T19:22:56","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/best-practices-database-indexing\/"},"modified":"2018-01-19T19:24:51","modified_gmt":"2018-01-19T19:24:51","slug":"best-practices-database-indexing","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/best-practices-database-indexing\/","title":{"rendered":"Best Practices: Database Indexing"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Tim Perdue<\/div>\n<div class=\"\">on January 8, 2001<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/tim2.jpg\" class=\"articleAuthorImg\" alt=\"picture of Tim Perdue\" height=\"90\" width=\"100\" align=\"left\"\/><\/p>\n<div class=\"articlePara\">\nLike most PHP developers out there, I started off using databases to<br \/>\nstore relatively simple data structures for my dynamic sites. PHP&#8217;s<br \/>\nslickness and ease of database connectivity is no doubt a major reason<br \/>\nfor its wild success, and also probably a reason why databases get<br \/>\ncreated and used without a huge amount of design and documentation.<\/div>\n<div class=\"articlePara\">\nIn my next couple of articles, I&#8217;m going to attempt to talk about<br \/>\nthe best way to use databases with<br \/>\nPHP. I&#8217;m not going to cover normalization, as Barry Wise wrote a<br \/>\n<a href=\"barry20000731.html\" target=\"_blank\">fantastic article<\/a> on that topic.<\/div>\n<div class=\"articlePara\">\nInstead, I&#8217;m going to cover 3 other, arguably less important, advanced<br \/>\ntopics:<\/div>\n<div class=\"articleList\">\n<ol>\n<li>Proper use of indices (indexes) &#8211; this article<\/li>\n<li>Transactions &#8211; next week<\/li>\n<li>Foreign Keys &#8211; two weeks<\/li>\n<\/ol>\n<\/div>\n<h2>Indexing<\/h2>\n<div class=\"articlePara\">\nLet&#8217;s start with indexing. I was asked the other day by a fairly advanced<br \/>\nprogrammer what indices are, and that scared me. I knew I had to write<br \/>\nup an article as this programmer is clearly not alone. Tens of thousands of<br \/>\ndevelopers (most probably using MySQL) have little formal training with<br \/>\ndatabases and yet make a living developing on them for clients.<\/div>\n<div class=\"articlePara\">\nThe very first thing you should do, if you do nothing more, is build an<br \/>\nindex on any column in a table that is included in a WHERE clause.<\/div>\n<div class=\"articlePara\">\n<pre>\nCREATE TABLE mytable (\nid serial primary key,\ncategory_id int not null default 0,\nuser_id int not null default 0,\nadddate int not null default 0\n);\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nThat&#8217;s a pretty simple table that will suffice for most of this discussion.\n<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tim200101104658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tim200101109ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"tim200101104658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Tim Perdue on January 8, 2001 Like most PHP developers out there, I started off using databases to store relatively simple data structures for my dynamic sites. PHP&#8217;s slickness and ease of database connectivity is no doubt a major reason for its wild success, and also probably a reason&#8230; <a href=\"https:\/\/phpbuilder.com\/best-practices-database-indexing\/\" class=\"readmore\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1527","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1527"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1527\/revisions"}],"predecessor-version":[{"id":2271,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1527\/revisions\/2271"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}