Showing posts with label query. Show all posts
Showing posts with label query. Show all posts

Friday, March 30, 2012

long time to establish a connection

When I login using QA to my SQL Server database, it takes 15-20 seconds
to establish a connection and open a query window. Drilling into a
database via Enterprise Manager is similar. Once the connection is
established, the server runs plenty fast however.

Can someone tell me why it could take a long time for a connection to
be established?

This behavior occurs when I am local on the box.

Thanks,
John(john.livermore@.inginix.com) writes:
> When I login using QA to my SQL Server database, it takes 15-20 seconds
> to establish a connection and open a query window. Drilling into a
> database via Enterprise Manager is similar. Once the connection is
> established, the server runs plenty fast however.
> Can someone tell me why it could take a long time for a connection to
> be established?
> This behavior occurs when I am local on the box.

One thing to check for is the autoclose on databases. You should be
able to investigate this with sp_helpdb.

Another thing that I have seen is that if I have a QA window open, stop
SQL Server and restart, connections after this takes a long time. Apparently
something is happening to shared memory. Needless to say, these problems
goes away after a reboot.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Long running SQL query in ASP.Net

Greetings,
I have to run a parametirzed sproc on SQL that can take 5-10 minutes to
complete. The query returns a few megs of data that I want to let the
clients download as a CSV. Any ideas on a quick and dirty way to do this
without invoiking SSIS?
Thanks
On Jun 12, 2:44 am, "Shkedy" <shk...@.newsgroups.nospam> wrote:
> Greetings,
> I have to run a parametirzed sproc on SQL that can take 5-10 minutes to
> complete. The query returns a few megs of data that I want to let the
> clients download as a CSV. Any ideas on a quick and dirty way to do this
> without invoiking SSIS?
> Thanks
You can use bcp but this requires xp_cmdshell nned to be enabled ,if
you call from SQL Server
|||Hi Shkedy,
I recommend that you try using bcp command and Import/Export Data Wizard.
Either of the two methods is very convenient, but Import/Export Data Wizard
may be more convenient for you.
For bcp utility, you can refer to:
bcp Utility
http://msdn2.microsoft.com/en-us/library/ms162802(SQL.90).aspx
For Import/Export Data Wizard, please refer to:
How to: Start the SQL Server Import and Export Wizard
http://technet.microsoft.com/en-us/library/ms140052(SQL.90).aspx
Hope this helps. If you have any other questions or concerns, please feel
free to let me know.
Best regards,
Charles Wang
Microsoft Online Community Support
================================================== ===
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications
If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
================================================== ====
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====
|||Thanks for the prompt and informative response.
I ended up using C# to implement this by launching a new thread that
traverses a reader and writes a text file. I added some more bells and
whistles that I omitted from here but this is the gist of the code:
private void btnSubmit_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
File.Delete(Server.MapPath("~") + "\\reports\\" + CreateFileName());
System.Threading.ThreadStart launcher = new System.Threading.ThreadStart(
CreateCSVReport);
System.Threading.Thread thread = new System.Threading.Thread(launcher);
thread.Start();
Page.RegisterStartupScript("popup", String.Format("<script
language='javascript'>popitup('ReportStatus.aspx?f ={0}')</script>'",CreateFileName()));
}
}
private void CreateCSVReport()
{
SqlDataReader rdr;
SqlConnection cnn = new
SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings.Get("ConnectionString"));
SqlCommand cmd = new SqlCommand("sp_mysproc");
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@.param1", SomeValue);
cmd.Parameters.Add("@.param2", SomeValue);
cmd.Connection.Open();
cmd.CommandTimeout = 900;
string tempFileName = Server.MapPath("~") + "\\reports\\temp\\" +
Guid.NewGuid().ToString() + ".csv";
System.IO.StreamWriter wrtr = (File.CreateText(tempFileName));
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection) ;
int rowCount = 0;
for (int i =0; i < rdr.FieldCount; i++)
{
wrtr.Write("{0}",rdr.GetName(i));
if (i < rdr.FieldCount -1 )
wrtr.Write(",");
}
wrtr.WriteLine();
while (rdr.Read())
{
for (int i =0; i < rdr.FieldCount; i++)
{
wrtr.Write("{0}",rdr[i]);
if (i < rdr.FieldCount -1 )
wrtr.Write(",");
}
rowCount++;
wrtr.WriteLine();
if (rowCount % 1000 == 0)
wrtr.Flush();
}
wrtr.Flush();
rdr.Close();
wrtr.Close();
File.Move(tempFileName, Server.MapPath("~") + "\\reports\\" +
CreateFileName());
}
"Charles Wang[MSFT]" <changliw@.online.microsoft.com> wrote in message
news:XKigCINrHHA.1032@.TK2MSFTNGHUB02.phx.gbl...
> Hi Shkedy,
> I recommend that you try using bcp command and Import/Export Data Wizard.
> Either of the two methods is very convenient, but Import/Export Data
> Wizard
> may be more convenient for you.
> For bcp utility, you can refer to:
> bcp Utility
> http://msdn2.microsoft.com/en-us/library/ms162802(SQL.90).aspx
> For Import/Export Data Wizard, please refer to:
> How to: Start the SQL Server Import and Export Wizard
> http://technet.microsoft.com/en-us/library/ms140052(SQL.90).aspx
> Hope this helps. If you have any other questions or concerns, please feel
> free to let me know.
> Best regards,
> Charles Wang
> Microsoft Online Community Support
> ================================================== ===
> Get notification to my posts through email? Please refer to:
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications
> If you are using Outlook Express, please make sure you clear the check box
> "Tools/Options/Read: Get 300 headers at a time" to see your reply
> promptly.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ================================================== ====
> When responding to posts, please "Reply to Group" via
> your newsreader so that others may learn and benefit
> from this issue.
> ================================================== ====
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> ================================================== ====
>
>
|||Hi Shkedy,
Thank you for your reply and the detailed additional feedback on how you
were successful in resolving this issue. This information has been added to
Microsoft's database. Your solution will benefit many other users, and we
really value having you as a Microsoft customer.
If you have any other questions or concerns, please do not hesitate to
contact us. It is always our pleasure to be of assistance.
Have a nice day!
Best regards,
Charles Wang
Microsoft Online Community Support
================================================== ===
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications
If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
================================================== ====
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====

Long running SQL query in ASP.Net

Greetings,
I have to run a parametirzed sproc on SQL that can take 5-10 minutes to
complete. The query returns a few megs of data that I want to let the
clients download as a CSV. Any ideas on a quick and dirty way to do this
without invoiking SSIS?
ThanksOn Jun 12, 2:44 am, "Shkedy" <shk...@.newsgroups.nospam> wrote:
> Greetings,
> I have to run a parametirzed sproc on SQL that can take 5-10 minutes to
> complete. The query returns a few megs of data that I want to let the
> clients download as a CSV. Any ideas on a quick and dirty way to do this
> without invoiking SSIS?
> Thanks
You can use bcp but this requires xp_cmdshell nned to be enabled ,if
you call from SQL Server|||Hi Shkedy,
I recommend that you try using bcp command and Import/Export Data Wizard.
Either of the two methods is very convenient, but Import/Export Data Wizard
may be more convenient for you.
For bcp utility, you can refer to:
bcp Utility
http://msdn2.microsoft.com/en-us/library/ms162802(SQL.90).aspx
For Import/Export Data Wizard, please refer to:
How to: Start the SQL Server Import and Export Wizard
http://technet.microsoft.com/en-us/library/ms140052(SQL.90).aspx
Hope this helps. If you have any other questions or concerns, please feel
free to let me know.
Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications
If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================|||Thanks for the prompt and informative response.
I ended up using C# to implement this by launching a new thread that
traverses a reader and writes a text file. I added some more bells and
whistles that I omitted from here but this is the gist of the code:
private void btnSubmit_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
File.Delete(Server.MapPath("~") + "\\reports\\" + CreateFileName());
System.Threading.ThreadStart launcher = new System.Threading.ThreadStart(
CreateCSVReport);
System.Threading.Thread thread = new System.Threading.Thread(launcher);
thread.Start();
Page.RegisterStartupScript("popup", String.Format("<script
language='javascript'>popitup('ReportStatus.aspx?f={0}')</script>'",CreateFileName()));
}
}
private void CreateCSVReport()
{
SqlDataReader rdr;
SqlConnection cnn = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString"));
SqlCommand cmd = new SqlCommand("sp_mysproc");
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@.param1", SomeValue);
cmd.Parameters.Add("@.param2", SomeValue);
cmd.Connection.Open();
cmd.CommandTimeout = 900;
string tempFileName = Server.MapPath("~") + "\\reports\\temp\\" +
Guid.NewGuid().ToString() + ".csv";
System.IO.StreamWriter wrtr = (File.CreateText(tempFileName));
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
int rowCount = 0;
for (int i =0; i < rdr.FieldCount; i++)
{
wrtr.Write("{0}",rdr.GetName(i));
if (i < rdr.FieldCount -1 )
wrtr.Write(",");
}
wrtr.WriteLine();
while (rdr.Read())
{
for (int i =0; i < rdr.FieldCount; i++)
{
wrtr.Write("{0}",rdr[i]);
if (i < rdr.FieldCount -1 )
wrtr.Write(",");
}
rowCount++;
wrtr.WriteLine();
if (rowCount % 1000 == 0)
wrtr.Flush();
}
wrtr.Flush();
rdr.Close();
wrtr.Close();
File.Move(tempFileName, Server.MapPath("~") + "\\reports\\" +
CreateFileName());
}
"Charles Wang[MSFT]" <changliw@.online.microsoft.com> wrote in message
news:XKigCINrHHA.1032@.TK2MSFTNGHUB02.phx.gbl...
> Hi Shkedy,
> I recommend that you try using bcp command and Import/Export Data Wizard.
> Either of the two methods is very convenient, but Import/Export Data
> Wizard
> may be more convenient for you.
> For bcp utility, you can refer to:
> bcp Utility
> http://msdn2.microsoft.com/en-us/library/ms162802(SQL.90).aspx
> For Import/Export Data Wizard, please refer to:
> How to: Start the SQL Server Import and Export Wizard
> http://technet.microsoft.com/en-us/library/ms140052(SQL.90).aspx
> Hope this helps. If you have any other questions or concerns, please feel
> free to let me know.
> Best regards,
> Charles Wang
> Microsoft Online Community Support
> =====================================================> Get notification to my posts through email? Please refer to:
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications
> If you are using Outlook Express, please make sure you clear the check box
> "Tools/Options/Read: Get 300 headers at a time" to see your reply
> promptly.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ======================================================> When responding to posts, please "Reply to Group" via
> your newsreader so that others may learn and benefit
> from this issue.
> ======================================================> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> ======================================================>
>|||Hi Shkedy,
Thank you for your reply and the detailed additional feedback on how you
were successful in resolving this issue. This information has been added to
Microsoft's database. Your solution will benefit many other users, and we
really value having you as a Microsoft customer.
If you have any other questions or concerns, please do not hesitate to
contact us. It is always our pleasure to be of assistance.
Have a nice day!
Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications
If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================sql

Long running SQL query in ASP.Net

Greetings,
I have to run a parametirzed sproc on SQL that can take 5-10 minutes to
complete. The query returns a few megs of data that I want to let the
clients download as a CSV. Any ideas on a quick and dirty way to do this
without invoiking SSIS?
ThanksOn Jun 12, 2:44 am, "Shkedy" <shk...@.newsgroups.nospam> wrote:
> Greetings,
> I have to run a parametirzed sproc on SQL that can take 5-10 minutes to
> complete. The query returns a few megs of data that I want to let the
> clients download as a CSV. Any ideas on a quick and dirty way to do this
> without invoiking SSIS?
> Thanks
You can use bcp but this requires xp_cmdshell nned to be enabled ,if
you call from SQL Server|||Hi Shkedy,
I recommend that you try using bcp command and Import/Export Data Wizard.
Either of the two methods is very convenient, but Import/Export Data Wizard
may be more convenient for you.
For bcp utility, you can refer to:
bcp Utility
http://msdn2.microsoft.com/en-us/library/ms162802(SQL.90).aspx
For Import/Export Data Wizard, please refer to:
How to: Start the SQL Server Import and Export Wizard
http://technet.microsoft.com/en-us/library/ms140052(SQL.90).aspx
Hope this helps. If you have any other questions or concerns, please feel
free to let me know.
Best regards,
Charles Wang
Microsoft Online Community Support
========================================
=============
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications
If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...t/default.aspx.
========================================
==============
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
========================================
==============
This posting is provided "AS IS" with no warranties, and confers no rights.
========================================
==============|||Thanks for the prompt and informative response.
I ended up using C# to implement this by launching a new thread that
traverses a reader and writes a text file. I added some more bells and
whistles that I omitted from here but this is the gist of the code:
private void btnSubmit_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
File.Delete(Server.MapPath("~") + "\\reports\\" + CreateFileName());
System.Threading.ThreadStart launcher = new System.Threading.ThreadStart(
CreateCSVReport);
System.Threading.Thread thread = new System.Threading.Thread(launcher);
thread.Start();
Page.RegisterStartupScript("popup", String.Format("<script
language='javascript'>popitup('ReportStatus.aspx?f={0}')</script>'",Cre
ateFileName()));
}
}
private void CreateCSVReport()
{
SqlDataReader rdr;
SqlConnection cnn = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("Co
nnectionString"));
SqlCommand cmd = new SqlCommand("sp_mysproc");
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@.param1", SomeValue);
cmd.Parameters.Add("@.param2", SomeValue);
cmd.Connection.Open();
cmd.CommandTimeout = 900;
string tempFileName = Server.MapPath("~") + "\\reports\\temp\\" +
Guid.NewGuid().ToString() + ".csv";
System.IO.StreamWriter wrtr = (File.CreateText(tempFileName));
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
int rowCount = 0;
for (int i =0; i < rdr.FieldCount; i++)
{
wrtr.Write("{0}",rdr.GetName(i));
if (i < rdr.FieldCount -1 )
wrtr.Write(",");
}
wrtr.WriteLine();
while (rdr.Read())
{
for (int i =0; i < rdr.FieldCount; i++)
{
wrtr.Write("{0}",rdr[i]);
if (i < rdr.FieldCount -1 )
wrtr.Write(",");
}
rowCount++;
wrtr.WriteLine();
if (rowCount % 1000 == 0)
wrtr.Flush();
}
wrtr.Flush();
rdr.Close();
wrtr.Close();
File.Move(tempFileName, Server.MapPath("~") + "\\reports\\" +
CreateFileName());
}
"Charles Wang[MSFT]" <changliw@.online.microsoft.com> wrote in message
news:XKigCINrHHA.1032@.TK2MSFTNGHUB02.phx.gbl...
> Hi Shkedy,
> I recommend that you try using bcp command and Import/Export Data Wizard.
> Either of the two methods is very convenient, but Import/Export Data
> Wizard
> may be more convenient for you.
> For bcp utility, you can refer to:
> bcp Utility
> http://msdn2.microsoft.com/en-us/library/ms162802(SQL.90).aspx
> For Import/Export Data Wizard, please refer to:
> How to: Start the SQL Server Import and Export Wizard
> http://technet.microsoft.com/en-us/library/ms140052(SQL.90).aspx
> Hope this helps. If you have any other questions or concerns, please feel
> free to let me know.
> Best regards,
> Charles Wang
> Microsoft Online Community Support
> ========================================
=============
> Get notification to my posts through email? Please refer to:
> [url]http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif[/ur
l]
> ications
> If you are using Outlook Express, please make sure you clear the check box
> "Tools/Options/Read: Get 300 headers at a time" to see your reply
> promptly.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscript...t/default.aspx.
> ========================================
==============
> When responding to posts, please "Reply to Group" via
> your newsreader so that others may learn and benefit
> from this issue.
> ========================================
==============
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> ========================================
==============
>
>|||Hi Shkedy,
Thank you for your reply and the detailed additional feedback on how you
were successful in resolving this issue. This information has been added to
Microsoft's database. Your solution will benefit many other users, and we
really value having you as a Microsoft customer.
If you have any other questions or concerns, please do not hesitate to
contact us. It is always our pleasure to be of assistance.
Have a nice day!
Best regards,
Charles Wang
Microsoft Online Community Support
========================================
=============
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications
If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...t/default.aspx.
========================================
==============
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
========================================
==============
This posting is provided "AS IS" with no warranties, and confers no rights.
========================================
==============

Wednesday, March 28, 2012

Long Running Query - Only from .NET

I have a pretty complex query that aggregates lots of data and inserts multiple rows of that data into a reporting table. When I call this SPROC from SQL Server Management Studio, it executes in under 3 seconds. When I try to execute the same SPROC using .NET's SqlCommand object the query runs indefinitely until the CommandTimeout is reached.

Why would this SPROC behave differently with the same inputs, but being called from .NET?


Thanks for your help!

Turn on debugging, make sure the query isn't causing exception.

ERic

|||

I am debugging in .Net, I don't get an exception until the Timeout is reached.

|||

I fixed it! It was the return method. I was using 'RETURN @.@.ROWCOUNT' rather than 'SELECT @.@.ROWCOUNT'

Thanks for your help

Long running query

Hello group,
We have a query that calculates certain dates, etc. It runs very slow on
this server (3 hours!) which is a Gateway quad 600mhz with 2GB RAM running
Windows 2003 server and SQL 2000. If you copy the database over to another
server or XP workstation (even running a single 2.6 GHZ CPU with 1gb ram)
and run the same query it runs in 2-3 minutes. This is a huge gap of time
and I don't see a whole lot of performance changes using perfmon on the
troubled server.
Any ideas what I can use to troubleshoot this? Do you think this may be
hardware related or database or ?
TIA,
Jason
There can be many reasons. Did you verify using sp_who that this query is
not getting blocked? Did you monitor the physical disk counters to see if
there are any issues with disk subsystem performance?
What about the execution plans? Did you compare the execution plan of the
queries between your server and workstation? This will help in determining
any missing indexes or out of date stats.
Worth checking the fragmentation on your bigger tables and also updating the
statistics.
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Jason King" <jasonk@.bham.wednet.edu> wrote in message
news:%23ETRjMgOFHA.3356@.TK2MSFTNGP12.phx.gbl...
> Hello group,
> We have a query that calculates certain dates, etc. It runs very slow on
> this server (3 hours!) which is a Gateway quad 600mhz with 2GB RAM running
> Windows 2003 server and SQL 2000. If you copy the database over to another
> server or XP workstation (even running a single 2.6 GHZ CPU with 1gb ram)
> and run the same query it runs in 2-3 minutes. This is a huge gap of time
> and I don't see a whole lot of performance changes using perfmon on the
> troubled server.
> Any ideas what I can use to troubleshoot this? Do you think this may be
> hardware related or database or ?
> TIA,
> Jason
>
|||Thanks. We found the problem. It was due to passing NULL parameters within
the SP for retrieving certain dates. If we call the dates first then execute
the procedure without any NULL parameters, it runs very fast.
"Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
news:uIDsfRnOFHA.3280@.TK2MSFTNGP10.phx.gbl...
> There can be many reasons. Did you verify using sp_who that this query is
> not getting blocked? Did you monitor the physical disk counters to see if
> there are any issues with disk subsystem performance?
> What about the execution plans? Did you compare the execution plan of the
> queries between your server and workstation? This will help in determining
> any missing indexes or out of date stats.
> Worth checking the fragmentation on your bigger tables and also updating
the[vbcol=seagreen]
> statistics.
> --
> Vyas, MVP (SQL Server)
> SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
>
> "Jason King" <jasonk@.bham.wednet.edu> wrote in message
> news:%23ETRjMgOFHA.3356@.TK2MSFTNGP12.phx.gbl...
running[vbcol=seagreen]
another[vbcol=seagreen]
ram)[vbcol=seagreen]
time
>
sql

long running query

Hi Every one,
In the next query, it's longer when a spell the fields i
want to be returned than if i just write select * .
This :
SELECT Product.Prd_Dc1 , Product.Prd_Dc1A,LotWO_Printed ,
DetCmd.Cmd_No , ProDep.Pro_Des,ProDep.Pro_DesA,
from .... many tables with inner join
is longer than this :
select * from ...the same table
There must be something not up to date in the database
but what is it ? Stats ? Index ?
thanks !
donald
Well, it may just be miscommunication, but your two examples are not the
same thing. One includes at least a reference to "many...joins" and the
other is just a SELECT. Are the two statements truly the same except for
the *?
"Donald" <anonymous@.discussions.microsoft.com> wrote in message
news:ca4d01c43908$a28037d0$a101280a@.phx.gbl...
> Hi Every one,
> In the next query, it's longer when a spell the fields i
> want to be returned than if i just write select * .
> This :
> SELECT Product.Prd_Dc1 , Product.Prd_Dc1A,LotWO_Printed ,
> DetCmd.Cmd_No , ProDep.Pro_Des,ProDep.Pro_DesA,
> from .... many tables with inner join
> is longer than this :
> select * from ...the same table
>
> There must be something not up to date in the database
> but what is it ? Stats ? Index ?
> thanks !
> donald
|||Sorry, yes, they are truly the same expect for the
Select part.
One is : Select *
and the other is : select field1, field2, etc...
I do not understand. some cue please.
Thanks !
Donald

>--Original Message--
>Well, it may just be miscommunication, but your two
examples are not the
>same thing. One includes at least a reference
to "many...joins" and the
>other is just a SELECT. Are the two statements truly the
same except for
>the *?
>"Donald" <anonymous@.discussions.microsoft.com> wrote in
message[vbcol=seagreen]
>news:ca4d01c43908$a28037d0$a101280a@.phx.gbl...
Product.Prd_Dc1A,LotWO_Printed ,
>
>.
>

long running query

Using SS2000 SP4. I have a query that has been running for 17 hours now and
I'm wondering if it is doing anything. The transaction log for the database
has the same time stamp on it that it had yesterday before I started running
the query and the file size is the same. Shouldn't something have been
written to the transaction logs by now?
There is nothing else running on the server of any consequence. There is
disk activity (reads but no writes ) on the disk where the database is. There
are no reads or writes on the disk where the transaction logs are located.
The processor is at 2%. There is no blocking going on.
Here's the query. I have indexes on all columns in the join and where
clause. There are about 11M records in the table. There is no problem with
free disk space. The tempdb and logs also have a time stamp before I started
the query.
UPDATE tblwf3
SET BRANCH = a.branch_id
from dbo.tblBranchZip a, tblwf3 b
WHERE a.zip = b.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
--and b.branch is null
go
Any idea what might be going on? Thanks.
Dan D.
How about some nasty cartesian product? joining on a zip field (assuming
zip code) would likely result in billions or possibly trillions of joins
between the tables.
Do this. Take the update statement and paste it into another QA window.
Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
over the connecting lines in the plan and note the estimated row count.
Also check the left most icon for total estimated rows and the estimated
query cost. I think you may be surprised by what you see.
Also, note that your join is not ANSI compliant and will not be supported by
sql server at some point in the future. Start working to rewrite stuff like
that into this format:
UPDATE tblwf3
SET BRANCH = a.branch_id
from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
WHERE a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
TheSQLGuru
President
Indicium Resources, Inc.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
> Using SS2000 SP4. I have a query that has been running for 17 hours now
> and
> I'm wondering if it is doing anything. The transaction log for the
> database
> has the same time stamp on it that it had yesterday before I started
> running
> the query and the file size is the same. Shouldn't something have been
> written to the transaction logs by now?
> There is nothing else running on the server of any consequence. There is
> disk activity (reads but no writes ) on the disk where the database is.
> There
> are no reads or writes on the disk where the transaction logs are located.
> The processor is at 2%. There is no blocking going on.
> Here's the query. I have indexes on all columns in the join and where
> clause. There are about 11M records in the table. There is no problem with
> free disk space. The tempdb and logs also have a time stamp before I
> started
> the query.
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a, tblwf3 b
> WHERE a.zip = b.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> --and b.branch is null
> go
> Any idea what might be going on? Thanks.
> --
> Dan D.
|||As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
(s)he posted is actually standard.
The version below is, and because it (also) eliminates the implicit
cross join it should run in a reasonable amount of time.
UPDATE tblwf3
SET BRANCH = (
SELECT a.branch_id
from dbo.tblBranchZip a
WHERE a.zip = tblwf3.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
)
--where b.branch is null
HTH,
Gert-Jan
Dan D. wrote:
> Using SS2000 SP4. I have a query that has been running for 17 hours now and
> I'm wondering if it is doing anything. The transaction log for the database
> has the same time stamp on it that it had yesterday before I started running
> the query and the file size is the same. Shouldn't something have been
> written to the transaction logs by now?
> There is nothing else running on the server of any consequence. There is
> disk activity (reads but no writes ) on the disk where the database is. There
> are no reads or writes on the disk where the transaction logs are located.
> The processor is at 2%. There is no blocking going on.
> Here's the query. I have indexes on all columns in the join and where
> clause. There are about 11M records in the table. There is no problem with
> free disk space. The tempdb and logs also have a time stamp before I started
> the query.
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a, tblwf3 b
> WHERE a.zip = b.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> --and b.branch is null
> go
> Any idea what might be going on? Thanks.
> --
> Dan D.
|||I had checked the execution plan and didn't get any large numbers that seemed
out of the ordinary. On the lower level the first step uses a clustered index
and gives a row count 0f 504. The step on the level above it has another
clustered index and gives a row count of 24,000+. Then there is a nested loop
and parallelism steps that give row counts of 11M but since I know that it's
updating every row in the table I think that is to be expected. The left most
icon shows 11M rows.
Thanks,
Dan D.
"TheSQLGuru" wrote:

> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported by
> sql server at some point in the future. Start working to rewrite stuff like
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
>
>
|||I didn't respond to your suggested rewrite of the query. That is normally how
I write queries and it is what I tried first but there was no difference in
performance. I was grasping at straws. I can update the with either query in
< 10 secs if I have 100K rows. I just didn't expect 11M rows to take so long.
I have 1.74T of free space on RAID 0. We wanted to do RAID 10 but found out
the controller wouldn't support it so I decided to test with RAID 0.
Thanks,
Dan D.
"TheSQLGuru" wrote:

> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported by
> sql server at some point in the future. Start working to rewrite stuff like
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
>
>
|||I should have added that I can also run a query and update 5M rows in about
10 minis. It's just when I try 11M rows something happens and it takes
forever.
Dan D.
"TheSQLGuru" wrote:

> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported by
> sql server at some point in the future. Start working to rewrite stuff like
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
>
>
|||I can see that the execution plan is quite different from the one I was
using. I tried your query on 5M rows and it finished in 11mins which is the
same as the query I was using. However, when I ran it on the 22M row table it
only took 2mins. The indexes are the same so I'm not sure what is going on.
I'll test some more.
Thanks,
Dan D.
"Gert-Jan Strik" wrote:

> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:
>
|||I don't doubt that you are updating 11M rows. The question is how many
TIMES are you updating those 11M rows. 24000+ times perhaps? Not sure
since I don't have access to the data.
However, I can state with almost 100% certainty that this update should NOT
be using a nested loop!! Try using OPTION (HASH JOIN) or MERGE JOIN (prolly
not right if not in sorted order already). See BOL for details.
TheSQLGuru
President
Indicium Resources, Inc.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:E77D9053-A6FE-4E46-9A2F-A42CA967B0B2@.microsoft.com...[vbcol=seagreen]
>I had checked the execution plan and didn't get any large numbers that
>seemed
> out of the ordinary. On the lower level the first step uses a clustered
> index
> and gives a row count 0f 504. The step on the level above it has another
> clustered index and gives a row count of 24,000+. Then there is a nested
> loop
> and parallelism steps that give row counts of 11M but since I know that
> it's
> updating every row in the table I think that is to be expected. The left
> most
> icon shows 11M rows.
> Thanks,
> --
> Dan D.
>
> "TheSQLGuru" wrote:
|||1) From SQL 2005 BOL,
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/40e63302-0c68-4593-af3e-6d190181fee7.htm,
about 3/4 down the page, you will find this example:
USE AdventureWorks;
GO
UPDATE Sales.SalesPerson
SET SalesYTD = SalesYTD + SubTotal
FROM Sales.SalesPerson AS sp
JOIN Sales.SalesOrderHeader AS so
ON sp.SalesPersonID = so.SalesPersonID
AND so.OrderDate = (SELECT MAX(OrderDate)
FROM Sales.SalesOrderHeader
WHERE SalesPersonID =
sp.SalesPersonID);
GO2) "As usual, TheSQLGuru is spot on..." - thanks very much for the
compliment!!
3) "(s)he" - it is he actually. Kevin. Glad to meet you! :-)
TheSQLGuru
President
Indicium Resources, Inc.
"Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
news:46324FBF.204D08B8@.toomuchspamalready.nl...[vbcol=seagreen]
> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:
|||I've tried your method on a couple of other queries and it is definitely
faster. How would you write the query if I wanted to update several rows. For
instance,
UPDATE tblwf3
SET BRANCH = a.branch_id, term=a.term, hhseg=a.hhseg
from dbo.tblBranchZip a inner join tblwf3 b
ON a.zip = b.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
Thanks,
Dan D.
"Gert-Jan Strik" wrote:

> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:
>

Long running Query

Hi,

I'm trying to optimize a long running (several hours) query. This query is a cross join on two tables. Table 1 has 3 fields - ROWID, LAt and Long. Table 2 has Name, Addr1,Addr2,City,State,Zip,Lat,Long.

Both tables has LatRad - Lat in radians, LonRad- Lon in Radians. Sin and Cos values of Lat and Lon are calulated and stored to be used in the distance formula.

What I'm trying to do here is find the nearest dealer (Table 2) for each of Table 1 rows. The Select statement takes a long time to execute as there are about 19 million recs on table 1 and 1250 rows in table 2. I ran into Log issues- filling the transaction log, so I'm currently using table variables and split up the process into 100000 recs at a time. I cross join and calculate the distance (@.DistValues) and then find the minimum distance (tablevar2) for each rowid and then the result is inserted into another Table (ResultTable).

My Code looks like this:

Declare @.DistValues table (DataSeqno varchar(10),Lat2 numeric(20,6),Lon2 numeric(20,6),StoreNo varchar(60), Lat1 numeric(20,6),Long1 numeric(20,6),Distance numeric(20,15))

Declare @.MinDistance table (DataSeqno varchar(10) primary key,distance numeric(20,15))

Insert into @.DistValues

Select DataSeqno,T.Lat Lat2,T.Lon Lon2,S.StoreNo,S.Lat Lat1,S.Long Long1,

distance=3963.1*Case when cast(S.SinLat * T.SinLat + S.CosLat * T.cosLat * cos(T.Lonrad - s.Lonrad) as numeric(20,15)) not between -1.0 and 1.0 then 0.0 else acos(cast(S.SinLat * T.SinLat + S.CosLat * T.cosLat * cos(T.Lonrad - s.Lonrad) as numeric(20,15))) end

from dbo.TopNForProcess T , dbo.Table2 S where Isnull(T.Lat,0) <> 0 and Isnull(T.Lon,0)<> 0

Insert into @.MinDistance

Select DataSeqno,Min(distance) From @.DistValues Group by DataSeqno

Insert into ResultTable (DataSeqno,Lat2,Lon2,StoreNo,LAt1,Long1,distance)

Select D.DataSeqno, D.Lat2, D.Lon2, D.StoreNo, D.LAt1, D.Long1, M.distance from @.DistValues D Inner Join @.MinDistance M on D.DataSeqno = M.DataSeqno and D.Distance = M.Distance

I created a View called TopNForProcess which looks like this. This cut down the processing time compared to when I had this as the Subquery.

SELECT TOP (100000) DataSeqno, lat, Lon, LatRad, LonRad, SinLat, cosLat, SinLon, CosLon FROM Table1 WHERE (DataSeqno NOT IN (SELECT DataSeqno FROM dbo.ResultTable)) AND (ISNULL(lat, 0) <> 0) AND (ISNULL(Lon, 0) <> 0)

I have indexes on table table1 - Rowid and another one with Lat and lon. Table2 - Lat and Long.

Is there any way this can be optimized/improved? This is already in a stored procedure.

Thanks in advance.

Hello

I just wrote a longer post but i deleted it by pressing the wrong mouse key :(

Anyway.. Tips to optimize - short edition now...

1. Avoid a FULL cross join... If someone is 180° from your store, then i BET he wont be a best match. Limit the stores to the max range you expect + some room for error (depending on how good you can guess) Example:

where Store.Lang between cust.lang - 10 and cust.lang + 10

This is simplified, since i dont take the rollovers at 180° into account (Lang gues from -180 to + 180 right? I allways mix them up... Even in my native language)

If you really want to make it optimal, then you could analyse the store table 1st and determine the "closest" lang and long for each store... So you dont use the "worst case" range for all stores... I am sure in NYC there will be a shorter range then in "Nowhere",Tx

2. Avoid a function on the LEFT side of the where clause! So NO whatever(store.lang) = Bla Bla... This will prohibit the use of an Index...

3. Do you need ALL data NOW or only some data on request? (Like for 1 Custumer?) If its only some on request. Consider using a calculated collumn... But this collumn cant be indexed since the function would point to an external resource. Another thing is how often do stores open/close? If its often then the function becomes more attractive...

|||Use the Sql server 2005 CLR integration|||

Thanks. I do have a parameter that would limit the range and I used only 1 deg and even then it took hours. All the customers had to be calculated at one time and we're planning to schedule this as a job.

|||

joeydj wrote:

Use the Sql server 2005 CLR integration

We are yet to try this. Will sure try this one.Thanks.

|||

Hello

Try to precalculate a "cover Area" for each store. Define this area by 4 Numbers. Max and min long/lat. Store this information in each store record.

This cover area should only take other stores to calculate. Then define 2 new Indexes on those fields. Each consisting max and min of the cover area (for each long and lat)

Now you will be able to reduce the amount of hits to a few per custumer. From those you can calculate the exact distance and extract the closest one. Thats still a lot of processing of geometry functions, but it should be a lot faster. You might also think about simplifying the formulat you use to a^2+b^2=c^2 (There is also no need to calculate the root from c in order get the closest value of c)

Yes... Its kinda cheating but the error you would get here ~0... Its at least so small that the custumer in the same region wont be able to tell the difference ;)

|||Thanks. We decided to follow this route as we just found out now that our current legacy program was doing this. thanks for your inputs.

long running query

Using SS2000 SP4. I have a query that has been running for 17 hours now and
I'm wondering if it is doing anything. The transaction log for the database
has the same time stamp on it that it had yesterday before I started running
the query and the file size is the same. Shouldn't something have been
written to the transaction logs by now?
There is nothing else running on the server of any consequence. There is
disk activity (reads but no writes ) on the disk where the database is. Ther
e
are no reads or writes on the disk where the transaction logs are located.
The processor is at 2%. There is no blocking going on.
Here's the query. I have indexes on all columns in the join and where
clause. There are about 11M records in the table. There is no problem with
free disk space. The tempdb and logs also have a time stamp before I started
the query.
UPDATE tblwf3
SET BRANCH = a.branch_id
from dbo.tblBranchZip a, tblwf3 b
WHERE a.zip = b.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
--and b.branch is null
go
Any idea what might be going on? Thanks.
--
Dan D.How about some nasty cartesian product? joining on a zip field (assuming
zip code) would likely result in billions or possibly trillions of joins
between the tables.
Do this. Take the update statement and paste it into another QA window.
Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
over the connecting lines in the plan and note the estimated row count.
Also check the left most icon for total estimated rows and the estimated
query cost. I think you may be surprised by what you see.
Also, note that your join is not ANSI compliant and will not be supported by
sql server at some point in the future. Start working to rewrite stuff like
that into this format:
UPDATE tblwf3
SET BRANCH = a.branch_id
from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
WHERE a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
TheSQLGuru
President
Indicium Resources, Inc.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
> Using SS2000 SP4. I have a query that has been running for 17 hours now
> and
> I'm wondering if it is doing anything. The transaction log for the
> database
> has the same time stamp on it that it had yesterday before I started
> running
> the query and the file size is the same. Shouldn't something have been
> written to the transaction logs by now?
> There is nothing else running on the server of any consequence. There is
> disk activity (reads but no writes ) on the disk where the database is.
> There
> are no reads or writes on the disk where the transaction logs are located.
> The processor is at 2%. There is no blocking going on.
> Here's the query. I have indexes on all columns in the join and where
> clause. There are about 11M records in the table. There is no problem with
> free disk space. The tempdb and logs also have a time stamp before I
> started
> the query.
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a, tblwf3 b
> WHERE a.zip = b.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> --and b.branch is null
> go
> Any idea what might be going on? Thanks.
> --
> Dan D.|||As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
(s)he posted is actually standard.
The version below is, and because it (also) eliminates the implicit
cross join it should run in a reasonable amount of time.
UPDATE tblwf3
SET BRANCH = (
SELECT a.branch_id
from dbo.tblBranchZip a
WHERE a.zip = tblwf3.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
)
--where b.branch is null
HTH,
Gert-Jan
Dan D. wrote:
> Using SS2000 SP4. I have a query that has been running for 17 hours now an
d
> I'm wondering if it is doing anything. The transaction log for the databas
e
> has the same time stamp on it that it had yesterday before I started runni
ng
> the query and the file size is the same. Shouldn't something have been
> written to the transaction logs by now?
> There is nothing else running on the server of any consequence. There is
> disk activity (reads but no writes ) on the disk where the database is. Th
ere
> are no reads or writes on the disk where the transaction logs are located.
> The processor is at 2%. There is no blocking going on.
> Here's the query. I have indexes on all columns in the join and where
> clause. There are about 11M records in the table. There is no problem with
> free disk space. The tempdb and logs also have a time stamp before I start
ed
> the query.
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a, tblwf3 b
> WHERE a.zip = b.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> --and b.branch is null
> go
> Any idea what might be going on? Thanks.
> --
> Dan D.|||I had checked the execution plan and didn't get any large numbers that seeme
d
out of the ordinary. On the lower level the first step uses a clustered inde
x
and gives a row count 0f 504. The step on the level above it has another
clustered index and gives a row count of 24,000+. Then there is a nested loo
p
and parallelism steps that give row counts of 11M but since I know that it's
updating every row in the table I think that is to be expected. The left mos
t
icon shows 11M rows.
Thanks,
--
Dan D.
"TheSQLGuru" wrote:

> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported
by
> sql server at some point in the future. Start working to rewrite stuff li
ke
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
>
>|||I didn't respond to your suggested rewrite of the query. That is normally ho
w
I write queries and it is what I tried first but there was no difference in
performance. I was grasping at straws. I can update the with either query in
< 10 secs if I have 100K rows. I just didn't expect 11M rows to take so long
.
I have 1.74T of free space on RAID 0. We wanted to do RAID 10 but found out
the controller wouldn't support it so I decided to test with RAID 0.
Thanks,
--
Dan D.
"TheSQLGuru" wrote:

> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported
by
> sql server at some point in the future. Start working to rewrite stuff li
ke
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
>
>|||I should have added that I can also run a query and update 5M rows in about
10 minis. It's just when I try 11M rows something happens and it takes
forever.
--
Dan D.
"TheSQLGuru" wrote:

> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported
by
> sql server at some point in the future. Start working to rewrite stuff li
ke
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
>
>|||I can see that the execution plan is quite different from the one I was
using. I tried your query on 5M rows and it finished in 11mins which is the
same as the query I was using. However, when I ran it on the 22M row table i
t
only took 2mins. The indexes are the same so I'm not sure what is going on.
I'll test some more.
Thanks,
--
Dan D.
"Gert-Jan Strik" wrote:

> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:
>|||I don't doubt that you are updating 11M rows. The question is how many
TIMES are you updating those 11M rows. 24000+ times perhaps? Not sure
since I don't have access to the data.
However, I can state with almost 100% certainty that this update should NOT
be using a nested loop!! Try using OPTION (HASH JOIN) or MERGE JOIN (prolly
not right if not in sorted order already). See BOL for details.
TheSQLGuru
President
Indicium Resources, Inc.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:E77D9053-A6FE-4E46-9A2F-A42CA967B0B2@.microsoft.com...[vbcol=seagreen]
>I had checked the execution plan and didn't get any large numbers that
>seemed
> out of the ordinary. On the lower level the first step uses a clustered
> index
> and gives a row count 0f 504. The step on the level above it has another
> clustered index and gives a row count of 24,000+. Then there is a nested
> loop
> and parallelism steps that give row counts of 11M but since I know that
> it's
> updating every row in the table I think that is to be expected. The left
> most
> icon shows 11M rows.
> Thanks,
> --
> Dan D.
>
> "TheSQLGuru" wrote:
>|||1) From SQL 2005 BOL,
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/40e63302-0c68-4593-af3e-
6d190181fee7.htm,
about 3/4 down the page, you will find this example:
USE AdventureWorks;
GO
UPDATE Sales.SalesPerson
SET SalesYTD = SalesYTD + SubTotal
FROM Sales.SalesPerson AS sp
JOIN Sales.SalesOrderHeader AS so
ON sp.SalesPersonID = so.SalesPersonID
AND so.OrderDate = (SELECT MAX(OrderDate)
FROM Sales.SalesOrderHeader
WHERE SalesPersonID =
sp.SalesPersonID);
GO2) "As usual, TheSQLGuru is spot on..." - thanks very much for the
compliment!!
3) "(s)he" - it is he actually. Kevin. Glad to meet you! :-)
TheSQLGuru
President
Indicium Resources, Inc.
"Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
news:46324FBF.204D08B8@.toomuchspamalready.nl...[vbcol=seagreen]
> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:|||I've tried your method on a couple of other queries and it is definitely
faster. How would you write the query if I wanted to update several rows. Fo
r
instance,
UPDATE tblwf3
SET BRANCH = a.branch_id, term=a.term, hhseg=a.hhseg
from dbo.tblBranchZip a inner join tblwf3 b
ON a.zip = b.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
Thanks,
Dan D.
"Gert-Jan Strik" wrote:

> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:
>

Long Running Query

how much data including messages is being returned by the
sp,
if very little, then it probably is not a network issue.
if a lot, then everything points to a network issue.
transfer a large file (~100MB) between the client and
server, how long does it take? Fast Ethernet at
100Mbit/sec in full-duplex should move data between 5-
10MByte/sec depending on the number of hops.
if you are not on Fast Ethernet with Full-duplex mode,
then it may very well take forever.
given that FE switches are so inexpensive, i would not
bother trying other tricks to make this work on half-duplex

>--Original Message--
>I have a SP, which displays results within 15 secs on
local SQL server, but when trying execute from other SQL
Client, it takes more than 5 mins. And I don't find any
network issues. Can anybody help me to isolate the issue
>Thanks
>Bhagya
>.
>Hi,
Thanks, for the reply.
But the data that is returned by SP is hardly 50kb. What could be the reaso
n for the delay.
Thanks
Bhagya|||Do you have SET NOCOUNT ON?
Andrew J. Kelly
SQL Server MVP
"Bhagya" <anonymous@.discussions.microsoft.com> wrote in message
news:DAC51A3C-550C-4163-AADB-86BDC8270F47@.microsoft.com...
> Hi,
> Thanks, for the reply.
> But the data that is returned by SP is hardly 50kb. What could be the
reason for the delay.
> Thanks
> Bhagya|||15 sec on the local server is still an expensive query.
what is it doing?
does the execution plan show a hash match or hash join?
if so, are more than 10K rows in hash ? regardless of the
final row count.
when you say local, are you running the sp from QA,
from other clients: is that QA or your own application

>--Original Message--
>Hi,
>Thanks, for the reply.
>But the data that is returned by SP is hardly 50kb. What
could be the reason for the delay.
>Thanks
>Bhagya
>.
>|||Hi Joe,
Basically this SP is returning the data used in a report. It is doing Hash
match / Part Aggregate join. There are about 5000000 row in the Hash.
The table has about more than 10000000 rows and it is indexed, as per requir
ement, and columns used in joins and filters
Thanks
Bhagya|||In-Reply-To: <FD457C8C-D6E2-4AFF-A92A-3F8D19D7CC89@.microsoft.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <eevt6#V9DHA.3176@.TK2MSFTNGP11.phx.gbl>
Newsgroups: microsoft.public.sqlserver.server
NNTP-Posting-Host: h-67-101-128-225.nycmny83.dynamic.covad.net 67.101.128.22
5
Path: TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
Lines: 1
Xref: TK2MSFTNGP08.phx.gbl microsoft.public.sqlserver.server:331063
Bhagya,
Is it possible that you are seeing this bug (does your query have a
LIKE operator, and are you using SQL Server 7.0?)
http://support.microsoft.com/defaul...5&Product=sql2k
SK
Bhagya wrote:

>Hi Joe,
>Basically this SP is returning the data used in a report. It is doing Hash
match / Part Aggregate join. There are about 5000000 row in the Hash.
>The table has about more than 10000000 rows and it is indexed, as per requi
rement, and columns used in joins and filters
>Thanks
>Bhagya
>sql

Long running query

Hello group,
We have a query that calculates certain dates, etc. It runs very slow on
this server (3 hours!) which is a Gateway quad 600mhz with 2GB RAM running
Windows 2003 server and SQL 2000. If you copy the database over to another
server or XP workstation (even running a single 2.6 GHZ CPU with 1gb ram)
and run the same query it runs in 2-3 minutes. This is a huge gap of time
and I don't see a whole lot of performance changes using perfmon on the
troubled server.
Any ideas what I can use to troubleshoot this? Do you think this may be
hardware related or database or '
TIA,
JasonThere can be many reasons. Did you verify using sp_who that this query is
not getting blocked? Did you monitor the physical disk counters to see if
there are any issues with disk subsystem performance?
What about the execution plans? Did you compare the execution plan of the
queries between your server and workstation? This will help in determining
any missing indexes or out of date stats.
Worth checking the fragmentation on your bigger tables and also updating the
statistics.
--
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Jason King" <jasonk@.bham.wednet.edu> wrote in message
news:%23ETRjMgOFHA.3356@.TK2MSFTNGP12.phx.gbl...
> Hello group,
> We have a query that calculates certain dates, etc. It runs very slow on
> this server (3 hours!) which is a Gateway quad 600mhz with 2GB RAM running
> Windows 2003 server and SQL 2000. If you copy the database over to another
> server or XP workstation (even running a single 2.6 GHZ CPU with 1gb ram)
> and run the same query it runs in 2-3 minutes. This is a huge gap of time
> and I don't see a whole lot of performance changes using perfmon on the
> troubled server.
> Any ideas what I can use to troubleshoot this? Do you think this may be
> hardware related or database or '
> TIA,
> Jason
>|||Thanks. We found the problem. It was due to passing NULL parameters within
the SP for retrieving certain dates. If we call the dates first then execute
the procedure without any NULL parameters, it runs very fast.
"Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
news:uIDsfRnOFHA.3280@.TK2MSFTNGP10.phx.gbl...
> There can be many reasons. Did you verify using sp_who that this query is
> not getting blocked? Did you monitor the physical disk counters to see if
> there are any issues with disk subsystem performance?
> What about the execution plans? Did you compare the execution plan of the
> queries between your server and workstation? This will help in determining
> any missing indexes or out of date stats.
> Worth checking the fragmentation on your bigger tables and also updating
the
> statistics.
> --
> Vyas, MVP (SQL Server)
> SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
>
> "Jason King" <jasonk@.bham.wednet.edu> wrote in message
> news:%23ETRjMgOFHA.3356@.TK2MSFTNGP12.phx.gbl...
running[vbcol=seagreen]
another[vbcol=seagreen]
ram)[vbcol=seagreen]
time[vbcol=seagreen]
>

long running query

Hi Every one,
In the next query, it's longer when a spell the fields i
want to be returned than if i just write select * .
This :
SELECT Product.Prd_Dc1 , Product.Prd_Dc1A,LotWO_Printed ,
DetCmd.Cmd_No , ProDep.Pro_Des,ProDep.Pro_DesA,
from .... many tables with inner join
is longer than this :
select * from ...the same table
There must be something not up to date in the database
but what is it ? Stats ? Index ?
thanks !
donaldWell, it may just be miscommunication, but your two examples are not the
same thing. One includes at least a reference to "many...joins" and the
other is just a SELECT. Are the two statements truly the same except for
the *?
"Donald" <anonymous@.discussions.microsoft.com> wrote in message
news:ca4d01c43908$a28037d0$a101280a@.phx.gbl...
> Hi Every one,
> In the next query, it's longer when a spell the fields i
> want to be returned than if i just write select * .
> This :
> SELECT Product.Prd_Dc1 , Product.Prd_Dc1A,LotWO_Printed ,
> DetCmd.Cmd_No , ProDep.Pro_Des,ProDep.Pro_DesA,
> from .... many tables with inner join
> is longer than this :
> select * from ...the same table
>
> There must be something not up to date in the database
> but what is it ? Stats ? Index ?
> thanks !
> donald|||Sorry, yes, they are truly the same expect for the
Select part.
One is : Select *
and the other is : select field1, field2, etc...
I do not understand. some cue please.
Thanks !
Donald

>--Original Message--
>Well, it may just be miscommunication, but your two
examples are not the
>same thing. One includes at least a reference
to "many...joins" and the
>other is just a SELECT. Are the two statements truly the
same except for
>the *?
>"Donald" <anonymous@.discussions.microsoft.com> wrote in
message
>news:ca4d01c43908$a28037d0$a101280a@.phx.gbl...
Product.Prd_Dc1A,LotWO_Printed ,[vbcol=seagreen]
>
>.
>

long running query

Using SS2000 SP4. I have a query that has been running for 17 hours now and
I'm wondering if it is doing anything. The transaction log for the database
has the same time stamp on it that it had yesterday before I started running
the query and the file size is the same. Shouldn't something have been
written to the transaction logs by now?
There is nothing else running on the server of any consequence. There is
disk activity (reads but no writes ) on the disk where the database is. There
are no reads or writes on the disk where the transaction logs are located.
The processor is at 2%. There is no blocking going on.
Here's the query. I have indexes on all columns in the join and where
clause. There are about 11M records in the table. There is no problem with
free disk space. The tempdb and logs also have a time stamp before I started
the query.
UPDATE tblwf3
SET BRANCH = a.branch_id
from dbo.tblBranchZip a, tblwf3 b
WHERE a.zip = b.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
--and b.branch is null
go
Any idea what might be going on? Thanks.
--
Dan D.How about some nasty cartesian product? joining on a zip field (assuming
zip code) would likely result in billions or possibly trillions of joins
between the tables.
Do this. Take the update statement and paste it into another QA window.
Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
over the connecting lines in the plan and note the estimated row count.
Also check the left most icon for total estimated rows and the estimated
query cost. I think you may be surprised by what you see.
Also, note that your join is not ANSI compliant and will not be supported by
sql server at some point in the future. Start working to rewrite stuff like
that into this format:
UPDATE tblwf3
SET BRANCH = a.branch_id
from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
WHERE a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
TheSQLGuru
President
Indicium Resources, Inc.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
> Using SS2000 SP4. I have a query that has been running for 17 hours now
> and
> I'm wondering if it is doing anything. The transaction log for the
> database
> has the same time stamp on it that it had yesterday before I started
> running
> the query and the file size is the same. Shouldn't something have been
> written to the transaction logs by now?
> There is nothing else running on the server of any consequence. There is
> disk activity (reads but no writes ) on the disk where the database is.
> There
> are no reads or writes on the disk where the transaction logs are located.
> The processor is at 2%. There is no blocking going on.
> Here's the query. I have indexes on all columns in the join and where
> clause. There are about 11M records in the table. There is no problem with
> free disk space. The tempdb and logs also have a time stamp before I
> started
> the query.
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a, tblwf3 b
> WHERE a.zip = b.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> --and b.branch is null
> go
> Any idea what might be going on? Thanks.
> --
> Dan D.|||As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
(s)he posted is actually standard.
The version below is, and because it (also) eliminates the implicit
cross join it should run in a reasonable amount of time.
UPDATE tblwf3
SET BRANCH = (
SELECT a.branch_id
from dbo.tblBranchZip a
WHERE a.zip = tblwf3.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
)
--where b.branch is null
HTH,
Gert-Jan
Dan D. wrote:
> Using SS2000 SP4. I have a query that has been running for 17 hours now and
> I'm wondering if it is doing anything. The transaction log for the database
> has the same time stamp on it that it had yesterday before I started running
> the query and the file size is the same. Shouldn't something have been
> written to the transaction logs by now?
> There is nothing else running on the server of any consequence. There is
> disk activity (reads but no writes ) on the disk where the database is. There
> are no reads or writes on the disk where the transaction logs are located.
> The processor is at 2%. There is no blocking going on.
> Here's the query. I have indexes on all columns in the join and where
> clause. There are about 11M records in the table. There is no problem with
> free disk space. The tempdb and logs also have a time stamp before I started
> the query.
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a, tblwf3 b
> WHERE a.zip = b.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> --and b.branch is null
> go
> Any idea what might be going on? Thanks.
> --
> Dan D.|||I had checked the execution plan and didn't get any large numbers that seemed
out of the ordinary. On the lower level the first step uses a clustered index
and gives a row count 0f 504. The step on the level above it has another
clustered index and gives a row count of 24,000+. Then there is a nested loop
and parallelism steps that give row counts of 11M but since I know that it's
updating every row in the table I think that is to be expected. The left most
icon shows 11M rows.
Thanks,
--
Dan D.
"TheSQLGuru" wrote:
> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported by
> sql server at some point in the future. Start working to rewrite stuff like
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
> > Using SS2000 SP4. I have a query that has been running for 17 hours now
> > and
> > I'm wondering if it is doing anything. The transaction log for the
> > database
> > has the same time stamp on it that it had yesterday before I started
> > running
> > the query and the file size is the same. Shouldn't something have been
> > written to the transaction logs by now?
> >
> > There is nothing else running on the server of any consequence. There is
> > disk activity (reads but no writes ) on the disk where the database is.
> > There
> > are no reads or writes on the disk where the transaction logs are located.
> > The processor is at 2%. There is no blocking going on.
> >
> > Here's the query. I have indexes on all columns in the join and where
> > clause. There are about 11M records in the table. There is no problem with
> > free disk space. The tempdb and logs also have a time stamp before I
> > started
> > the query.
> >
> > UPDATE tblwf3
> > SET BRANCH = a.branch_id
> > from dbo.tblBranchZip a, tblwf3 b
> > WHERE a.zip = b.zip5
> > and a.branch_id < '2900'
> > and a.Franchise is null
> > and a.closed is null
> > and a.commercial is null
> > and a.fume is null
> > --and b.branch is null
> >
> > go
> >
> > Any idea what might be going on? Thanks.
> > --
> > Dan D.
>
>|||I didn't respond to your suggested rewrite of the query. That is normally how
I write queries and it is what I tried first but there was no difference in
performance. I was grasping at straws. I can update the with either query in
< 10 secs if I have 100K rows. I just didn't expect 11M rows to take so long.
I have 1.74T of free space on RAID 0. We wanted to do RAID 10 but found out
the controller wouldn't support it so I decided to test with RAID 0.
Thanks,
--
Dan D.
"TheSQLGuru" wrote:
> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported by
> sql server at some point in the future. Start working to rewrite stuff like
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
> > Using SS2000 SP4. I have a query that has been running for 17 hours now
> > and
> > I'm wondering if it is doing anything. The transaction log for the
> > database
> > has the same time stamp on it that it had yesterday before I started
> > running
> > the query and the file size is the same. Shouldn't something have been
> > written to the transaction logs by now?
> >
> > There is nothing else running on the server of any consequence. There is
> > disk activity (reads but no writes ) on the disk where the database is.
> > There
> > are no reads or writes on the disk where the transaction logs are located.
> > The processor is at 2%. There is no blocking going on.
> >
> > Here's the query. I have indexes on all columns in the join and where
> > clause. There are about 11M records in the table. There is no problem with
> > free disk space. The tempdb and logs also have a time stamp before I
> > started
> > the query.
> >
> > UPDATE tblwf3
> > SET BRANCH = a.branch_id
> > from dbo.tblBranchZip a, tblwf3 b
> > WHERE a.zip = b.zip5
> > and a.branch_id < '2900'
> > and a.Franchise is null
> > and a.closed is null
> > and a.commercial is null
> > and a.fume is null
> > --and b.branch is null
> >
> > go
> >
> > Any idea what might be going on? Thanks.
> > --
> > Dan D.
>
>|||I should have added that I can also run a query and update 5M rows in about
10 minis. It's just when I try 11M rows something happens and it takes
forever.
--
Dan D.
"TheSQLGuru" wrote:
> How about some nasty cartesian product? joining on a zip field (assuming
> zip code) would likely result in billions or possibly trillions of joins
> between the tables.
> Do this. Take the update statement and paste it into another QA window.
> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> over the connecting lines in the plan and note the estimated row count.
> Also check the left most icon for total estimated rows and the estimated
> query cost. I think you may be surprised by what you see.
> Also, note that your join is not ANSI compliant and will not be supported by
> sql server at some point in the future. Start working to rewrite stuff like
> that into this format:
> UPDATE tblwf3
> SET BRANCH = a.branch_id
> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> WHERE a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
>
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
> > Using SS2000 SP4. I have a query that has been running for 17 hours now
> > and
> > I'm wondering if it is doing anything. The transaction log for the
> > database
> > has the same time stamp on it that it had yesterday before I started
> > running
> > the query and the file size is the same. Shouldn't something have been
> > written to the transaction logs by now?
> >
> > There is nothing else running on the server of any consequence. There is
> > disk activity (reads but no writes ) on the disk where the database is.
> > There
> > are no reads or writes on the disk where the transaction logs are located.
> > The processor is at 2%. There is no blocking going on.
> >
> > Here's the query. I have indexes on all columns in the join and where
> > clause. There are about 11M records in the table. There is no problem with
> > free disk space. The tempdb and logs also have a time stamp before I
> > started
> > the query.
> >
> > UPDATE tblwf3
> > SET BRANCH = a.branch_id
> > from dbo.tblBranchZip a, tblwf3 b
> > WHERE a.zip = b.zip5
> > and a.branch_id < '2900'
> > and a.Franchise is null
> > and a.closed is null
> > and a.commercial is null
> > and a.fume is null
> > --and b.branch is null
> >
> > go
> >
> > Any idea what might be going on? Thanks.
> > --
> > Dan D.
>
>|||I can see that the execution plan is quite different from the one I was
using. I tried your query on 5M rows and it finished in 11mins which is the
same as the query I was using. However, when I ran it on the 22M row table it
only took 2mins. The indexes are the same so I'm not sure what is going on.
I'll test some more.
Thanks,
--
Dan D.
"Gert-Jan Strik" wrote:
> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:
> >
> > Using SS2000 SP4. I have a query that has been running for 17 hours now and
> > I'm wondering if it is doing anything. The transaction log for the database
> > has the same time stamp on it that it had yesterday before I started running
> > the query and the file size is the same. Shouldn't something have been
> > written to the transaction logs by now?
> >
> > There is nothing else running on the server of any consequence. There is
> > disk activity (reads but no writes ) on the disk where the database is. There
> > are no reads or writes on the disk where the transaction logs are located.
> > The processor is at 2%. There is no blocking going on.
> >
> > Here's the query. I have indexes on all columns in the join and where
> > clause. There are about 11M records in the table. There is no problem with
> > free disk space. The tempdb and logs also have a time stamp before I started
> > the query.
> >
> > UPDATE tblwf3
> > SET BRANCH = a.branch_id
> > from dbo.tblBranchZip a, tblwf3 b
> > WHERE a.zip = b.zip5
> > and a.branch_id < '2900'
> > and a.Franchise is null
> > and a.closed is null
> > and a.commercial is null
> > and a.fume is null
> > --and b.branch is null
> >
> > go
> >
> > Any idea what might be going on? Thanks.
> > --
> > Dan D.
>|||I don't doubt that you are updating 11M rows. The question is how many
TIMES are you updating those 11M rows. 24000+ times perhaps? Not sure
since I don't have access to the data.
However, I can state with almost 100% certainty that this update should NOT
be using a nested loop!! Try using OPTION (HASH JOIN) or MERGE JOIN (prolly
not right if not in sorted order already). See BOL for details.
--
TheSQLGuru
President
Indicium Resources, Inc.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:E77D9053-A6FE-4E46-9A2F-A42CA967B0B2@.microsoft.com...
>I had checked the execution plan and didn't get any large numbers that
>seemed
> out of the ordinary. On the lower level the first step uses a clustered
> index
> and gives a row count 0f 504. The step on the level above it has another
> clustered index and gives a row count of 24,000+. Then there is a nested
> loop
> and parallelism steps that give row counts of 11M but since I know that
> it's
> updating every row in the table I think that is to be expected. The left
> most
> icon shows 11M rows.
> Thanks,
> --
> Dan D.
>
> "TheSQLGuru" wrote:
>> How about some nasty cartesian product? joining on a zip field (assuming
>> zip code) would likely result in billions or possibly trillions of joins
>> between the tables.
>> Do this. Take the update statement and paste it into another QA window.
>> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
>> over the connecting lines in the plan and note the estimated row count.
>> Also check the left most icon for total estimated rows and the estimated
>> query cost. I think you may be surprised by what you see.
>> Also, note that your join is not ANSI compliant and will not be supported
>> by
>> sql server at some point in the future. Start working to rewrite stuff
>> like
>> that into this format:
>> UPDATE tblwf3
>> SET BRANCH = a.branch_id
>> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
>> WHERE a.branch_id < '2900'
>> and a.Franchise is null
>> and a.closed is null
>> and a.commercial is null
>> and a.fume is null
>>
>> --
>> TheSQLGuru
>> President
>> Indicium Resources, Inc.
>> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
>> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
>> > Using SS2000 SP4. I have a query that has been running for 17 hours now
>> > and
>> > I'm wondering if it is doing anything. The transaction log for the
>> > database
>> > has the same time stamp on it that it had yesterday before I started
>> > running
>> > the query and the file size is the same. Shouldn't something have been
>> > written to the transaction logs by now?
>> >
>> > There is nothing else running on the server of any consequence. There
>> > is
>> > disk activity (reads but no writes ) on the disk where the database is.
>> > There
>> > are no reads or writes on the disk where the transaction logs are
>> > located.
>> > The processor is at 2%. There is no blocking going on.
>> >
>> > Here's the query. I have indexes on all columns in the join and where
>> > clause. There are about 11M records in the table. There is no problem
>> > with
>> > free disk space. The tempdb and logs also have a time stamp before I
>> > started
>> > the query.
>> >
>> > UPDATE tblwf3
>> > SET BRANCH = a.branch_id
>> > from dbo.tblBranchZip a, tblwf3 b
>> > WHERE a.zip = b.zip5
>> > and a.branch_id < '2900'
>> > and a.Franchise is null
>> > and a.closed is null
>> > and a.commercial is null
>> > and a.fume is null
>> > --and b.branch is null
>> >
>> > go
>> >
>> > Any idea what might be going on? Thanks.
>> > --
>> > Dan D.
>>|||1) From SQL 2005 BOL,
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/40e63302-0c68-4593-af3e-6d190181fee7.htm,
about 3/4 down the page, you will find this example:
USE AdventureWorks;
GO
UPDATE Sales.SalesPerson
SET SalesYTD = SalesYTD + SubTotal
FROM Sales.SalesPerson AS sp
JOIN Sales.SalesOrderHeader AS so
ON sp.SalesPersonID = so.SalesPersonID
AND so.OrderDate = (SELECT MAX(OrderDate)
FROM Sales.SalesOrderHeader
WHERE SalesPersonID = sp.SalesPersonID);
GO2) "As usual, TheSQLGuru is spot on..." - thanks very much for the
compliment!!
3) "(s)he" - it is he actually. Kevin. Glad to meet you! :-)
--
TheSQLGuru
President
Indicium Resources, Inc.
"Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
news:46324FBF.204D08B8@.toomuchspamalready.nl...
> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:
>> Using SS2000 SP4. I have a query that has been running for 17 hours now
>> and
>> I'm wondering if it is doing anything. The transaction log for the
>> database
>> has the same time stamp on it that it had yesterday before I started
>> running
>> the query and the file size is the same. Shouldn't something have been
>> written to the transaction logs by now?
>> There is nothing else running on the server of any consequence. There is
>> disk activity (reads but no writes ) on the disk where the database is.
>> There
>> are no reads or writes on the disk where the transaction logs are
>> located.
>> The processor is at 2%. There is no blocking going on.
>> Here's the query. I have indexes on all columns in the join and where
>> clause. There are about 11M records in the table. There is no problem
>> with
>> free disk space. The tempdb and logs also have a time stamp before I
>> started
>> the query.
>> UPDATE tblwf3
>> SET BRANCH = a.branch_id
>> from dbo.tblBranchZip a, tblwf3 b
>> WHERE a.zip = b.zip5
>> and a.branch_id < '2900'
>> and a.Franchise is null
>> and a.closed is null
>> and a.commercial is null
>> and a.fume is null
>> --and b.branch is null
>> go
>> Any idea what might be going on? Thanks.
>> --
>> Dan D.|||How can I tell how many times I'm updating the rows? The cost for the nested
loop is 2%. And if I write the query as you suggested the execution plan is
exactly the same.
I tried the way Gert-Jan suggested and I get a much different execution plan
and the query runs in a little over 2 minutes.
Thanks,
--
Dan D.
"TheSQLGuru" wrote:
> I don't doubt that you are updating 11M rows. The question is how many
> TIMES are you updating those 11M rows. 24000+ times perhaps? Not sure
> since I don't have access to the data.
> However, I can state with almost 100% certainty that this update should NOT
> be using a nested loop!! Try using OPTION (HASH JOIN) or MERGE JOIN (prolly
> not right if not in sorted order already). See BOL for details.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:E77D9053-A6FE-4E46-9A2F-A42CA967B0B2@.microsoft.com...
> >I had checked the execution plan and didn't get any large numbers that
> >seemed
> > out of the ordinary. On the lower level the first step uses a clustered
> > index
> > and gives a row count 0f 504. The step on the level above it has another
> > clustered index and gives a row count of 24,000+. Then there is a nested
> > loop
> > and parallelism steps that give row counts of 11M but since I know that
> > it's
> > updating every row in the table I think that is to be expected. The left
> > most
> > icon shows 11M rows.
> >
> > Thanks,
> > --
> > Dan D.
> >
> >
> > "TheSQLGuru" wrote:
> >
> >> How about some nasty cartesian product? joining on a zip field (assuming
> >> zip code) would likely result in billions or possibly trillions of joins
> >> between the tables.
> >>
> >> Do this. Take the update statement and paste it into another QA window.
> >> Hit CTRL-L to have the Estimated Query Plan displayed. Place the cursor
> >> over the connecting lines in the plan and note the estimated row count.
> >> Also check the left most icon for total estimated rows and the estimated
> >> query cost. I think you may be surprised by what you see.
> >>
> >> Also, note that your join is not ANSI compliant and will not be supported
> >> by
> >> sql server at some point in the future. Start working to rewrite stuff
> >> like
> >> that into this format:
> >>
> >> UPDATE tblwf3
> >> SET BRANCH = a.branch_id
> >> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> >> WHERE a.branch_id < '2900'
> >> and a.Franchise is null
> >> and a.closed is null
> >> and a.commercial is null
> >> and a.fume is null
> >>
> >>
> >> --
> >> TheSQLGuru
> >> President
> >> Indicium Resources, Inc.
> >>
> >> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> >> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
> >> > Using SS2000 SP4. I have a query that has been running for 17 hours now
> >> > and
> >> > I'm wondering if it is doing anything. The transaction log for the
> >> > database
> >> > has the same time stamp on it that it had yesterday before I started
> >> > running
> >> > the query and the file size is the same. Shouldn't something have been
> >> > written to the transaction logs by now?
> >> >
> >> > There is nothing else running on the server of any consequence. There
> >> > is
> >> > disk activity (reads but no writes ) on the disk where the database is.
> >> > There
> >> > are no reads or writes on the disk where the transaction logs are
> >> > located.
> >> > The processor is at 2%. There is no blocking going on.
> >> >
> >> > Here's the query. I have indexes on all columns in the join and where
> >> > clause. There are about 11M records in the table. There is no problem
> >> > with
> >> > free disk space. The tempdb and logs also have a time stamp before I
> >> > started
> >> > the query.
> >> >
> >> > UPDATE tblwf3
> >> > SET BRANCH = a.branch_id
> >> > from dbo.tblBranchZip a, tblwf3 b
> >> > WHERE a.zip = b.zip5
> >> > and a.branch_id < '2900'
> >> > and a.Franchise is null
> >> > and a.closed is null
> >> > and a.commercial is null
> >> > and a.fume is null
> >> > --and b.branch is null
> >> >
> >> > go
> >> >
> >> > Any idea what might be going on? Thanks.
> >> > --
> >> > Dan D.
> >>
> >>
> >>
>
>|||I've tried your method on a couple of other queries and it is definitely
faster. How would you write the query if I wanted to update several rows. For
instance,
UPDATE tblwf3
SET BRANCH = a.branch_id, term=a.term, hhseg=a.hhseg
from dbo.tblBranchZip a inner join tblwf3 b
ON a.zip = b.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
Thanks,
--
Dan D.
"Gert-Jan Strik" wrote:
> As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> (s)he posted is actually standard.
> The version below is, and because it (also) eliminates the implicit
> cross join it should run in a reasonable amount of time.
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> --where b.branch is null
> HTH,
> Gert-Jan
> Dan D. wrote:
> >
> > Using SS2000 SP4. I have a query that has been running for 17 hours now and
> > I'm wondering if it is doing anything. The transaction log for the database
> > has the same time stamp on it that it had yesterday before I started running
> > the query and the file size is the same. Shouldn't something have been
> > written to the transaction logs by now?
> >
> > There is nothing else running on the server of any consequence. There is
> > disk activity (reads but no writes ) on the disk where the database is. There
> > are no reads or writes on the disk where the transaction logs are located.
> > The processor is at 2%. There is no blocking going on.
> >
> > Here's the query. I have indexes on all columns in the join and where
> > clause. There are about 11M records in the table. There is no problem with
> > free disk space. The tempdb and logs also have a time stamp before I started
> > the query.
> >
> > UPDATE tblwf3
> > SET BRANCH = a.branch_id
> > from dbo.tblBranchZip a, tblwf3 b
> > WHERE a.zip = b.zip5
> > and a.branch_id < '2900'
> > and a.Franchise is null
> > and a.closed is null
> > and a.commercial is null
> > and a.fume is null
> > --and b.branch is null
> >
> > go
> >
> > Any idea what might be going on? Thanks.
> > --
> > Dan D.
>|||The syntax that ANSI has for that is not supported by SQL Server. It
would be something along the lines of
UPDATE ...
SET (BRANCH, term, hhseg) = (
SELECT branch_id, term, hhseg
FROM ...
WHERE ...zip = ...zip5
)
So then you have to make a choice. If you feel strongly about using the
ANSI SQL standard syntax, you would get something like this:
UPDATE tblwf3
SET BRANCH = (
SELECT a.branch_id
from dbo.tblBranchZip a
WHERE a.zip = tblwf3.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
), term=(
SELECT a.term
from dbo.tblBranchZip a
WHERE a.zip = tblwf3.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
), hhseg=(
SELECT a.hhseg
from dbo.tblBranchZip a
WHERE a.zip = tblwf3.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
)
However, chances are that this syntax (above) will not run very fast,
because it it probably not optimized to do only one index seek/scan per
row in tblwf3.
So the other choice is to use the UPDATE ... FROM syntax as described
before. See untested version below:
UPDATE tblwf3
SET BRANCH = a.branch_id, term=a.term, hhseg=a.hhseg
from dbo.tblBranchZip a
WHERE a.zip = tblwf3.zip5
and a.branch_id < '2900'
and a.Franchise is null
and a.closed is null
and a.commercial is null
and a.fume is null
Note that this version is potentially different, because it will only
update rows in tblwf3 where there is a matching row in tblBranchZip. The
earlier syntax will update all rows in tblwf3, unless you add a WHERE
clause.
Gert-Jan
Dan D. wrote:
> I've tried your method on a couple of other queries and it is definitely
> faster. How would you write the query if I wanted to update several rows. For
> instance,
> UPDATE tblwf3
> SET BRANCH = a.branch_id, term=a.term, hhseg=a.hhseg
> from dbo.tblBranchZip a inner join tblwf3 b
> ON a.zip = b.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> Thanks,
> --
> Dan D.
> "Gert-Jan Strik" wrote:
> > As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> > (s)he posted is actually standard.
> >
> > The version below is, and because it (also) eliminates the implicit
> > cross join it should run in a reasonable amount of time.
> >
> > UPDATE tblwf3
> > SET BRANCH = (
> > SELECT a.branch_id
> > from dbo.tblBranchZip a
> > WHERE a.zip = tblwf3.zip5
> > and a.branch_id < '2900'
> > and a.Franchise is null
> > and a.closed is null
> > and a.commercial is null
> > and a.fume is null
> > )
> > --where b.branch is null
> >
> > HTH,
> > Gert-Jan
> >
> > Dan D. wrote:
> > >
> > > Using SS2000 SP4. I have a query that has been running for 17 hours now and
> > > I'm wondering if it is doing anything. The transaction log for the database
> > > has the same time stamp on it that it had yesterday before I started running
> > > the query and the file size is the same. Shouldn't something have been
> > > written to the transaction logs by now?
> > >
> > > There is nothing else running on the server of any consequence. There is
> > > disk activity (reads but no writes ) on the disk where the database is. There
> > > are no reads or writes on the disk where the transaction logs are located.
> > > The processor is at 2%. There is no blocking going on.
> > >
> > > Here's the query. I have indexes on all columns in the join and where
> > > clause. There are about 11M records in the table. There is no problem with
> > > free disk space. The tempdb and logs also have a time stamp before I started
> > > the query.
> > >
> > > UPDATE tblwf3
> > > SET BRANCH = a.branch_id
> > > from dbo.tblBranchZip a, tblwf3 b
> > > WHERE a.zip = b.zip5
> > > and a.branch_id < '2900'
> > > and a.Franchise is null
> > > and a.closed is null
> > > and a.commercial is null
> > > and a.fume is null
> > > --and b.branch is null
> > >
> > > go
> > >
> > > Any idea what might be going on? Thanks.
> > > --
> > > Dan D.
> >|||Nice knowing you Kevin.
Usually, when people talk about "standard" SQL, they refer to queries
that conform to the ANSI SQL-92 standard, or ANSI SQL-99. AFAIK, those
standards to not support UPDATE ... FROM.
Gert-Jan
TheSQLGuru wrote:
> 1) From SQL 2005 BOL,
> ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/40e63302-0c68-4593-af3e-6d190181fee7.htm,
> about 3/4 down the page, you will find this example:
> USE AdventureWorks;
> GO
> UPDATE Sales.SalesPerson
> SET SalesYTD = SalesYTD + SubTotal
> FROM Sales.SalesPerson AS sp
> JOIN Sales.SalesOrderHeader AS so
> ON sp.SalesPersonID = so.SalesPersonID
> AND so.OrderDate = (SELECT MAX(OrderDate)
> FROM Sales.SalesOrderHeader
> WHERE SalesPersonID => sp.SalesPersonID);
> GO2) "As usual, TheSQLGuru is spot on..." - thanks very much for the
> compliment!!
> 3) "(s)he" - it is he actually. Kevin. Glad to meet you! :-)
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
> news:46324FBF.204D08B8@.toomuchspamalready.nl...
> > As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> > (s)he posted is actually standard.
> >
> > The version below is, and because it (also) eliminates the implicit
> > cross join it should run in a reasonable amount of time.
> >
> > UPDATE tblwf3
> > SET BRANCH = (
> > SELECT a.branch_id
> > from dbo.tblBranchZip a
> > WHERE a.zip = tblwf3.zip5
> > and a.branch_id < '2900'
> > and a.Franchise is null
> > and a.closed is null
> > and a.commercial is null
> > and a.fume is null
> > )
> > --where b.branch is null
> >
> > HTH,
> > Gert-Jan
> >
> > Dan D. wrote:
> >>
> >> Using SS2000 SP4. I have a query that has been running for 17 hours now
> >> and
> >> I'm wondering if it is doing anything. The transaction log for the
> >> database
> >> has the same time stamp on it that it had yesterday before I started
> >> running
> >> the query and the file size is the same. Shouldn't something have been
> >> written to the transaction logs by now?
> >>
> >> There is nothing else running on the server of any consequence. There is
> >> disk activity (reads but no writes ) on the disk where the database is.
> >> There
> >> are no reads or writes on the disk where the transaction logs are
> >> located.
> >> The processor is at 2%. There is no blocking going on.
> >>
> >> Here's the query. I have indexes on all columns in the join and where
> >> clause. There are about 11M records in the table. There is no problem
> >> with
> >> free disk space. The tempdb and logs also have a time stamp before I
> >> started
> >> the query.
> >>
> >> UPDATE tblwf3
> >> SET BRANCH = a.branch_id
> >> from dbo.tblBranchZip a, tblwf3 b
> >> WHERE a.zip = b.zip5
> >> and a.branch_id < '2900'
> >> and a.Franchise is null
> >> and a.closed is null
> >> and a.commercial is null
> >> and a.fume is null
> >> --and b.branch is null
> >>
> >> go
> >>
> >> Any idea what might be going on? Thanks.
> >> --
> >> Dan D.|||You can tell by doing an aggregate on zip on each table. Any value for zip
that comes up with more than one row on each table will be doing multiple
rows.
select zip, count(*) from tblBranchZip (nolock) group by zip
select zip5, count(*) from tblwf3 (nolock) group by zip5
Even worse is when you have multiple entries for a given zip in BOTH tables
say tblBranchZip has 147 rows with zip 30338 and tblwf3 has 9137 rows with
zip 30338. When you join on these and do the update as you had it, you will
have 147*9137 join hits. That means more than 1.3MILLION updates for a
SINGLE zip value.
--
TheSQLGuru
President
Indicium Resources, Inc.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:1DD9F77F-9AD2-4729-8149-FA5D694F4FB6@.microsoft.com...
> How can I tell how many times I'm updating the rows? The cost for the
> nested
> loop is 2%. And if I write the query as you suggested the execution plan
> is
> exactly the same.
> I tried the way Gert-Jan suggested and I get a much different execution
> plan
> and the query runs in a little over 2 minutes.
> Thanks,
> --
> Dan D.
>
> "TheSQLGuru" wrote:
>> I don't doubt that you are updating 11M rows. The question is how many
>> TIMES are you updating those 11M rows. 24000+ times perhaps? Not sure
>> since I don't have access to the data.
>> However, I can state with almost 100% certainty that this update should
>> NOT
>> be using a nested loop!! Try using OPTION (HASH JOIN) or MERGE JOIN
>> (prolly
>> not right if not in sorted order already). See BOL for details.
>> --
>> TheSQLGuru
>> President
>> Indicium Resources, Inc.
>> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
>> news:E77D9053-A6FE-4E46-9A2F-A42CA967B0B2@.microsoft.com...
>> >I had checked the execution plan and didn't get any large numbers that
>> >seemed
>> > out of the ordinary. On the lower level the first step uses a clustered
>> > index
>> > and gives a row count 0f 504. The step on the level above it has
>> > another
>> > clustered index and gives a row count of 24,000+. Then there is a
>> > nested
>> > loop
>> > and parallelism steps that give row counts of 11M but since I know that
>> > it's
>> > updating every row in the table I think that is to be expected. The
>> > left
>> > most
>> > icon shows 11M rows.
>> >
>> > Thanks,
>> > --
>> > Dan D.
>> >
>> >
>> > "TheSQLGuru" wrote:
>> >
>> >> How about some nasty cartesian product? joining on a zip field
>> >> (assuming
>> >> zip code) would likely result in billions or possibly trillions of
>> >> joins
>> >> between the tables.
>> >>
>> >> Do this. Take the update statement and paste it into another QA
>> >> window.
>> >> Hit CTRL-L to have the Estimated Query Plan displayed. Place the
>> >> cursor
>> >> over the connecting lines in the plan and note the estimated row
>> >> count.
>> >> Also check the left most icon for total estimated rows and the
>> >> estimated
>> >> query cost. I think you may be surprised by what you see.
>> >>
>> >> Also, note that your join is not ANSI compliant and will not be
>> >> supported
>> >> by
>> >> sql server at some point in the future. Start working to rewrite
>> >> stuff
>> >> like
>> >> that into this format:
>> >>
>> >> UPDATE tblwf3
>> >> SET BRANCH = a.branch_id
>> >> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
>> >> WHERE a.branch_id < '2900'
>> >> and a.Franchise is null
>> >> and a.closed is null
>> >> and a.commercial is null
>> >> and a.fume is null
>> >>
>> >>
>> >> --
>> >> TheSQLGuru
>> >> President
>> >> Indicium Resources, Inc.
>> >>
>> >> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
>> >> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
>> >> > Using SS2000 SP4. I have a query that has been running for 17 hours
>> >> > now
>> >> > and
>> >> > I'm wondering if it is doing anything. The transaction log for the
>> >> > database
>> >> > has the same time stamp on it that it had yesterday before I started
>> >> > running
>> >> > the query and the file size is the same. Shouldn't something have
>> >> > been
>> >> > written to the transaction logs by now?
>> >> >
>> >> > There is nothing else running on the server of any consequence.
>> >> > There
>> >> > is
>> >> > disk activity (reads but no writes ) on the disk where the database
>> >> > is.
>> >> > There
>> >> > are no reads or writes on the disk where the transaction logs are
>> >> > located.
>> >> > The processor is at 2%. There is no blocking going on.
>> >> >
>> >> > Here's the query. I have indexes on all columns in the join and
>> >> > where
>> >> > clause. There are about 11M records in the table. There is no
>> >> > problem
>> >> > with
>> >> > free disk space. The tempdb and logs also have a time stamp before I
>> >> > started
>> >> > the query.
>> >> >
>> >> > UPDATE tblwf3
>> >> > SET BRANCH = a.branch_id
>> >> > from dbo.tblBranchZip a, tblwf3 b
>> >> > WHERE a.zip = b.zip5
>> >> > and a.branch_id < '2900'
>> >> > and a.Franchise is null
>> >> > and a.closed is null
>> >> > and a.commercial is null
>> >> > and a.fume is null
>> >> > --and b.branch is null
>> >> >
>> >> > go
>> >> >
>> >> > Any idea what might be going on? Thanks.
>> >> > --
>> >> > Dan D.
>> >>
>> >>
>> >>
>>|||It is certainly the case that there are multiple zips in both tables. But
since the cost of the nested loop was only 2% wouldn't you think that it
isn't having much impact on the overall process?
The execution plan for the query of Gert-Jan didn't have the nested loop. In
this case after several tests, that query seems to be the one to use.
Thanks,
--
Dan D.
"TheSQLGuru" wrote:
> You can tell by doing an aggregate on zip on each table. Any value for zip
> that comes up with more than one row on each table will be doing multiple
> rows.
> select zip, count(*) from tblBranchZip (nolock) group by zip
> select zip5, count(*) from tblwf3 (nolock) group by zip5
> Even worse is when you have multiple entries for a given zip in BOTH tables
> say tblBranchZip has 147 rows with zip 30338 and tblwf3 has 9137 rows with
> zip 30338. When you join on these and do the update as you had it, you will
> have 147*9137 join hits. That means more than 1.3MILLION updates for a
> SINGLE zip value.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:1DD9F77F-9AD2-4729-8149-FA5D694F4FB6@.microsoft.com...
> > How can I tell how many times I'm updating the rows? The cost for the
> > nested
> > loop is 2%. And if I write the query as you suggested the execution plan
> > is
> > exactly the same.
> >
> > I tried the way Gert-Jan suggested and I get a much different execution
> > plan
> > and the query runs in a little over 2 minutes.
> >
> > Thanks,
> > --
> > Dan D.
> >
> >
> > "TheSQLGuru" wrote:
> >
> >> I don't doubt that you are updating 11M rows. The question is how many
> >> TIMES are you updating those 11M rows. 24000+ times perhaps? Not sure
> >> since I don't have access to the data.
> >>
> >> However, I can state with almost 100% certainty that this update should
> >> NOT
> >> be using a nested loop!! Try using OPTION (HASH JOIN) or MERGE JOIN
> >> (prolly
> >> not right if not in sorted order already). See BOL for details.
> >>
> >> --
> >> TheSQLGuru
> >> President
> >> Indicium Resources, Inc.
> >>
> >> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> >> news:E77D9053-A6FE-4E46-9A2F-A42CA967B0B2@.microsoft.com...
> >> >I had checked the execution plan and didn't get any large numbers that
> >> >seemed
> >> > out of the ordinary. On the lower level the first step uses a clustered
> >> > index
> >> > and gives a row count 0f 504. The step on the level above it has
> >> > another
> >> > clustered index and gives a row count of 24,000+. Then there is a
> >> > nested
> >> > loop
> >> > and parallelism steps that give row counts of 11M but since I know that
> >> > it's
> >> > updating every row in the table I think that is to be expected. The
> >> > left
> >> > most
> >> > icon shows 11M rows.
> >> >
> >> > Thanks,
> >> > --
> >> > Dan D.
> >> >
> >> >
> >> > "TheSQLGuru" wrote:
> >> >
> >> >> How about some nasty cartesian product? joining on a zip field
> >> >> (assuming
> >> >> zip code) would likely result in billions or possibly trillions of
> >> >> joins
> >> >> between the tables.
> >> >>
> >> >> Do this. Take the update statement and paste it into another QA
> >> >> window.
> >> >> Hit CTRL-L to have the Estimated Query Plan displayed. Place the
> >> >> cursor
> >> >> over the connecting lines in the plan and note the estimated row
> >> >> count.
> >> >> Also check the left most icon for total estimated rows and the
> >> >> estimated
> >> >> query cost. I think you may be surprised by what you see.
> >> >>
> >> >> Also, note that your join is not ANSI compliant and will not be
> >> >> supported
> >> >> by
> >> >> sql server at some point in the future. Start working to rewrite
> >> >> stuff
> >> >> like
> >> >> that into this format:
> >> >>
> >> >> UPDATE tblwf3
> >> >> SET BRANCH = a.branch_id
> >> >> from dbo.tblBranchZip a inner join tblwf3 b ON a.zip = b.zip5
> >> >> WHERE a.branch_id < '2900'
> >> >> and a.Franchise is null
> >> >> and a.closed is null
> >> >> and a.commercial is null
> >> >> and a.fume is null
> >> >>
> >> >>
> >> >> --
> >> >> TheSQLGuru
> >> >> President
> >> >> Indicium Resources, Inc.
> >> >>
> >> >> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> >> >> news:EAFCE68D-33C7-48BC-A16A-AA42821FBB17@.microsoft.com...
> >> >> > Using SS2000 SP4. I have a query that has been running for 17 hours
> >> >> > now
> >> >> > and
> >> >> > I'm wondering if it is doing anything. The transaction log for the
> >> >> > database
> >> >> > has the same time stamp on it that it had yesterday before I started
> >> >> > running
> >> >> > the query and the file size is the same. Shouldn't something have
> >> >> > been
> >> >> > written to the transaction logs by now?
> >> >> >
> >> >> > There is nothing else running on the server of any consequence.
> >> >> > There
> >> >> > is
> >> >> > disk activity (reads but no writes ) on the disk where the database
> >> >> > is.
> >> >> > There
> >> >> > are no reads or writes on the disk where the transaction logs are
> >> >> > located.
> >> >> > The processor is at 2%. There is no blocking going on.
> >> >> >
> >> >> > Here's the query. I have indexes on all columns in the join and
> >> >> > where
> >> >> > clause. There are about 11M records in the table. There is no
> >> >> > problem
> >> >> > with
> >> >> > free disk space. The tempdb and logs also have a time stamp before I
> >> >> > started
> >> >> > the query.
> >> >> >
> >> >> > UPDATE tblwf3
> >> >> > SET BRANCH = a.branch_id
> >> >> > from dbo.tblBranchZip a, tblwf3 b
> >> >> > WHERE a.zip = b.zip5
> >> >> > and a.branch_id < '2900'
> >> >> > and a.Franchise is null
> >> >> > and a.closed is null
> >> >> > and a.commercial is null
> >> >> > and a.fume is null
> >> >> > --and b.branch is null
> >> >> >
> >> >> > go
> >> >> >
> >> >> > Any idea what might be going on? Thanks.
> >> >> > --
> >> >> > Dan D.
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>|||When you say that "this version is potentially different, because it will only
update rows in tblwf3 where there is a matching row in tblBranchZip. The
earlier syntax will update all rows in tblwf3, unless you add a WHERE
clause", I don't understand because each query has a join of "a.zip =tblwf3.zip5". Could you explain that a little more?
Thanks,
--
Dan D.
"Gert-Jan Strik" wrote:
> The syntax that ANSI has for that is not supported by SQL Server. It
> would be something along the lines of
> UPDATE ...
> SET (BRANCH, term, hhseg) = (
> SELECT branch_id, term, hhseg
> FROM ...
> WHERE ...zip = ...zip5
> )
> So then you have to make a choice. If you feel strongly about using the
> ANSI SQL standard syntax, you would get something like this:
> UPDATE tblwf3
> SET BRANCH = (
> SELECT a.branch_id
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> ), term=(
> SELECT a.term
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> ), hhseg=(
> SELECT a.hhseg
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> )
> However, chances are that this syntax (above) will not run very fast,
> because it it probably not optimized to do only one index seek/scan per
> row in tblwf3.
> So the other choice is to use the UPDATE ... FROM syntax as described
> before. See untested version below:
> UPDATE tblwf3
> SET BRANCH = a.branch_id, term=a.term, hhseg=a.hhseg
> from dbo.tblBranchZip a
> WHERE a.zip = tblwf3.zip5
> and a.branch_id < '2900'
> and a.Franchise is null
> and a.closed is null
> and a.commercial is null
> and a.fume is null
> Note that this version is potentially different, because it will only
> update rows in tblwf3 where there is a matching row in tblBranchZip. The
> earlier syntax will update all rows in tblwf3, unless you add a WHERE
> clause.
> Gert-Jan
>
> Dan D. wrote:
> >
> > I've tried your method on a couple of other queries and it is definitely
> > faster. How would you write the query if I wanted to update several rows. For
> > instance,
> > UPDATE tblwf3
> > SET BRANCH = a.branch_id, term=a.term, hhseg=a.hhseg
> > from dbo.tblBranchZip a inner join tblwf3 b
> > ON a.zip = b.zip5
> > and a.branch_id < '2900'
> > and a.Franchise is null
> > and a.closed is null
> > and a.commercial is null
> > and a.fume is null
> >
> > Thanks,
> >
> > --
> > Dan D.
> >
> > "Gert-Jan Strik" wrote:
> >
> > > As usual, TheSQLGuru is spot on, although I am not sure whether the SQL
> > > (s)he posted is actually standard.
> > >
> > > The version below is, and because it (also) eliminates the implicit
> > > cross join it should run in a reasonable amount of time.
> > >
> > > UPDATE tblwf3
> > > SET BRANCH = (
> > > SELECT a.branch_id
> > > from dbo.tblBranchZip a
> > > WHERE a.zip = tblwf3.zip5
> > > and a.branch_id < '2900'
> > > and a.Franchise is null
> > > and a.closed is null
> > > and a.commercial is null
> > > and a.fume is null
> > > )
> > > --where b.branch is null
> > >
> > > HTH,
> > > Gert-Jan
> > >
> > > Dan D. wrote:
> > > >
> > > > Using SS2000 SP4. I have a query that has been running for 17 hours now and
> > > > I'm wondering if it is doing anything. The transaction log for the database
> > > > has the same time stamp on it that it had yesterday before I started running
> > > > the query and the file size is the same. Shouldn't something have been
> > > > written to the transaction logs by now?
> > > >
> > > > There is nothing else running on the server of any consequence. There is
> > > > disk activity (reads but no writes ) on the disk where the database is. There
> > > > are no reads or writes on the disk where the transaction logs are located.
> > > > The processor is at 2%. There is no blocking going on.
> > > >
> > > > Here's the query. I have indexes on all columns in the join and where
> > > > clause. There are about 11M records in the table. There is no problem with
> > > > free disk space. The tempdb and logs also have a time stamp before I started
> > > > the query.
> > > >
> > > > UPDATE tblwf3
> > > > SET BRANCH = a.branch_id
> > > > from dbo.tblBranchZip a, tblwf3 b
> > > > WHERE a.zip = b.zip5
> > > > and a.branch_id < '2900'
> > > > and a.Franchise is null
> > > > and a.closed is null
> > > > and a.commercial is null
> > > > and a.fume is null
> > > > --and b.branch is null
> > > >
> > > > go
> > > >
> > > > Any idea what might be going on? Thanks.
> > > > --
> > > > Dan D.
> > >
>|||Dan,
When you use the UPDATE ... FROM syntax, what the engine basically does
is execute the query as specified with the FROM clause and predicates in
the WHERE clause and create a resultset. Only matching rows of this
resultset to the UPDATE table are updated.
So in your particular case, if there is no matching row for a zip5 in
tblwf3, then this row in tblwf3 will not be updated if you use the
UPDATE ... FROM syntax.
What TheSQLGuru was suggesting, was that you have even more serious
problems, because he suspects that you might have several rows for one
zip in tblBranchZip. And you basically said that that was true. This
fact will have its effect on the way the UPDATE statement is executed.
Let's say there is a row in tblwf3 with zip5 of '90210', and let's
assume that you have 5 rows for this zip in tblBranchZip, like this:
zip | branch_id
--+--
90210 | 1200
90210 | 1201
90210 | 2000
90210 | 2001
90210 | 2002
If you use the UPDATE ... FROM syntax, SQL Server will try to update the
row in tblwf3 for zip5 90210 at least 5 times. I am saying at least
here, because it depends on the number of times tblwf3 has a zip5 of
90210. If there are two rows in tblwf3 with a zip5 of 90210, then each
row will be updated 10 times.
If you use the UPDATE with a correlated query, then the correlated query
should return one scalar for each row in tblwf3. So if there are two
rows in tblwf3 with a zip5 of 90210, then for each of these 2 rows SQL
Server will find the branch_id and will update each row only once.
But your real problem is not that of performance. It is that of
function. Because look at the example above. Which branch_id should SQL
Server pick for zip 90210? There shouldn't be 5 possible answers. There
should be one (or zero) answer. That is why TheSQLGuru posted the
example using the MAX aggregate.
My advice would be: first figure out how to write the correct query
without the UPDATE, and make sure that there will only be one row for
each zip code. When that is all working well, then change it to an
UPDATE query.
Gert-Jan
Dan D. wrote:
> When you say that "this version is potentially different, because it will only
> update rows in tblwf3 where there is a matching row in tblBranchZip. The
> earlier syntax will update all rows in tblwf3, unless you add a WHERE
> clause", I don't understand because each query has a join of "a.zip => tblwf3.zip5". Could you explain that a little more?
> Thanks,
> --
> Dan D.
[snip]|||Gotcha on the "standard" thing. I figured since this was a Microsoft SQL
Server-only forum (and also since I only work with clients that are SQL
Server only) we were talking about Transact SQL as the "standard". :-D
--
TheSQLGuru
President
Indicium Resources, Inc.
"Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
news:463388E3.EDE740BE@.toomuchspamalready.nl...
> Nice knowing you Kevin.
> Usually, when people talk about "standard" SQL, they refer to queries
> that conform to the ANSI SQL-92 standard, or ANSI SQL-99. AFAIK, those
> standards to not support UPDATE ... FROM.
> Gert-Jan|||Sorry I didn't have a chance to get back to this sooner. I appreciate your
help. The object of this is to match potential customers (in tblwf3) with
company branches (tblBranchZip). There can be more than one branch that
covers a particular zip code. It is usually only two branches at most but
sometimes more.
There is no data (like using zip+4) that will allow us to match a particular
customer better to a branch on the first pass. So what we've done in the past
is do a rough match like the one I'm asking about and then use other data in
the file like demographic data to more evenly distribute the customers
between two or more branches.
I'm still trying to understand the technical part of how each of these two
queries are processed. In the update ... from in your example, I'm not sure I
understand why a row could be processed 10 times. I thought that there would
be a result set of all of the rows in tblwf3 and that the program would loop
through that result set and for each row it would match to the tblbranchzip
table based on the join tblwf3.zip5=tblbranchzip.zip. In your example if
there are 5 rows in tblbranchzip for a particular zip code wouldn't it only
process 5 times rather than 10?
In the second example of your code with the correlated query, I don't
understand why it wouldn't process the same way - for each row of the tblwf3
result set, it joins to the subquery using the zip for the particular row of
tblwf3 that it's on and find a branch for that zip code. I know that must not
be how it works because if it was the subquery could return 2 rows or more
for some zip and the subquery would fail. I can also tell from the execution
plans that the two queries work differently (the first one has a nested loop
but not the second one) but I'm still not clear why. How does the subquery
not return more than row for some zips since there are two or more branches
in some zips?
I have what I think is a very good book on SS2005 T-SQL Querying by Ben-Gan
that I'm reading to help understand this better. We process mailfiles and the
faster and more accurate we can make the process, the better.
Thanks again,
--
Dan D.
"Gert-Jan Strik" wrote:
> Dan,
> When you use the UPDATE ... FROM syntax, what the engine basically does
> is execute the query as specified with the FROM clause and predicates in
> the WHERE clause and create a resultset. Only matching rows of this
> resultset to the UPDATE table are updated.
> So in your particular case, if there is no matching row for a zip5 in
> tblwf3, then this row in tblwf3 will not be updated if you use the
> UPDATE ... FROM syntax.
>
> What TheSQLGuru was suggesting, was that you have even more serious
> problems, because he suspects that you might have several rows for one
> zip in tblBranchZip. And you basically said that that was true. This
> fact will have its effect on the way the UPDATE statement is executed.
> Let's say there is a row in tblwf3 with zip5 of '90210', and let's
> assume that you have 5 rows for this zip in tblBranchZip, like this:
> zip | branch_id
> --+--
> 90210 | 1200
> 90210 | 1201
> 90210 | 2000
> 90210 | 2001
> 90210 | 2002
> If you use the UPDATE ... FROM syntax, SQL Server will try to update the
> row in tblwf3 for zip5 90210 at least 5 times. I am saying at least
> here, because it depends on the number of times tblwf3 has a zip5 of
> 90210. If there are two rows in tblwf3 with a zip5 of 90210, then each
> row will be updated 10 times.
> If you use the UPDATE with a correlated query, then the correlated query
> should return one scalar for each row in tblwf3. So if there are two
> rows in tblwf3 with a zip5 of 90210, then for each of these 2 rows SQL
> Server will find the branch_id and will update each row only once.
>
> But your real problem is not that of performance. It is that of
> function. Because look at the example above. Which branch_id should SQL
> Server pick for zip 90210? There shouldn't be 5 possible answers. There
> should be one (or zero) answer. That is why TheSQLGuru posted the
> example using the MAX aggregate.
> My advice would be: first figure out how to write the correct query
> without the UPDATE, and make sure that there will only be one row for
> each zip code. When that is all working well, then change it to an
> UPDATE query.
> Gert-Jan
>
> Dan D. wrote:
> >
> > When you say that "this version is potentially different, because it will only
> > update rows in tblwf3 where there is a matching row in tblBranchZip. The
> > earlier syntax will update all rows in tblwf3, unless you add a WHERE
> > clause", I don't understand because each query has a join of "a.zip => > tblwf3.zip5". Could you explain that a little more?
> >
> > Thanks,
> >
> > --
> > Dan D.
> [snip]
>|||Dan D. wrote:
[snip]
> I'm still trying to understand the technical part of how each of these two
> queries are processed. In the update ... from in your example, I'm not sure I
> understand why a row could be processed 10 times. I thought that there would
> be a result set of all of the rows in tblwf3 and that the program would loop
> through that result set and for each row it would match to the tblbranchzip
> table based on the join tblwf3.zip5=tblbranchzip.zip. In your example if
> there are 5 rows in tblbranchzip for a particular zip code wouldn't it only
> process 5 times rather than 10?
It all depends how the optimizer happens to generate the query plan. But
you think of it like this: the query will be performed first. A join
between
zip5
--
90210
90210
and
zip | branch_id
--+--
90210 | 1200
90210 | 1201
90210 | 2000
will result in 6 rows, which all happen to have the same zip code. Then,
this resultset is processed. For each row in the resultset, the UPDATE
table is looked up and updated. So it would do 6 scans and therefore
update both rows 6 times.
Please note that this is not necessarily how the UPDATE ... FROM query
is executed, but sometimes it is. You would have to check your query
plan to see what happens in your case.
> In the second example of your code with the correlated query, I don't
> understand why it wouldn't process the same way - for each row of the tblwf3
> result set, it joins to the subquery using the zip for the particular row of
> tblwf3 that it's on and find a branch for that zip code. I know that must not
> be how it works because if it was the subquery could return 2 rows or more
> for some zip and the subquery would fail.
Yes, in that case it should fail.
> I can also tell from the execution
> plans that the two queries work differently (the first one has a nested loop
> but not the second one) but I'm still not clear why.
The optimizer does not need a reason to execute two logically equivalent
queries with two different query plans. However, in this case I would
say that the query plan must be different, since the two queries are
logically not equivalent. One query updates all rows, the other query
updates at least 0 and at most all rows.
> How does the subquery not return more than row for some zips since there are two or more branches
> in some zips?
I don't know. You have the query and the data and therefore the means to
check it out. I don't even have the query plan(s).
[snip]
> > My advice would be: first figure out how to write the correct query
> > without the UPDATE, and make sure that there will only be one row for
> > each zip code. When that is all working well, then change it to an
> > UPDATE query.
Gert-Jan|||I now understand the difference now of why the update..from processes so many
more rows than the correlated subquery. I still don't know why the subquery
doesn't give an error because in some cases it should return more than one
row but I'll work on that.
Thanks for your patience and help Gert-Jan.
--
Dan D.
"Gert-Jan Strik" wrote:
> Dan D. wrote:
> [snip]
> > I'm still trying to understand the technical part of how each of these two
> > queries are processed. In the update ... from in your example, I'm not sure I
> > understand why a row could be processed 10 times. I thought that there would
> > be a result set of all of the rows in tblwf3 and that the program would loop
> > through that result set and for each row it would match to the tblbranchzip
> > table based on the join tblwf3.zip5=tblbranchzip.zip. In your example if
> > there are 5 rows in tblbranchzip for a particular zip code wouldn't it only
> > process 5 times rather than 10?
> It all depends how the optimizer happens to generate the query plan. But
> you think of it like this: the query will be performed first. A join
> between
> zip5
> --
> 90210
> 90210
> and
> zip | branch_id
> --+--
> 90210 | 1200
> 90210 | 1201
> 90210 | 2000
> will result in 6 rows, which all happen to have the same zip code. Then,
> this resultset is processed. For each row in the resultset, the UPDATE
> table is looked up and updated. So it would do 6 scans and therefore
> update both rows 6 times.
> Please note that this is not necessarily how the UPDATE ... FROM query
> is executed, but sometimes it is. You would have to check your query
> plan to see what happens in your case.
> > In the second example of your code with the correlated query, I don't
> > understand why it wouldn't process the same way - for each row of the tblwf3
> > result set, it joins to the subquery using the zip for the particular row of
> > tblwf3 that it's on and find a branch for that zip code. I know that must not
> > be how it works because if it was the subquery could return 2 rows or more
> > for some zip and the subquery would fail.
> Yes, in that case it should fail.
> > I can also tell from the execution
> > plans that the two queries work differently (the first one has a nested loop
> > but not the second one) but I'm still not clear why.
> The optimizer does not need a reason to execute two logically equivalent
> queries with two different query plans. However, in this case I would
> say that the query plan must be different, since the two queries are
> logically not equivalent. One query updates all rows, the other query
> updates at least 0 and at most all rows.
> > How does the subquery not return more than row for some zips since there are two or more branches
> > in some zips?
> I don't know. You have the query and the data and therefore the means to
> check it out. I don't even have the query plan(s).
> [snip]
> > > My advice would be: first figure out how to write the correct query
> > > without the UPDATE, and make sure that there will only be one row for
> > > each zip code. When that is all working well, then change it to an
> > > UPDATE query.
> Gert-Jan
>