On 15/02/2021 21:19, Rick Hillegas wrote:
It looks like the subquery isn't doing any good. I would have hoped that the optimizer would be smart enough to join the subquery result to the primary key on id and then use the resulting row positions to probe into the heap. But the plan is just joining the subquery result to a full table scan. I don't understand why the subquery plan is so convoluted. However, it looks like you eliminated the memory-exhausting sort.

Here for comparison is the result of the original query, which takes about the same time despite requiring a sort. I still don't understand why the descending index on time is being ignored:

From "Tuning Derby" (https://db.apache.org/derby/docs/10.8/tuning/ctunoptimz56859.html): "Usually, sorting requires an extra step to put the data into the right order. This extra step can be avoided for data that are already in the right order. *For example, if a single-table query has an ORDER BY on a single column, and there is an index on that column*, sorting can be avoided if Derby uses the index as the access path."

The query below seems to me to satisfy that requirement.

Statement Name:
    null
Statement Text:
    SELECT DateTimeFormat(t_time,null) AS t_time,
       facility,event,details,name,username,sector,item
    FROM system_log_view
    ORDER BY time DESC
    NULLS LAST
    FETCH NEXT 20 ROWS ONLY
Parse Time: 0
Bind Time: 0
Optimize Time: 0
Generate Time: 0
Compile Time: 0
Execute Time: 0
Begin Compilation Timestamp : null
End Compilation Timestamp : null
Begin Execution Timestamp : null
End Execution Timestamp : null
Statement Execution Plan Text:
Scroll Insensitive ResultSet:
Number of opens = 1
Rows seen = 20
Number of reads from hash table = 20
Number of writes to hash table = 20
    constructor time (milliseconds) = 0
    open time (milliseconds) = 0
    next time (milliseconds) = 0
    close time (milliseconds) = 0
    optimizer estimated row count: 261301.00
    optimizer estimated cost: 269004.12
Source result set:
    Row Count (1):
    Number of opens = 1
    Rows seen = 20
    Rows filtered = 0
        constructor time (milliseconds) = 0
        open time (milliseconds) = 0
        next time (milliseconds) = 0
        close time (milliseconds) = 0
        optimizer estimated row count: 261301.00
        optimizer estimated cost: 269004.12
    Source result set:
        Project-Restrict ResultSet (5):
        Number of opens = 1
        Rows seen = 20
        Rows filtered = 0
        restriction = false
        projection = true
            constructor time (milliseconds) = 0
            open time (milliseconds) = 0
            next time (milliseconds) = 0
            close time (milliseconds) = 0
            restriction time (milliseconds) = 0
            projection time (milliseconds) = 0
            optimizer estimated row count: 261301.00
            optimizer estimated cost: 269004.12
        Source result set:
            Sort ResultSet:
            Number of opens = 1
            Rows input = 261297
            Rows returned = 20
            Eliminate duplicates = false
            In sorted order = false
            Sort information:
                Number of merge runs=18
                Number of rows input=261297
                Number of rows output=261297
Size of merge runs=[14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335, 14335]
                Sort type=external
                constructor time (milliseconds) = 0
                open time (milliseconds) = 0
                next time (milliseconds) = 0
                close time (milliseconds) = 0
                optimizer estimated row count: 261301.00
                optimizer estimated cost: 269004.12
            Source result set:
                Project-Restrict ResultSet (3):
                Number of opens = 1
                Rows seen = 261297
                Rows filtered = 0
                restriction = false
                projection = true
                    constructor time (milliseconds) = 0
                    open time (milliseconds) = 0
                    next time (milliseconds) = 0
                    close time (milliseconds) = 0
                    restriction time (milliseconds) = 0
                    projection time (milliseconds) = 0
                    optimizer estimated row count: 261301.00
                    optimizer estimated cost: 269004.12
                Source result set:
Table Scan ResultSet for SYSTEM_LOG at read uncommitted isolation level using share row locking chosen by the optimizer
                    Number of opens = 1
                    Rows seen = 261297
                    Rows filtered = 0
                    Fetch Size = 1
                        constructor time (milliseconds) = 0
                        open time (milliseconds) = 0
                        next time (milliseconds) = 0
                        close time (milliseconds) = 0
                        next time in milliseconds/row = 0

                    scan information:
                        Bit set of columns fetched={1, 2, 3, 4, 5, 6, 7, 8}
                        Number of columns fetched=8
                        Number of pages visited=1791
                        Number of rows qualified=261297
                        Number of rows visited=261483
                        Scan type=heap
                        start position:
                            null
                        stop position:
                            null
                        qualifiers:
                            None
                        optimizer estimated row count: 261301.00
                        optimizer estimated cost: 269004.12

--
John English

Reply via email to