Reputation: 161
I have a site that displays a list of articles, along with category filters. The filters can be toggled on and off and the article list will adjust automatically.
Currently, I have it so when filters are clicked, AJAX makes a request to a script that queries the database based on the selected filters, then the results are output to the user.
Is this an inefficient method? The other idea I had recently was to just query the database for EVERY article once when the page loads. Then instead of making AJAX requests, the filters would toggle the display element of each post accordingly.
This sounds a lot better of an idea than using AJAX, but I wanted to get other opinions on the method regarding efficiency and semantics.
Upvotes: 4
Views: 156
Reputation: 887
How you do this query? GET with params?
If yes, you could read about varnish to cache it. No more worries about it.
The first request will be cached in memory. The others never will touch the DB neither the scriptfile, just the cached response.
Configure the varnish to expires this in 2 - 2 minutes and have a high performance request (even if millions request this AJAX, just one query will be made in DB)
Upvotes: 0
Reputation: 5935
everything depends from your specific numbers. How many visitors you have ? How heavy is the whole page to load ? How many seconds increase you page load to load all the articles.
Generally speaking if you article are not so many i suggest to load them all at once, the user benefits from faster loading and your life is easier because you don't have to write a specific service to load articles one by one.
That said your ajax solution it's not inefficient and it's the best solution if the number of article increase.
Upvotes: 2
Reputation: 758
I guess it depends on how many rows you are trying to cache. If it is less than say 1000 then I would probably cache it but if its more than that you can use AJAX. AJAX is inefficient in general due to the constraints of having data travel over http.
Upvotes: 1
Reputation: 78850
It really depends on how many articles you have. If there are hundreds or thousands of them, you would probably save processing by doing fetches over AJAX. Otherwise, client-side filtering makes more sense.
Upvotes: 2