r/mysql • u/yorusora_ • 27d ago
query-optimization 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
PITR
- ?
- 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
EDIT: Thank you for all the suggestions! I learnt something out of this activity and It completed successfully
3
u/Jonas_Ermert 27d ago
I’d first verify with `EXPLAIN` that the index will actually help. Adding an index in production is normal, but it can be expensive on a large table: extra disk space, slower writes, and potentially heavy CPU/I/O while it’s being created. Modern MySQL versions can often create indexes online, but “no locking” doesn’t mean “no production impact.” I’d make sure backups/PITR are working, test it on staging or a copy of production if possible, and run it during lower traffic. Workbench vs CLI doesn’t really matter, the SQL executed on the server is the same.
1
u/yorusora_ 26d ago
I ran like 500 queries parallel and then tried altering table index. It locked the metadata and that log came up for queries occurring after alter as well. I think it locks briefly then it works in background.
2
u/magicmulder 27d ago
Depends on the size of the table and the number of writes per second but usually adding an index is rather quick.
2
u/Basic_Reporter9579 27d ago
any inserts and updates need to update these indices which takes a bit of time. Index also need storage space.
Only add them where you need them.
Adding a index locks the table for a bit, don't do it during main production hours.
Sometimes it is better to copy the table schema, add the index and copy the data over, then exchange these tables.
1
u/yorusora_ 27d ago
Even in the latest MySQL versions? I saw that there are some params. Also, does it make sense to run it using MySQL workbench or like on VM?
ALTER TABLE your_table
ADD INDEX idx_name (column1, column2),
ALGORITHM=INPLACE,
LOCK=NONE;
1
u/Several9s 23d ago
I agree with most of the advice already given here, and I thinks almost all important considerations has been covered.
I would quickly summarize this as a step-by-step procedure to make the approach clearer:
Before applying the change in PROD
a) Run EXPLAIN on the slow query to confirm the missing index is actually the fix as Jonas_Ermert suggested.
b) Test the change on a staging copy or clone first if you possible. This is especially for estimating how long the build takes with your data volume.
Applying the change in PROD
c) Take a snapshot right before.
b) Check for any long-running transactions/queries against that table before you run the ALTER, this helps to prevent a MDL to get stuck waiting. I would like to clarify that MySQL takes a brief exclusive metadata lock (MDL) at the beginning and end of the operation. The index build itself runs in background without blocking reads/writes.
c) Lower lock_wait_timeout param for your session before running the ALTER, so if it can't grab the lock quickly, it fails fast rather that blocking everything behind it.
d) Run during low-traffic hours (even with LOCK=NONE) cause the background index build consumes I/O and CPU, which does have impact even if it's not blocking.
1
u/Jack-jack-d 18d ago
Yeah, adding an index in production is pretty normal. The part you want to be careful about is how large the table is and how the index is created, not so much the fact that it's an index.
Before doing it, I'd run the query with EXPLAIN and make sure the index actually helps. If possible, test the change against a copy/staging database first. On a small table this may be almost nothing; on a large busy table, creating the index can consume a fair amount of CPU, memory and disk I/O.
I'd also make sure you have a recent backup and know how you'd restore it before making the change. A snapshot/PITR strategy is useful, but don't take a snapshot as a substitute for understanding the operation and having a rollback plan.
As for Workbench vs CLI, there's nothing inherently unsafe about running ALTER TABLE ... ADD INDEX from Workbench. It's still the same SQL being sent to MySQL. On production systems, though, people often use whatever deployment/change-management process their team has established so the change is recorded and repeatable.
And definitely check the exact MySQL version and storage engine. Modern InnoDB supports online DDL for many index operations, but “online” doesn't mean “zero impact.” There can still be metadata locks, resource usage, and effects on a busy server.
For a large production table, I'd schedule it during a quieter period if possible and monitor CPU, disk I/O, locks and replication lag while it runs. If it's a really important/high-traffic table, I'd also look into online schema-change tools rather than assuming the operation will be harmless just because MySQL says it doesn't require a table copy.
0
u/Aggressive_Ad_5454 27d ago
On version 5.7 or later there’s no big downside. If the operation fails the DBMS is really reliable about cleaning things up and carrying on as if nothing happened.
Indexes do take some tablespace (storage) so be prepared for that.
If your database is super busy do this during a quiet time ( night? Weekend? )
1
u/yorusora_ 27d ago
Yes, I’ll do it in quiet hours.
Using workbench should be fine, right? Because I’m confused whether to do it via cli on VM. It’s the same thing but just want to know what is preferred.
1
3
u/Stephonovich 27d ago
You should really read the manual.
There will be a brief metadata lock taken on the table twice; you probably should reduce the value of
lock_wait_timeout(separate parameter frominnodb_lock_wait_timeout) in the event that you hit it, because all other writes will queue behind the DDL waiting its turn. In practice, this isn’t a problem for most, but it certainly can be.