What are some best practices for structuring database tables in PHP to avoid performance issues when running complex queries?
When structuring database tables in PHP to avoid performance issues when running complex queries, it is important to normalize the database by breaking down data into smaller, related tables. This helps reduce redundancy and improves query performance. Additionally, indexing columns that are frequently used in queries can significantly speed up data retrieval.
// Example of creating a normalized table structure in PHP
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(50),
email VARCHAR(100)
);
CREATE TABLE posts (
id INT PRIMARY KEY,
user_id INT,
title VARCHAR(100),
content TEXT,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE INDEX idx_user_id ON posts(user_id);