{"id":7316,"date":"2025-07-23T05:44:31","date_gmt":"2025-07-23T05:44:31","guid":{"rendered":"https:\/\/impulsebyte.com\/blogs\/?p=7316"},"modified":"2025-07-23T05:49:48","modified_gmt":"2025-07-23T05:49:48","slug":"increase-page-speed-reduce-database-queries-in-codeigniter-4","status":"publish","type":"post","link":"https:\/\/impulsebyte.com\/blogs\/codeigniter\/increase-page-speed-reduce-database-queries-in-codeigniter-4\/","title":{"rendered":"Increase Page Speed &amp; Reduce Database Queries in CodeIgniter 4"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"methods-to-increase-page-load-speed--reduce-databa\">Methods to Increase Page Speed &amp; Reduce Database Queries in CodeIgniter 4<\/h2>\n\n\n\n<p>Besides server-side caching with <code>cache()-&gt;save()<\/code>, here are more effective strategies to optimize site speed and minimize unnecessary database hits in your CodeIgniter 4 project:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>Full Page Caching<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Store and serve entire HTML responses for pages that rarely change.<\/li>\n\n\n\n<li>On the first request, render the view and save the output to cache; deliver cache on subsequent requests until expiration.<\/li>\n\n\n\n<li>Can be done per-controller\/method basis.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>$cacheKey = 'homepage_html';\n$html = cache($cacheKey);\nif (!$html) {\n    $html = view('inc\/header')\n          . view('home')\n          . view('inc\/footer');\n    cache()->save($cacheKey, $html, 300);\n}\necho $html;\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #9CDCFE\">$cacheKey<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #CE9178\">&#39;homepage_html&#39;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$html<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">cache<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">$cacheKey<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> (!<\/span><span style=\"color: #9CDCFE\">$html<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$html<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">view<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;inc\/header&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">          <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">view<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;home&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">          <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">view<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;inc\/footer&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">cache<\/span><span style=\"color: #D4D4D4\">()-&gt;<\/span><span style=\"color: #DCDCAA\">save<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">$cacheKey<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">$html<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">300<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$html<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>Fragment or Partial Caching<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Cache portions of your page (e.g., portfolio slider, navigation, sidebar) individually.<\/li>\n\n\n\n<li>Useful for components appearing on multiple pages.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>Query Result Caching<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use database query caching if supported by your DB driver. This automatically caches result sets.<\/li>\n\n\n\n<li>In MySQL\/MariaDB: leverage query cache or in-memory structures for repetitive queries on unchanging tables.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>Asset Optimization<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Minimize, combine, and cache CSS\/JS files (use HTTP caching headers).<\/li>\n\n\n\n<li>Use CDN for static assets and images.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>Database Optimization<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add proper indexes to columns regularly used in <code>WHERE<\/code>, <code>ORDER BY<\/code>, or <code>JOIN<\/code>.<\/li>\n\n\n\n<li>Only select columns you need (<code>select('id, title, image')<\/code>) instead of <code>findAll()<\/code>.<\/li>\n\n\n\n<li>Paginate large queries to reduce result set size.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>HTTP and Browser Caching<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set appropriate HTTP cache headers so repeat visits load static resources from browser cache, not the server.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>AJAX (Client-Side) Rendering with Cache<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Load non-critical data (like sliders, galleries) via AJAX after initial page load, and cache the results in browser-side storage (<code>localStorage<\/code>, <code>sessionStorage<\/code>) for instant re-use on future page visits.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">8. <strong>Optimize Model and View Cell Layer<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Limit data in all reusable View Cells; avoid loading large datasets when only a few items are needed.<\/li>\n\n\n\n<li>Use parameterized View Cell calls to control data quantity and prevent unnecessary cache duplication.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">9. <strong>Efficient Use of Config\/Constants<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">10. <strong>Queue or Lazy Load Heavy Operations<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Offload slow or complex processing (report generation, stats) to queues or background jobs so page requests return quickly.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"summary-table\">Summary Table<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Technique<\/th><th>Purpose<\/th><th>Typical Use Case<\/th><\/tr><\/thead><tbody><tr><td>Full Page Cache<\/td><td>Serve entire page HTML<\/td><td>Brochure, landing, or unchanging pages<\/td><\/tr><tr><td>Fragment\/Partial Cache<\/td><td>Cache sections\/components<\/td><td>Portfolio carousels, menus, featured lists<\/td><\/tr><tr><td>DB Query Cache<\/td><td>Store SQL results<\/td><td>Product\/category lists, analytics queries<\/td><\/tr><tr><td>Asset\/HTTP Cache<\/td><td>Client-side speed<\/td><td>CSS, JS, images, fonts<\/td><\/tr><tr><td>AJAX + Browser Cache<\/td><td>Reduce re-fetching, lazy load<\/td><td>Gallery feeds, widgets, load-more buttons<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Combining these strategies provides exponential benefits: user experience improves and server\/database load decreases dramatically. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Methods to Increase Page Speed &amp; Reduce Database Queries in CodeIgniter 4 Besides server-side caching with cache()-&gt;save(), here are more effective strategies to optimize site speed and minimize unnecessary database &hellip; <a title=\"Increase Page Speed &amp; Reduce Database Queries in CodeIgniter 4\" class=\"hm-read-more\" href=\"https:\/\/impulsebyte.com\/blogs\/codeigniter\/increase-page-speed-reduce-database-queries-in-codeigniter-4\/\"><span class=\"screen-reader-text\">Increase Page Speed &amp; Reduce Database Queries in CodeIgniter 4<\/span>Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[140,114],"tags":[],"class_list":["post-7316","post","type-post","status-publish","format-standard","hentry","category-codeigniter","category-pageload-speed"],"_links":{"self":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/comments?post=7316"}],"version-history":[{"count":6,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7316\/revisions"}],"predecessor-version":[{"id":7325,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7316\/revisions\/7325"}],"wp:attachment":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/media?parent=7316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/categories?post=7316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/tags?post=7316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}