Wednesday, May 16, 2012
Maybe you should leave those key lookups alone
So.... In my last post I shined a flashlight on the concept of creating covering indexes to eliminate key lookups for your queries. Turns out that covering indexes are killer to tables that endure constant updates. After adding covering indexes to several of these kind of tables, my database saves came crashing to a halt, overwhelmed with locks. I'm not sure yet what the answer is, but for the time being, I've made amends with those key lookups.
Monday, April 16, 2012
Eliminate those key-lookups
If you are developing for any database involved application that deals with a moderate quantity of data and you stick around long enough, you will find yourself investigating performance. Maybe you operate like our team did on a recent project where we started with some base expectations of performance, moved forward until gross disappointment set it, hacked at performance enhancements until we felt better about things, adjusted our expectations, and moved on. This cycle repeated itself many times and each time, I learned something new about how not to do a variety to things.
I won't spoil the fun for you, but if you are using SQL Server 2005 or greater, I will offer this one thing that I found enlightening: key-lookups in my query execution plans were my failure to understand(or care about) included columns. I would explain more myself, but that has already been done so very well in this post here by Joe Webb.
So this still counts as a blog entry right?
Cheers!
I won't spoil the fun for you, but if you are using SQL Server 2005 or greater, I will offer this one thing that I found enlightening: key-lookups in my query execution plans were my failure to understand(or care about) included columns. I would explain more myself, but that has already been done so very well in this post here by Joe Webb.
So this still counts as a blog entry right?
Cheers!
Friday, February 3, 2012
Comparing nullable dates
Sometimes working with Nullable<T> can be like arguing with a pedantic nerd. And while being able to do this effectively is probably not something you want to advertise in the skills section of your résumé, you can win friends by translating for such challenged communicators. Just as such, you can win programmer friends by writing extension methods that simplify overly verbose operations with... say... Nullable<DateTime>.
If you accept all that, then I present this here "friend catcher" to you. Keep the anecdote close to heart that with great power comes great responsibility and always remember that you cannot hold me responsible if you crash Facebook with all the new friends you amass. I will ignore any emails I receive blaming me so.
If you accept all that, then I present this here "friend catcher" to you. Keep the anecdote close to heart that with great power comes great responsibility and always remember that you cannot hold me responsible if you crash Facebook with all the new friends you amass. I will ignore any emails I receive blaming me so.
/// <summary> /// Compares two nullable dates using only the day, month, and year. /// Ignores hours, minutes, seconds... /// </summary> public static int DateCompare(this DateTime? dt, DateTime? compareDate) { DateTime? a = dt.HasValue ? dt.Value.Date as DateTime? : null; DateTime? b = compareDate.HasValue ? compareDate.Value.Date as DateTime? : null; return Nullable.Compare<DateTime>(a, b); }
Wednesday, November 30, 2011
Unlocking the power of IEnumerable T
Since becoming proficient with using LINQ to find shit, I have never looked back. That is until I run into collection types that I can’t immediately query with LINQ expressions. And it seems like every time I encounter such a collection, my first course of action is hate on it for a little while. ControlCollection.FindControl hardly counts as a way to find controls and NameValueCollection has no apparent way to find things just to mention a couple. This goes for any of your custom collection types as well, unless you implemented your own search methods.
So one thing I have found myself doing time and time again is adding an extension method called AsEnumerable() to these types so that I don’t have to hate them. This method returns the collection as an IEnumerable<T>, unlocking a pile of extension methods that allow me to query and otherwise manipulate said collection in more ways than I can imagine.
Many collection classes in the framework already provide such a method, but many still do not. Also note that the non-generic IEnumerable is not the same as the generic one and is far lesser in power compared to its generic counterpart IEnumerable.
As an example, lets do this for the ControlCollection class so we can find all the controls on a page that have a certain css class.
Now that the extension is in place, we can write some code like this to find what we want:
And for working with QueryStrings, this extension for the NameValueCollection class does the trick:
Allowing us to do something like this:
Go forth now and be cured of lame collection hate knowing that you can spend less time crawling collections and more time racing against the clock that is always counting, always ticking away at the relevancy of your current developer skill set. Hell, with all the extra time you can spend now writing LINQ instead of wrestling with iterators, think of how much longer you can procrastinate now on that certification your boss has pestering you about...
So one thing I have found myself doing time and time again is adding an extension method called AsEnumerable() to these types so that I don’t have to hate them. This method returns the collection as an IEnumerable<T>, unlocking a pile of extension methods that allow me to query and otherwise manipulate said collection in more ways than I can imagine.
Many collection classes in the framework already provide such a method, but many still do not. Also note that the non-generic IEnumerable is not the same as the generic one and is far lesser in power compared to its generic counterpart IEnumerable
As an example, lets do this for the ControlCollection class so we can find all the controls on a page that have a certain css class.
public static IEnumerable<Control> AsEnumerable(this ControlCollection controls) { foreach (Control ctrl in controls) { foreach (var subCtrl in ctrl.Controls.AsEnumerable()) { yield return subCtrl; } yield return ctrl; } }
Now that the extension is in place, we can write some code like this to find what we want:
protected void Page_Load(object sender, EventArgs e) { IEnumerable<Control>blueControls = Controls.AsEnumerable().Where(c => c.CssClass.Contains(“blue”)); }
And for working with QueryStrings, this extension for the NameValueCollection class does the trick:
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this NameValueCollection col) { return col.Cast<string>().Select(key => new KeyValuePair<string, string>(key, col[key])); }
Allowing us to do something like this:
Request.QueryString.AsEnumerable().Where(c => c.Key == "someParameter");
Go forth now and be cured of lame collection hate knowing that you can spend less time crawling collections and more time racing against the clock that is always counting, always ticking away at the relevancy of your current developer skill set. Hell, with all the extra time you can spend now writing LINQ instead of wrestling with iterators, think of how much longer you can procrastinate now on that certification your boss has pestering you about...
Friday, March 12, 2010
Making Sharepoint RTE aways create a popup window for editing
stick this hackjob of a function in your head tag and call it at the end of your document.
function fixEditorButtons() {
// rewire rich text editor buttons to popup source view RTE window in content mode
var anchors = document.getElementsByTagName("A");
if(anchors.length == 0) return;
var rteArray = new Array();
// gather up the rich text editor edit buttons
for(var i = 0; i < anchors.length; i++) {
if(anchors[i].attributes.getNamedItem("title") != null) {
if(anchors[i].attributes.getNamedItem("title").value == "Edit Content") {
rteArray.push(anchors[i]); }}}
for(var i = 0; i < rteArray.length; i++) {
var oEditButton = rteArray[i];
// grab the code as SP created it
var code = new String(oEditButton.onclick);
// snag the control name of the RTE current display control
var controlnameBeginIndex = code.search(/RTE2_LaunchEditor/) + 27;
var temp = code.substring(controlnameBeginIndex);
var controlnameEndIndex = temp.search(/\'/);
var controlName = temp.substring(0,controlnameEndIndex) + "_displayContent";
// find where we need to insert our additional code that creates the popup window
var insertPoint = code.search(/cancelBubble/) - 67;
// build new code, start with old code
var newcode = code.substring(0,insertPoint);
newcode += "RTE2_SaveHtmlStateIfChanged('" + controlName + "');";
newcode += "var instanceVariables=RTE_GetEditorInstanceVariables('"+ controlName +"');";
newcode += "RTE2_LaunchEditor( instanceVariables.aSettings, instanceVariables.clientId,
instanceVariables.displayContentElement.id, instanceVariables.emptyPanel.id,
instanceVariables.hiddenInputField.id, instanceVariables.webUrl,
'True', false,true);";
newcode += code.substring(insertPoint); rteArray[i].onclick =
Function(newcode.slice(20,newcode.length - 1));
}
}
function fixEditorButtons() {
// rewire rich text editor buttons to popup source view RTE window in content mode
var anchors = document.getElementsByTagName("A");
if(anchors.length == 0) return;
var rteArray = new Array();
// gather up the rich text editor edit buttons
for(var i = 0; i < anchors.length; i++) {
if(anchors[i].attributes.getNamedItem("title") != null) {
if(anchors[i].attributes.getNamedItem("title").value == "Edit Content") {
rteArray.push(anchors[i]); }}}
for(var i = 0; i < rteArray.length; i++) {
var oEditButton = rteArray[i];
// grab the code as SP created it
var code = new String(oEditButton.onclick);
// snag the control name of the RTE current display control
var controlnameBeginIndex = code.search(/RTE2_LaunchEditor/) + 27;
var temp = code.substring(controlnameBeginIndex);
var controlnameEndIndex = temp.search(/\'/);
var controlName = temp.substring(0,controlnameEndIndex) + "_displayContent";
// find where we need to insert our additional code that creates the popup window
var insertPoint = code.search(/cancelBubble/) - 67;
// build new code, start with old code
var newcode = code.substring(0,insertPoint);
newcode += "RTE2_SaveHtmlStateIfChanged('" + controlName + "');";
newcode += "var instanceVariables=RTE_GetEditorInstanceVariables('"+ controlName +"');";
newcode += "RTE2_LaunchEditor( instanceVariables.aSettings, instanceVariables.clientId,
instanceVariables.displayContentElement.id, instanceVariables.emptyPanel.id,
instanceVariables.hiddenInputField.id, instanceVariables.webUrl,
'True', false,true);";
newcode += code.substring(insertPoint); rteArray[i].onclick =
Function(newcode.slice(20,newcode.length - 1));
}
}
Thursday, March 6, 2008
Are parents responsible the delinquency of teenaged SPLists?
Ever since I hooked up with Microsoft and began fathering little bits of softwares, I always thought that I would raise good code. However, like many have endured, one of my softwares began acting defiant and outrageous. We debugged the software repeatedly and how stubborn it was! Certainly didn't get any of that from MY influence! ahem! Lets get into the psyche of this little rebel a bit...
The victim of such a crime is one that wants the list for its views and is given this list as joke. How did I deal with such reckless behavior? Well, it didn't go down without a hairy fight thats for sure, but in the end, I taught it to return regular SPLists that were not special.
public static SPList GetListByName(SPWeb rootWeb, string listName)
{
foreach (SPList list in rootWeb.Lists)
{
if (list.Title.Equals(listName,
StringComparison.InvariantCultureIgnoreCase))
{
return list;
}
}
foreach (SPWeb web in rootWeb.Webs)
{
SPList resultList = GetListByName(web, listName);
if (resultList != null)
{
return resultList;
}
}
return null;
}
Take a look at the base case where the list is found and returned. Pretty standard stuff no? What is not (by even a far stretch of even the most eclectic programmer's imagination) obvious here is that the list being returned has a... how do I say this.... special Views property (of type SPViewCollection). The crime here is that these views are wrong. No, The collection isn't empty, it doesn't throw exceptions when you try to access it, it isn't null, it doesn't contain any number of views other than the amount of views it really does have, and the views aren't uninitialized or anything. They are wrong because they only have two columns, no matter what.The victim of such a crime is one that wants the list for its views and is given this list as joke. How did I deal with such reckless behavior? Well, it didn't go down without a hairy fight thats for sure, but in the end, I taught it to return regular SPLists that were not special.
SPSite s = SPContext.Current.Site;
SPList l = s.AllWebs[rootWeb.Name].Lists[listName];
return l;
Tuesday, February 5, 2008
Revision# 6,450,002.0 of the command line parameter parsing piece
One of the most re-invented wheels of all time is the one which takes options from a set of command line arguments. It's a fine tradition to reinvent this wheel and here I present the fruits of today's celebration.
static void Main(string[] args)
{
string option1 = getCommandLineParameter("-option1", args);
string option2 = getCommandLineParameter("-option2", args);
if (option1 != null && option2 != null)
{
// react to these parameters
}
else
{
// react to these parameters not being given
}
}
private static string getCommandLineParameter(string optionName, string[] args)
{
short i;
for (i = 0; i < args.Length; i++)
{
if (args[i] == optionName)
{
if (args[i + 1] != null)
{
return args[i + 1];
}
else
{
return null;
}
}
}
return null;
}
Subscribe to:
Posts (Atom)