Package com.foundationdb.server.store.statistics

Examples of com.foundationdb.server.store.statistics.IndexStatistics


    protected long getTableRowCountFromStatistics(Table table) {
        // This implementation is only for testing; normally overridden by real server.
        // Return row count (not sample count) from analysis time.
        for (Index index : table.getIndexes()) {
            IndexStatistics istats = getIndexStatistics(index);
            if (istats != null)
                return istats.getRowCount();
        }
        return -1;              // Not analyzed.
    }
View Full Code Here


    public abstract IndexStatistics getIndexStatistics(Index index);

    public void getIndexColumnStatistics(Index index, Index[] indexColumnsIndexes, Histogram[] histograms) {
        List<IndexColumn> allIndexColumns = index.getAllColumns();
        IndexStatistics statsForRequestedIndex = getIndexStatistics(index);
        int nIndexColumns = allIndexColumns.size();
        int nKeyColumns = index.getKeyColumns().size();
        for (int i = 0; i < nIndexColumns; i++) {
            Histogram histogram = null;
            Index indexColumnsIndex = null;
            // Use a histogram of the index itself, if possible.
            if (i < nKeyColumns && statsForRequestedIndex != null) {
                indexColumnsIndex = index;
                histogram = statsForRequestedIndex.getHistogram(i, 1);
            }
            if (histogram == null) {
                indexColumnsIndex = (i == 0) ? index : null;
                // If none, find a TableIndex whose first column is leadingColumn
                IndexStatistics indexStatistics = null;
                Column leadingColumn = allIndexColumns.get(i).getColumn();
                for (TableIndex tableIndex : leadingColumn.getTable().getIndexes()) {
                    if (tableIndex.getKeyColumns().get(0).getColumn() == leadingColumn) {
                        indexStatistics = getIndexStatistics(tableIndex);
                        if (indexStatistics != null) {
                            indexColumnsIndex = tableIndex;
                            histogram = indexStatistics.getHistogram(0, 1);
                            break;
                        }
                        else if (indexColumnsIndex == null) {
                            indexColumnsIndex = tableIndex;
                        }
                    }
                }
                // If none, find a GroupIndex whose first column is leadingColumn
                if (indexStatistics == null) {
                    groupLoop: for (Group group : schema.ais().getGroups().values()) {
                        for (GroupIndex groupIndex : group.getIndexes()) {
                            if (groupIndex.getKeyColumns().get(0).getColumn() == leadingColumn) {
                                indexStatistics = getIndexStatistics(groupIndex);
                                if (indexStatistics != null) {
                                    indexColumnsIndex = groupIndex;
                                    histogram = indexStatistics.getHistogram(0, 1);
                                    break groupLoop;
                                }
                                else if (indexColumnsIndex == null) {
                                    indexColumnsIndex = groupIndex;
                                }
View Full Code Here

        if (mostlyDistinct(histograms)) scaleCount = false;
        // statsCount: Number of rows in the table based on an index of the table, according to index
        //    statistics, which may be stale.
        // rowCount: Approximate number of rows in the table, reasonably up to date.
        long statsCount;
        IndexStatistics stats = tableIndexStatistics(indexedTable, indexColumnsIndexes, histograms);
        if (stats != null) {
            statsCount = stats.getSampledCount();
        }
        else {
            statsCount = rowCount;
            scaleCount = false;
        }
View Full Code Here

    public double conditionsSelectivity(SelectivityConditions conditions) {
        double selectivity = 1.0;
        for (ColumnExpression entry : conditions.getColumns()) {
            Index index = null;
            IndexStatistics indexStatistics = null;
            Column column = entry.getColumn();
            // Find a TableIndex whose first column is leadingColumn
            for (TableIndex tableIndex : column.getTable().getIndexes()) {
                if (!tableIndex.isSpatial() && tableIndex.getKeyColumns().get(0).getColumn() == column) {
                    indexStatistics = getIndexStatistics(tableIndex);
                    if (indexStatistics != null) {
                        index = tableIndex;
                        break;
                    }
                }
            }
            // If none, find a GroupIndex whose first column is leadingColumn
            if (indexStatistics == null) {
                groupLoop: for (Group group : schema.ais().getGroups().values()) {
                    for (GroupIndex groupIndex : group.getIndexes()) {
                        if (!groupIndex.isSpatial() && groupIndex.getKeyColumns().get(0).getColumn() == column) {
                            indexStatistics = getIndexStatistics(groupIndex);
                            if (indexStatistics != null) {
                                index = groupIndex;
                                break groupLoop;
                            }
                        }
                    }
                }
            }
            if (indexStatistics == null) continue;
            ExpressionNode eq = null, ne = null, lo = null, hi = null;
            boolean loInc = false, hiInc = false;
            List<ExpressionNode> in = null;
            for (ConditionExpression cond : conditions.getConditions(entry)) {
                if (cond instanceof ComparisonCondition) {
                    ComparisonCondition ccond = (ComparisonCondition)cond;
                    switch (ccond.getOperation()) {
                    case EQ:
                        eq = ccond.getRight();
                        break;
                    case NE:
                        ne = ccond.getRight();
                        break;
                    case LT:
                        hi = ccond.getRight();
                        hiInc = false;
                        break;
                    case LE:
                        hi = ccond.getRight();
                        hiInc = true;
                        break;
                    case GT:
                        lo = ccond.getRight();
                        loInc = false;
                        break;
                    case GE:
                        lo = ccond.getRight();
                        loInc = true;
                        break;
                    }
                }
                else if (cond instanceof InListCondition) {
                    in = ((InListCondition)cond).getExpressions();
                }
            }
            Histogram histogram = indexStatistics.getHistogram(0, 1);
            if (eq != null) {
                selectivity *= fractionEqual(column, index, histogram, eq);
            }
            else if (ne != null)
                selectivity *= (1.0 - fractionEqual(column, index, histogram, eq));
View Full Code Here

    @Test
    public void test()
    {
        IndexStatisticsService statsService = statsService();
        IndexStatistics stats = statsService.getIndexStatistics(session(), index);
        Histogram histogram;
        List<HistogramEntry> entries;
        // Multi-column histogram on (a)
        histogram = stats.getHistogram(0, 1);
        assertEquals(0, histogram.getFirstColumn());
        assertEquals(1, histogram.getColumnCount());
        assertEquals(A_COUNT, histogram.totalDistinctCount());
        entries = histogram.getEntries();
        int a = 0;
        for (HistogramEntry entry : entries) {
            assertEquals(String.format("{(long)%s}", a++), entry.getKeyString());
            assertEquals(N / A_COUNT, entry.getEqualCount());
            assertEquals(0, entry.getDistinctCount());
            assertEquals(0, entry.getLessCount());
        }
        // Not checking other multi-column entries in detail, because there's nothing new, and they're pretty useless.
        // Single-column histogram on (b)
        histogram = stats.getHistogram(1, 1);
        assertEquals(1, histogram.getFirstColumn());
        assertEquals(1, histogram.getColumnCount());
        assertEquals(B_COUNT, histogram.totalDistinctCount());
        entries = histogram.getEntries();
        int b = 0;
        for (HistogramEntry entry : entries) {
            assertEquals(String.format("{(long)%s}", b++), entry.getKeyString());
            assertEquals(N / B_COUNT, entry.getEqualCount());
            assertEquals(0, entry.getDistinctCount());
            assertEquals(0, entry.getLessCount());
        }
        // Single-column histogram on (c)
        histogram = stats.getHistogram(2, 1);
        assertEquals(2, histogram.getFirstColumn());
        assertEquals(1, histogram.getColumnCount());
        assertEquals(C_COUNT, histogram.totalDistinctCount());
        entries = histogram.getEntries();
        int c = 0;
View Full Code Here

TOP

Related Classes of com.foundationdb.server.store.statistics.IndexStatistics

Copyright © 2018 www.massapicom. 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.