Examples of CostEstimate


Examples of org.apache.derby.iapi.sql.compile.CostEstimate

    {
      resultSetString = "getAnyResultSet";
    }

    // Get cost estimate for underlying subquery
    CostEstimate costEstimate = resultSet.getFinalCostEstimate();

    /* Generate a new method.  It's only used within the other
     * exprFuns, so it could be private. but since we don't
     * generate the right bytecodes to invoke private methods,
     * we just make it protected.  This generated class won't
     * have any subclasses, certainly! (nat 12/97)
     */
    String subqueryTypeString =
              getTypeCompiler().interfaceName();
    MethodBuilder  mb = acb.newGeneratedFun(subqueryTypeString, Modifier.PROTECTED);

    /* Declare the field to hold the suquery's ResultSet tree */
    LocalField rsFieldLF = acb.newFieldDeclaration(Modifier.PRIVATE, ClassName.NoPutResultSet);

    ResultSetNode subNode = null;

    if (!isMaterializable())
    {
            MethodBuilder executeMB = acb.getExecuteMethod();
      if (pushedNewPredicate && (! hasCorrelatedCRs()))
      {
        /* We try to materialize the subquery if it can fit in the memory.  We
         * evaluate the subquery first.  If the result set fits in the memory,
         * we replace the resultset with in-memory unions of row result sets.
         * We do this trick by replacing the child result with a new node --
         * MaterializeSubqueryNode, which essentially generates the suitable
         * code to materialize the subquery if possible.  This may have big
         * performance improvement.  See beetle 4373.
         */
        if (SanityManager.DEBUG)
        {
          SanityManager.ASSERT(resultSet instanceof ProjectRestrictNode,
            "resultSet expected to be a ProjectRestrictNode!");
        }
        subNode = ((ProjectRestrictNode) resultSet).getChildResult();
        LocalField subRS = acb.newFieldDeclaration(Modifier.PRIVATE, ClassName.NoPutResultSet);
        mb.getField(subRS);
        mb.conditionalIfNull();

        ResultSetNode materialSubNode = new MaterializeSubqueryNode(subRS);

        // Propagate the resultSet's cost estimate to the new node.
        materialSubNode.costEstimate = resultSet.getFinalCostEstimate();

        ((ProjectRestrictNode) resultSet).setChildResult(materialSubNode);

        /* Evaluate subquery resultset here first.  Next time when we come to
         * this subquery it may be replaced by a bunch of unions of rows.
         */
        subNode.generate(acb, mb);
        mb.startElseCode();
        mb.getField(subRS);
        mb.completeConditional();
   
        mb.setField(subRS);

                executeMB.pushNull( ClassName.NoPutResultSet);
                executeMB.setField(subRS);
      }

            executeMB.pushNull( ClassName.NoPutResultSet);
            executeMB.setField(rsFieldLF);
      // now we fill in the body of the conditional
      mb.getField(rsFieldLF);
      mb.conditionalIfNull();
    }

    acb.pushGetResultSetFactoryExpression(mb);

    // start of args
    int nargs;

    /* Inside here is where subquery could already have been materialized. 4373
     */
    resultSet.generate(acb, mb);

    /* Get the next ResultSet #, so that we can number the subquery's
     * empty row ResultColumnList and Once/Any ResultSet.
     */
    int subqResultSetNumber = cc.getNextResultSetNumber();

    /* We will be reusing the RCL from the subquery's ResultSet for the
     * empty row function.  We need to reset the resultSetNumber in the
     * RCL, before we generate that function.  Now that we've called
     * generate() on the subquery's ResultSet, we can reset that
     * resultSetNumber.
     */
    resultSet.getResultColumns().setResultSetNumber(subqResultSetNumber);

    /* Generate code for empty row */
    resultSet.getResultColumns().generateNulls(acb, mb);

    /*
     *  arg1: suqueryExpress - Expression for subquery's
     *      ResultSet
     *  arg2: Activation
     *  arg3: Method to generate Row with null(s) if subquery
     *      Result Set is empty
     */
    if (subqueryType == EXPRESSION_SUBQUERY)
    {
      int cardinalityCheck;

      /* No need to do sort if subquery began life as a distinct expression subquery.
       * (We simply check for a single unique value at execution time.)
       * No need for cardinality check if we know that underlying
       * ResultSet can contain at most 1 row.
       * RESOLVE - Not necessary if we know we
       * are getting a single row because of a unique index.
       */
      if (distinctExpression)
      {
        cardinalityCheck = OnceResultSet.UNIQUE_CARDINALITY_CHECK;
      }
      else if (resultSet.returnsAtMostOneRow())
      {
        cardinalityCheck = OnceResultSet.NO_CARDINALITY_CHECK;
      }
      else
      {
        cardinalityCheck = OnceResultSet.DO_CARDINALITY_CHECK;
      }

      /*  arg4: int - whether or not cardinality check is required
       *        DO_CARDINALITY_CHECK - required
       *        NO_CARDINALITY_CHECK - not required
       *        UNIQUE_CARDINALITY_CHECK - verify single
       *                      unique value
       */
      mb.push(cardinalityCheck);
      nargs = 8;

    } else {
      nargs = 7;
    }

    mb.push(subqResultSetNumber);
    mb.push(subqueryNumber);
    mb.push(pointOfAttachment);
    mb.push(costEstimate.rowCount());
    mb.push(costEstimate.getEstimatedCost());

    mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, resultSetString, ClassName.NoPutResultSet, nargs);



View Full Code Here

Examples of org.apache.derby.iapi.sql.compile.CostEstimate

    ** strategies like materialization (hash join can work only on
    ** base tables).  The join strategy for a base table under a
    ** ProjectRestrict is set in the base table itself.
    */

    CostEstimate childCost;

    costEstimate = getCostEstimate(optimizer);

    /*
    ** Don't re-optimize a child result set that has already been fully
    ** optimized.  For example, if the child result set is a SelectNode,
    ** it will be changed to a ProjectRestrictNode, which we don't want
    ** to re-optimized.
    */
    // NOTE: TO GET THE RIGHT COST, THE CHILD RESULT MAY HAVE TO BE
    // OPTIMIZED MORE THAN ONCE, BECAUSE THE NUMBER OF OUTER ROWS
    // MAY BE DIFFERENT EACH TIME.
    // if (childResultOptimized)
    //   return costEstimate;

    // It's possible that a call to optimize the left/right will cause
    // a new "truly the best" plan to be stored in the underlying base
    // tables.  If that happens and then we decide to skip that plan
    // (which we might do if the call to "considerCost()" below decides
    // the current path is infeasible or not the best) we need to be
    // able to revert back to the "truly the best" plans that we had
    // saved before we got here.  So with this next call we save the
    // current plans using "this" node as the key.  If needed, we'll
    // then make the call to revert the plans in OptimizerImpl's
    // getNextDecoratedPermutation() method.
    updateBestPlanMap(ADD_PLAN, this);

    /* If the childResult is instanceof Optimizable, then we optimizeIt.
     * Otherwise, we are going into a new query block.  If the new query
     * block has already had its access path modified, then there is
     * nothing to do.  Otherwise, we must begin the optimization process
     * anew on the new query block.
     */
    if (childResult instanceof Optimizable)
    {
      childCost = ((Optimizable) childResult).optimizeIt(
                              optimizer,
                              restrictionList,
                              outerCost,
                              rowOrdering);
      /* Copy child cost to this node's cost */
      costEstimate.setCost(
              childCost.getEstimatedCost(),
              childCost.rowCount(),
              childCost.singleScanRowCount());


      // Note: we don't call "optimizer.considerCost()" here because
      // a) the child will make that call as part of its own
      // "optimizeIt()" work above, and b) the child might have
      // different criteria for "considering" (i.e. rejecting or
      // accepting) a plan's cost than this ProjectRestrictNode does--
      // and we don't want to override the child's decision.  So as
      // with most operations in this class, if the child is an
      // Optimizable, we just let it do its own work and make its
      // own decisions.
    }
    else if ( ! accessPathModified)
    {
      if (SanityManager.DEBUG)
      {
        if (! ((childResult instanceof SelectNode) ||
                 (childResult instanceof RowResultSetNode)))
        {
          SanityManager.THROWASSERT(
            "childResult is expected to be instanceof " +
            "SelectNode or RowResultSetNode - it is a " +
            childResult.getClass().getName());
        }
      }
      childResult = childResult.optimize(optimizer.getDataDictionary(),
                         restrictionList,
                         outerCost.rowCount());

      /* Copy child cost to this node's cost */
      childCost = childResult.costEstimate;

      costEstimate.setCost(
              childCost.getEstimatedCost(),
              childCost.rowCount(),
              childCost.singleScanRowCount());

      /* Note: Prior to the fix for DERBY-781 we had calls here
       * to set the cost estimate for BestAccessPath and
       * BestSortAvoidancePath to equal costEstimate.  That used
       * to be okay because prior to DERBY-781 we would only
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.