Someone recently asked me the differences between Indexes and views. Here is what I thought, can someone add anything more to this or explain to me where I'm wrong? LOL Jeff - In what ways are views better than well designed (and correctly used) indexes? Answer (Although I haven't sent it yet cause I hate being wrong) When the Optimizer makes a decision on how to retrieve records it bases that decision on an estimate of the number of rows the request may return, against the number of rows within the table. Utilizing a View of the actual table prior to the request would alter that estimate. The Optimizer will be making it's decision based on the number of rows available through the view. As in the case below this view is substantially smaller than the Physical Table. That saves overhead on the processor and speeds the request along. Whereas an index of the actual table is still a full set of rows keyed to a particular field or even multiple keys within the table. When using an index to retrieve the records for a program into main memory, the system will retrieved deleted records and page them into memory as well as good rows. When using a view where the data is pre-selected on a machine level the deleted records are omitted and never paged into memory, making the program more efficient. In addition to the overhead there is also the benefit of data independence. If your programs are compiled to the tables themselves, any changes to the tables will require a recreation of the programs and indexes to incorporate the changes. By utilizing the View in your program compilation instead of the tables record format, it frees you to make changes to the tables without having to recompile your load libraries. A view is not necessarily an index. A view can bea subselect of one or multiple large tables. The advantage to this can not be correlated with Interactive SQL Vs a Batch request since your statistics will be very different utilizing similar SQL statements against the same table. Although the particular request you added to this email will deliver similar response time whether the index is used or whether a view is created and used, the views offer greater application flexibility overall no matter how the requests are actually written, which is why I suggested their use. HREF="mailto:Jeff_Importico@Vanguard.com">(Jeff_Importico@Vanguard.com)

Reply With Quote