December 21, 2005 at 5:34 am
I have a table of production data with this basic schema. We need to query based on the time stamp. The time stamp consists of the year, (2005) the month (12) the day(12), the hour(16), the minute(55) and the second(24) and the line number(07) in the case of 005121216552407
ProductID WO_ID TimeStamp
A2C20009365 9432101 2005121216552407
A2C20009365 9432102 2005121216552407
A2C20009365 9432103 2005121216552407
A2C20009365 9432104 2005121216552407
A2C20009365 9432105 2005121318532807
I have a UDF to return a datetime for reporting, but we need to search and group by this field and therefore we need to index this timestamp as a datetime. We will have several million rows of data. Any Ideas?
CREATE function dbo.PCRBuildDate(@BN varchar(32))
Returns DateTime
as
BEGIN
declare @buildate datetime
select @buildate = left(@BN,4) + '-' + substring(@BN,5,2) + '-'
+ substring(@BN,7,2) + ' ' + substring(@BN,9,2)
+ ':' + substring(@BN,9,2) + ':' + substring(@BN,11,2) + '.000'
RETURN @buildate
END
December 21, 2005 at 7:08 am
I am not sure what you want to do, but I think I would add a Date column and a LineNumber column, then transfer data in the TimeStamp column to these columns and perhaps then drop the TimeStamp column... Does this sound reasonable?
December 21, 2005 at 7:44 am
You could make the extra Date and Linenumber fields computed fields and add an index on them. Everything stays in sync.
Any reason not to use left(BN,14) instead of all those substrings?
December 21, 2005 at 7:51 am
Thanks for the replys.
This is what I am trying to do. This data is generated by a manufacturing line we received from Germany. The line can build up to 36 parts per minute. Each part will have a record with this type of data. With these files we need to be able to calculate rates per minute etc. Trying to query on the table and use the UDF to sort was inefficient.
After reading your posts I have created a view that derives a datetime from the TimeStamp column, then I have indexed that derived datetime column in the view. Does this sound like an efficient solution?
December 21, 2005 at 8:07 am
A side remark: Why sort by the result of the UDF? Why not sort by the TimeStamp column (or perhaps left(TimeStamp, 14))? That will amount to the same thing (as far as I can see) but be much more efficient...
December 21, 2005 at 8:16 am
Good Idea, Thanks
December 21, 2005 at 8:28 am
In fact, forget about extra columns, views and UDFs. Put an index on the TimeStamp column, then I think you can extract all the information you want from this table (if you don't agree, please post a more detailed example of what you want).
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply