Methods to Increase Page Speed & Reduce Database Queries in CodeIgniter 4
Besides server-side caching with cache()->save()
, here are more effective strategies to optimize site speed and minimize unnecessary database hits in your CodeIgniter 4 project:
1. Full Page Caching
- Store and serve entire HTML responses for pages that rarely change.
- On the first request, render the view and save the output to cache; deliver cache on subsequent requests until expiration.
- Can be done per-controller/method basis.
Example:
$cacheKey = 'homepage_html';
$html = cache($cacheKey);
if (!$html) {
$html = view('inc/header')
. view('home')
. view('inc/footer');
cache()->save($cacheKey, $html, 300);
}
echo $html;
2. Fragment or Partial Caching
- Cache portions of your page (e.g., portfolio slider, navigation, sidebar) individually.
- Useful for components appearing on multiple pages.
3. Query Result Caching
- Use database query caching if supported by your DB driver. This automatically caches result sets.
- In MySQL/MariaDB: leverage query cache or in-memory structures for repetitive queries on unchanging tables.
4. Asset Optimization
- Minimize, combine, and cache CSS/JS files (use HTTP caching headers).
- Use CDN for static assets and images.
5. Database Optimization
- Add proper indexes to columns regularly used in
WHERE
,ORDER BY
, orJOIN
. - Only select columns you need (
select('id, title, image')
) instead offindAll()
. - Paginate large queries to reduce result set size.
6. HTTP and Browser Caching
- Set appropriate HTTP cache headers so repeat visits load static resources from browser cache, not the server.
7. AJAX (Client-Side) Rendering with Cache
- Load non-critical data (like sliders, galleries) via AJAX after initial page load, and cache the results in browser-side storage (
localStorage
,sessionStorage
) for instant re-use on future page visits.
8. Optimize Model and View Cell Layer
- Limit data in all reusable View Cells; avoid loading large datasets when only a few items are needed.
- Use parameterized View Cell calls to control data quantity and prevent unnecessary cache duplication.
9. Efficient Use of Config/Constants
- If some semi-dynamic data changes only with deployment (like menu categories, site settings), load from config files or PHP constants, not from the database on every request.
10. Queue or Lazy Load Heavy Operations
- Offload slow or complex processing (report generation, stats) to queues or background jobs so page requests return quickly.
Summary Table
Technique | Purpose | Typical Use Case |
---|---|---|
Full Page Cache | Serve entire page HTML | Brochure, landing, or unchanging pages |
Fragment/Partial Cache | Cache sections/components | Portfolio carousels, menus, featured lists |
DB Query Cache | Store SQL results | Product/category lists, analytics queries |
Asset/HTTP Cache | Client-side speed | CSS, JS, images, fonts |
AJAX + Browser Cache | Reduce re-fetching, lazy load | Gallery feeds, widgets, load-more buttons |
Combining these strategies provides exponential benefits: user experience improves and server/database load decreases dramatically.