473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Slow Query with Date

Hello Everybody

I run the following query against to identical databases. Execution
time on the first DB is 0 seconds, on the other 6 seconds!

SELECT dbo.HRMABZ.EMPKEY , dbo.HRMABZ.CONNUMB
, dbo.HRM_CALENDER.Datum, dbo.HRMABZ.ABZTXT

FROM dbo.HRM_CALENDER INNER JOIN dbo.HRMABZ
ON dbo.HRM_CALENDER.Datum >= dbo.HRMABZ.ABZDATF
AND dbo.HRM_CALENDER.Datum <= dbo.HRMABZ.ABZDATT

WHERE (dbo.HRMABZ.ABZTECH = 'N')
AND (dbo.HRMABZ.ABZLDLT = 'N')
AND (dbo.HRM_CALENDER.Valid = 1)

I tried to analyze to query execution in the execution plan and found
two different arguments in a 'nested loop / inner join' which I dont
understand. The nested loop does consume most of the time:

On the fast Server the argument says:
'Outer References:([HRMABZ].[ABZDATT],[HRMABZ].[ABZDATF])'

On the slow server the argument says:
'WHERE: ([HRM_Calender].[Datum]>=[HRMABZ].[ABZDATF] AND
[HRM_Calender].[]<=[HRMABZ].[ABZDATT])'

Additional information for the two databases:
- Slow database runs on a SQL2000 SP3a English, W2K SP3
- Fast database runs on a SQL2000 SP3a German, W2k SP3

Can somebody explain me the difference between the two execution plans
and how I could force the slow database acting the same way as the
fast one does?

Thank you
Thomi
Jul 23 '05 #1
7 3606
Thomi Baechler (th***************@azul.ch) writes:
I run the following query against to identical databases. Execution
time on the first DB is 0 seconds, on the other 6 seconds!
So what you mean with identical? Do they have exactly the same data? Or
do they have the same schemas but different data?
SELECT dbo.HRMABZ.EMPKEY , dbo.HRMABZ.CONNUMB
, dbo.HRM_CALENDER.Datum, dbo.HRMABZ.ABZTXT

FROM dbo.HRM_CALENDER INNER JOIN dbo.HRMABZ
ON dbo.HRM_CALENDER.Datum >= dbo.HRMABZ.ABZDATF
AND dbo.HRM_CALENDER.Datum <= dbo.HRMABZ.ABZDATT

WHERE (dbo.HRMABZ.ABZTECH = 'N')
AND (dbo.HRMABZ.ABZLDLT = 'N')
AND (dbo.HRM_CALENDER.Valid = 1)

I tried to analyze to query execution in the execution plan and found
two different arguments in a 'nested loop / inner join' which I dont
understand. The nested loop does consume most of the time:

On the fast Server the argument says:
'Outer References:([HRMABZ].[ABZDATT],[HRMABZ].[ABZDATF])'

On the slow server the argument says:
'WHERE: ([HRM_Calender].[Datum]>=[HRMABZ].[ABZDATF] AND
[HRM_Calender].[]<=[HRMABZ].[ABZDATT])'


Well, the quey is a join between two tables, and SQL Server could
start with any of the two using the filters in the WHERE clause
for an initial filter, and then look up a row in the other table.
In the two different query plans, is the same table being subject
to a clustered index scan?

Could you post the complete query plans?
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Thomi Baechler (th***************@azul.ch) writes:
I tried to analyze to query execution in the execution plan and found
two different arguments in a 'nested loop / inner join' which I dont
understand. The nested loop does consume most of the time:

On the fast Server the argument says:
'Outer References:([HRMABZ].[ABZDATT],[HRMABZ].[ABZDATF])'

On the slow server the argument says:
'WHERE: ([HRM_Calender].[Datum]>=[HRMABZ].[ABZDATF] AND
[HRM_Calender].[]<=[HRMABZ].[ABZDATT])'


Also, information about the number of rows in each table would help.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3
Hi Erland

Thank you for your fast answer!

Identical means: Full backup of the database on our clients server and
restore on our testserver -> exact the same data in both databases

Complete query plans: sure, I can post the complete plan. I am an absolute
newbie. Can your tell me how to do that?

Number of rows: HRMABZ has about 4000 rows and HRM_CALENDER about 3800 rows

As far as I know is the only differnce between the two DBs/Servers the
language. I have to join dates (datetime) and my guess would be that the
english server has to do a language conversion of the date which leads to .
Could that be possible?

Thank you very much!
Thomi


So what you mean with identical? Do they have exactly the same data? Or
do they have the same schemas but different data?

Well, the quey is a join between two tables, and SQL Server could
start with any of the two using the filters in the WHERE clause
for an initial filter, and then look up a row in the other table.
In the two different query plans, is the same table being subject
to a clustered index scan?

Could you post the complete query plans?

Jul 23 '05 #4
Thomi Bächler (th***************@azul.ch) writes:
Identical means: Full backup of the database on our clients server and
restore on our testserver -> exact the same data in both databases
Hm, I wonder if the statistics are the same? Run an UPDATE STATISTICS
WITH FULLSCAN on bnth tables on both servers.
Complete query plans: sure, I can post the complete plan. I am an absolute
newbie. Can your tell me how to do that?
For a simple query, you issue this command before you issue the query:

SET SHOWPLAN_TEXT ON

This command is also an implicit SET NOEXEC ON, so the query will not be
executed.

You can also use SET STATISTICS_PROFILE ON. In this case the query will
be executed.
Number of rows: HRMABZ has about 4000 rows and HRM_CALENDER about 3800
rows
Ah, that appears it would make it a toss up which plan to use.
As far as I know is the only differnce between the two DBs/Servers the
language. I have to join dates (datetime) and my guess would be that the
english server has to do a language conversion of the date which leads
to . Could that be possible?


Not very likely. If all date columns are datetime, there is no conversion
to make, since datetime is a binary value. If some column is varchar, there
is always a conversion, no matter the language of the server. And what
matters more than the server language is the collations, but they are
obviously the same, as you copied the database with BACKUP/RESTORE.

But the hardware configuration of the machines could matter, for instance
the number of available processors.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5
Hello

I run the UPDATE...STATISTICS on both tables, on both servers. Nothing
changed!

Execution plan is as follows:

Fast Server (german, SQL2000 SP3a, single Pentium, 768MB RAM)
-------------------------------------------------------------
|--Nested Loops(Inner Join, OUTER REFERENCES:([HRMABZ].[ABZDATT],
[HRMABZ].[ABZDATF]))
|--Sort(ORDER BY:([HRMABZ].[ABZDATT] ASC, [HRMABZ].[ABZDATF] ASC))
| |--Clustered Index
Scan(OBJECT:([XHRMDTA036].[dbo].[HRMABZ].[PK__HRMABZ__52442E1F]),
WHERE:([HRMABZ].[ABZTECH]='N' AND [HRMABZ].[ABZLDLT]='N'))
|--Table Spool
|--Index Spool(SEEK:([HRM_CALENDER].[Datum] >=
[HRMABZ].[ABZDATF] AND [HRM_CALENDER].[Datum] <= [HRMABZ].[ABZDATT]))
|--Table Scan(OBJECT:([XHRMDTA036].[dbo].[HRM_CALENDER]),
WHERE:([HRM_CALENDER].[Valid]=1))
Slow Server (english, SQL2000 SP3a, dual Pentium, 1G RAM)
---------------------------------------------------------
|--Parallelism(Gather Streams)
|--Nested Loops(Inner Join,
WHERE:([HRM_CALENDER].[Datum]>=[HRMABZ].[ABZDATF] AND
[HRM_CALENDER].[Datum]<=[HRMABZ].[ABZDATT]))
|--Clustered Index
Scan(OBJECT:([XHRMDTA036].[dbo].[HRMABZ].[PK__HRMABZ__52442E1F]),
WHERE:([HRMABZ].[ABZTECH]='N' AND [HRMABZ].[ABZLDLT]='N'))
|--Table Spool
|--Table Scan(OBJECT:([XHRMDTA036].[dbo].[HRM_CALENDER]),
WHERE:([HRM_CALENDER].[Valid]=1))

To be honest, I dont understand the details in the execution plan. I would
appreciate any help!

Any ideas?
Thank you
Thomi
Am Wed, 8 Dec 2004 20:44:16 +0000 (UTC) schrieb Erland Sommarskog:
Thomi Bächler (th***************@azul.ch) writes:
Identical means: Full backup of the database on our clients server and
restore on our testserver -> exact the same data in both databases


Hm, I wonder if the statistics are the same? Run an UPDATE STATISTICS
WITH FULLSCAN on bnth tables on both servers.
Complete query plans: sure, I can post the complete plan. I am an absolute
newbie. Can your tell me how to do that?


For a simple query, you issue this command before you issue the query:

SET SHOWPLAN_TEXT ON

This command is also an implicit SET NOEXEC ON, so the query will not be
executed.

You can also use SET STATISTICS_PROFILE ON. In this case the query will
be executed.
Number of rows: HRMABZ has about 4000 rows and HRM_CALENDER about 3800
rows


Ah, that appears it would make it a toss up which plan to use.
As far as I know is the only differnce between the two DBs/Servers the
language. I have to join dates (datetime) and my guess would be that the
english server has to do a language conversion of the date which leads
to . Could that be possible?


Not very likely. If all date columns are datetime, there is no conversion
to make, since datetime is a binary value. If some column is varchar, there
is always a conversion, no matter the language of the server. And what
matters more than the server language is the collations, but they are
obviously the same, as you copied the database with BACKUP/RESTORE.

But the hardware configuration of the machines could matter, for instance
the number of available processors.

Jul 23 '05 #6
Thomi Bächler (th***************@azul.ch) writes:
Execution plan is as follows:

Fast Server (german, SQL2000 SP3a, single Pentium, 768MB RAM)
-------------------------------------------------------------

Slow Server (english, SQL2000 SP3a, dual Pentium, 1G RAM)
---------------------------------------------------------
|--Parallelism(Gather Streams)


So there one was one more differences between the machines you didn't
tell us about! To wit, the number of CPUs.

Parallelism can be a great thing for a really wild query. But unfortunately,
the optimizer appears to be overly optimistic about the wonders of
parallelism, and use parallel plans when it shouldn't.

Add this at the end of the query:

OPTION (MAXDOP 1)

MAXDOP = Max Degree of Parallelism

This option forces a non-parallel plan. Sometimes a non-parallel plan
may be slower than the parallel plan, but this option may still be useful.
If you have an 8-way box, you may not want that wild query to monopolize
the entire server, but if 2-3 CPUs are working with it that may be OK.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7
Hi Erland

That did the trick! The query runs now with the same speed on both DBs. We
changed to: Properties -> Processor -> Parallelism -> Use 1 processor. We
think in our environment that might be best anyway.

Thank you for your help!
Thomi

Thomi Bächler (th***************@azul.ch) writes:
Execution plan is as follows:

Fast Server (german, SQL2000 SP3a, single Pentium, 768MB RAM)
-------------------------------------------------------------

Slow Server (english, SQL2000 SP3a, dual Pentium, 1G RAM)
---------------------------------------------------------
|--Parallelism(Gather Streams)


So there one was one more differences between the machines you didn't
tell us about! To wit, the number of CPUs.

Parallelism can be a great thing for a really wild query. But unfortunately,
the optimizer appears to be overly optimistic about the wonders of
parallelism, and use parallel plans when it shouldn't.

Add this at the end of the query:

OPTION (MAXDOP 1)

MAXDOP = Max Degree of Parallelism

This option forces a non-parallel plan. Sometimes a non-parallel plan
may be slower than the parallel plan, but this option may still be useful.
If you have an 8-way box, you may not want that wild query to monopolize
the entire server, but if 2-3 CPUs are working with it that may be OK.

Jul 23 '05 #8

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

Similar topics

0
by: Ray Gurganus | last post by:
I'm running MySQL 4.0.16 on Windows 2003. I just added the mysqld-nt command line option to enable the slow query log, started MySQL, and the option showed up as turned on. Then later I restarted...
1
by: Gary Wales | last post by:
We have two main query types running against a table of some 2 million rows and have gotten query response down to well under a second by using the right indexes. Problem is that we are running...
1
by: Thomas Bartkus | last post by:
Is it possible that the elapsed time being measured includes waiting for the client to acknowledge that it has received all the data? In *addition* to the server execution time? Documentation...
5
by: Stefan | last post by:
The following query gives me the right output, but takes almost 10 minutes to run... Any obvious things that I can improve on? I know this is diffuclt to do without realizign what I'm trying to...
2
by: zdk | last post by:
I have table name "actionlog",and one field in there is "date_time" date_time (Type:datetime) example value : 11/1/2006 11:05:07 if I'd like to query date between 24/07/2006 to 26/07/2006(I...
0
by: Dave Hammond | last post by:
Hi All, I'm trying to use the slow-query-log (with --log-queries-not-using-indexes enabled) to determine if any queries need optimization, and have a few questions about some entries I'm...
3
by: sbowman | last post by:
I have a database that I was using for Monthly Statistics. I've recently loaded hundreds of thousands of records of historical data. Now my queries are running really slow!! Is there anything I can...
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
3
by: xoinki | last post by:
hi all, I wanted to know few general details about MySQL slow query log. My slow log has a statement like # User@Host: root @ localhost here, what does this "" signify? why is this needed?...
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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

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