What is the best way (performance wise) to display all the categories and respective sub-categories with images when the page loads so that I may style the content with an UL LI to display inside Mega Menu?

I was thinking to run a query from the control that contains the Mega Menu which should output all the published categories and sub categories. But this will constantly go back to the server to run the query? Bad for performance!

Then I thought run the query that outputs the data in an XML file on the client machine (obviously it will run the query from the server the first time) but subsequently it will read off the XML file. Then I style the XML data using XSLT within my Mega Menu control.

Please advise? I am using NopCommerce 1.9 using C# ASP.NET

有帮助吗?

解决方案

You're essentially just shifting one performance concern for another. What you're describing is known as the N + 1 SELECT problem. See this code project article for a quick description: Select N+1 Problem – How to Decrease Your ORM Performance.

Now, I don't know if you're using an ORM, but it's the same problem regardless of your data access layer. Should you lazy load only one level of your nested hierarchy and save your client from having to run N queries (each requiring a round trip) to load children of each node? Or should you eagerly load your entire hierarchy to the client and render it on demand. The answer is "it depends". You likely know your data best.

If your hierarchy is really deep and has many levels, see if your typical user usage drills deep into this hierarchy or not. If only 20% or less of users drill down then maybe a lazy load solution is fine for you.

If the data that comprises your categories isn't very large (I would say, less than 50K), then it's a no brainer to eagerly load all your data and render it on your client.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top