Hi Gerrit,
I suspect that your query performs poorly because your indexes do not
cover the query. That means that you are selecting columns which don't
appear in the indexes. In this case, the optimizer knows that Derby
cannot satisfy the query by simply reading index pages. Derby also has
to read base table pages in order to fetch the other columns. Please see
http://db.apache.org/derby/docs/10.15/tuning/ctunoptimz30768.html
Derby query plans are not easy to read. If you are snapshotting plans
with XPLAIN style, then the details go into snapshot tables. In theory,
you could write a tool to read those snapshot results and format them
better.
Hope this helps,
-Rick
On 7/6/20 12:46 AM, Hohl, Gerrit wrote:
Hello everyone,
me again.
In the meantime I tried to tune the table using the
SYSCS_UTIL.SYSCS_COMPRESS_TABLE
and
SYSCS_UTIL.SYSCS_UPDATE_STATISTICS
functions. It didn't have any effect on the query runtime.
I also tried the query analyse functions:
CALL SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1);
CALL SYSCS_UTIL.SYSCS_SET_STATISTICS_TIMING(1);
<Query>
VALUES SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS();
CALL SYSCS_UTIL.SYSCS_SET_STATISTICS_TIMING(0);
CALL SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(0);
The result was long, localized (German in my case - nice)... and absolutely
cryptic.
It didn't mention any of the tokens of my SQL query.
Instead it was taking about UNIONs and JOINs, also I don't use any. At least
not explicitly.
I couldn't figure at all which entry of the analysis belonged to which part of
the SQL query.
I remember when I worked with PostgreSQL there was that neat EXPLAIN command.
That command had a very good structure of its output.
And it seems they still have it in their current version:
https://www.postgresql.org/docs/current/using-explain.html
Regards,
Gerrit
-----Ursprüngliche Nachricht-----
Von: Hohl, Gerrit <g.h...@aurenz.de>
Gesendet: Freitag, 3. Juli 2020 16:22
An: Derby Discussion <derby-user@db.apache.org>
Betreff: Questions about query execution and optimization
Hello everyone,
this week I came across a behaviour of Apache Derby which I couldn't explain to
myself.
I'm using version 10.14.2.0 and the structure of the database looks like this:
CREATE TABLE license (
id BIGINT NOT NULL,
[...]
);
CREATE TABLE installation (
id BIGINT NOT NULL,
[...]
license_id BIGINT NOT NULL,
[...]
);
CREATE TABLE log (
id BIGINT NOT NULL,
action VARCHAR(255) NOT NULL,
create_timestamp TIMESTAMP NOT NULL,
entity_cls VARCHAR(255),
entity_id BIGINT,
type INTEGER NOT NULL,
message VARCHAR(32672) NOT NULL,
PRIMARY KEY (ID)
);
CREATE INDEX logcreatetimestampindex ON log (create_timestamp);
CREATE INDEX logentityclsentityidindex ON log (entity_cls, entity_id);
ALTER TABLE installation ADD CONSTRAINT fkinstallationlicense FOREIGN KEY
(license_id) REFERENCES license (id);
The log table contains log messages about actions on records in other tables.
Because it should be useable with all tables, there can't be any foreign keys.
Especially as also logs should be kept when the corresponding record is deleted.
Now I wanted to create a query which returns all logs of a certain license and
its installations:
SELECT l.*
FROM log l
WHERE ((l.entity_cls = 'License') AND (l.entity_id = ?))
OR ((l.entity_cls = 'Installation) AND (l.entity_id IN (
SELECT i.id FROM installation i WHERE (i.license_id = ?)
)))
ORDER BY l.create_timestamp DESC, l.id DESC;
But that thing took forever (~ 12.5s).
I thought the reason would maybe my index, so I introduced two more:
CREATE INDEX logentityidindex ON log (entity_cls);
CREATE INDEX logentityclsindex ON log (entity_id);
Unfortunately that didn't change much. The query took almost the exact same
amount of time.
Next thing was removing the sub-select and directly giving a list of IDs for
the installation records - just for testing.
SELECT l.*
FROM log l
WHERE ((l.entity_cls = 'License') AND (l.entity_id = 123))
OR ((l.entity_cls = 'Installation) AND (l.entity_id IN (234, 345)))
ORDER BY l.create_timestamp DESC, l.id DESC;
Much to my amazement that also didn't change anything.
In black despair I split the query into two while keeping the sub-select:
SELECT l.*
FROM log l
WHERE ((l.entity_cls = 'License') AND (l.entity_id = ?))
ORDER BY l.create_timestamp DESC, l.id DESC;
SELECT l.*
FROM log l
WHERE ((l.entity_cls = 'Installation) AND (l.entity_id IN (
SELECT i.id FROM installation.id WHERE (l.license_id = ?)
)))
ORDER BY l.create_timestamp DESC, l.id DESC;
Unbelievable: The 1st query took ~0.15s while the 2nd query took ~0.2s.
As the sub-select seems not to have any effect how fast or slow the query is,
that can be ignored.
In all cases I only have columns in my WHERE clause which are in the indexes.
But somehow Apache Derby seems not to notice it and not using them, if the
WHERE clause gets too complicated.
Is there anything I haven't seen? Anything I'm doing wrong by structuring the
query like I did?
Somehow I don't get it why the execution time gets 100 times slower just by
having everything in one query.
Regards,
Gerrit