473,669 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ranged datetime predicates & cardinality estimates

Hello all. I'm running SQL Server 2000 and I'm trying to get a very
few, recent rows of data from a table based on an indexed datetime
column. Here's my predicate:

where order_date > dateadd(hour, -1, getdate())

i.e. everything more recent than one hour ago. This corresponds to the
3 or 4 rows in which I'm interested. I have order_date indexed and I
have current statistics. When I check the explain plan for this query
I see expected rows returned: 114,000. When I go on to join to several
other tables I end up with unnecessary hash joins -- due to the
inaccurate cardinality estimates on this table.

However, if I use the following predicate (which corresponds to data
within the last 3 days):

where order_date > '2006-03-24'

then I see an estimated rows returned: 6 -- which is pretty accurate.
From there the rest of the query's explain plan falls together nicely.

So my question is: how do I get the optimizer to realize that one hour
ago is pretty recent?

Many thanks,
Scott

Mar 27 '06 #1
2 1910
(sc*********@gm ail.com) writes:
Hello all. I'm running SQL Server 2000 and I'm trying to get a very
few, recent rows of data from a table based on an indexed datetime
column. Here's my predicate:

where order_date > dateadd(hour, -1, getdate())

i.e. everything more recent than one hour ago. This corresponds to the
3 or 4 rows in which I'm interested. I have order_date indexed and I
have current statistics. When I check the explain plan for this query
I see expected rows returned: 114,000. When I go on to join to several
other tables I end up with unnecessary hash joins -- due to the
inaccurate cardinality estimates on this table.

However, if I use the following predicate (which corresponds to data
within the last 3 days):

where order_date > '2006-03-24'

then I see an estimated rows returned: 6 -- which is pretty accurate.
From there the rest of the query's explain plan falls together nicely.

So my question is: how do I get the optimizer to realize that one hour
ago is pretty recent?


To do this properly, you need to add another call level. One way is
to write an inner procedure and pass that procedure the computation
of dateadd(hour, -1, getdate() to that procedure. As alternative
you could call sp_executesql, but this reqiures the user to have
SELECT permission on the table.

The problem is that getdate() is a moving target. SQL Server does
not know that order_date is only in the past. Since getdate() is
an unknown value, it makes a standard assumption of a 30% hit-rate,
and then it goes downhill from there.

Another alternative is to use an query hint of some sort.

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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 27 '06 #2
Why thank you, that took care of the issue. I knew that it was
something reasonably simple.

Scott

Mar 27 '06 #3

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

Similar topics

1
8263
by: Shaun | last post by:
Hi, I have a table called Bookings which has two important columns; Booking_Start_Date and Booking_End_Date. These columns are both of type DATETIME. The following query calculates how many hours are available between the hours of 09.00 and 17.30 so a user can see at a glance how many hours they have unbooked on a particular day (i.e. 8.5 hours less the time of any bookings on that day). However, when a booking spans more than one day...
4
14413
by: Negroup | last post by:
Hi, all. I would like to know if it is possible to create a datetime instance using a tuple instead of single values. I mean: >>> from datetime import datetime >>> t = (1, 2, 3) >>> dt = datetime(t) Traceback (most recent call last): File "<stdin>", line 1, in ?
2
1723
by: Thong Nguyen | last post by:
I wrote my own Predicate class for .NET 1.1 which allowed composite predicates using operator overloading... for example: Predicate p1 = {...}; Predicate p2 = {...}; Predicate p1andp2 = ~(p1 ^ p2); // ^ is the AND operator in logic notation
2
10445
by: Rizyak | last post by:
-----Cross posted on comp.databases.ms-sqlserver and microsoft.public.sqlserver.programming----- Can someone please explain cardinality to me? I am having a problem with an insert from a webpage. When a user adds a venue with an event I need both tables to populate. Currently only the event table populates the right info and the venue does not. I am using SELECT SCOPE IDENTITY to get the last inserted venue to associate with each...
2
4675
by: kma | last post by:
I am designing an Access 2000 database on a computer running Windows 98. I have one form with several tabs; all of which have sub forms, some with a subform on a subform. For example, on my main Job form I have a tab for PO's to keep track of all purchase orders pertaining to a specific job. The first subform is for the PO numbers related to the jobs on the main form and the next subform is for all of the PO lines for each PO. ...
5
4926
by: MLH | last post by:
Cardinality? I hear it spoken of by MySQL users. Does it matter to MS Access if MySQL tables are attached via ODBC?
5
1356
by: Jason Madison | last post by:
A programmer I work with spent 4 hours producing a 14 day estimate for a project. In the event the project took 15 days and his manager was annoyed that he had estimated incorrectly. Is there any research into the ratio between time taken to produce an estimate and the accuracy of an estimate? Could it be said for example that if the programmer had spent 8 hours on the estimate he would have been more likely to have arrived at 15 days?
2
5530
by: uwcssa | last post by:
is there a way to see the actual (not just estimated) cost and cardinality at each plan operator? SQL Server and Oracle has such feature but I failed to find a way in DB2 yet (up to V9.5)
4
3877
by: Arun Srinivasan | last post by:
Hi I was using a query previously, that was efficient select * from table where pred1 and pred2 and pred3; Later I was asked to introduce new ones, but they were not based on table columns but variables declared in SP. select * from table where pred1 and pred2 and pred3 and variable1 ='number1 and variable2 =number2;
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8894
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.