3.22 Reduce the number and complexity of database queries
Optimize any information provided via databases, both in terms of access schedules and how queries are sent and received.
Criteria
- Database queries: Human-testable
Optimize database queries, especially for frequently accessed information. If you need information that is stored in a database, and you require it or it is likely to be requested more than once in your code, the database should only be accessed once and the data stored locally for subsequent processing. Avoid relying on framework helpers that might defer filtering to later in the process.- get_post_meta
- GPF – General Policy Framework (PDF) – 4.9 – UX and UI (Server Requests)
- GPF – General Policy Framework (PDF) – 6.5 – Front-End (Upload Triggers)
- GPF – General Policy Framework (PDF) – 7.3 – Back-End (Background Processing)
- High Performance MySQL: Query Performance Optimization
- Improving PHP Performance for Web Applications
- Minimizing SQL query response times
- Performance Tuning SQL Queries
- Proactive Energy Management in Database Systems (PDF)
- Query optimization techniques in SQL Server
- SQL Query Optimization
Benefits
- Economic
Optimizing the codebase to avoid pushing multiple additional demands to the server reduces bandwidth overheads, while also reducing the risk of stress failures and lost business. - Environment
Filtering out unneeded data at a deeper level of the application can reduce energy usage, as it reduces the processing required for (de)serialization. - Performance
Holding the data locally rather than remotely eliminates the need to wait for additional requests to process the query. Relational databases and other specialist data stores are usually heavily optimized for data filtering and retrieval. Performing transformations at this level of the application creduces processing time and delivers responses faster.
GRI
- Materials: Low
- Energy: Low
- Water: Low
- Emissions: Low