473,465 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can I make this Stored Proc more efficient?

Roy
Hey all, I'm a relative newcomer to asp.net and have 2 simple code
snippets below. Everything works fine, I'm just curious if there is a
more efficient way to do the job as the update takes quite a while.

Here's the proc:
****************************************
CREATE PROCEDURE [Update_Unassigned_VD]
@Recon char(10),
@Book nvarchar(50),
@Van nvarchar(5),
@ContNum nvarchar(8),
@VDN nvarchar(8),
@POE nvarchar(4),
@Shipname nvarchar(50),
@Saildate nvarchar(20),
@POD nvarchar(4),
@Carrier nvarchar(8),
@PCFN nvarchar(10),
@TCN nvarchar(20),
@Lastevent nvarchar(5),
@leloc nvarchar(50),
@leshipname nvarchar(50),
@ccity nvarchar(50),
@preBook nvarchar(50),
@preVan nvarchar(5),
@preCNUM nvarchar(8)
AS

SET NOCOUNT ON

UPDATE [first]
SET carrier_booking_nr = @Book,
van_owner = @Van,
tcon_container_num = @ContNum,
voydoc = @VDN,
poe = @POE,
Ship_Name = @Shipname,
minof315_event_date = @Saildate,
pod = @POD,
ocean_carrier_cd = @Carrier,
PCFN = @PCFN,
tcn = @TCN,
lastevent = @Lastevent,
lasteventloc = @leloc,
Lastevent_shipname = @leshipname,
consigneecity = @ccity,
Recon_Status = @Recon
WHERE carrier_booking_nr = @preBook AND
van_owner = @preVan AND
tcon_container_num = @preCNUM
GO
*****************************

Here's the vb code (this is the "update" portion of a datagrid, btw)
which calls the stored proc:
************************************

Sub FVDGrid_UpdateCommand(Sender As Object, E As
DataGridCommandEventArgs)
FVDConnection = New SqlConnection(blahblahblah)
Dim strRecon as String = CType(e.Item.Cells(2).Controls(0),
TextBox).Text
Dim strBook As String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
Dim strVan As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim strContNum as String = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
Dim strVDN as String = CType(e.Item.Cells(6).Controls(0), TextBox).Text
Dim strPoE as String = CType(e.Item.Cells(7).Controls(0), TextBox).Text
Dim strShipName as String = CType(e.Item.Cells(8).Controls(0),
TextBox).Text
Dim strSailDate as String = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
Dim strPod as String = CType(e.Item.Cells(10).Controls(0),
TextBox).Text
Dim strCarrier As String = CType(e.Item.Cells(11).Controls(0),
TextBox).Text
Dim strPCFN as String = CType(e.Item.Cells(12).Controls(0),
TextBox).Text
Dim strTCN as String = CType(e.Item.Cells(13).Controls(0),
TextBox).Text
Dim strLastEvent As String = CType(e.Item.Cells(14).Controls(0),
TextBox).Text
Dim strLELoc as String = CType(e.Item.Cells(15).Controls(0),
TextBox).Text
Dim strLEShipName as String = CType(e.Item.Cells(17).Controls(0),
TextBox).Text
Dim strCCity as String = CType(e.Item.Cells(18).Controls(0),
TextBox).Text

Dim objCommand As SqlCommand = New SqlCommand("Update_Unassigned_VD",
FVDConnection)
Dim Cnt_Command As SqlCommand = New SqlCommand("Update_Counts",
FVDConnection)

With objCommand
..CommandType = CommandType.StoredProcedure
..Parameters.Add (New SqlParameter("@Recon", SqlDbType.char,10)).Value =
strRecon.Trim
..Parameters.Add (New SqlParameter("@Book",
SqlDbType.nvarchar,50)).Value = strBook.Trim
..Parameters.Add (New SqlParameter("@Van", SqlDbType.nvarchar,5)).Value
= strVan.Trim
..Parameters.Add (New SqlParameter("@ContNum",
SqlDbType.nvarchar,8)).Value = strContNum.Trim
..Parameters.Add (New SqlParameter("@VDN", SqlDbType.nvarchar,8)).Value
= strVDN.Trim
..Parameters.Add (New SqlParameter("@POE", SqlDbType.nvarchar,4)).Value
= strPoE.Trim
..Parameters.Add (New SqlParameter("@Shipname",
SqlDbType.nvarchar,50)).Value = strShipName.Trim
..Parameters.Add (New SqlParameter("@Saildate",
SqlDbType.nvarchar,20)).Value = strSailDate.Trim
..Parameters.Add (New SqlParameter("@POD", SqlDbType.nvarchar,4)).Value
= strPod.Trim
..Parameters.Add (New SqlParameter("@Carrier",
SqlDbType.nvarchar,8)).Value = strCarrier.Trim
..Parameters.Add (New SqlParameter("@PCFN",
SqlDbType.nvarchar,10)).Value = strPCFN.Trim
..Parameters.Add (New SqlParameter("@TCN", SqlDbType.nvarchar,20)).Value
= strTCN.Trim
..Parameters.Add (New SqlParameter("@Lastevent",
SqlDbType.nvarchar,5)).Value = strLastEvent.Trim
..Parameters.Add (New SqlParameter("@leloc",
SqlDbType.nvarchar,50)).Value = strLELoc.Trim
..Parameters.Add (New SqlParameter("@leshipname",
SqlDbType.nvarchar,50)).Value = strLEShipName.Trim
..Parameters.Add (New SqlParameter("@ccity",
SqlDbType.nvarchar,50)).Value = strCCity.Trim
..Parameters.Add (New SqlParameter("@preBook",
SqlDbType.nvarchar,50)).Value = strPreBook
..Parameters.Add (New SqlParameter("@preVan",
SqlDbType.nvarchar,5)).Value = strPreVan
..Parameters.Add (New SqlParameter("@preCNUM",
SqlDbType.nvarchar,8)).Value = strPreCNUM
End With

FVDConnection.Open()
objCommand.ExecuteNonQuery()
Cnt_Command.ExecuteNonQuery()
FVDConnection.Close()

FVDGrid.EditItemIndex = -1
FVDGrid.DataBind()

Bind_FVD_Grid()
End Sub

Nov 19 '05 #1
6 1425
Roy:

Couple things I noticed:

What do:
FVDGrid.DataBind()
Bind_FVD_Grid()
those do? Just by the name I'd expect Bind_FVD_Grid() to be doing a
DataBind() on FVDGrid...so maybe that's being called twice when it
shouldn't?

My guess is that you can combine: Update_Unassigned_VD and Update_Counts
into a single Sproc call, but without knowing what Update_Counts does,
that's a guess

I'd be interested in knowing what's taking the time. Is it specifically the
update SPROC ? have you profiled it? Have you looked at the execution plan?
If [first] is a large table, you might need to look at a better indexing
strategy.

That's all i got...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Roy" <ro**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hey all, I'm a relative newcomer to asp.net and have 2 simple code
snippets below. Everything works fine, I'm just curious if there is a
more efficient way to do the job as the update takes quite a while.

Here's the proc:
****************************************
CREATE PROCEDURE [Update_Unassigned_VD]
@Recon char(10),
@Book nvarchar(50),
@Van nvarchar(5),
@ContNum nvarchar(8),
@VDN nvarchar(8),
@POE nvarchar(4),
@Shipname nvarchar(50),
@Saildate nvarchar(20),
@POD nvarchar(4),
@Carrier nvarchar(8),
@PCFN nvarchar(10),
@TCN nvarchar(20),
@Lastevent nvarchar(5),
@leloc nvarchar(50),
@leshipname nvarchar(50),
@ccity nvarchar(50),
@preBook nvarchar(50),
@preVan nvarchar(5),
@preCNUM nvarchar(8)
AS

SET NOCOUNT ON

UPDATE [first]
SET carrier_booking_nr = @Book,
van_owner = @Van,
tcon_container_num = @ContNum,
voydoc = @VDN,
poe = @POE,
Ship_Name = @Shipname,
minof315_event_date = @Saildate,
pod = @POD,
ocean_carrier_cd = @Carrier,
PCFN = @PCFN,
tcn = @TCN,
lastevent = @Lastevent,
lasteventloc = @leloc,
Lastevent_shipname = @leshipname,
consigneecity = @ccity,
Recon_Status = @Recon
WHERE carrier_booking_nr = @preBook AND
van_owner = @preVan AND
tcon_container_num = @preCNUM
GO
*****************************

Here's the vb code (this is the "update" portion of a datagrid, btw)
which calls the stored proc:
************************************

Sub FVDGrid_UpdateCommand(Sender As Object, E As
DataGridCommandEventArgs)
FVDConnection = New SqlConnection(blahblahblah)
Dim strRecon as String = CType(e.Item.Cells(2).Controls(0),
TextBox).Text
Dim strBook As String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
Dim strVan As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim strContNum as String = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
Dim strVDN as String = CType(e.Item.Cells(6).Controls(0), TextBox).Text
Dim strPoE as String = CType(e.Item.Cells(7).Controls(0), TextBox).Text
Dim strShipName as String = CType(e.Item.Cells(8).Controls(0),
TextBox).Text
Dim strSailDate as String = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
Dim strPod as String = CType(e.Item.Cells(10).Controls(0),
TextBox).Text
Dim strCarrier As String = CType(e.Item.Cells(11).Controls(0),
TextBox).Text
Dim strPCFN as String = CType(e.Item.Cells(12).Controls(0),
TextBox).Text
Dim strTCN as String = CType(e.Item.Cells(13).Controls(0),
TextBox).Text
Dim strLastEvent As String = CType(e.Item.Cells(14).Controls(0),
TextBox).Text
Dim strLELoc as String = CType(e.Item.Cells(15).Controls(0),
TextBox).Text
Dim strLEShipName as String = CType(e.Item.Cells(17).Controls(0),
TextBox).Text
Dim strCCity as String = CType(e.Item.Cells(18).Controls(0),
TextBox).Text

Dim objCommand As SqlCommand = New SqlCommand("Update_Unassigned_VD",
FVDConnection)
Dim Cnt_Command As SqlCommand = New SqlCommand("Update_Counts",
FVDConnection)

With objCommand
.CommandType = CommandType.StoredProcedure
.Parameters.Add (New SqlParameter("@Recon", SqlDbType.char,10)).Value =
strRecon.Trim
.Parameters.Add (New SqlParameter("@Book",
SqlDbType.nvarchar,50)).Value = strBook.Trim
.Parameters.Add (New SqlParameter("@Van", SqlDbType.nvarchar,5)).Value
= strVan.Trim
.Parameters.Add (New SqlParameter("@ContNum",
SqlDbType.nvarchar,8)).Value = strContNum.Trim
.Parameters.Add (New SqlParameter("@VDN", SqlDbType.nvarchar,8)).Value
= strVDN.Trim
.Parameters.Add (New SqlParameter("@POE", SqlDbType.nvarchar,4)).Value
= strPoE.Trim
.Parameters.Add (New SqlParameter("@Shipname",
SqlDbType.nvarchar,50)).Value = strShipName.Trim
.Parameters.Add (New SqlParameter("@Saildate",
SqlDbType.nvarchar,20)).Value = strSailDate.Trim
.Parameters.Add (New SqlParameter("@POD", SqlDbType.nvarchar,4)).Value
= strPod.Trim
.Parameters.Add (New SqlParameter("@Carrier",
SqlDbType.nvarchar,8)).Value = strCarrier.Trim
.Parameters.Add (New SqlParameter("@PCFN",
SqlDbType.nvarchar,10)).Value = strPCFN.Trim
.Parameters.Add (New SqlParameter("@TCN", SqlDbType.nvarchar,20)).Value
= strTCN.Trim
.Parameters.Add (New SqlParameter("@Lastevent",
SqlDbType.nvarchar,5)).Value = strLastEvent.Trim
.Parameters.Add (New SqlParameter("@leloc",
SqlDbType.nvarchar,50)).Value = strLELoc.Trim
.Parameters.Add (New SqlParameter("@leshipname",
SqlDbType.nvarchar,50)).Value = strLEShipName.Trim
.Parameters.Add (New SqlParameter("@ccity",
SqlDbType.nvarchar,50)).Value = strCCity.Trim
.Parameters.Add (New SqlParameter("@preBook",
SqlDbType.nvarchar,50)).Value = strPreBook
.Parameters.Add (New SqlParameter("@preVan",
SqlDbType.nvarchar,5)).Value = strPreVan
.Parameters.Add (New SqlParameter("@preCNUM",
SqlDbType.nvarchar,8)).Value = strPreCNUM
End With

FVDConnection.Open()
objCommand.ExecuteNonQuery()
Cnt_Command.ExecuteNonQuery()
FVDConnection.Close()

FVDGrid.EditItemIndex = -1
FVDGrid.DataBind()

Bind_FVD_Grid()
End Sub

Nov 19 '05 #2
Roy
Awesome Karl, thanks!
You're right on the extra Databinds, I stripped those out.
You're also right on combining those two procs... don't know why that
hadn't occurred to me before.

And your mentioning "profiling" and "execution plan" makes me realize
how much I still have to learn about sql server... How do I profile it
and look at the exec plan? Do you have a link handy?

Thanks again.

Nov 19 '05 #3
I don' thave one handy...but start up SQL Profiler, go "File" --> "New" -->
"Trace" enter an admin username/password to your database

go to "Events" and make sure only "Stored Procedure:RPC:Completed" is in the
right box and click "run"

go to your page and make that sproc execute...you should see it (and
possibly others) in the the profiler. It will also include a time in
milliseconds. If it's a long time (say more than 300ms) click on it and
copy the command it executed....paste it in Query Analyzer, under "Query"
select "Show Execution Plan" and run the query...you'll see anew tab at
the bottom called "Execution Plan"

It's kinda messy figuring that out :) but look for Table Scans which is
typically a sign that you need some more indexes...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Roy" <ro**********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Awesome Karl, thanks!
You're right on the extra Databinds, I stripped those out.
You're also right on combining those two procs... don't know why that
hadn't occurred to me before.

And your mentioning "profiling" and "execution plan" makes me realize
how much I still have to learn about sql server... How do I profile it
and look at the exec plan? Do you have a link handy?

Thanks again.

Nov 19 '05 #4
Roy
Great, I'll give it a try!

BTW, a quick question on the index which you may know... When I execute
stored proc "x" and lets say I have 4 indexes on the table that "x"
works on, how does "x" decide which index to use (assuming that all 4
indexes include the fields in the stored proc's WHERE clause)?

Thanks for the input!

Nov 19 '05 #5
Roy,
This is outside my field of expertise :) I just know how to read basic
execution plans and attempt to make them better :)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Roy" <ro**********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Great, I'll give it a try!

BTW, a quick question on the index which you may know... When I execute
stored proc "x" and lets say I have 4 indexes on the table that "x"
works on, how does "x" decide which index to use (assuming that all 4
indexes include the fields in the stored proc's WHERE clause)?

Thanks for the input!

Nov 19 '05 #6
Hi Roy:

It gets to be quite a complicated topic actually and quite
interesting. SQL Server has a cost based optimizer that looks at a
number of factors, including how selective the index is and how much
disk IO it will take to move through the index. In my experience SQL
really likes covering indexes (indexes that include all the columns
needed in the query) - probably because they will be the most IO
efficient (no need to follow a bookmark back to the original data to
grab columns that are not part of the index).

I have a little introductory article on the subject:
http://odetocode.com/Articles/237.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/
On 15 Feb 2005 08:01:44 -0800, "Roy" <ro**********@gmail.com> wrote:
Great, I'll give it a try!

BTW, a quick question on the index which you may know... When I execute
stored proc "x" and lets say I have 4 indexes on the table that "x"
works on, how does "x" decide which index to use (assuming that all 4
indexes include the fields in the stored proc's WHERE clause)?

Thanks for the input!


Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: LineVoltageHalogen | last post by:
Greetings All, currentley there is a heated discussion in my place of work over which method is better/more efficient for simple selects. Background: 1.) Simple Application that uses sql server...
2
by: Rhino | last post by:
I am getting an sqlcode of -927 when I execute SQL within a COBOL stored procedure in DB2 OS/390 Version 6 on OS/390. I have looked at the error message for that condition and tried everything I...
1
by: mike | last post by:
If I try and do a "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1" after I have run a stored procedure in DB2 version 7.2, I get the last generated Key before the CallableStatement was executed...
14
by: Roy | last post by:
Apologies for the cross-post, but this truly is a two-sided question. Given the option of creating Looping statements within a stored proc of sql server or in the code-behind of an .net webpage,...
4
by: Andrew Baker | last post by:
I have the following code that calles a stored proc in SQLServer. When the output parameter @custref is null (System.DBNull) I cant seem to find a test for this and I get an exception. I know I...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
4
by: hicks | last post by:
I'm trying to invoke a DB2 stored procedure. The stored proc is coded in C and compiled to a shared library, which has been placed in the <DB2 dir>/functions directory. The platform is Solaris....
0
by: balaji krishna | last post by:
Hi, I need to handle the return set from COBOL stored procedure from my invoking Java program. I do not know, how many rows the stored proc SQL fetches.I have declared the cursor in that proc, but i...
3
by: codefragment | last post by:
Hi I have a chunky bit of sql that I will want to call from a number of places. It will return a few thousand rows. Whats the best way of structuring this? 1) I initially thought of using...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.