home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / database / 6321 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  4.8 KB

  1. Xref: sparky comp.databases:6321 comp.databases.theory:389 comp.databases.oracle:1375
  2. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!rutgers!uwvax!hellers
  3. From: hellers@wisc.edu (Joseph Hellerstein)
  4. Newsgroups: comp.databases,comp.databases.theory,comp.databases.oracle
  5. Subject: Re: Is this a bug or a limitation of the SQL language
  6. Message-ID: <HELLERS.92Aug25181305@cleo.wisc.edu>
  7. Date: 26 Aug 92 00:13:05 GMT
  8. References: <1992Aug23.074048.16681@prism.poly.edu> <1992Aug24.100941.24827@dk.oracle.com>
  9.     <JOEY.92Aug24091348@elysium.berkeley.edu>
  10.     <BtJKIG.2K5@watdragon.uwaterloo.ca>
  11. Sender: news@cs.wisc.edu (The News)
  12. Organization: UW-Madison
  13. Lines: 85
  14. In-Reply-To: gnpaulle@maytag.uwaterloo.ca's message of Tue, 25 Aug 1992 13:37:27 GMT
  15.  
  16. In article <BtJKIG.2K5@watdragon.uwaterloo.ca> gnpaulle@maytag.uwaterloo.ca (Glenn Paulley) writes:
  17. >    > [I wrote:]
  18. >    >   |select * from emp 
  19. >    >   |where salary between
  20. >    >   |(select salary from emp where ename = 'Larry')
  21. >    >   |and
  22. >    >   |(select salary from emp where ename = 'John')
  23. >    > You want to avoid subqueries whenever possible.  To get what you need,
  24. >    > try the following:
  25. >    2) Sahil's query, if optimized using subqueries for the where predicate,
  26. >    will probably execute much faster than doing a three-way join (the
  27. >    conjuncts in the where clause are single-value lookups; if ename is
  28. >    indexed, for example, the entire result can be found using two indexed
  29. >    retrievals, and a single table scan.... no joins, no sorts). 
  30.  
  31. This is incorrect.  Subquery processing is identical to *one kind* of
  32. join processing, namely nested-loop with the subquery as the inner.
  33. Hence joins will always do at least as well as subqueries, and often
  34. better, since the space of paths for the optimizer to choose from is
  35. larger.  To argue that subquery processing is simpler than join
  36. processing is just wrong.  It's a kind of join processing itself, with
  37. restricted output semantics.
  38.  
  39. >    Avoiding
  40. >    subqueries altogether, or always attempting to transform subqueries to
  41. >    joins, is a dangerous heuristic. 
  42.  
  43. A heuristic, yes, as noted below (duplicate elimination may be
  44. required).  Dangerous?  Not really.  For exactly the reason I
  45. outlined above, subquery-to-join is a stable conversion, meaning that
  46. it will almost always do as well, and often better.  To do duplicate
  47. semantics correctly, a sort or hash is sometimes required after
  48. transformation (SELECT DISTINCT instead of SELECT), and this may
  49. result in some slowdown.  But the win from conversion can result in
  50. orders-of-magnitude improvement, while the loss of an extra sort (say)
  51. usually slows things down by much less (in the < 100% slowdown range).
  52.  
  53.  
  54. >    RDBMS optimizers should also determine
  55. >    if transforming a join into a subquery will result in a better execution
  56. >    plan.
  57. This can be done, but as noted above, this logic is not essential
  58. since subquery-to-join conversion is quite stable.  Since optimization
  59. time is already dangerously complex, adding this dimension is probably
  60. not worth it.
  61.  
  62. >    Having the *user* perform query rewrite for "optimization" is unacceptable
  63. >    (it's what query optimizers in RDBMS are all about). Sahil's query is 
  64. >    straightforward and specifies exactly what he wants. It's not his fault
  65. >    if today's query optimizers fail to execute this query in the most 
  66. >    optimal way. 
  67.  
  68. Absolutely 100% agreed.  This is a serious failing in the existing SQL
  69. products, namely that different expressions of the same query may have
  70. wildly different performance.  As a result, if you open up a vendor's
  71. manual, you will usually find such "tips" as: "convert subqueries to
  72. joins in the case where blah blah blah".  Naturally these tips will
  73. stymie all but the most sophisticated users.  And every shop needs
  74. such a user, or they're in trouble.  The Starburst research project at
  75. IBM Almaden may result in IBM's products having such optimizations be
  76. automatic some years from now.
  77.  
  78. >    It's also a dangerous practice
  79. >    to generalize (that is, transform subqueries to joins) because not all
  80. >    transformations result in equivalent queries (particularly when the
  81. >    database contains null values). See the paper by Negri et al.
  82. >    in ACM TODS, Vol 17, No. 3, Sept. 1991 for formal semantics of entry-level
  83. >    SQL and query equivalence classes.
  84.  
  85. And see the paper by Pirahesh, Hellerstein, and Hasan in SIGMOD '92
  86. which demonstrates that subquery-to-join *can* be done correctly (and
  87. is done automatically in Starburst) for all existential subqueries
  88. that are Boolean Factors (i.e. IN, ANY, or EXISTS subqueries that are
  89. outer conjuncts of the WHERE clause, not nested under NOT or OR, etc.)
  90.  
  91. You have to be careful, but the rewrite I gave him will work
  92. correctly, may well speed up his query, and may even sneak by his
  93. limited parser.
  94.  
  95. Happy rewriting.
  96.  
  97. Joe Hellerstein
  98.