3.24 Run Fewer, Simpler Queries As Possible
Making multiple requests whether HTTP or within a database has a carbon cost as infrastructure has to send that information back and forth. As such, managing how you store and use data locally for a visitor will help reduce wasted cycles.
Criteria
- Database Queries: If you need information that is stored in a database, and you require it (or it’s likely to be requested) more than once in your code, access the database only once, and store the data locally for subsequent processing. Also, avoid reliance on framework helpers that might defer filtering to later on in the process.
Impact
Medium
Effort
Low
Benefits
- Environmental:
Filtering out unneeded data at a deeper level of the application may reduce energy usage, as less processing is required for (de)serialization. - Performance:
By holding the data locally rather than remotely, you can avoid waiting for an additional HTTP request to occur to process the string. Relational databases and other specialist data stores are generally heavily optimized for data filtering and retrieval. Performing transformations at this level of the application may lead to reduced CPU time and faster responses. - Economic:
Rather than pushing multiple additional demands to the server (which could lead to stress failures and lost business), an optimized codebase can reduce bandwidth overheads.
GRI
- materials: Low
- energy: Low
- water: Low
- emissions: Low
Example
- Code:
$value = get_post_meta( int $post_id, string $key = '', bool $single = false ): mixed
- get_post_meta Function.
Resources
- [GPFEDS] 4.9 – UX and UI (Server Requests) (PDF)
- [GPFEDS] 7.3 – Back-End (Background Processing) (PDF)
- [GPFEDS] 6.5 – Front-End (Upload Triggers) (PDF)
- High Performance MySQL: Query Performance Optimization
- Improving PHP Performance for Web Applications
- Minimizing SQL query response times
- Performance Tuning SQL Queries
- Query optimization techniques in SQL Server
- SQL Query Optimization