Answer the question
In order to leave comments, you need to log in
How to dynamically create a sitemap in yii2?
Please help me how to dynamically create a simple sitemap in this framework? There are articles on the site, the link to them is "site name/forecast/1" where 1 is the id of the article.
How is it generally done? I couldn't find clear explanations in the search engine.
I roughly imagine that a request is made to the database, then xml is generated and saved to a file. But with a request to the database, the number of these requests also increases, I would not really like there to be many of them. Hosting complains about them. Yii2 is very poorly optimized in this regard.
Answer the question
In order to leave comments, you need to log in
Without seeing the code and debug data, it's hard to say anything concretely.
The number of requests depends on the number of entities for which you are building a sitemap (N).
Also, the number of requests depends on the number of connected connections in them (N*M).
Read about delayed and eager loading.
Learn how to use the debugger (Yii Debug Panel). It says how many resources and where it goes, how much time is spent on executing requests, in what order they are executed, and what section of the code causes them to be executed.
Read more about caching query results in the framework.
And yet, it is possible that the fields in the database are not indexed and therefore the database gives a load (stupidly iterates through all the records instead of searching by index).
If all this doesn't help.
Do a task in the console to generate a file with a sitemap.
The task puts the file where you need it.
Next, set up cron with this task. The frequency you choose depends on the frequency of updating the site and the need for updating. This avoids having to make complicated queries every time you visit or request a sitemap.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question