Protection software for my laptop
Just recently been suggested by my colleague to use these software for antivirus, firewall and spyware protection. It’s all free and lightweight
Avast Anti Virus – http://www.avast.com/en-au/index
Comodo Firewall – http://personalfirewall.comodo.com/free-download.html
Malware Bytes – http://www.malwarebytes.org/products/malwarebytes_free
Table Spool (Lazy Spool) in SQL Server 2005
I have a web app that recently timing out and the timing out exception is actually coming from the SQL Server. When I run the execution plan I found that there is one item called Table Spool (Lazy Spool) which is costing about 20%. I thought it was caused by my full text search but when I drilled down further more is because of DISTINCT keyword. So I decided to change to use GROUP BY instead. In my case it is only a single column so It won’t make any difference at all. Once I’ve changed that my web application running fast and no more timeout
I got this explanation from this website
Explain Distinct:
3) We do an all-AMPs RETRIEVE step from … by way of an
all-rows scan with no residual conditions into Spool x
(group_amps), which is redistributed by hash code to all AMPs.
Then we do a SORT to order Spool 1 by the sort key in spool field1
eliminating duplicate rows.
First there’s a redistribution, then duplicate rows are removed:
Efficient, if there are just a few rows per value [per AMP].
Spool size is always about the same, but may be extremely skewed → 2646: No more Spool Space
Explain Group By:
3) We do an all-AMPs SUM step to aggregate from … by way
of an all-rows scan with no residual conditions, and the grouping
identifier in field 1025. Aggregate Intermediate Results are
computed globally, then placed in Spool x.
First each AMP removes duplicate rows locally (first aggregate) and hashes/redistributes the resulting
rows, then there’s a second aggregation to remove duplicate rows:
Efficient, if there are lots of rows per value [per AMP].
Large number of rows per value Spool → small spool size
Small number of rows per value Spool → large spool size
Spool is never skewed.
Other interesting fact quoted from this article/discussion
http://www.simple-talk.com/sql/learn-sql-server/showplan-operator-of-the-week—lazy-spool/
http://www.sql-server-performance.com/forum/threads/table-spool-lazy-spool.15647/
INDEXING: Take a look at your indices to make sure that they’re all covering the columns that you’re selecting out of the tables. You’ll want to aim to get all the columns included in JOINs and WHERE clauses within the indices. All other columns that are in the SELECT statements should be INCLUDEd, or covered, by the index.
OPERATORS: See if you can get rid of the not equals (“<>”) operators, in favor of a single greater than or less than operator. Can this statement and T.CurrentHorizon <> 0 be changed to this and T.CurrentHorizon > 0?
JOINS: Get rid of the subqueries that are JOINing to tables outside of themselves. For instance, this line and FV2.elementId = FV.elementID might be causing some problems. There’s no reason you can’t move that out of a subquery and into a JOIN to dbo.aowCollectedFact FV, given that you’re GROUPing (DISTINCT) in the main query already.
DISTINCT: Change it to a GROUP BY. I’ve got no reason other than, because it’s good practice and takes two minutes.
LAST NOTE: The exception to all the above might be to leave the final subquery, the IF NOT EXISTS, as a subquery. If you change it to a JOIN, it’ll have to be a LEFT JOIN...WHERE NULL statement, which can actually cause spooling operations. No great way to get around that one.
Simple Paging using jQuery -Pajinate
By using this library, it allows you to do the paging through the HTML DOM from the client side (Note: this is not about the ideal way or not the ideal way, I know the ideal way is to do paging server side)
To implement you just need to do 3 things:
1. Create a div container that wraps the container of item that you want to repeat and the navigation div, you can call it whatever you want
2. Create a div inside the container with class “page_navigation”
3. put class “content” on the container of the list item
Sample
- <div id=”page_container”>
- <div class=”page_navigation”></div>
- <ul class=”content”>
- <li>
- <p>One</p>
- </li>
- <li>
- <p>Two</p>
- </li>
- <li>
- <p>Three</p>
- </li>
- <li>
- <p>Four</p>
- </li>
- <li>
- <p>Five</p>
- </li>
- <li>
- <p>Six</p>
- </li>
- <li>
- <p>Seven</p>
- </li>
- <li>
- <p>Eight</p>
- </li>
- </ul>
- </div>
and I put this code on the document.ready event based on the id set on item 1
- <SCRIPT>
- jQuery(document).ready(function () {
- jQuery(‘#page_container’).pajinate({ items_per_page: 2 });
- });
- </SCRIPT>
The source code can be downloaded from here
and you can read the documentation from this github page
Yield keyword in .NET
I believe some of you already know about this but for me I never used it. Yield keyword has been existed since .NET 2.0 so I decided to look up of what it does and try to understand it
Based on MSDN
Yield is used in an iterator block to provide a value to the enumerator object or to signal the end of iteration, it takes one of the following form
Based on my understanding
Yield is a concatenation for a collection, or in SQL we normally use UNION
Yield break; is used to exit from the concatenation (remember it is not used to skip !)
One practical sample that I can think of is to get the enumerable of exception from inner exception (e.g stack trace)
sample code
- class Program
- {
- ///<summary>
- /// simple function to return IEnumerable of integer
- ///</summary>
- ///<returns></returns>
- private static IEnumerable<int> GetIntegers()
- {
- for (int i = 0; i <= 10; i++)
- yield return i;
- }
- ///<summary>
- /// simple function to return collection of class
- ///</summary>
- ///<returns></returns>
- private static IEnumerable<MyClass> GetMyNumbers()
- {
- for (int i = 0; i <= 10; i++)
- if (i > 5)
- yield break;
- else
- yield return new MyClass() { Number = i };
- }
- internal class MyClass
- {
- public int Number { get; set; }
- public string PrintNumber
- {
- get {
- return “This is no “ + Number.ToString();
- }
- }
- }
- static void Main(string[] args)
- {
- Console.WriteLine(“Simple array of integer”);
- foreach (var number in GetIntegers())
- Console.WriteLine(number.ToString());
- Console.WriteLine();
- Console.WriteLine(“Collection of classes”);
- foreach (var myclass in GetMyNumbers())
- Console.WriteLine(myclass.PrintNumber);
- Console.ReadLine();
- }
- }
Output
0
1
2
3
4
5
6
7
8
9
10Collection of classes
This is no 0
This is no 1
This is no 2
This is no 3
This is no 4
This is no 5
Knockout MVVM Javascript
Knockout allows you to bind the HTML to your javascript object. It simplifies DOM manipulation and allow the portability of the javascript object and action. It is pretty much the same concept as MVVM in silverlight. You can wire up the function with button click easily, you can have for each against your array (e.g like repeater). It is so elegant, but debugging sometimes can be challenging as well. I’ve used Knockout along with JSON that allows me to build rich and interactive website
2 powerful function: ko.observable – this allow knockout to monitor this object value, ko.observableArray this is the extension of ko.observable against the array. With observable, knockout will keep tracking the value of that property and allow the DOM that has been bind against it to refresh
You can bind initial data from your MVC model to the variable in javascript and bind it, in this sample below, I use ToJson extension function
- namespace System.Web.Mvc
- {
- public static class HtmlHelperExtensions
- {
- ///<summary>
- /// Serializes an object to Javascript Object Notation.
- ///</summary>
- ///<param name=”item”>The item to serialize.</param>
- ///<returns>
- /// The item serialized as Json.
- ///</returns>
- public static string ToJson(this object item)
- {
- return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(item);
- }
- }
- }
Sample code
- <script type=”text/javascript”>
- var initialData = @(new MvcHtmlString(Model.ToJson()));
- function JobResultViewModel()
- {
- var self = this;
- self.Jobs = ko.observableArray(initialData.JobSearchResults);
- self.Search = ko.observable(initialData.JobSearchModel);
- self.Pageno = ko.observable(initialData.PageNo);
- self.TotalPage = ko.observable(initialData.TotalPage);
- self.TotalRecord = initialData.TotalRecord;
- self.ShowNextButton = ko.computed(function(){
- return self.Pageno() < (self.TotalPage() – 1);
- });
- self.LoadNextPage = function() {
- $.getJSON(‘@Url.Action(“ResultJson”)‘, { Keyword: (self.Search().Keyword == null) ? “” : self.Search().Keyword,
- ProfessionId: self.Search().ProfessionId,
- RoleIds: self.Search().RoleId,
- SalaryTypeId: self.Search().SalaryTypeId,
- SalaryFromId: self.Search().SalaryFromId,
- SalaryToId: self.Search().SalaryToId,
- LocationId: self.Search().LocationId,
- AreaIds: (self.Search().AreaId.length == 0) ? 0 : self.Search().AreaId,
- WorkTypeId: self.Search().WorkTypeId,
- Pageno: self.Pageno() + 1
- }, function (SearchResult) {
- $.each(SearchResult, function(i, item)
- {
- self.Jobs.push(item);
- });
- self.Pageno(self.Pageno() + 1);
- //we need to refresh the repeater when we use jquery mobile ONLY
- $(“#JobRepeater”).listview(“refresh”);
- });
- }
- }
- ko.applyBindings(new JobResultViewModel());
- </script>
- <h2>Result</h2>
- <h1>There are <span data-bind=”text: TotalRecord”></span> jobs</h1>
- <a>Save Search</a>
- <ul name=”JobRepeater” id=”JobRepeater” data-role=”listview” data-bind=”foreach: Jobs”>
- <li><a data-bind=”attr: { href: UrlAction, title: JobName },text : JobName”></a><span data-bind=”text: Description”></span></li>
- </ul>
- <div data-bind=”visible: ShowNextButton”>
- <input type=”button” id=”btn_load_next” value=”Load More” data-bind=”click: LoadNextPage”/>
- </div>
Source:
Browser Extension Plugin for VS2010
This is a nice extension for VS2010 to allow you to change the default browser on debugging mode
http://visualstudiogallery.msdn.microsoft.com/bb424812-f742-41ef-974a-cdac607df921