|
Best Practices: Database Indexing
Tim Perdue
Like most PHP developers out there, I started off using databases to
store relatively simple data structures for my dynamic sites. PHP's
slickness and ease of database connectivity is no doubt a major reason
for its wild success, and also probably a reason why databases get
created and used without a huge amount of design and documentation.
In my next couple of articles, I'm going to attempt to talk about
the best way to use databases with
PHP. I'm not going to cover normalization, as Barry Wise wrote a
fantastic article on that topic.
Instead, I'm going to cover 3 other, arguably less important, advanced
topics:
- Proper use of indices (indexes) - this article
- Transactions - next week
- Foreign Keys - two weeks
Indexing
Let's start with indexing. I was asked the other day by a fairly advanced
programmer what indices are, and that scared me. I knew I had to write
up an article as this programmer is clearly not alone. Tens of thousands of
developers (most probably using MySQL) have little formal training with
databases and yet make a living developing on them for clients.
The very first thing you should do, if you do nothing more, is build an
index on any column in a table that is included in a WHERE clause.
CREATE TABLE mytable ( id serial primary key, category_id int not null default 0, user_id int not null default 0, adddate int not null default 0 );
That's a pretty simple table that will suffice for most of this discussion.
[ Next Page ]
| Comments: | ||
| How does indexing work ? | Andy | 09/30/07 18:56 |
| Large Database with frequent inserts and upda | Krish | 05/19/05 08:03 |
| RE: So what are indexes? | Kenny G. | 04/29/04 19:56 |
| Nice article! | Fabien Papleux | 08/06/03 14:49 |
| php pgm in doubt | pravin | 01/27/03 05:55 |
| RE: Damaged Indexes | devlin | 12/13/02 17:48 |
| So what are indexes? | Amos | 12/05/02 17:42 |
| Damaged Indexes | George Esoimeme | 10/16/02 05:10 |
| question | madEr | 09/10/02 06:38 |
| indexing and optimlzation | Michel Schellekens | 08/12/02 06:05 |
| RE: how to search 1 table with 350 columns ? | Dan Reiland | 07/09/02 13:53 |
| RE: WHERE col LIKE '%df%' | Tim | 07/08/02 07:42 |
| Dyanmic WHERE clauses | Jonathan Chum | 06/06/02 14:38 |
| RE: mysql speed | Son Nguyen | 05/18/02 17:22 |
| WHERE col LIKE '%df%' | A Martin | 05/15/02 18:26 |
| how to search 1 table with 350 columns ? | Pietro | 05/08/02 06:51 |
| Creating Index for Simple MySQL Database | mKarston | 04/23/02 11:57 |
| mysql speed | udayk | 04/07/02 03:27 |
| Postgres optimization - hints needed | Per | 03/22/02 03:04 |
| Indexes | Louise | 03/18/02 14:54 |
| Databases Indexing | Huilee | 03/07/02 21:29 |
| winmysql connection problem. | Jong | 03/04/02 00:00 |
| Foreign Keys - indexed by default ? | Sanjeev Deora | 02/26/02 15:41 |
| What about selects with OR instead of AND | Ryan Barnett | 01/22/02 10:46 |
| example | J. M. Weggemans | 01/22/02 09:24 |
| RE: K-Sensitive login | Bob | 01/21/02 08:59 |
| Re: Indexing - what if I add a row? | James Puddicombe | 11/10/01 05:03 |
| Indexing - what if I add a row? | Jay Vincent | 11/08/01 11:23 |
| RE: Optimization musings-comment threads | syco | 11/05/01 11:57 |
| RE: K-Sensitive login | richard | 09/24/01 17:42 |
| K-Sensitive login | Storsh | 09/12/01 04:53 |
| RE: EXPLAIN: MySQL vs. Postgres | Patrick Massey | 06/25/01 13:44 |
| EXPLAIN: MySQL vs. Postgres | Vincent Driessen | 05/06/01 15:06 |
| RE: Newbie needs a litlle help | JimmiZ | 04/16/01 11:17 |
| RE: Optimization musings-comment threads | Boon | 03/28/01 23:27 |
| RE: Newbie needs a litlle help | Ken Robson | 03/28/01 14:11 |
| Newbie needs a litlle help | Maarten Verheijen | 03/25/01 12:04 |
| Knowing whether mysql used your index | greg | 03/17/01 16:39 |
| Indexing a JOIN | Nathan | 03/13/01 03:57 |
| RE: Optimization musings-comment threads | Phillip Oertel | 03/02/01 15:06 |
| RE: LEFT JOINs under MySQL | rod k | 02/15/01 19:15 |
| LEFT JOINs under MySQL | spikeymikey | 02/15/01 16:54 |
| Multiple ors in sql statement | Wes | 02/09/01 13:05 |
| RE: multi-column indices and MySQL - Tim | rod k | 02/09/01 07:05 |
| RE: multi-column indices and MySQL - Tim | Ben Schumacher | 02/08/01 13:13 |
| pgsql: CLUSTER command | Terence | 02/04/01 01:23 |
| RE: comment threads - and other gripes | Terence | 02/04/01 01:08 |
| RE: Yay indices | Yaron Yogev | 02/03/01 22:34 |
| Yay indices | Anna | 01/31/01 18:37 |
| RE: The how is well explained but why? | doug mackenzie | 01/30/01 21:43 |
| RE: multi-column indices and MySQL | Don Baccus | 01/26/01 21:13 |
| RE: Optimization musings-comment threads | Glenn | 01/22/01 23:06 |
| Prev - Next article links | Will | 01/22/01 08:22 |
| RE: order of fields | Anna | 01/19/01 14:37 |
| RE: Optimization musings-comment threads | Scott Molinari | 01/19/01 13:16 |
| RE: order of fields | Chris Newman | 01/19/01 10:21 |
| order of fields | Anna | 01/19/01 00:16 |
| RE: The how is well explained but why? | Anna | 01/18/01 20:28 |
| RE: Optimization musings-comment threads | Tim Perdue, PHPBuilder.com | 01/18/01 13:39 |
| RE: Optimization musings-comment threads | Scott Molinari | 01/18/01 13:04 |
| The how is well explained but why? | Scott Molinari | 01/18/01 12:40 |
| RE: multi-column indices and MySQL - Tim | Alan L | 01/15/01 09:34 |
| cardinality | Yaron Yogev | 01/14/01 09:23 |
| RE: Index: What for ? | Yaron Yogev | 01/14/01 09:01 |
| RE: Optimization musings | Brian | 01/13/01 20:10 |
| RE: primary key == index ? | Baruch Even | 01/12/01 16:36 |
| primary key == index ? | juozas salna | 01/12/01 13:18 |
| RE: You need to know your data | Chris Newman | 01/12/01 07:29 |
| RE: Optimization musings | Baruch Even | 01/11/01 16:51 |
| RE: Optimization musings | Kirk Parker | 01/11/01 15:07 |
| RE: Optimization musings | Baruch Even | 01/11/01 12:49 |
| RE: Optimization musings | Kirk Parker | 01/11/01 00:40 |
| RE: What about MySQL? - Rod | Brian | 01/10/01 19:10 |
| MySQL vs. Postgres | Jeremy Rempel | 01/10/01 19:05 |
| RE: multi-column indices and MySQL - Tim | Tim Perdue, PHPBuilder.com | 01/10/01 17:37 |
| RE: What about MySQL? - Brian | rod k | 01/10/01 17:31 |
| RE: multi-column indices and MySQL - Tim | rod k | 01/10/01 17:29 |
| RE: Optimization musings | Brian | 01/10/01 12:52 |
| Index: What for ? | Anderson Fortaleza | 01/10/01 12:48 |
| Optimization musings | Baruch Even | 01/10/01 06:31 |
| RE: Order descending without sorting? | Andrew Coldham | 01/10/01 02:34 |
| RE: What about MySQL? | curt | 01/10/01 00:33 |
| RE: What about MySQL? | Tim Perdue, PHPBuilder.com | 01/09/01 20:34 |
| Order descending without sorting? | Mats | 01/09/01 19:14 |
| RE: What about MySQL? | curt | 01/09/01 17:41 |
| RE: A little caution | Björn Brändewall | 01/09/01 16:34 |
| You need to know your data | Frank Feingold | 01/09/01 12:37 |
| What about MySQL? | Brian | 01/09/01 12:06 |
| RE: um... | Paul K Egell-Johnsen | 01/09/01 11:07 |
| um... | Chris Snyder | 01/09/01 10:05 |
| RE: A little caution | Tim Perdue, PHPBuilder.com | 01/09/01 05:57 |
| Confused one.. | Arni | 01/09/01 03:57 |
| Indexes can be temporary | Peter Moulding | 01/09/01 02:26 |
| RE: A little caution | Kirk Parker | 01/09/01 00:39 |
| RE: multi-column indices and MySQL | Tim Perdue, PHPBuilder.com | 01/08/01 23:00 |
| RE: hmm... | Tim Perdue, PHPBuilder.com | 01/08/01 22:59 |
| RE: A little caution | Tim Perdue, PHPBuilder.com | 01/08/01 22:57 |
| multi-column indices and MySQL | rod k | 01/08/01 22:50 |
| A little caution | Kirk Parker | 01/08/01 22:27 |
| hmm... | Andrew | 01/08/01 22:12 |
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||


