What is CI4's database query profiling?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

CI4 provides several tools for database query profiling. The Debug Toolbar's Database tab shows all queries executed during a request with their execution times, affected rows, and the line of code that triggered them — the primary tool for identifying N+1 problems. DB debugging: in app/Config/Database.php, set $DBDebug = true to throw exceptions on database errors instead of failing silently. Query logging: $db->query("...") then $db->getLastQuery() returns the last executed query. Explain queries: run EXPLAIN on slow queries to check index usage: $db->query("EXPLAIN " . $originalQuery). Connection info: $db->getConnectDuration() for connection time. For production profiling, enable MySQL's slow query log with long_query_time = 1 to catch queries over 1 second. Use the CI4 Benchmark class to time specific operations.

Pro Tip

This topic has CodeIgniter-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.