<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Oracle Views: Are calculated columns always evaluated even when they are not considered in a query?</title>
	<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query/</link>
	<description>Comments on Ask MetaFilter post Oracle Views: Are calculated columns always evaluated even when they are not considered in a query?</description>
	<pubDate>Tue, 21 Apr 2009 14:40:26 -0800</pubDate>
	<lastBuildDate>Tue, 21 Apr 2009 14:40:26 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Oracle Views: Are calculated columns always evaluated even when they are not considered in a query?</title>
		<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query</link>	
		<description>Oracle Views: Are calculated columns always evaluated even when they are not considered in a query? &lt;br /&gt;&lt;br /&gt; I&apos;d like some advice regarding Oracle views. Are fields always evaluated regardless of whether they are required in the results set?&lt;br&gt;
&lt;br&gt;
Let&apos;s say I have tables PRODUCTS and ORDERS. And I create a view called PRODUCTS_VIEW which consists of something like:&lt;br&gt;
&lt;br&gt;
SELECT &lt;br&gt;
	PRODUCT.ID, &lt;br&gt;
	PRODUCT.NAME,&lt;br&gt;
	fcnGetMostRecentOrderDate(PRODUCT.ID) AS MostRecentOrderDate,&lt;br&gt;
	fcnGetTotalOrdersCount(PRODUCT.ID) AS TotalOrdersCount,&lt;br&gt;
	fcnGetMaxOrderVolumn(PRODUCT.ID) AS MaxOrderVolumn&lt;br&gt;
        and so on&lt;br&gt;
FROM&lt;br&gt;
     PRODUCTS,&lt;br&gt;
     ORDERS&lt;br&gt;
&lt;br&gt;
I.e. a flat view that encapsulates all possible attributes--I estimate around 20 static and 60 calculated fields.&lt;br&gt;
&lt;br&gt;
This is for reporting purposes, either queried directly or perhaps wrapped up in an API. Maintainability is important, if I need a new attribute then I can easily add a new field as necessary. Performance is certainly a factor but, since it&apos;s only for reporting purposes, there is a degree of flexibility.&lt;br&gt;
&lt;br&gt;
If I run a query that only considers 2 or 3 fields e.g.&lt;br&gt;
&lt;br&gt;
SELECT PRODUCT.NAME, MostRecentOrderDate&lt;br&gt;
 FROM PRODUCTS_VIEW &lt;br&gt;
WHERE... say....CLIENT = Heinz&lt;br&gt;
&lt;br&gt;
Would it still evaluate *all* the other calculated fields? (I think it does unfortunately: and I&apos;m concerned that performance would degrade as new fields are added.)&lt;br&gt;
&lt;br&gt;
If it *does* behave thus is there a way around it? I suppose a materialized view is an option. Or is this an inappropriate use? I am avoiding dynamic SQL since I would rather consolidate the logic into something that can be made (easily) accessible through ODBC, ADO, a bundle of Web Service methods etc. Note that a data warehouse approach (e.g. a daily dump of denormalised data) is not appropriate since data updates need to reflected immediately.&lt;br&gt;
&lt;br&gt;
Thanks.</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2009:site.120110</guid>
		<pubDate>Tue, 21 Apr 2009 14:14:16 -0800</pubDate>
		<dc:creator>NailsTheCat</dc:creator>
		
			<category>oracle</category>
		
			<category>view</category>
		
			<category>materializedview</category>
		
			<category>datawarehouse</category>
		
			<category>SQL</category>
		
			<category>resolved</category>
		
	</item> <item>
		<title>By: spatula</title>
		<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query#1719302</link>	
		<description>Can you not make additional views with just the fields you need? You will need to include them as objects in your external connections which will add some upkeep if you are adding new attributes frequently though.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.120110-1719302</guid>
		<pubDate>Tue, 21 Apr 2009 14:40:26 -0800</pubDate>
		<dc:creator>spatula</dc:creator>
	</item><item>
		<title>By: NailsTheCat</title>
		<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query#1719332</link>	
		<description>Thanks spatula. Yes, that&apos;s an option too. I&apos;ve pondered over this before, something like:&lt;br&gt;
PRODUCTS_VIEW_MAIN&lt;br&gt;
PRODUCTS_VIEW_ORDER_DATA&lt;br&gt;
PRODUCTS_VIEW_CLIENT_DATA&lt;br&gt;
PRODUCTS_VIEW_RESOURCE_HUNGRY_STUFF&lt;br&gt;
etc. &lt;br&gt;
this way at least I could avoid calculating ALL the fields ALL the time.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.120110-1719332</guid>
		<pubDate>Tue, 21 Apr 2009 15:20:14 -0800</pubDate>
		<dc:creator>NailsTheCat</dc:creator>
	</item><item>
		<title>By: orthogonality</title>
		<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query#1719337</link>	
		<description>My guess is that unused columns get optimized out and so the functions aren&apos;t called. &lt;br&gt;
&lt;br&gt;
But test this; write a function that sets some observable state, run the query without the column that calls that function, and then look at the state.&lt;br&gt;
&lt;br&gt;
One other note: your function names suggest that you could use subqueries rather than functions for most of them. Also, what&apos;s the point of prefixing all functions with &apos;fcn&apos;?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.120110-1719337</guid>
		<pubDate>Tue, 21 Apr 2009 15:24:37 -0800</pubDate>
		<dc:creator>orthogonality</dc:creator>
	</item><item>
		<title>By: NailsTheCat</title>
		<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query#1719371</link>	
		<description>thanks orthogonality: I was hoping that unused columns would be optimized out, but when I used EXPLAIN PLAN this didn&apos;t seem to be the case. That&apos;s a good suggestion (setting an observable state) I shall give it a shot.&lt;br&gt;
&lt;br&gt;
&lt;small&gt;Re. function names: I just pulled the example data out of my proverbial. Some of them will be sub-queries while some will be calling horribly substantial PL/SQL functions. I prefixed them with &quot;fcn&quot; so that it was explicit in my example that they were functions. This isn&apos;t representative of the actual DB--fyi, it&apos;s nothing to do with products, orders.&lt;/small&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.120110-1719371</guid>
		<pubDate>Tue, 21 Apr 2009 15:56:10 -0800</pubDate>
		<dc:creator>NailsTheCat</dc:creator>
	</item><item>
		<title>By: devilsbrigade</title>
		<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query#1719417</link>	
		<description>I&apos;d trust EXPLAIN PLAN, though that seems to jive with &lt;a href=&quot;http://www.oraclebrains.com/2007/10/understanding-virtual-columns/&quot;&gt;this&lt;/a&gt;, which indicates it&apos;ll be computed only when queried. If it does get evaluated even when not queried, making a view with the virtual column seems like a better plan to me.&lt;br&gt;
&lt;br&gt;
I don&apos;t really know Oracle, but it seems like if there&apos;s a possibility of the expression changing state, they&apos;d make a big deal out of exactly when it was evaluated. Sure it&apos;s not in the docs?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.120110-1719417</guid>
		<pubDate>Tue, 21 Apr 2009 16:30:02 -0800</pubDate>
		<dc:creator>devilsbrigade</dc:creator>
	</item><item>
		<title>By: NailsTheCat</title>
		<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query#1719445</link>	
		<description>thanks devilsbrigade: I&apos;m using 10g which doesn&apos;t support Virtual Columns (very nice they look too). However, I think you&apos;re right and the same logic applies to regular views, I just found this &lt;a href=&quot;http://www.orafaq.com/node/1013&quot;&gt;article&lt;/a&gt;. (Haven&apos;t found anything in any documentation yet.)&lt;br&gt;
&lt;br&gt;
Following on from orthogonality&apos;s suggestion I tried updating some data within a function called by one of my view&apos;s columns. Oracle won&apos;t actually let you do this through a SELECT and raises an error (makes sense). If I exclude the offending column from my query I don&apos;t get the error and therefore it&apos;s pretty clear that the column is not considered. Which makes far more sense to me (although it contradicts what I was told by a DBA today...).&lt;br&gt;
&lt;br&gt;
Thanks all.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.120110-1719445</guid>
		<pubDate>Tue, 21 Apr 2009 17:12:42 -0800</pubDate>
		<dc:creator>NailsTheCat</dc:creator>
	</item><item>
		<title>By: NailsTheCat</title>
		<link>http://ask.metafilter.com/120110/Oracle-Views-Are-calculated-columns-always-evaluated-even-when-they-are-not-considered-in-a-query#1756795</link>	
		<description>As an addendum, while functions are not evaluated, obviously if the query upon which the view is based uses joins, those must be evaluated and therefore there are always some performance implications.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.120110-1756795</guid>
		<pubDate>Sat, 23 May 2009 13:19:36 -0800</pubDate>
		<dc:creator>NailsTheCat</dc:creator>
	</item>
	</channel>
</rss>
