Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

How do you optimize slow database queries in PHP applications? PHP applications में slow database queries को कैसे optimize करें?

Answer
TechniqueHow it helps
Add indexes on frequently queried columnsAvoids full table scans
Select only needed columnsReduces data transfer, avoids SELECT *
Use EXPLAIN to analyze queriesShows whether indexes are actually used
Eager load relationshipsAvoids N+1 problem
Paginate large result setsAvoids loading thousands of rows at once
// Analyzing a slow query
EXPLAIN SELECT * FROM orders WHERE customer_id = 5 AND status = 'pending';

// Adding a composite index
CREATE INDEX idx_customer_status ON orders (customer_id, status);

// Selecting only needed columns
$users = User::select('id', 'name', 'email')->get();
Techniqueकैसे मदद करता है
Frequently queried columns पर indexesFull table scan से बचाता है
ज़रूरी columns ही select करेंData transfer कम करता है
EXPLAIN से query analyze करेंIndex इस्तेमाल हो रहा है या नहीं दिखाता है
EXPLAIN SELECT * FROM orders WHERE customer_id = 5 AND status = 'pending';

CREATE INDEX idx_customer_status ON orders (customer_id, status);

Was this answer clear?