doubledee — 2013-05-01T21:12:55-04:00 — #1
I'm curious what impact Indexes have on the performance of MySQL and ultimately my website...
For example, what is the "cost" of a Standard Index in MySQL?
And what is the "cost" of a Unique Index in MySQL?
And how about the "cost" of a Primary Key/Index?
For example, I have a table with the following Fields and associated Indexes...
- id (PK)
- slug (Unique Index)
- name (Unique Index)
I feel guilty having 3 Indexes of a Table with only 5 Fields, but what are you going to do?!
felgall — 2013-05-01T22:35:18-04:00 — #2
indexes slow the updating of the content of the data because the indexes need to be updated as well as the content.
indexes speed up the reading of the data by providing quicker ways of locating what is wanted.
the cost of having or not having an index depends on the ratio of the time saving for reads that are speeded up by it compared to the extra time required by updates that are slowed down by it.
doubledee — 2013-05-01T22:54:45-04:00 — #3
Well, the table I described is really a "lookup" table, so it will rarely get updated.
felgall — 2013-05-02T03:01:15-04:00 — #4
So whatever indexes you add that will speed up those lookups will be making things faster. You only need the ones that are actually going to get used though.
r937 — 2013-05-02T06:12:11-04:00 — #5
if slug and name are supposed to be unique, you aren't going to touch them at all
imagine ensuring their uniqueness in some other way -- you'll end up doing one or two SELECTs before the INSERT, to see if the values are already in the table, and then of course you'll have to wrap them in a transaction to prevent race conditions, etc.
you ~could~ get rid of the id, and use either slug or name as the PK