Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


When an indexed view is not an option..

Posted on 2007-12-05
Medium Priority
Last Modified: 2012-06-22
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...
Question by:gdemaria
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
LVL 30

Accepted Solution

nmcdermaid earned 1600 total points
ID: 20416430
>> 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.
LVL 39

Author Comment

ID: 20417039
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.
LVL 30

Expert Comment

ID: 20417302
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.
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

LVL 12

Assisted Solution

kselvia earned 400 total points
ID: 20512263
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.
LVL 39

Author Comment

ID: 20513946
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() ?

LVL 12

Expert Comment

ID: 20517656
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.
LVL 30

Expert Comment

ID: 20518073
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.
LVL 30

Expert Comment

ID: 20518076
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.
LVL 39

Author Closing Comment

ID: 31412996
Thanks for the great information.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question