Trace database queries for your session
Enable module#mod_server_storage if the Development page says it is needed for database tracing. On the Development page, enable Trace all database queries for the current session.
Enable module#mod_server_storage if the Development page says it is needed for database tracing. On the Development page, enable Trace all database queries for the current session.
Reproduce one page load or action in that session and inspect the trace output. Look for repeated queries, unexpectedly large result sets, and queries that dominate the request time. A trace from another session may not include your request.
Turn tracing off when finished. Use the evidence to choose a query to investigate; do not add indexes based on query count alone.
Add a database query when models are not enough
Prefer Zotonic models for resources and edges. Use z_db for application tables or queries that the model APIs do not cover.
Pass parameters separately from SQL text. Never interpolate a request value into SQL. Use a transaction when several database changes must succeed or fail together, and handle transaction failures explicitly.
Inspect the query plan and row count before adding indexes. Test realistic data sizes and keep site separation intact when working with schemas. Database access does not automatically apply resource visibility checks to arbitrary SQL.