John's profileJohn BarshingerPhotosBlogLists Tools Help

Blog


    July 24

    Indexed View in SQL Server 2005

    Indexed Views can be now used in any version of SQL 2005 but will not get used automatically by the query optimizer unless you’re running enterprise edition (SQL2000 required the enterprise or developer edition to even create them).

    Example (not a good one since this query is not very expensive):

    IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[INDEXED_VIEW_TEST]'))
    DROP VIEW [dbo].[INDEXED_VIEW_TEST]
    GO

    CREATE VIEW [dbo].[INDEXED_VIEW_TEST] WITH SCHEMABINDING
    AS
    SELECT  A.[Name],
                      AMC.[AudienceID],
                      AMC.[CompanyLocations],
                      AMC.[Sections],
                      AMC.[Departments],
                      AMC.[JobTitles],
                      AMC.[CustomFieldValue1],
                      AMC.[CustomFieldValue2],
                      AMC.[CustomFieldValue3],
                      AMC.[CustomFieldValue4],
                      AMC.[CustomFieldValue5]
    FROM    [dbo].[AudienceMemberCriteria] AMC
    INNER JOIN [dbo].[Audiences] A ON AMC.[AudienceID] = A.[ID]
    GO

    IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[INDEXED_VIEW_TEST]') AND name = N'IDX_INDEXED_VIEW_TEST')
    DROP INDEX [IDX_INDEXED_VIEW_TEST] ON [dbo].[INDEXED_VIEW_TEST] WITH ( ONLINE = OFF )
    GO

    CREATE UNIQUE CLUSTERED INDEX [IDX_INDEXED_VIEW_TEST] ON [dbo].[INDEXED_VIEW_TEST]  ([AudienceID] ASC)
    GO

    -- this query does not use the index, this is query 1 below
    SELECT * FROM INDEXED_VIEW_TEST
    GO

    -- this query DOES use the index, this is query 2 below
    SELECT * FROM INDEXED_VIEW_TEST WITH (noexpand)
    GO

    You can see that query 2 is much more efficient. Query 1 takes 70% of total time to run while Query 2 takes only 30% of the time. Also, there is no table join for Query 2.

    clip_image002

    July 21

    iPhone (not 3G) with OS 2.0 works on T-Mobile

    I can vouch that this actually works: http://lifehacker.com/398906/jailbreak-iphone-20-with-pwnagetool

    I used the previous version (2.0) of the pwnagetool (macOS version) with no problems whatsoever (there is a newer release) on one iPhone with 2.0, one iPhone with 1.1.4 and on one with 1.1.3 installed prior to using this tool.

    I created only 1 patched 2.0 ipsw file using this tool. And then I restored the phones from DFU mode using this same ipsw file. You have to hold down option and click restore so iTunes will prompt you for the file to use. It’s all documented on the lifehacker link above.

    Finally, I recommend getting the $19.99/month Blackberry Internet Service from T-Mobile. Then all you have to do is set the APN for the network on the iPhone to “wap.voicestream.com” and everything will work, yup Safari, YouTube, Google maps, App Store, … it all just works.

    Good Luck, all I can say it worked for me with three gen 1 iPhones. This does NOT work with the new 3G phones. Wife and kids are happy with them. I gotta get one more so I can pwn it for me!

    I love the pineapple dudes!

    July 13

    MagicJack

    I’ve had MagicJack for over 6 months now and have been using it as a second line at my house. I must admit that I am underwhelmed. Sound quality is not great, neither when using a headset on my computer with it nor with a regular phone plugged into it. It probably sounds as good as a cell phone with 4 out of 5 bars except for all the occasional burps and squeeks that come from MagicJack. Skype sound quality is much better. However, the price of MagicJack is really cheap. There is no free ride, you get what you pay for. I guess the real test will be when renewal comes up, will I renew it? Don’t know yet…

    Anyway, I’ve found that it works better for me just to connect it to a regular phone and not use the headset on the computer where MagicJack is being run. So I have it hooked up to my Windows 2003 X64 Server and am running it as a service. This works as well as any other scenario for me. I found instructions on how to run MagicJack as a Windows Service here, it’s really pretty easy to do.

    July 07

    Firefox 3 hits the spot!

    I have been using Firefox 3 on both PC and Mac since soon after it was released and have to say that I am pretty impressed. As a developer (and also when I am testing) I need to have “display a notification about every script error” turned on in IE so that I know if any JavaScript errors occur on the code I'm slinging. Unfortunately, this causes errors to pop up regularly when I am using IE on other sites that I read or search which really sux. So now I use Firefox for searching and reading and IE for developing/testing which is much better.

    Also, there are some pretty cool add-ons to make Google stuff much easier to look at. Google (gmail, reader, gcal, …) stuff usually works really well but it’s just plain ugly. The add-ons: Better Gmail2, Better GCal, Better GReader make these things look much better and add more functionality.

    I have to admit that I do like FireFox better than Safari as well and it is nice to have all the same shortcut keys, add-ons, etc on both platforms.

    July 02

    C# Boxing and Unboxing (Cast, AS, Convert, Parse, TryParse)

    There are a lot of ways to do type casting in C#. Hopefully, these examples can help you decide what the best method for each situation you run into…

    const int theInt = 12;
    const string theString = "fred";
    
    object boxedInt = theInt;               // value of theInt is boxed into boxedInt
    object boxedString = theString;         // value of theString is boxed into boxedString
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    // this method of unboxing throws exception on failure
    ///////////////////////////////////////////////////////////////////////////////////////////
    int unboxedI = (int)boxedInt;           // unbox boxedInt to get the int value
    Console.WriteLine("(int) boxedInt={0}, isNull={1}", unboxedI, (unboxedI == null));
    
    string unboxedString = (string) boxedString; // unbox boxedString to get the string value
    Console.WriteLine("(string) boxedString={0}, isNull={1}", unboxedString, (unboxedString == null));
    
    try
    {
        unboxedString = (string) boxedInt;
    } catch (Exception e)
    {
        Console.WriteLine("(string) boxedInt={0}", e.Message);
    }
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    // this method of unboxing returns null on failure (can't do value types with as)
    // unboxedI = boxedInt as int; is a compiler error, int is a value type
    ///////////////////////////////////////////////////////////////////////////////////////////
    unboxedString = boxedString as string;  // returns value of boxed string
    Console.WriteLine("boxedString as string={0}, isNull={1}", unboxedString, (unboxedString == null));
    unboxedString = boxedInt as string;     // returns null
    Console.WriteLine("boxedInt as string={0}, isNull={1}", unboxedString, (unboxedString == null));
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    // this method of unboxing throws exception when it can't determine how to convert
    ///////////////////////////////////////////////////////////////////////////////////////////
    unboxedI = Convert.ToInt32(boxedInt);
    Console.WriteLine("Convert.ToInt32(boxedInt)={0}, isNull={1}", unboxedI, (unboxedI == null));
    unboxedString = Convert.ToString(boxedString);
    Console.WriteLine("Convert.ToString(boxedString)={0}, isNull={1}", unboxedString, (unboxedString == null));
    try
    {
        unboxedI = Convert.ToInt32(boxedString);
    }
    catch (Exception e)
    {
        Console.WriteLine("Convert.ToInt32(boxedString)={0}", e.Message);
    }
    unboxedString = Convert.ToString(boxedInt);
    Console.WriteLine("Convert.ToString(boxedInt)={0}, isNull={1}", unboxedString, (unboxedString == null));
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    // this method of unboxing throws exception when it can't determine how to convert
    ///////////////////////////////////////////////////////////////////////////////////////////
    unboxedI = Int32.Parse(boxedInt.ToString());
    Console.WriteLine("Int32.Parse(boxedInt.ToString())={0}, isNull={1}", unboxedI, (unboxedI == null));
    unboxedString = boxedString.ToString();
    Console.WriteLine("boxedString.ToString()={0}, isNull={1}", unboxedString, (unboxedString == null));
    try
    {
        unboxedI = Int32.Parse(boxedString.ToString());
    }
    catch (Exception e)
    {
        Console.WriteLine("Int32.Parse(boxedString.ToString())={0}", e.Message);
    }
    unboxedString = boxedInt.ToString();
    Console.WriteLine("boxedInt.ToString()={0}, isNull={1}", unboxedString, (unboxedString == null));
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    // this method of unboxing allows you to define the behavior, throw an exception if you want.
    ///////////////////////////////////////////////////////////////////////////////////////////
    int? nullableUnboxedI = null;
    if (Int32.TryParse(boxedInt.ToString(), out unboxedI)) nullableUnboxedI = unboxedI;
    Console.WriteLine("Int32.TryParse(boxedInt.ToString())={0}, isNull={1}", nullableUnboxedI, (nullableUnboxedI == null));
    
    nullableUnboxedI = null;
    if (Int32.TryParse(boxedString.ToString(), out unboxedI)) nullableUnboxedI = unboxedI;
    Console.WriteLine("Int32.TryParse(boxedString.ToString())={0}, isNull={1}", nullableUnboxedI, (nullableUnboxedI == null));
    
    // TryParse for string is simply ".ToString()" ;-). There is a TryParse for Boolean, Byte, Char, DateTime,
    // DateTimeOffset, Decimal, Double, Int16, Int32, Int64, IPAddress, SByte, Single, TimeSpan, UInt16, UInt32, UInt64 
    
    /* results of running this code copied from the console window:
        (int) boxedInt=12, isNull=False
        (string) boxedString=fred, isNull=False
        (string) boxedInt=Unable to cast object of type 'System.Int32' to type 'System.String'.
        boxedString as string=fred, isNull=False
        boxedInt as string=, isNull=True
        Convert.ToInt32(boxedInt)=12, isNull=False
        Convert.ToString(boxedString)=fred, isNull=False
        Convert.ToInt32(boxedString)=Input string was not in a correct format.
        Convert.ToString(boxedInt)=12, isNull=False
        Int32.Parse(boxedInt.ToString())=12, isNull=False
        boxedString.ToString()=fred, isNull=False
        Int32.Parse(boxedString.ToString())=Input string was not in a correct format.
        boxedInt.ToString()=12, isNull=False
        Int32.TryParse(boxedInt.ToString())=12, isNull=False
        Int32.TryParse(boxedString.ToString())=, isNull=True
        Press any key to continue . . .
    */
    
    July 01

    TryParse anyone?

    Hey, I just found out this new TryParse method for objects that can be Parsed in .NET 2.0. It still falls a little short of what I need but it is a step in the right direction. For example, I have a bunch of utilities methods where I pass in an object and “try” to convert it to a float or int or datetime, … If the conversion fails, I’ll return the “default value”. If the conversion succeeds, I’ll return the parsed value. Here’s an example:

    public static float GetFloatFromObject(object objToFloat, float defaultValue)
            {
                float retValue;
                try
                {
                    retValue = Convert.ToSingle(objToFloat.ToString());
                }
                catch (Exception)
                {
                    retValue = defaultValue;
                }
                return retValue;
            }

    Now with .Net 2.0, I can do this instead:

    public static float GetFloatFromObject(object objToFloat, float defaultValue)
    {
        float retValue;

        if ((objToFloat == null) || (!float.TryParse(objToFloat.ToString(), out retValue))) retValue = defaultValue;

        return retValue;
    }

    Not only is this less code, but I hear it performs much better since there is no exception handling during the process.

    Now, of course, if you want to do something when the parse would fail, you’ll need to do it the old fashioned way and do that something when you catch the exception.