When an indexed view is not an option..

I am wondering how I should most quickly fetch the data from a group of tables for reporting / viewing.

My tables are slowly changing, meaning that I use a getDate() function to pull the right records between effective and expire dates.  An admin may be making changes occassionally to some records, but mostly the data is read-only by site visitors.  It's ok for the changes made by the admin not to go live immediately, of if urgent, for them to click a button or something to force them live.

I have a big query which brings a dozen tables together and can be a bit slow.
It includes case statements and left joins and getDate() which not allowed in Indexed Views.

I'm wondering if it makes sence to create a table that I populate from my big query, add various indexes to the table and populate it every night or every few hours or something.   Are there downsides to this?   It seems indexed views are not an option...
LVL 39
Who is Participating?
>> I'm wondering if it makes sence to create a table that I populate from my big query, add various indexes to the table and populate it every night or every few hours or something.   Are there downsides to this?

This is a classic start to a data warehouse or data mart.

If you can deal with data being slightly out of date then you can run an hourly/nightly/whatever job to populate your data mart.

You can take advantage of this step to denormalise the data to make things faster.

Another option is to build a cube. If the data is numerical in nature and there's lots of summaries going on, build a cube.

If the data is list-like in nature (big strings and ordered detail lists) then go the data mart route.
gdemariaAuthor Commented:
Hi nmcdermaid,
 Thanks for your response.

 Although numbers are involved, the numbers are not totalled or averaged in any way, so I htink data warehousing is the way to go.
 I understand how to denormalize the data with indexes for faster quering.   I am wondering, however, if there are specific SQL Server 2005 features or special data definitions that I need to look for in order to accomplish this.  Or am I basically building the tables and indexes and that's the meat of it.

  Also, does SQL Server 2005 have anything like Oracle's Materialized Views?  I've heard that Indexed Views are Microsoft's answer, but they aren't even close.  You may not be familiar with Oracle, but I'm wondering if there are other SQL Server options that I am not aware of take "snap shots" of the data.
I haven't got much experience with Oracle. The main idea of indexed views in SQL is so that when a query refers to tables, if there is pre-joined indexed view that already has the answer, it automatically goes to the indexed view, without a code change. Is that the way Oracle materialized views work?

Couldn't you use the GETDATE() outside the indexed view? or is it more complicated than that?

i.e. you create an indexed view that satisfied everything in your query (joins, summarys, but no getdate()). Then run your normal select (including GETDATE())

You'll have to experiment but its probably worth

1. Creating an indexed view as close as possible to your query
2. Run your original query and check the execution plan (CTRL-L)
3. See if the execution plan refers to your indexed view

You may get a little performance increase even if you can't code the whole lot in the view.

There are other things you can do to increase performance like set the database file to read only, mess with disk I/O, stuff like that.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Ken SelviaRetiredCommented:
What if you created another table

WarehouseDate (WHDate datetime)

and joined to that on WHDate instead of using getdate() in your view.

Then you can have an indexed view that will change when the value of WHDate changes.

Change WHDate on some regular basis (hourly, daily etc.) using a SQL Agent job.
gdemariaAuthor Commented:
kselvia, that's a very clever idea !
I would only need to change the value of the WHdate nightly as time is not a factor.

I have tried, however, to create an indexed view in another, less complex, situation and found it nearly impossible.  Indexed Views have so many restrictions it's incrediblely difficult to make any use of them at all;  no unions, no case statements, nothing inside of count(xxx), no 'distinct' or left joins (i think).  

What a nightmare !

But I wonder if your suggestion about the WHdate would help speed up regular queries or views rather than  using getDate() ?

Ken SelviaRetiredCommented:
Using WHdate in other queries would not be faster than using getdate().  Chances are it may even hurt performance.

As nmcdermaid said, it seems you could utilize an indexed view to generate a basis from which to make further queries which CAN have your aggregates, cases, etc.

Are you pretty confident your existing tables have the proper indexes for the kinds of reports/complex queries you are running?

Maybe there are tuning opportunities there.
Actually the seperate table thng is along the lines of a calendar table.. you have a flag for current date, and you have another two dozen flags for same day lastweek, same day last year, week to date etc.

I recall now that the oracle equivalent of indexed views is query rewrite.
Futher to that, if you are indeed creating a data warehouse, then all of your case statements, outer joins etc. would have already been processed, and no complex queries would be required.

It sounds like many of your issues would be satisfied with a data warehouse.
gdemariaAuthor Commented:
Thanks for the great information.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.