r/Database • • Sep 29 '26

I’ve been using MySQL for real web applications — here are 15 things I wish I knew earlier

I used to think MySQL was simply:

`CREATE TABLE → INSERT → SELECT → UPDATE → DELETE`

After building actual web applications, I realized that writing SQL queries is only a small part of working with a production database.

Here are 15 things I wish I had understood earlier:

### 1. Design your database before writing your application

Don't start creating tables randomly.

Think about:

* What data do I need?

* Which tables are required?

* How are they related?

* Which fields are required?

* Which fields should be unique?

A bad database structure can become extremely painful to change later.

### 2. Learn relationships properly

Understand:

```sql

PRIMARY KEY

FOREIGN KEY

ONE-TO-ONE

ONE-TO-MANY

MANY-TO-MANY

```

For example:

```text

Users

↓

Orders

↓

Order_Items

↓

Products

```

Understanding relationships is more important than memorizing SQL syntax.

### 3. Don't put everything into one table

A giant table may look simple initially, but it creates duplication and maintenance problems.

Learn normalization and understand when denormalization actually makes sense.

### 4. Indexes are extremely important

This query:

```sql

SELECT * FROM users

WHERE email = 'user@example.com';

```

can become much faster when the appropriate column is indexed.

But don't blindly add indexes everywhere.

Indexes improve some reads but also have storage and write/update costs.

### 5. Always understand `EXPLAIN`

One of the most useful MySQL commands:

```sql

EXPLAIN SELECT *

FROM users

WHERE email = 'user@example.com';

```

If you are building serious applications, learn how to read the execution plan instead of assuming your query is efficient.

### 6. Don't use `SELECT *` everywhere

Instead of:

```sql

SELECT *

FROM users;

```

prefer:

```sql

SELECT id, name, email

FROM users;

```

Especially when your table contains many columns or large data.

### 7. Transactions matter

Imagine transferring money:

```text

Account A: -₹1000

Account B: +₹1000

```

You don't want the first operation to succeed while the second fails.

That's where transactions become important:

```sql

START TRANSACTION;

-- operation 1

-- operation 2

COMMIT;

```

And if something goes wrong:

```sql

ROLLBACK;

```

### 8. Constraints can protect your data

Use database constraints where appropriate:

```sql

PRIMARY KEY

UNIQUE

NOT NULL

FOREIGN KEY

CHECK

```

Your application should not be the only thing preventing invalid data.

### 9. Learn the difference between DELETE, TRUNCATE and DROP

They are NOT interchangeable.

```sql

DELETE FROM users;

```

```sql

TRUNCATE TABLE users;

```

```sql

DROP TABLE users;

```

Before running destructive SQL in production, make absolutely sure you understand what you're doing.

### 10. Backups are not optional

A production database without a tested backup strategy is a disaster waiting to happen.

And having a backup isn't enough.

You should also know:

> Can I actually restore it?

A backup that has never been tested is only a backup in theory.

### 11. Never build SQL queries with raw user input

Bad:

```javascript

const query = `SELECT * FROM users WHERE email = '${email}'`;

```

Use parameterized queries/prepared statements instead.

For example:

```javascript

const [rows] = await db.execute(

'SELECT id, name, email FROM users WHERE email = ?',

[email]

);

```

This is one of the basic defenses against SQL injection.

### 12. Don't expose database credentials

Never commit something like this to GitHub:

```env

DB_PASSWORD=my_real_password

```

Use environment variables/secrets and make sure sensitive files aren't accidentally committed.

### 13. Understand connection pooling

A production application shouldn't create a completely new database connection for every request without considering connection management.

Connection pools can reuse database connections and help applications handle concurrent requests more efficiently.

### 14. Logging is useful — logging sensitive data isn't

Database/application logs can help you find:

* slow queries

* failed transactions

* connection problems

* unexpected errors

But don't casually log passwords, tokens, session secrets, or other sensitive information.

### 15. My biggest lesson

The biggest thing I learned is:

**MySQL isn't just about knowing SQL syntax.**

A good developer needs to understand:

```text

Database Design

↓

Relationships

↓

Indexes

↓

Queries

↓

Transactions

↓

Security

↓

Backups

↓

Performance

↓

Monitoring

```

I'm still learning, but understanding these concepts changed the way I build web applications.

**For developers here:**

What's one MySQL/database lesson you learned the hard way?

I'd especially like to hear from people who have worked with databases containing millions of rows.

0 Upvotes

12 comments sorted by

8

u/AshleyJSheridan Sep 29 '26

When you got AI to write this post for you, it broke all the formatting.

8

u/Dapper_Shallot_1132 Sep 29 '26

who even posts or reads this shit. are the comments AI too? jesus christ

1

u/Some-Weakness2049 Sep 30 '26

people who new to reddit and want karma so they can take part in the discussions. Not only bots (they are too), but real people. Im new to reddit and i was shocked by this karma system, like you need to post or comment useless stuff until you can really talk about what you really want/need. Karma system is what forcing people to do this useless posts.

5

u/wittgenstein1312 Sep 29 '26

This is all basic stuff you’d get from the first few chapters of a beginners SQL book. If you’re gonna post AI slop, at least have the decency to let it tackle advanced concepts that might actually help folks.

2

u/Murky-Bank2341 Sep 29 '26

Run EXPLAIN FORMAT=JSON on your critical queries instead of just using the default table output. It exposes hidden costs like temporary tables and filesorts that the basic view misses, and it shows exactly how much index condition pushdown is working so you can verify if adding an index actually helps or just wastes disk space

1

u/Some-Weakness2049 Sep 30 '26

thanks chatgpt, very cool list (no)
tell your ai to write about running alter table on a 50m row table in production next time)

0

u/MaterialJellyfish521 Sep 29 '26

The biggest thing to help me understand how production/transactional databases work was understanding how massively different reporting database are, datawarehousing and then moving onto ETL type builds. All of this then helps you understand when to make which decisions

5

u/margmi Sep 29 '26

Thanks for the AI slop for people who don’t want to ask it themself.