<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codality &#187; Microsoft SqlServer</title>
	<atom:link href="http://blog.najmanowicz.com/category/software-development/sql/microsoft-sqlserver/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.najmanowicz.com</link>
	<description>Code and Effect - solving problem with just enough amount of code - by Adam Najmanowicz</description>
	<lastBuildDate>Mon, 08 Feb 2010 12:01:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Database-based paged EPiServer searches for CMS 5 R2 SP2</title>
		<link>http://blog.najmanowicz.com/2009/07/07/database-based-paged-episerver-searches-for-cms-5-r2-sp2/</link>
		<comments>http://blog.najmanowicz.com/2009/07/07/database-based-paged-episerver-searches-for-cms-5-r2-sp2/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 11:15:25 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[EPiCode]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Microsoft SqlServer]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Solution]]></category>
		<category><![CDATA[Web applications]]></category>

		<guid isPermaLink="false">http://blog.najmanowicz.com/2009/07/07/database-based-paged-episerver-searches-for-cms-5-r2-sp2/</guid>
		<description><![CDATA[Aparently I have written something on that note before for CMS 4 and it looks like someone still needs it as I got a request for an updated version for it a couple of days ago. So here we go: for the most part the syntax for the call is equivalent to what is was [...]]]></description>
			<content:encoded><![CDATA[<p>Aparently I have written <a href="http://blog.najmanowicz.com/2007/05/21/database-based-paged-episerver-searches/">something on that note before for CMS 4</a> and it looks like someone still needs it as I got a request for an updated version for it a couple of days ago. So here we go:</p>
<p>for the most part the syntax for the call is equivalent to what is was before so go to my previous article regarding that (check out the old article for details). What I’ve added this time around is:</p>
<ul>
<li>the @PropertyName can be declared as ‘%’ if you want to look in all property names </li>
<li>@PropertyType can be –1 if you want to look in all property types otherwise you need to specify type id (this has changed from type name before due to database schema changes) </li>
<li>additionally this version of the stored proc will only look in the Master language Branch, so it will work for the single language pages and for multi-language but for language agnostic properties. (should you require the language to be variable the change is pretty simple – I can send you the updated version by email. </li>
</ul>
<pre class="brush: sql; highlight: [32];">/****** Object:  StoredProcedure [dbo].[PagedSearch]    Script Date: 07/07/2009 12:18:10 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PagedSearch]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PagedSearch]
GO

CREATE Procedure PagedSearch
    @Condition varchar(1024),
    @PropertyName varchar(1024),
    @PropertyType int,
    @PageSize int,
    @PageNumber int,
    @Offset int
AS
BEGIN

    DECLARE @RowStart int
    DECLARE @RowEnd int

    SET @RowStart = @PageSize * @PageNumber + @Offset;
    SET @RowEnd = @RowStart + @PageSize + @Offset;

    WITH PageRefs AS
        (SELECT page.pkID as PageId,
            ROW_NUMBER() OVER (ORDER BY pageLang.StartPublish DESC) as RowNumber
            FROM tblPage page, tblProperty propValue, tblPageDefinition propDef, tblPageLanguage pageLang
            WHERE page.pkID = propValue.fkPageID
                AND page.fkMasterLanguageBranchID = pageLang.fkLanguageBranchID
                AND page.pkID = pageLang.fkPageID
                AND propValue.fkPageDefinitionID = propDef.pkID
                AND (@propertyType = -1 or propDef.fkPageDefinitionTypeID = @propertyType) -- is proper type
                AND propDef.Searchable = 1 -- the property is searchable
                AND propValue.String like @Condition -- contains facets
                AND propDef.[Name] like @PropertyName) -- property of proper name
    SELECT PageId
        FROM PageRefs
        WHERE (RowNumber Between @RowStart and @RowEnd) or (@PageSize = 0);
END
GO</pre>
<p>However&#8230; looking how the schema has changed over time, I am not convinced this approach is really the best one for someone who is not prepared to deal with the changes (e.g. you better be able to change the stored procedure based on the schema changes &#8211; or bribe me with pizza and beers for updates :) ).</p>
<p>Additionally this procedure only searches for properties that store their value in Short string field. To make it look into long string you need to Change the highlighted line to.</p>
<pre class="brush: sql;">AND (propValue.LongString like @Condition)</pre>
<p>or alternatively to look in both change it to:</p>
<pre class="brush: sql;">AND ((propValue.String like @Condition) or (propValue.LongString like @Condition))</pre>
<p>Enjoy!</p>
<img src="http://blog.najmanowicz.com/?ak_action=api_record_view&id=143&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.najmanowicz.com/2009/07/07/database-based-paged-episerver-searches-for-cms-5-r2-sp2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Advanced Language Manipulation Tool for EPiServer</title>
		<link>http://blog.najmanowicz.com/2009/04/06/advanced-language-manipulation-tool-for-episerver/</link>
		<comments>http://blog.najmanowicz.com/2009/04/06/advanced-language-manipulation-tool-for-episerver/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 15:49:11 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Downloadable]]></category>
		<category><![CDATA[EPiCode]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Microsoft SqlServer]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Solution]]></category>
		<category><![CDATA[Web applications]]></category>

		<guid isPermaLink="false">http://blog.najmanowicz.com/2009/04/06/advanced-language-manipulation-tool-for-episerver/</guid>
		<description><![CDATA[Have you ever (or have your customers) created and edited a page in one language only to realize that their selected locale was wrong? Have you ever wished you could delete a master language branch of a page&#160; after creating its localized counterpart but you could only delete the newly created slave language instead? Have [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever (or have your customers) created and edited a page in one language only to realize that their selected locale was wrong? Have you ever wished you could delete a master language branch of a page&#160; after creating its localized counterpart but you could only delete the newly created slave language instead? Have a customer ever requested that they could copy a whole branch and you convert it to another language so that they could then translate in-place?</p>
<p>Well I have… and I’m sure I will. And so did Fredrikj on the our #epicode IRC channel ;).</p>
<p>Basically I had the tool that would convert from one language to another, but Fredrikj requested something that would switch master language of a page from one to another. Since I’ve already had some of the work done, I’ve updated the stored procedure I’ve written some time ago and slapped a nice GUI up on it. Here’s the result:</p>
<p>&#160;</p>
<p align="center"><a href="http://blog.najmanowicz.com/wp-content/uploads/2009/04/andvancedlanguagetool.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="AndvancedLanguageTool" border="0" alt="AndvancedLanguageTool" src="http://blog.najmanowicz.com/wp-content/uploads/2009/04/andvancedlanguagetool-thumb.png" width="480" height="484" /></a> </p>
<p>What the tool allows you to do is perform either language conversion or master branch switching on a selected page and all of its children (if you choose so).</p>
<p>The stored procedure have been updated to work on CMS5 R2 (will no longer work on R1 – but if you need that functionality, comment here or give me a shout and I’ll create a compatible version for you).</p>
<p>A word of caution though – I take no guarantee whatsoever about its operation. Especially, if you wreck your client’s database with it. I did what I could to prevent some of the obvious problems (like <strong><em>switching</em></strong> to a non existing master or <strong><em>converting</em></strong> to an existing one) but I will not be responsible if it won’t work for you. make a database backup and experiment there before you do any changes on the real data. That said – it works for me, so I think it should also work for you.</p>
<p>You can download the <a title="Episerver Advanced Language Tools" href="http://www.najmanowicz.com/blog_bin/EPiServerLanguageTools.zip" rel="enclosure">archive containing the tool here</a>. unzip it to your EPiServer web application folder keeping the folder structure or the plugin reference will be wrong. Include the *.aspx and the *.cs files in your project and apply the SQL file to your database (The manipulation is performed by a stored procedure located in the file).</p>
<p>Also if you’re performing the change in a load balanced environment, you may need to restart the other servers once you do the changes. I reset the DataFactory cache, but I am not sure it propagates through to other servers.</p>
<img src="http://blog.najmanowicz.com/?ak_action=api_record_view&id=124&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.najmanowicz.com/2009/04/06/advanced-language-manipulation-tool-for-episerver/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>CogniScale &#8211; virtual hosting made easy</title>
		<link>http://blog.najmanowicz.com/2008/06/02/cogniscale-virtual-hosting-made-easy/</link>
		<comments>http://blog.najmanowicz.com/2008/06/02/cogniscale-virtual-hosting-made-easy/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 21:10:03 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Downloadable]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Internet Information Services]]></category>
		<category><![CDATA[Microsoft SqlServer]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web applications]]></category>

		<guid isPermaLink="false">http://blog.najmanowicz.com/2008/06/02/cogniscale-virtual-hosting-made-easy/</guid>
		<description><![CDATA[We&#8217;ve not been talking much about it and that&#8217;s partially my fault as well (busy with other projects), but Cognifide has a really cool initiative called Cognifide Labs that we intend to grow over time. The plan is to devote up to 10% company time into side projects that help us grow expertise and allow [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve not been talking much about it and that&#8217;s partially my fault as well (busy with other projects), but <a href="http://www.cognifide.com/">Cognifide</a> has a really cool initiative called <a href="http://labs.cognifide.com/">Cognifide Labs</a> that we intend to grow over time. The plan is to devote up to 10% company time into side projects that help us grow expertise and allow our devs to dwell into interesting technologies, methodologies and languages and develop their skills.</p>
<p>One of the first projects (that I took part in) is <a href="http://cogniscale.cognifide.com/">CogniScale</a> &#8211; an app that allows <a href="http://www.flexiscale.com/">FlexiScale</a> users to manage their servers. Here&#8217;s the story&#8230; </p>
<p>Being the agile company taking part in many <a href="http://www.episerver.com/">EPiServer</a> projects we never seem to have enough environments to test our web-apps and software in general in various scenarios. We find ourselves constantly reinstalling and trying to keep our servers in a state that can can at least remotely be called as stable. After all how many deployments and tearing down of various <a href="http://www.episerver.com/">EPiServer</a>, <a href="http://ccnet.thoughtworks.com/">CruiseControl</a>, <a href="http://www.jetbrains.com/teamcity/">TeamCity</a>, SQL Server and other &quot;I need to have&quot; apps can a server take before slowing down to a crawl or collapsing all together (that said I bow before our faithful THOTH for taking all the abuse it does). We definitely needed more servers! And we needed them now! </p>
<p>Early this year we&#8217;ve started to talk to the guys at XCalibre that came up with a great idea. What if you could have an unlimited amount of servers available for you at any given time? I mean really what if you could have 0 servers one day and the next day have a rich farm of servers for literally no cost, paying only when you power them up and not paying a bit if you take them down.&#160; This turned out to be quite a project for them that turned to materialize as FlexiScale. (<a href="http://flexiscale.com/about_us.html">you can read more about it here</a>). Looking at all that I&#8217;ve mentioned before while eliminating the cost of maintaining the servers locally we decided to give FlexiScale a spin. </p>
<p><span id="more-97"></span></p>
<p>Early this year FlexiScale published a set of API&#8217;s that we looked at and (being the geeks we are) tried to utilize it in a desktop application with a limited success. It turned out that the initial API was perfectly accessible from dynamically typed languages while statically typed languages like C# didn&#8217;t really get their love in the initial release :) So we started opening the support tickets and shortly after that establishing a lively dialogue with the great folks there. Let me tell you, the guys in the FlexiScale support are really an amazing and responsive bunch. Not only did we get the support and the fixes we needed for the language we chose but we also pretty much got all our suggestions implemented and added to the API&#8217;s.</p>
<p>Thus <a href="http://cogniscale.cognifide.com/">CogniScale</a> was born:</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="470" alt="cogniscale" src="http://blog.najmanowicz.com/wp-content/uploads/2008/06/cogniscale_main_form.jpg" width="644" border="0" />&#160; </p>
<p>The app allows for easy powering up and down the servers (allowing you to describe the reason for the action) from a nice and shiny GUI as well as scheduling it with windows task scheduler through its command line interface. it allows you to connect using FTP or Remote Desktop as well as any other app you can run from command line (it features a custom command editor that allows you to configure any app to work with it) with a single click. The app also allows you to quickly go through the history of servers&#8217; maintenance to determine how often and for what reason were they taken on and off-line.</p>
<p>Should you decide that FlexiScale is your thing, please give <a href="http://cogniscale.cognifide.com/">CogniScale</a> a whirl and let us know what you think.</p>
<img src="http://blog.najmanowicz.com/?ak_action=api_record_view&id=97&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.najmanowicz.com/2008/06/02/cogniscale-virtual-hosting-made-easy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The challenges of a high traffic site with EPiServer</title>
		<link>http://blog.najmanowicz.com/2008/01/23/the-challenges-of-a-high-traffic-site-with-episerver/</link>
		<comments>http://blog.najmanowicz.com/2008/01/23/the-challenges-of-a-high-traffic-site-with-episerver/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 14:43:38 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[EPiCode]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Faceted Navigation]]></category>
		<category><![CDATA[Microsoft SqlServer]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web applications]]></category>

		<guid isPermaLink="false">http://blog.najmanowicz.com/2008/01/23/the-challenges-of-a-high-traffic-site-with-episerver/</guid>
		<description><![CDATA[&#8230;with an unconventional approach to data fetching. This article is a first of a series describing the faceted navigation system for EPiServer that we have internally developed in Cognifide and that&#8217;s already proven to be a robust solution for delivering tagged content a heavy traffic site, which will be released shortly as an open source [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230;with an unconventional approach to data fetching.</p>
<p>This article is a first of a <a href="http://blog.najmanowicz.com/category/episerver/faceted-navigation/">series describing the faceted navigation system for EPiServer</a> that we have internally developed in <a href="http://www.cognifide.com/">Cognifide</a> and that&#8217;s already proven to be a robust solution for delivering tagged content a heavy traffic site, which will be released shortly as an open source project. The article outlines some pitfalls of EPiServer that we&#8217;ve run into and the nature of the <a href="http://www.setantasports.com/">project</a> in which the module was used first and which influenced a lot of our design decisions.</p>
<p>This article and the Faceted Navigtation module is developed on EPiServer 4.61 and not the latest version 5 of the CMS so far, so mind that some of my reservations may not be a problem if you&#8217;re just starting to work on a brand new project and have the luxury of using new features of it. </p>
<p>Also (which may be a good thing) our sites uses a different approach to navigation the content, we do not really care much for the tree structure, but we treat all EPiServer pages equally when looking for content because of how the site is designed from the creative point of view.</p>
<p><span id="more-87"></span>
<p>The <a href="http://www.setantasports.com/">project</a> is a vast site with:</p>
<ul>
<li>tens of editors working on it simultaneously.</li>
<li>millions of uniques per month</li>
<li>tens of thousands of articles in just about half a year</li>
<li>5 IIS servers working in tandem on a single SQL database</li>
<li>The page cannot be cached as a whole as it contains dynamic and Geo-targeted content.</li>
<li>The content on the page changes every other minute or more often</li>
<li>There is a great deal of automated content coming from various feeds.</li>
<li>A content can be both feeded automatically based on the time of publishing as well as forced in some controls and no article can appear on a page twice, yet controls have to have predefined number of links. May not seem hard to do, but believe me once controls will start stealing articles form each other you&#8217;ll see why I listed it as a main feature.</li>
</ul>
<p>First thing to give the credit where it&#8217;s due &#8211; let me enumerate the good sides of using EPiServer in the scenario (the parts that we found particularly useful):</p>
<ul>
<li>EPiServer provides a beautiful interface to the editors. Both for editing pages and organizing the content.</li>
<li>EPiServer provides a great infrastructure for us to plug into it with property types, and GUI plugins</li>
<li>EPiServer allows us to use as much or as many of its elements as we need &#8211; from our point of view we mostly use it as a repository and pretty much ignored all of its custom controls and a great deal of its APIs (which I&#8217;ll explain later why.</li>
</ul>
<p>The list does not seem like a long one &#8211; but believe me &#8211; all of the points are fundamental to the success of the site! The editors love the UI, the developers love the extensibility and &#8220;pluggability&#8221; of the APIs. And the logical structure of the database allowed us for flexibility in skipping some APIs where it proved that a direct database call was beneficial.</p>
<p>The bad part is basically that the EPiServer API is slow for what we needed it to perform (the site can easily reach a couple of thousand simultaneous connections at peak times), if we wanted to use GetPage() for every link on the site we would be in serious troubles.&nbsp; At our average of 155 links per page, this was a serious problem. </p>
<p>Also since we have a structure of the tree that is not directly reflected on the site, but rather we search by &#8220;categories&#8221;. The idea of the site is that it is a huge search engine that displays the freshest content content based on the facets it is tagged with. Using the EPiServer APIs we would have to use FindPagesWithCriteria() which in itself is probably the most disliked call speed-wise. Also categories in themselves do not support any metadata (there are no properties for categories). Needless to say &#8211; we had to implement our own code for this kind of creative approach and that&#8217;s what we did.</p>
<p>In effect, what we limited to the list of hardest abusers are:</p>
<ul>
<li>FindPagesWithCriteria()</li>
<li>Getpage()</li>
<li>GetChildren()</li>
<li>and (mostly based on <a href="http://www.episerver.com/templates/PageWithRightListing.aspx?id=8128&amp;epslanguage=EN">Mat&#8217;s article</a>) use of dynamic properties.</li>
</ul>
<p>Basically all the calls that return PageData or PageDataCollection are all potential candidates for heavy caching.</p>
<p>The main approach that we took in our solution is &#8220;cache the hell out of it, but do it smart&#8221;. Don&#8217;t cache PageData unless there is absolutely no other way. Cache links, page titles and data that&#8217;s potentially used on every page.</p>
<p>Also where needed and justified by the speed increase we reach directly to the database &#8211; skipping some EPiServer APIs (At this point this is just one call as we want the solution to be easily portable between various EPiServer versions).</p>
<p>More (and the solution) coming shortly&#8230;</p>
<img src="http://blog.najmanowicz.com/?ak_action=api_record_view&id=87&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.najmanowicz.com/2008/01/23/the-challenges-of-a-high-traffic-site-with-episerver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Misadventures with Database Engine Tuning Advisor</title>
		<link>http://blog.najmanowicz.com/2007/09/05/misadventures-with-database-engine-tuning-advisor/</link>
		<comments>http://blog.najmanowicz.com/2007/09/05/misadventures-with-database-engine-tuning-advisor/#comments</comments>
		<pubDate>Wed, 05 Sep 2007 13:19:09 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Microsoft SqlServer]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.najmanowicz.com/2007/09/05/misadventures-with-database-engine-tuning-advisor/</guid>
		<description><![CDATA[I&#8217;ve been on a sole task today to improve a database performance of the project we&#8217;ve been working on. As much as I enjoyed the task there is one thing that costed me quite a bit of head scratching. Being a standard nerd I usually have about 20-30 applications running on my machine at a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been on a sole task today to improve a database performance of the project we&#8217;ve been working on. As much as I enjoyed the task there is one thing that costed me quite a bit of head scratching. Being a standard nerd I usually have about 20-30 applications running on my machine at a time, I didn&#8217;t immediately associated the error with the tuning advisor but rather assumed it&#8217;s one of my other 29 applications/servers/daemons/services in background did something stupid, probably even some of my code craving for a bit of attention, right? So I moved to tune the database on the server via remote desktop, but when I hit a wall there I&#8217;ve looked around for the solution and it looks like that it&#8217;s been <a href="http://support.microsoft.com/kb/913395">known to MS for almost 2 years now</a>.</p>
<p>Found it hilarious enough though to post it here though :)</p>
<p align="center"><img src="http://www.najmanowicz.com/blog_images/tuning_advisor_error.png"/> </p>
<p>&#8220;This indicates a bug in your application&#8221;, how vicious of them show a dialog like this to a developer. It&#8217;s not in MY code you bad thing you! It&#8217;s in YOUR code!</p>
<p>The solution or rather a workaround is available <a href="http://support.microsoft.com/kb/913395">here</a>. And NO, you don&#8217;t need to restart your computer after applying the change to the registry, just kill and restart Explorer and then start the Tuning advisor again. Make sure to restart the SQL Server Management Studio as well if you&#8217;re launching the tuning advisor form it, the setting is applied on app startup.</p>
<img src="http://blog.najmanowicz.com/?ak_action=api_record_view&id=80&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.najmanowicz.com/2007/09/05/misadventures-with-database-engine-tuning-advisor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database-based paged EPiServer searches</title>
		<link>http://blog.najmanowicz.com/2007/05/21/database-based-paged-episerver-searches/</link>
		<comments>http://blog.najmanowicz.com/2007/05/21/database-based-paged-episerver-searches/#comments</comments>
		<pubDate>Mon, 21 May 2007 16:04:56 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Microsoft SqlServer]]></category>
		<category><![CDATA[Web applications]]></category>

		<guid isPermaLink="false">http://blog.najmanowicz.com/2007/05/21/database-based-paged-episerver-searches/</guid>
		<description><![CDATA[Searches paged in the database have posed a problem in SQL Server at least prior to version 2005. I&#8217;ve found some solution to the problem on the net but they were so cludgy that I would never really put anything like that in a production server. I hope the following will shed some light on [...]]]></description>
			<content:encoded><![CDATA[<p>Searches paged in the database have posed a problem in SQL Server at least prior to version 2005. I&#8217;ve found some solution to the problem on the net but they were so cludgy that I would never really put anything like that in a production server. I hope the following will shed some light on how they work in general by using in in EPiServer context. </p>
<p>Theoretically in EPiServer you can pull the pages that match the criteria from the database with EPiServer.Global.EPDataFactory.FindPagesWithCriteria()&nbsp;into the PageList but that seemed to be imposing a strong performance penalty with increased number of&nbsp;pages meeting the criteria. Since this search is sometimes done even multiple times on a page request in our project we needed something better.</p>
<p><span id="more-76"></span>
<pre>
<div style="font-size: 9pt; color: black; font-family: courier new"><span style="color: blue">CREATE Procedure </span>PagedSearch
    @Condition <span style="color: blue">varchar</span>(1024),
    @PropertyName <span style="color: blue">varchar</span>(1024),
    @PropertyType <span style="color: blue">varchar</span>(1024),
    @PageSize <span style="color: blue">int</span>,
    @PageNumber <span style="color: blue">int</span>,
    @Offset <span style="color: blue">int</span>
<span style="color: blue">AS</span>
<span style="color: blue">BEGIN</span>
    <span style="color: blue">DECLARE </span>@RowStart <span style="color: blue">int</span>
    <span style="color: blue">DECLARE </span>@RowEnd <span style="color: blue">int</span>

    <span style="color: blue">SET </span>@RowStart = @PageSize * @PageNumber + @Offset;
    <span style="color: blue">SET </span>@RowEnd = @RowStart + @PageSize + @Offset;

    <span style="color: blue">WITH </span>PageRefs <span style="color: blue">AS</span>
        (<span style="color: blue">SELECT </span>page.pkID <span style="color: blue">as </span>PageId,
            <b>ROW_NUMBER() <span style="color: blue">OVER </span>(<span style="color: blue">ORDER BY </span>page.StartPublish <span style="color: blue">DESC</span>) <span style="color: blue">as </span>RowNumber</b>
            <span style="color: blue">FROM </span>tblPage page, tblProperty propValue, tblPageDefinition propDef
            <span style="color: blue">WHERE </span>page.pkID = propValue.fkPageID
                <span style="color: blue">AND </span>propValue.fkPageDefinitionID = propDef.pkID
                <span style="color: blue">AND </span>propDef.fkPageDefinitionTypeID = @propertyType <span style="color: green">-- is proper type</span>
<span style="color: blue">                AND </span>propValue.Searchable <span style="color: green">-- the property is searchable</span>
<span style="color: green"></span>                <span style="color: blue">AND </span>propValue.String <span style="color: blue">like </span>@Condition <span style="color: green">-- contains facets</span>
                <span style="color: blue">AND </span>propDef.[Name] = @PropertyName) <span style="color: green">-- property of proper name</span>
    <span style="color: blue">SELECT </span>PageId
        <span style="color: blue">FROM </span>PageRefs
        <span style="color: blue">WHERE </span>(RowNumber <span style="color: blue">Between </span>@RowStart <span style="color: blue">and </span>@RowEnd) <span style="color: blue">or </span>(@PageSize = 0);
<span style="color: blue">END</span></div>
</pre>
<p>The procedure searches the database and return the results on&nbsp;a chunk specified by @PageNumber, where each chunk is of size specified by @PageSize. If e.g. on the first page you only have a place for first 3 articles you can specify an @Offset for the &#8220;Next page &gt;&gt;&#8221;, otherwise you can set the @offset to 0.
</p>
<p>With a little effort you can adapt this stored procedure to do your job or just paged search outside of EPiServer. From the EPiServer point of view however you might find it a caveat, that you need to create your own paged repeater and query the server for the number of pages, which can be done easily with another variant of this stored procedure that asks for count of the same result set and divides it by a number of pages. But then again, <a href="http://blogs.cognifide.com/cleve/2006/05/31/hello-world/">if you want it to be done right, it&#8217;s usually no substitute to the long way round</a>.
</p>
<p>The stored procedure skips a lot of the complexity that it should probably have &#8211; meaning it only provides the search in reverse chronological order and disregards whether the page expired as well as any globalization settings, this has been omitted since I did not need it in my project and didn&#8217;t really want to add it here not to loose the clarity of the paged searches in Microsoft SQL Server.&nbsp;Just a&nbsp;place to start.</p>
<img src="http://blog.najmanowicz.com/?ak_action=api_record_view&id=76&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.najmanowicz.com/2007/05/21/database-based-paged-episerver-searches/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
