SQLite SELECT with maximum () efficiency

I have a table with 1.5 million rows and three columns. The "timestamp" column is of type REAL and is indexed. I am accessing a SQLite database through PHP PDO.

The following three selections are made in less than a millisecond:

select timestamp from trades
select timestamp + 1 from trades
select max(timestamp) from trades

The following selection requires almost half a second:

select max(timestamp) + 1 from trades

Why is this?

EDIT: Lasse asked to “clarify the query plan”, I ran it in a PHP PDO query, because at the moment I do not have direct access to SQLite3 command line tools. I think it doesn’t matter, here is the result:

explain query plan select max(timestamp) + 1 from trades:
    [selectid] => 0
    [order] => 0
    [from] => 0
    [detail] => SCAN TABLE trades (~1000000 rows)

explain query plan select max(timestamp) from trades:
    [selectid] => 0
    [order] => 0
    [from] => 0
    [detail] => SEARCH TABLE trades USING COVERING INDEX tradesTimestampIdx (~1 rows)
+4
source share
1 answer

Reason for this request

select max(timestamp) + 1 from trades

, MAX, . MAX , , .

select timestamp + 1 from trades

, .

select max(timestamp) from trades

, .

SQLite:

, MIN() MAX(), , , .

, , , SELECT MAX(x)+1 FROM table , x .

+4

Source: https://habr.com/ru/post/1606374/


All Articles