I've written a search for a website that iterates through all nodes under a certain folder and then filters out nodes based on certain criteria. The site itself is used to search apartments. Here is the function I have for building the collection:
private static List<Apartment> GetApartmentsCollection()
{
List<Apartment> apartmentsCollection = new List<Apartment>();
Document apartmentsContainer = new Document(Convert.ToInt32(ConfigurationManager.AppSettings["apartmentsContainerNodeId"]));
if (apartmentsContainer.HasChildren)
{
Document[] buildings = apartmentsContainer.Children;
foreach (Document building in buildings)
{
if (building.Published)
{
// Store building id for apartment
int buildingId = building.Id;
if (building.HasChildren)
{
Document[] apartments = building.Children;
foreach (Document apartment in apartments)
{
if (apartment.Published)
{
apartmentsCollection.Add(new Apartment(apartment.Id,
buildingId,
Convert.ToInt32(apartment.Text),
(int)apartment.getProperty("apartmentType").Value,
(int)apartment.getProperty("floor").Value,
(int)apartment.getProperty("status").Value,
(int)apartment.getProperty("bedrooms").Value,
Convert.ToInt32(apartment.getProperty("price").Value),
string.IsNullOrEmpty(apartment.getProperty("room1").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room1").Value.ToString()),
apartment.getProperty("room1Dimensions").Value.ToString(),
string.IsNullOrEmpty(apartment.getProperty("room2").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room2").Value.ToString()),
apartment.getProperty("room2Dimensions").Value.ToString(),
string.IsNullOrEmpty(apartment.getProperty("room3").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room3").Value.ToString()),
apartment.getProperty("room3Dimensions").Value.ToString(),
string.IsNullOrEmpty(apartment.getProperty("room4").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room4").Value.ToString()),
apartment.getProperty("room4Dimensions").Value.ToString(),
string.IsNullOrEmpty(apartment.getProperty("room5").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room5").Value.ToString()),
apartment.getProperty("room5Dimensions").Value.ToString(),
string.IsNullOrEmpty(apartment.getProperty("room6").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room6").Value.ToString()),
apartment.getProperty("room6Dimensions").Value.ToString(),
string.IsNullOrEmpty(apartment.getProperty("room7").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room7").Value.ToString()),
apartment.getProperty("room7Dimensions").Value.ToString(),
apartment.getProperty("totalArea").Value.ToString(),
apartment.getProperty("projectArea").Value.ToString()));
}
}
}
}
}
}
return apartmentsCollection;
}
The only problem is that this takes a good 8-10 seconds or so (even though there are only ~150 nodes tops) which is too long.
Bear in mind this is just for retrieving the raw collection. I then filter out out results using a handful of linq statements which takes a further few seconds. Ultimately this leads to a search of ~150 nodes taking around 15 seconds which is far too long.
Best bet is to avoid using Document - you're querying the database and as you're not updating/changing data, there's no need - hit the XML cache instead. Creating the apartment object as a Node will be much quicker, or getting the data via Examine will be quicker still. Using Examine you can do all your filtering as part of the search query, so you get the exact data you're chasing without any extra processing. Iterate the results and push it out into the List<Apartments> object and you're good to go. Guarantee it will be quicker than 8-10 seconds.
Fastest way to iterate through nodes?
Hi guys,
I've written a search for a website that iterates through all nodes under a certain folder and then filters out nodes based on certain criteria. The site itself is used to search apartments. Here is the function I have for building the collection:
The only problem is that this takes a good 8-10 seconds or so (even though there are only ~150 nodes tops) which is too long.
Bear in mind this is just for retrieving the raw collection. I then filter out out results using a handful of linq statements which takes a further few seconds. Ultimately this leads to a search of ~150 nodes taking around 15 seconds which is far too long.
How can I speed this up?
Thanks
Hi Alimac
Best bet is to avoid using Document - you're querying the database and as you're not updating/changing data, there's no need - hit the XML cache instead. Creating the apartment object as a Node will be much quicker, or getting the data via Examine will be quicker still. Using Examine you can do all your filtering as part of the search query, so you get the exact data you're chasing without any extra processing. Iterate the results and push it out into the List<Apartments> object and you're good to go. Guarantee it will be quicker than 8-10 seconds.
N
Perfect! Thanks so much
is working on a reply...