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
Human-testable
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.
Resources
- 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
Impact: Medium, Effort: Low
GRI | Impact |
---|---|
materials | Low |
energy | Low |
water | Low |
emissions | Low |
Benefits of this guideline
- Environment: 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.
Example
-
code
$value = get_post_meta( int $post_id, string $key = '', bool $single = false ): mixed -
content
get_post_meta Function.
Tags:
- Networking
- Performance