Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jcc5018's avatar

Looking for suggestions to reduce query count for index page

In this request I AM looking for alternative suggestions to what I am prompting in regard to the way I retrieve data.

I am trying to achieve an index page similar to SAMPLE

where I have pinned post at the top, then for each of the parent categories (there will be multiple child categories displayed for each parent) I want to display either the most popular or the newest. So I set up a system that allows switching between the two. These articles are also all filtered by Hobby. (ie baseball articles, gardening articles, etc.)

Currently, i managed to get the articles for each section to display (though im still working on the actual components for each section) However I end up with 29 queries. Although quick, I'm wondering if there is a better way to reduce these query counts.

So to recap, I need 1 query to determine the parent level categories, then for each category, i want the newest, and the most popular.

I also want articles that are pinned, all added to an articles collection.

I do still need to figure out what to do if at least 5 pinned articles are not present, or if no articles fit for a category.

At this point, I am wondering if it would be less resource intensive just to query all articles for a given topic, and filter at the view level. I'll assume caching might help also once I get the basic logic determined.

Current code:

This creates a collection like this:

$articles => [pinned => [articles 1-5],

Category 1 => [newest=>[articles 1-5],
popular = [articles 1-5] ] Category 2 => [newest=>[articles 1-5],
popular = [articles 1-5] ] ... ]

and so forth.

It works, but not sure if its the best way of doing this as there might be thousands of articles to filter if not 10's or 100's of thousands. With my current categories, I should end up with a collection of 65 articles max.

Is there a more efficient way to make this query?

0 likes
1 reply
LaryAI's avatar
Level 58

To reduce the number of queries and improve the efficiency of your index page, you can leverage eager loading and more complex queries to fetch the necessary data in fewer queries. Here's a revised approach:

  1. Eager Load Relationships: Use eager loading to reduce the number of queries when fetching related models.
  2. Single Query for Articles: Fetch all necessary articles in a single query and then group them in PHP.

Here's a revised version of your code:

Explanation:

  1. Eager Loading: The with(['categories' => function ($query) { ... }]) part ensures that the categories are loaded with the articles in a single query.
  2. Single Query for Articles: The $articlesQuery fetches all necessary articles in one go.
  3. Filtering and Sorting in PHP: After fetching all articles, we filter and sort them in PHP, which reduces the number of database queries.

Additional Tips:

  • Caching: Implement caching to store the results of these queries, especially if the data doesn't change frequently. This will significantly reduce the load on your database.
  • Indexes: Ensure that your database tables are properly indexed, especially on columns used in WHERE, ORDER BY, and JOIN clauses.

By following this approach, you should be able to reduce the number of queries and improve the performance of your index page.

Please or to participate in this conversation.