question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing Index on diagnosis_key.submission_timestamp

See original GitHub issue

Disclaimer: I assume, since this application is not complete, there will change a couple of things and maybe this was already planned. This issues it at least meant as a reminder.

Current Implementation

The current Flyway migration script db/migration/postgres/V1__createTables.sql creates only the table diagnosis_key itself.

There are already two data base accesses to the column submission_timestamp in DiagnosisKeyService:

  public List<DiagnosisKey> getDiagnosisKeys() {
    return keyRepository.findAll(Sort.by(Sort.Direction.ASC, "submissionTimestamp"));
  }

and

  public void applyRetentionPolicy(int daysToRetain) {
    long threshold = LocalDateTime
        .ofInstant(Instant.now(), UTC)
        .minusDays(daysToRetain)
        .toEpochSecond(UTC) / 3600L;
    keyRepository.deleteBySubmissionTimestampIsLessThanEqual(threshold);
  }

Suggested Enhancement

Depending on the expected number of entries in this table (I assume it will be a lot), this will result in very slow full table scans. There should be an index added to the column submission_timestamp.

Expected Benefits

  • Much faster data base access on high load and high number of db entries.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
christian-kirschnickcommented, Jun 22, 2020

Yeah, the TestDataGeneration class would need to be changed accordingly.

I just ran the retention for ~250k keys and I guess we should add the index:

2020-06-22T14:27:57+0200 INFO  main a.c.s.c.p.s.DiagnosisKeyService[74261]: Deleted 244717 diagnosis key(s) with a submission timestamp older than 14 day(s) ago. 
2020-06-22T14:32:45+0200 DEBUG main a.c.s.s.d.r.RetentionPolicy[74261]: Retention policy applied successfully. 

The first logging statement is misleading, as it is actually not reporting the end of the deletion task - so the actual deletion happens between both statements. That delete took 5 minutes on my machine, which is far too long.

1reaction
michael-burwigcommented, May 20, 2020

Hi Jan!

I also agree that the index will most likely make sense here.

As for the anticipated performance gains wrt full table scans I am not 100% sure whether the optimizer would care much for the index. See Postgres Documentation - 11.4. Indexes and ORDER BY: …For a query that requires scanning a large fraction of the table, the explicit sort is likely to be faster because it requires less disk I/O due to a better-ordered access pattern. Indexes are more useful when only a few rows need be fetched…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tune nonclustered indexes with missing index suggestions
The missing indexes feature is a lightweight tool for finding missing indexes that might significantly improve query performance.
Read more >
SQL Compare reports missing index when index exists on ...
Schema Compare is reporting a missing index on table when index already exists on the database. CREATE NONCLUSTERED INDEX [IX_PaymentGateWayLog_OrderID_Created]
Read more >
Show Missing Indexes when rendering query plan · Issue #3491
When rendering a SQL Server query plan the Plan visualizer does not show information about missing indexes, even if it is included in...
Read more >
Do Disabled Indexes Affect Missing Index Recommendations?
It does! SQL Server asks for the index. Sure, the index exists – it even has the same name – but it's currently...
Read more >
Index Analysis (including Missing Indexes) - Feature Requests
Since databasses report missing index data, it would be nice to see this in DPA so we can associate the largest wait time...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found