Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Goulasch
Starting Member
3 Posts |
Posted - 2010-07-23 : 09:46:24
|
Hello,I like to create an filtered index to get "all stored different customer-id´s with their latest stored date (also a col)"in an log-fileAs a result of a query I exspect to get only the latest record (current date) for every customer. The log-table contains millions of entries for every customer and a select * where ...without an index doesn´t make sense (performance reason).Any idears ? |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-07-23 : 10:42:50
|
Need more information. Table design, query, current indexes at least.You using SQL 2008?--Gail ShawSQL Server MVP |
|
|
Goulasch
Starting Member
3 Posts |
Posted - 2010-07-23 : 11:07:32
|
quote: [i]You using SQL 2008?
yesexample:cust-id timestamp............inf1.inf2--------------------------------------------------33091 20100723 12:00:56 xyz 12jnun33090 20100723 12:00:56 juhn cccccc33091 20100723 10:00:33 plok dfssdfsfs20333 20100629 16:45:10 pokf asdfadsfdafd44908 20100501 01:00:19 olkm dsadsdsa33090 20100710 18:53:59 dasff asfddfasresult:33091 20100723 12:00:56 xyz 12jnun33090 20100723 12:00:56 uhn cccccc20333 20100629 16:45:10 pokf asdfadsfdafd44908 20100501 01:00:19 olkm dsadsdsaIt´s not neccessary to get back the cust-id in any order (nor the timesstamp) |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-07-23 : 11:27:42
|
So you want the cust-id along with one (latest?) timestamp and associated inf1 and inf2?This isn't a filtered index, it's a reasonably trivial query. If my above assumption is correct...SELECT [cust-id], timestamp, inf1, inf2FROM(SELECT [cust-id], timestamp, inf1, inf2, ROW_NUMBER() OVER (Partition By [cust-id] Order By timestamp DESC) as RowNoFROM someTable) subWHERE RowNo = 1--Gail ShawSQL Server MVP |
|
|
Goulasch
Starting Member
3 Posts |
Posted - 2010-07-23 : 11:38:04
|
the select-clause will run every 5 sec. from 10 different clients;the table contain millions of records.Because of performance issue : why can´t we create an index with only 1 record per cust-id ?As an example:create INDEX log_idx ON cust-id, timestamp DESC WHERE RowNo=1? |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-07-23 : 16:10:27
|
Because it doesn't satisfy the requirements for a filtered index. RowNo is not a column in the table. It's a derived column. From BoL:quote: The filter predicate uses simple comparison logic and cannot reference a computed column, a UDT column, a spatial data type column, or a hierarchyID data type column. Comparisons using NULL literals are not allowed with the comparison operators. Use the IS NULL and IS NOT NULL operators instead.
An index on cust-id, timestamp should help out here.--Gail ShawSQL Server MVP |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-07-24 : 02:25:00
|
"the select-clause will run every 5 sec. from 10 different clients;"If the query is frequent, and performance (with indexes, carefully tuned) is not fast enough then you could store the Last order details for each Customer (in a new table) using a trigger on your log table. It would be important that the number of queries is much higher than the number of inserts into the log table that cause the trigger to fire |
|
|
|
|
|
|
|