Just learned about indexes, when should I actually use them?

FrostyBear

New Member
Messages
67
Reaction score
0
Location
Brisbane, AU

So I've been messing around with MySQL for a small side project (a personal recipe collection). I read that indexes make queries faster, so I added one to every column. Now my inserts are slow as hell. Did I mess up? How do you decide which columns to index? I'm totally new to this, so any tips would be awesome. Also, is there a limit to how many indexes you can have? I might've gone overboard.

LisaW_

New Member
Messages
53
Reaction score
0
Location
Edinburgh, UK

Ha, I did the exact same thing on a workout log. Indexed every column because more indexes = faster, right? Nope. My inserts crawled and I thought my laptop was dying lol.

From what I've picked up (still a noob), you only index columns you actually filter, sort, or join on. Like if you search recipes by cuisine or sort by prep time, index those. Not every column. Each index has to be updated on every insert, so yeah, that's your slowdown.

There's a max per table (I forget the exact number, maybe 64?) but you'll hit pain way before that. I dropped half mine and inserts felt normal again.

What queries are you running? That might narrow it down.

ChrisM85

New Member
Messages
62
Reaction score
0
Location
Dublin, IE

LisaW_ basically covered it. The "more indexes = faster" thing is the most common trap when you start out. I did the same on a little DB I use to track odds movement and bonus rollovers across a few books, indexed damn near every column, and my inserts went from instant to like a full second per batch. Took me an embarrassingly long time to figure out why.

One thing to add though, "index the columns you filter on" is a decent rule but it breaks down once your queries hit 2 or 3 columns at once. If you're filtering by cuisine AND sorting by prep_time, two separate single-column indexes don't help much, MySQL usually just picks one and ignores the other. You want a single composite index on (cuisine, prep_time) instead. Order matters too, leftmost column first. And don't bother indexing stuff like a boolean "is_vegetarian" column, the cardinality is so low the optimizer will often ignore it anyway.

To your question on the limit: yeah it's 64 secondary indexes per table in InnoDB, but honestly you'll hit real pain way before that. Dropping indexes doesn't hurt anything, so if you're not sure, just kill it and see if any query gets noticeably slower. If nothing does, it was dead weight. Run EXPLAIN on your actual queries too, that tells you way more than guessing.

What kind of queries are you actually running on the recipe table? That'd make it easier to say which ones are worth keeping.

nightowl87

New Member
Messages
60
Reaction score
0
Location
Portland, OR

Wait, the composite index thing just blew my mind a little. I've got queries like "show me all the slow-cooker recipes sorted by rating" and I was doing exactly what ChrisM85 said, two separate indexes and wondering why it still felt sluggish. So the order in (cuisine, prep_time) actually matters? Like if I flip it to (prep_time, cuisine) it changes whether MySQL can use it?

Also on the "just drop it and see if anything slows down" advice, how do you even tell with a tiny recipe DB? My whole table is like 200 rows so everything feels instant either way. Do I just... not worry about it until the data grows? Feels like I'd be flying blind.

ahmed_khan92

New Member
Messages
63
Reaction score
0
Location
Toronto, ON

Yeah the order matters, and it's not subtle. (cuisine, prep_time) and (prep_time, cuisine) are two completely different indexes. MySQL can only use the leftmost part, so if your index is (prep_time, cuisine) and your query filters on cuisine first, it basically can't use it at all. Rule of thumb I got burned by: put the column you filter with = first, the one you sort/range on second. If you're doing WHERE cuisine = 'thai' ORDER BY rating, then (cuisine, rating) is what you want.

On the 200 rows thing though, I'll push back a little on the "just drop it and see" advice for a tiny table. At 200 rows MySQL is gonna do a full table scan anyway and the optimizer will happily ignore your index, so you literally can't tell if dropping it hurt anything. That doesn't mean don't learn indexes now, just that your test is meaningless at this size. If you want to actually see what's happening, slap EXPLAIN in front of your query and look at the key column. If it says NULL, your index isn't being used and it's just dead weight slowing your inserts.

Honestly at 200 recipes I'd keep maybe one or two indexes that match your real queries and call it done. Come back to it when you've got 50k rows and things actually feel slow. There's a backend section here if you wanna dump your schema and get roasted, people post this stuff all the time:

https://websiteforums.info/forums/php-and-backend-development.6/

You must log in or register to reply here.