Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

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


Report this thread to moderator Post Follow-up to this message
Old Post
scott.swank@gmail.com
03-28-06 01:27 AM


Re: ranged datetime predicates & cardinality estimates
(scott.swank@gmail.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. 
> 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, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
03-28-06 01:27 AM


Re: ranged datetime predicates & cardinality estimates
Why thank you, that took care of the issue.  I knew that it was
something reasonably simple.

Scott


Report this thread to moderator Post Follow-up to this message
Old Post
scott.swank@gmail.com
03-28-06 01:27 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 09:51 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006