10
Nov

I've run into this a few different times at work.  You have a web page script that calls a SQL Stored Procedure and all of a sudden it takes 10-20x longer than it normally does to finish.  If it's a hefty query it could timeout.  When you execute the proc or take the code within and manually run it in a query window it runs as it should.

The solution is a quick Google search away (assuming you know what to search for).

"Parameter sniffing" refers to a process whereby SQL Server's execution environment "sniffs" the current parameter values during compilation or recompilation, and passes it along to the query optimizer so that they can be used to generate potentially faster query execution plans. The word "current" refers to the parameter values present in the statement call that caused a compilation or a recompilation.

Quick solution, change:

CREATE PROCEDURE books_by_subject_lis
   @foo nvarchar(50)
AS
BEGIN
   SELECT title, description
   FROM book
   WHERE subject = @foo
END

To:

CREATE PROCEDURE books_by_subject_lis
   @foo nvarchar(50)
AS
BEGIN
   DECLARE @localFoo INT -- Declare local var
   SET @localFoo = @foo -- Set local var
   
   SELECT title, description
   FROM book
   WHERE subject = @localFoo -- Use local var
END

Resources:

http://elegantcode.com/2008/05/17/sql-parameter-sniffing-and-what-to-do-about-it/
http://omnibuzz-sql.blogspot.com/2006/11/parameter-sniffing-stored-procedures.html

TLDR

Solution to SQL queries suddenly running slow


TLDR
'

Solution to SQL queries suddenly running slow

'
2
Nov

A problem I've been having with the Daily Puppy widget I have on Petnibs.com is it doesn't display the same size image each day.  Aside from the laziness on their part, it meant the iframe I was using would never be the perfect size.  Either there would be extra space below it or it'd cut off part of the featured image.

It's easy to resize/manipulate a local domain iFrame based on the content, but Petnibs is on a hosted platform and the Daily Puppy widget lives on another server.  It takes a little bit more effort, but the solution I found on Adam on Life explains it well and is pretty cool.

Pages Need:

  1. Parent - loads the iFrame widget, included global iFrame resizing javascript.
  2. iFrame 1 - the content of the widget, on another domain, includes iFrame 2, and tells iFrame 2 what size iFrame 1 is.
  3. iFrame 2 - is on the parent domain, checks to see if a height value exists in the URL string, passes value to iFrame resize script on parent.

Basically since Parent and iFrame 2 are on the same domain and linked together by iFrame 1, iFrame 2 is able to call functions on Parent and pass in values.

TLDR

Found solution to resizing iframe cross domain


TLDR
'

Found solution to resizing iframe cross domain

'
19
Oct

How to add a "What is your question?" field at the top of all your pages so users can start asking a question from anywhere. I posted screenshots on the Meta Stack Exchange site, but wanted to post more detail here for others to get at.

Submitting the form takes you to the normal Ask Question page, pre-populates the form with your question, and displays any related questions above the form field.

Below is the code to implement a generic version of this form. I removed any style attributes that were specific to Petnibs.

CSS
Add to the theme custom CSS field

/* ========================= */
/* Ask Question             */
/* ========================= */

#content { margin-top: 64px; }

#ask-question-form {
   padding: 0px;
   position: relative;
   top: 88px;
   width: 470px;
}

#ask-question-form form {
   margin: 0px;
   padding: 0px;
}

#ask-question-form input {
   font-size: 17px;
   float: left;
   width: 348px;
}

#ask-question-form button {
   cursor: pointer;
   float: left;
   font-size: 130%;
   font-weight: bold;
   height: 31px;
   line-height: 31px;
   margin: 5px;
   padding: 0px 8px;
}

/* ========================= */
/* Search results          */
/* ========================= */

#search-title {
   margin-top: 18px;
}

#search-message {
   color: #F00;
   line-height: 18px;
   margin-top: 18px;
}

#search-results {
   max-height: 165px;
   overflow: auto;
   width: 665px;
}

#search-results .question-summary {
   padding-bottom: 2px;
   padding-top: 2px;
   width: 640px;
}

#search-results .summary {
   width: 450px;
}

#search-results h3 {
   font-size: 14px;
   margin-bottom: 2px;
   margin-top: 2px;
}

#search-results .search-text {
   font-size: 11px;
   line-height: 13px;
}

#search-suggest {
   color: #888;
   padding-left: 8px;
}

Ask Question Quick Form Code
Add to the end of the Linkbar content field

<div id="ask-question-form">
   <form action="/questions/ask" method="get">
      <input id="question" name="question" type="text" value="What is your question?" />
      <button>Ask</button>
   </form>
</div>

Ask Question Form Code
Add both sets of code to the end of the Question Help content field. For the second block, wrap it with <script type="text/javascript>...</script>.

<style type="text/css">
#ask-question-form { display: none; }
#content { margin-top: 45px; }
#question-suggestions { display: none; }
</style>

Look for //modify and tweak those lines to your preference.

// Insert new elements
$("#post-form").before('<div id="search-message" style="display: none;"></div>');
$("#post-form").before('<h3 id="search-title" style="display: none;"></h3>');
$("#post-form").before('<div id="search-results" style="display: none;"></div>');

// Set question title field from url.question if blank

$().ready(function() {
   if ($("#ask-error-container").length == 0) {
      if ($("#title").val().length == 0) {
         if ($.getUrlVar('question').length > 2) {
            $("#title").val($.getUrlVar('question'));
            QuestionSuggestions();
         }
      }
   }
});

// Override QuestionSuggestions()

QuestionSuggestions = function() {
   var s = $("#title").val();
   if (s.length > 2) {
      document.title = s + " - [site_name.com]"; // Modify
      $.ajax({
         url: "/search/titles?like=" + escape(s),
         cache: false,
         success: function(data){
         $("#question-suggestions").html(data);
            process_results();
         }
      });
   }
}

// Plugin to retrieve URL variables

$.extend({
   getUrlVars: function(){
      var vars = [], hash;
      var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
      for(var i = 0; i < hashes.length; i++)
      {
         hash = hashes[i].split('=');
         vars.push(hash[0]);
         vars[hash[0]] = hash[1];
      }
      return vars;
   },
   getUrlVar: function(name){
      var return_string = $.getUrlVars()[name];
      return_string = jQuery.trim(return_string).replace(/\+/g,' ');
      return unescape(return_string);
   }
});

// Display related results

function process_results() {
   var results = $("#question-suggestions").html();
   var htmlString = "";
   
   results = jQuery.trim(results);
   
   if (results != "That's not a very good title. Can you add some more unique words to it?") {
      
      // Change results to related questions list, clear out any previous results, show count
      results = $(".answer-summary");
      $('#search-results').html('');
      $('#search-title').html('Related Questions (' + results.length + ')');
      
      // Loop over related questions and create new HTML string to display results
      $.each(results, function(i, n) {
         
         var htmlString = "<div class='question-summary narrow id-"+i+"'>";
         var textString = "";
         
         // Add question votes
         htmlString += "<div class='search-link'>" + $(this).find(".answer-link").html() + "</div>";
         
         // Add answer votes
         votesCount = $(this).find(".answer-votes").text();
         if ( votesCount == 1) { votesString = "<div>vote</div>"; } else { votesString = "<div>votes</div>"; }
         htmlString += "<div class='votes'><div class='mini-counts'>" + $(this).find(".answer-votes").text() + "</div>" + votesString + "</div>";
         
         // Add title
         htmlString += "<div class='summary'><h3>";
         htmlString += "<a href=" + $(this).find("a.question-hyperlink").attr("href") + ">";
         htmlString += $(this).find(".question-hyperlink").text();
         htmlString += "</a></h3>";
         
         // Add summary
         htmlString += "<div class='search-text'>" + $(this).find("a.question-hyperlink").attr("title") + "</div>";
         htmlString += "</div></div>";
         
         // Insert into DOM
         $('#search-results').append(htmlString);
         
         // hacky : Remove href from answer-link, string spaces and parentheses, replace content
         $('#search-results .id-'+i+' a.question-hyperlink').remove();
         textString = $('#search-results .id-'+i+' .search-link').text();
         textString = textString.replace(/[\(\)\.\-\s,]/g,'');
         if (textString == '') { textString = "<div class='status'><div class='mini-counts'>0" } else { textString = "<div class='status answered'><div class='mini-counts'>" + textString };
         if ( textString == 1) { answersString = "<div>answer</div>"; } else { answersString = "<div>answers</div>"; }
         textString = textString + "</div>" + answersString + "</div>";
         $('#search-results .id-'+i+' .search-link').replaceWith(textString);
         
      });
      
      $("#search-message").html("Your question may have already been asked.<br />Please review the following related questions before submitting a new one."); // Modify
      // Show related search results
      $("#search-message").show("fast");
      $("#search-title").show("fast");
      $('#search-results').show("slow");
      
   }
   else {
   
      $("#search-message").html("Your question seems too short. Are there additional unique words you can add to it?"); // Modify
      // Show results - message only
      $("#search-message").show("fast");
      $("#search-title").hide();
      $('#search-results').hide();
   }
}

TLDR

How to add Ask Question form to all Stack Exchange pages


TLDR
'

How to add Ask Question form to all Stack Exchange pages

'
16
Oct

Just got back from the Stack Overflow DevDays conference in LA.  Overall it was worth the price of $99 a ticket and is unheard of.  I enjoyed the speakers, many of which are making me itch to learn a new language (Python in particular).

Available Swag


The Crowd

The crowd seemed a bit light.  My guess would be 150 people.  A few members of my team weren't able to make it at the last minute either.  And as expected, most software developers are shy and had trouble mingling with strangers.  I know I personally, really wanted to say high to Joel & Jeff and shake hands.  Everyone was respectable, though.

Favorite Presentations


The Not-So-Favorites

  • Wi-fi was horrible and caused slowdowns / problems for some presenters not being able to pull up something in their browser.  Aside from the Google App Engine everything else should have been local or cached.
  • Some presentations dragged on and were uninteresting.  Or would have been more interesting had it not been slowed down by coding bugs or build executions of which I can't imagine an actual developer having to sit through 100 times a day.
  • Wished Jeff Atwood would have had more to present.  I liked listening to his short talk.
  • Was a bit hard to find the room, but maybe I should have read the directions before heading out rather than looking them up on my phone while wandering around.
  • Not enough networking with other developers, but as I said, probably the fault of scared developers released out into the real world.

Other Goods

  • The coffee was good, though at times not hot.
  • Cookies were amazing!
  • During the lunch break there were tables set out each with a specific topic to break off and discuss with others.  I missed listening in to any because we headed off to Chipotle for food.  The conference was serving salads, cookies, chips, muffins, and beverages.
  • Kiln for FogBugz was announced.  Looks pretty great, but not an option for people who host FogBugz internally.
  • Audience members had a chance to announce they were hiring and able to meet developers.  Also a time for anyone to mention local user groups in the area.  Sadly, very few were mentioned.

In the end, would I go next year?  Yes! The price alone makes it a no-brainer.  However, if it's the same talks it will get boring fast.  The presentations were all introductory and pretty easy to grasp.  More intermediate talks would be cool.  Technology is always advancing so I imagine it won't be hard to find new topics.  Couch DB or principle talks of software development / project management would be great. I hope some people post pics. I wanted to do a Qik video stream, but wasn't sure if it would be allowed.

TLDR

My experience at the Stack Overflow DevDays Conference


TLDR
'

My experience at the Stack Overflow DevDays Conference

'
7
Oct

Late last night / early this morning I launched Petnibs.com, a knowledge exchange site for pets.  Originally this was to launch as a social network site, but because of my work schedule I just never had the time to get it off the ground.  Graciously, I was able to turn it into a questions / answers site using the Stack Exchange platform which has already proven itself with the many sites that exist under it.

I was literally able to get this site up in under 3 days and am extremely excited to work on it.  I already have a long list of features I can't wait to implement.  You can follow the progress @petnibs and very soon I'll likely be putting up another blog to be able to go into more detail.

TLDR

I finally launched petnibs.com!


TLDR
'

I finally launched petnibs.com!

'