-1

I have two potential roads to take on the following problem, the try it and see methodology won't pay off for this solution as the load on the server is constantly in flux. The two approaches I have are as follows:

select *  
from  
  (  
      select foo.a,bar.b,baz.c  
      from foo,bar,baz
      -- updated for clarity sake  
      where foo.a=b.bar  
      and  b.bar=baz.c  
  )  
group by a,b,c

vice

create table results as
select foo.a,bar.b,baz.c  
from foo,bar,baz  
where foo.a=b.bar  
and  b.bar=baz.c ;  

create index results_spanning on results(a,b,c);  

select * from results group by a,b,c;

So in case it isn't clear. The top query performs the group by outright against the multi-table select thus preventing me from using an index. The second query allows me to create a new table that stores the results of the query, proceeding to create a spanning index, then finishing the group by query to utilize the index.

What is the complexity difference of these two approaches, i.e. how do they scale and which is preferable in the case of large quantities of data. Also, the main issue is the performance of the overall select so that is what I am attempting to fix here.

Comments

Are you really doing a CROSS JOIN on three tables? Are those three columns indexed in their own right? How often do you want to run the query which delivers the end result?

1) No.
2) Yes, where clause omitted for the sake of discussion as this is clearly a super trivial example
3) Doesn't matter.

2nd Update

This is a temporary table as it is only valid for a brief moment in time, so yes this table will only be queried against one time.

11
  • Well, first step is to not use SELECT * and do some join. As it is now, i'm not sure you are actually doing anything useful.
    – Plouf
    Jan 25, 2013 at 15:55
  • Are you really doing a CROSS JOIN on three tables? Are those three columns indexed in their own right? How often do you want to run the query which delivers the end result?
    – APC
    Jan 25, 2013 at 15:59
  • @APC The three tables inside the inner select are indexed and they share some relation, in reality there is a where clause that joins them together as it is required per the business case. The second part about how often I want it to run doesn't matter for this exercise because any table that was generated from the data would only be temporary.
    – Woot4Moo
    Jan 25, 2013 at 16:03
  • @Plouf I feel as though you have a fundamental misunderstanding of SQL. There are cases where a select * is valid, this is one of them. What isn't clear from my original post is that there is a where clause inside my inner query, which was omitted for the sake of discussion.
    – Woot4Moo
    Jan 25, 2013 at 16:04
  • 1
    @Woot4Moo I think there is some misunderstanding. What I mean is, you can remove your group by, and use a distinct, the result will be the same. Now I mostly agree with you, using a distinct is often an indication of some problems... So, you might have some problems. If you look at the bottom of the answer of @APC the idea is the same: there is probably some underlying problem which should be resolved.
    – Plouf
    Jan 25, 2013 at 17:11

2 Answers 2

0

If your query is executed frequently and unacceptably slow, you could look into creating materialized views to pre-compute the results. This gives you the benefit of an indexable "table", without the overhead of creating a table every time.

You'll need to refresh the materialized view (preferably fast if the tables are large) either on commit or on demand. There are some restrictions on how you can create on commit, fast refreshable views, and they will add to your commit time processing slightly, but they will always give the same result as running the base query. On demand MVs will become stale as the underlying data changes until these are refreshed. You'll need to determine whether this is acceptable or not.

4
  • Correct, normally I am all for materialized views. However, I cannot refresh these views as I have a very strict time constraint and the update for 100+million rows is unacceptable.
    – Woot4Moo
    Jan 25, 2013 at 16:38
  • Surely your query to build the MV will be the same as that to build the temp table? If so I can't see how creating the temp table will be quicker or better than an MV. Jan 25, 2013 at 16:42
  • You suggested refreshing the MV, which is different than doing a straight write with nologging.
    – Woot4Moo
    Jan 25, 2013 at 16:44
  • True, are you actually updating all 100m+ rows? Jan 25, 2013 at 16:47
0

So the question is, which is quicker?

  1. Run a query once and sort the result set?

  2. Run a query once to build a table, then build an index, then run the query again and sort the result set?

Hmmm. Tricky one.

The use cases for temporary tables are pretty rare in Oracle. They normally onlya apply when we need to freeze a result set which we are then going to query repeatedly. That is apparently not the case here.

So, take the first option and just tune the query if necessary.


The answer is, as is so often the case with tuning questions, it depends.

Why are you doing a GROUP BY in the first place. The query as you posted it doesn't do any aggregation so the only reason for doing GROUP BY woudl be to eliminate duplicate rows, i.e. a DISTINCT operation. If this is actually the case then you doing some form of cartesian join and one tuning the query would be to fix the WHERE clause so that it only returns discrete records.

1
  • The bottleneck is the non-indexed group by in the non-temporary table query. However, going the other way i eat the overhead of disk I/O and index creation. Is there a way to properly tune a non-indexed group by? I ask because I am not aware of a way to have an index span multiple tables.
    – Woot4Moo
    Jan 25, 2013 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.