Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

sorting or temporary tables in execution plan?

 
   Database Help (Home) -> Technology and Theory RSS
Next:  The Scret of The Magic Code  
Author Message
Darren

External


Since: Aug 20, 2008
Posts: 4



(Msg. 1) Posted: Thu Aug 21, 2008 12:47 pm
Post subject: sorting or temporary tables in execution plan?
Archived from groups: comp>databases>theory (more info?)

ok, the book I am reading discusses this type of query:

SELECT p.pname
FROM P, SH, S
WHERE P.pnum = SH.pnum
AND SH.snum = S.snum
AND S.city = 'NY';

and it compares whether to do the full join first then select, or the
other way round.

Anyway the example first joins S and SH over the common column to form
a table TempA, and then sorts the table? The sorting actually takes up
most of the time >80% of total cost.

Is sorting of a temporary table a necessary step in an execution plan?

 >> Stay informed about: sorting or temporary tables in execution plan? 
Back to top
Login to vote
-CELKO-

External


Since: Jan 31, 2008
Posts: 63



(Msg. 2) Posted: Thu Aug 21, 2008 3:02 pm
Post subject: Re: sorting or temporary tables in execution plan? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> Is sorting of a temporary table a necessary step in an execution plan? <<

No, many products use hashing instead of sequential access and
merges.

I would also have done this query like so in a conventional SQL engine
built on top of a file system with indexes:

1) Build temp table using (S.city = 'NY') -- should be pretty small
subset, but if it is not indexed, you may have to scan the table.
2) (SH.snum = S.snum) with an index on what looks like PK-FK. In some
products that relationship would be in a linked list structure and be
really fast.
3) (P.pnum = SH.pnum) with an index

In short, the answer is that it all depends...

 >> Stay informed about: sorting or temporary tables in execution plan? 
Back to top
Login to vote
Darren

External


Since: Aug 20, 2008
Posts: 4



(Msg. 3) Posted: Thu Aug 21, 2008 3:46 pm
Post subject: Re: sorting or temporary tables in execution plan? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 21, 12:47 pm, Darren wrote:
> Is sorting of a temporary table a necessary step in an execution plan?

Found this on DB2 site regarding sorting decisions:

"When it evaluates a nested loop join, the optimizer also decides
whether to sort the outer table before performing the join. If it
orders the outer table, based on the join columns, the number of read
operations to access pages from disk for the inner table might be
reduced, because they are more likely to be be in the buffer pool
already. If the join uses a highly clustered index to access the inner
table and if the outer table has been sorted, the number of index
pages accessed might be minimized.
In addition, if the optimizer expects that the join will make a later
sort more expensive, it might also choose to perform the sort before
the join. A later sort might be required to support a GROUP BY,
DISTINCT, ORDER BY or merge join."

http://publib.boulder.ibm.com/infocenter/db2luw/v8/topic/com.ibm.db2.u...doc/adm
 >> Stay informed about: sorting or temporary tables in execution plan? 
Back to top
Login to vote
Tim X

External


Since: Jul 17, 2008
Posts: 39



(Msg. 4) Posted: Fri Aug 22, 2008 2:13 pm
Post subject: Re: sorting or temporary tables in execution plan? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Darren writes:

> ok, the book I am reading discusses this type of query:
>
> SELECT p.pname
> FROM P, SH, S
> WHERE P.pnum = SH.pnum
> AND SH.snum = S.snum
> AND S.city = 'NY';
>
> and it compares whether to do the full join first then select, or the
> other way round.
>
> Anyway the example first joins S and SH over the common column to form
> a table TempA, and then sorts the table? The sorting actually takes up
> most of the time >80% of total cost.
>
> Is sorting of a temporary table a necessary step in an execution plan?

As is often the case, it depends on the database engine. There is no 'for all
cases do x then y'.

There is an interesting book called "databases for smarties' or
something similar, which shows how much variation you can get in sql
statements on different database engines. In some cases, even the order
of the components of the where clause can have significant differences
in either performance or results (particularly with respect to outer
joins apparently). these differences can exist even when the different
engines supposedly support the same SQL standard.

Tim

--
tcross (at) rapttech dot com dot au
 >> Stay informed about: sorting or temporary tables in execution plan? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
PLAN-X 2007: Call for Papers and Demos (Deadline Extended).. - Third Call for Papers and Software Demonstrations P L A N - X 2 0 0 7 Programming Language Techniques for XML An ACM SIGPLAN Workshop colocated with POPL 2007 Nice, France...

integer id columns for all tables - Hi, Someone with greater expertise than I recently told me that it is best to have an integer primary-key for all tables in a database, even if the table already has some non-integer primary key, or some sort of composite primary key. If there is alread...

Multiple tables refer to one -To use foreign keys or not? - As one of my first encounters with SQL (The RDBMS used is MySQL 5.1), my colleague and I have designed the following (it's a part of a catalog system for a museum): We have an "attachments" table, which can refer to "works_of_art", &q...

Multiple Parent Tables (or Multiple Inheritence, or - Hi, It seems to be a common enough problem, but it doesn't seem to ever be properly addressed in data modelling books, and from reading on the web its clear that other people have had this problem as well as had multiple solutions presented, but its..

Problem with Nested Sets - Hello, I have a table which represents a tree of forums using nested sets. Here are the fields: id, root_id, left, right, level, label. I have a string which is like a path. For example, Forum/Sub-forum/Sub-sub-forum I want to get the id of the forum..
   Database Help (Home) -> Technology and Theory All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]