|
I have a table like: I want to create a view that will show: Means The attributes values on every entity should be displayed separated by a comma. The number of options can be varied. But the most important issue is the query should not violate DRY Priciple The query should be very fast because the table are going to have millions of records. Any help is appreciated! This is the actual query that i created using the answers it takes about 20 seconds to get few million records but if i just wan to get few records like 20-30 it takes 2-3 secinds. I want to reduce the time by less than a half second I need to inject where conditions into it. Please specify what i am missing
(comments are locked)
|
|
Try this, it is ONE way of doing it gives I post the same question on stackoverflow too. and get the same reply. This query is too slow and does not solve my purpose. Isnt there is some other way. I want fully optimized query
Oct 19 '11 at 02:54 AM
Kamal Deep Singh Saini
How slow is slow, Jeff Moden talks about using this method in this post http://www.sqlservercentral.com/articles/Test+Data/61572/, and measures a 17 sec duration for a million row table. If you need to keep doing this over and over in a view, then you need to rethink your design. How about materializing this 'view' in a table? How about changing the base table design to store the data as a CSV? How about handling the string manipulation in the application layer/presentation layer?
Oct 19 '11 at 03:02 AM
Kev Riley ♦♦
This is the best way I know of. If it's slow, look at the query plan and see how you can optimise it.
Oct 19 '11 at 03:32 AM
David Wimbush
@Kamal Deep Singh Saini You can reduce the number of reads from the table a little bit by first grouping the records by 2 columns and then calling cross apply rather than calling it once for each row and then using distinct which does the grouping by 3 columns. This will not help a lot, but will be a bit cheaper to do because the grouping is easier and the number of calls to the inner query is smaller: select
t.EntityID, t.AttributeID,
stuff(x.Options, 1 , 1 , '') Options
from
(
select
EntityID, AttributeID
from @YourTable
group by EntityID, AttributeID
) t cross apply
(
select ',' + OptionText
from @YourTable
where
EntityID = t.EntityID
and AttributeID = t.AttributeID
for xml path('')
) x(Options);If this does not help much then you can try using the dynamic pivot first and then the normal concat to derive your delimited lists (one for each combination). This will make the query much more complex, but considerably cheaper and probably faster. Please let me know if you want it and I can post a sample.
Oct 19 '11 at 08:03 AM
Oleg
@Oleg you can please post a sample related to dynamic pivot. What finally i need to do is to get the data in the required format as specified by the question and then pivot it. Actually i am trying to create a Model that resembles EAV. I will post the actual query that i created using the sample given above
Oct 21 '11 at 04:30 AM
Kamal Deep Singh Saini
(comments are locked)
|


How often will the code to create have to run, is it a one off?