Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Select where DateTime field?
Message
 
 
À
17/09/2013 11:13:50
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Versions des environnements
SQL Server:
SQL Server 6.5 and older
Application:
Web
Divers
Thread ID:
01583397
Message ID:
01583407
Vues:
26
>For roughly 100,000 rows, I would say the difference would be in terms of milliseconds, or maybe a second at the most, certainly not minutes.
>
>I did the following, using the Purchasing.PurchaseOrderHeader table in AdventureWorks:
>
>
>create index [OrderDate_IX] on Purchasing.PurchaseOrderHeader (OrderDate)
>
>set statistics io on
>set statistics time on 
>
>select PurchaseOrderID,OrderDate from Purchasing.PurchaseOrderHeader
>   where OrderDate >= '20050517' and OrderDate < '20050518'
>
>select PurchaseOrderID,OrderDate from Purchasing.PurchaseOrderHeader
>   where cast(OrderDate as Date) = '20050517'
>
>
>They both will use an Index Seek, but there is a slight bit of additional overhead using the CAST that results in an increased execution time of anywhere from 20-80 milliseconds. I generally avoid expressions in a WHERE clause where I can - so I would say the first approach is technically better. It's an old trick developers (especially those writing reports) have used for years....to retrieve all the rows that are greater than or equal to midnight of a start date, and less than midnight of (final date that you want, plus one day)

First, thank you very much for taking the time to create this test script. For my use - tables, at most having 500,000 records (very seldom) - taking an extra second is not a problem. I like the Cast approach because in my code (which is VFP) I don't have to 'check' if user wants the same date or range of dates. I simply change the expression to use Cast( DateTime as Date) and it will work in every case. Less maintenance for me (which is lately my higher priority than speed).
Again, thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform