r/SQL • u/yorusora_ • Aug 15 '26
MySQL Indexing on DB?
Hi people, I haven’t done this before. But are there any downsides of indexing a column on production. Like some query is running very slow, and I figured out that u should put an index on one of the columns. I wouldn’t be here if I had someone experienced to ask from. I’ve a few questions-
- Is it okay to run the query on my sql workbench to add index?
- If something goes wrong what do you people generally do, like taking snapshots or PTIR ?
- Is it safe to run the query directly on the db or usually people run it some other way, like via cli on VM?
- I heard about locking and stuff. But the version I’m using says it won’t lock the DB. But still anything I should test before actually you know doing it live?
I’ve no idea what’s the general procedure and what could go wrong. If someone has done it before, Appreciate any sort of advice or pointers. Thanks
9
Upvotes
15
u/squadette23 Aug 15 '26
You probably need to read this book: https://use-the-index-luke.com/
3
u/ChaosEngine-6502 Aug 15 '26
My background is in MS SQL Server, not mySQL, so take that for what it's worth, but in general, building an index will improve read speeds, but can negatively impact inserts/updates.
Also, the number of rows in the table for the number of columns being indexed will affect how long the index will take to create, and can lock the entire table, which will block other transactions from running. Apparently, MySQL 5.6 and above supports creating indexes online, which will avoid table locks, but could take longer to complete and will lead to lower query performance during the time it takes to build.
I would definitely try building the index on a non-production copy of the database on a separate server to get an idea of timings and do some follow-up testing.
2
u/SQLDevDBA Aug 15 '26
Since you’re indicating that you’re on MySQL I’ll answer from my Oracle knowledge rather than my SQL server knowledge.
When was the last time your tables were analyzed?
What do your explain plans look like for the queries in question?
How often is the table written to, deleted from, and modified?
I’d probably try to run an Analyze/estimate before adding indexes, unless there aren’t any at all (that’s a separate problem).
You should be testing changes like this in a dev or staging environment first to see what breaks or slows down.
https://oneuptime.com/blog/post/2026-03-31-mysql-analyze-table-update-statistics/view
4
u/jshine13371 Aug 15 '26
The high level explanation is indexes trade write performance for read performance. What that means is every time your data changes in the indexed column(s), additional write operations have to happen. That's because an index is a data structure that organizes the data, sorted by that column. But now that the data is sorted, it's exponentially faster to read the data when the database needs to search by that column.
An analogous example is a phone book. The pages are all sorted by name alphabetically, making it really easy to find any particular name and phone number. But if a new person's name is added to the middle of the phone book, now all the names that come after it need to be re-written further down the phone book to make space for the new person to be added.
For most use cases, data is read more frequently than it's written / changed in a database. And very commonly read queries tend to be much heavier because they're doing a lot more operations to serve you the data (since most people don't just directly select the data from the table for all of their queries, there's usually additional operators they use like JOIN, GROUP BY, WHERE, ORDER BY, functions, etc). So most times the increased write overhead of indexes is worth it for the significant read improvement for your queries.
But it's still important you only add the indexes actually necessary to help your queries. Too many or too wide (too many columns in one) indexes can start to cause write contention, and the wrong indexes that aren't actually helping your queries are unnecessary overhead then too. Typically the columns in your
WHEREandJOIN ONclauses are good candidates for being indexed.ORDER BYandGROUP BYclauses can also take advantage of indexes with their columns in them. So that would be a good place to start, but there are a lot of caveats that affect whether a column makes for a good index, or the order of which you specify the columns in your index. E.g. think of a phone book again which is normally ordered byLastNamefirst, then byFirstName. If it was indexed byFirstNamefirst, then byLastNameobviously the data would be sorted differently.2
u/TheKerui Aug 15 '26
If you are brand new to owning tables etc, a single clustered index can go a long way.
Think of the most common where clauses or joins used against the table, create a clustered index on them ordered my the ones with the fewest distinct values.
Ex table named dbo. member_month
Create clustered index yo_soy_speed on dbo.member_month (month, member_id)
1
u/Blecki Aug 15 '26
If its already too slow, why is downtime while the index builds a problem?
1
u/NextVoiceUHear Aug 17 '26
It’s generally a counter productive idea to put an index on a Table Column that has relatively few (6?) possible or different values.
1
u/chocolateAbuser Aug 18 '26
seems to me like you have not been in the field enough to know the answer is it depends
do you have a SLA?
is there a decision log where to write "in date x/y/z i added an index to table T because of reason R"?
is there a cluster or sharding or partitioning?
how many indexes are already there?
what is throughput of db/machine?
is db used by service developed by your company?
if yes, are developers aware of this situation?
how big is the table?
are there periodic backups?
and few other questions; if you're at this point i hope data is few, database is simple, and there should really be no great complications, but you have to understand all of this depends on how this has to be safe and formal
1
u/Frone0910 Aug 18 '26
There is a new feature (at least on postgres) called hypopg that allows you to actually sandbox test what would happen if you applied the index in the explain plan. But in general, EXPLAIN plans for specific queries are the best ways to figure out if the query would break production to run, and which queries are necessary. I've had a lot of luck also pulling the whole schema and sending it along with the query to an LLM and ask it which indexes make sense, what the pros and cons are, etc.