Pages

Thursday, January 9, 2014

Dramatically Speed Up Stored Procedures using Temp Tables

If you're wondering how to create a list, or temp table in a SQL Server Stored Procedure, look here.

But what if you're joining to an in-memory table and you experience performance issues?  I had a multiple join against such a table, and found that the query was taking over 12 seconds to complete - causing a timeout ocassionally.  Here was the temp table declaration:

    declare @tempIDs TABLE
    (
        pick_list_id integer
    )
Simple enough, right? Just a long list of integers.  But like I said the entire stored procedure was taking over 12 seconds to execute.  In a database table, my first approach would be to make an index on the column.  So that's what I did in my stored procedure.  It turned out to be incredibly simple and improved performance from 12 seconds to 30 milliseconds.  Looky!

    declare @tempIDs TABLE
    (
        pick_list_id integer not null PRIMARY KEY
    )
That's it! The rest of the stored procedure is exactly the same.  That's a 40000% speed boost!  Not bad for one modified line of code!


...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Tuesday, January 7, 2014

Column Modification Checklist

This is one of those things that I always forget part of, so because I just went through this, I thought I would document what needs to be done to sync your application whenever you modify any column in your database.

This tutorial is designed for:
  • MS SQL Server
  • Visual Studio (for web or desktop)
Quick Checklist:
  1. In SQL Server Management Studio (SSMS)
    1. Check for source and destination columns (for instance if widening "Address1" from 40 to 50, make sure all the columns in the order table, address book, Shipping and Billing etc are all the same)
    2. Update All Views that depend on this column. (SQL Server does not do this automatically)
    3. Update all stored procedures that operate on this column (for instance in and out parameters that access the changed column)
  2. In your Desktop App:
    1. Check all dataset xsd files to ensure the result column maxlengths are updated.
    2. Check all dataset xsd files to ensure the query parameter maxlengths are updated.
    3. Ensure all databound textboxes are set to the correct MaxLength.
    4. Ensure all DataGridView Columns are set to the correct MaxInputLength.
Updating Views:

There is actually a stored procedure for updating views.  Once you have found a dependent view, just run...

EXECUTE sp_refreshview 'dbo.v_myViewName';

That will take care of it.  Of course if your view is no longer valid because of the change, you'll get an appropriately misleading error message from Microsoft.

Updating Stored Procs:

Your stored procs have a header much like this:

ALTER PROCEDURE [dbo].[StoredProcName]
    -- Add the parameters for the stored procedure here
    @customer varchar(10),
    @PurchaseOrder varchar(15),
    @Address1 varchar(40)
AS

These header parameters and any internally declared variables must be changed to match any column changes.



...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Thursday, January 2, 2014

How to access your Main Form from a Static Method

Of course the right way is to pass a handle to the form if it is needed in the static method.

public static void Import(MainForm parent)
{
    parent.whatever();
}
But in my case I just didn't want to pass that handle around - and my application only had one form, so...

Application.OpenForms[0].Refresh();


...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Share This!

Contact Us

Name

Email *

Message *