How can one optimize the database design to avoid constantly adding new tables and improve query efficiency in PHP?

To optimize the database design and avoid constantly adding new tables, consider using a flexible schema design such as Entity-Attribute-Value (EAV) model or JSON column to store dynamic data. This can help reduce the need for creating new tables for every new attribute. Additionally, to improve query efficiency, ensure proper indexing on frequently queried columns and use proper normalization techniques to reduce redundancy in the database.

// Example of using JSON column to store dynamic data in a table
// Create a table with a JSON column to store dynamic attributes
CREATE TABLE products (
    id INT PRIMARY KEY,
    name VARCHAR(255),
    attributes JSON
);

// Insert data with dynamic attributes
INSERT INTO products (id, name, attributes)
VALUES (1, 'Product A', '{"color": "red", "size": "medium"}');

// Query data from JSON column
SELECT * FROM products WHERE JSON_EXTRACT(attributes, '$.color') = 'red';