Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete all method for entities with @EmbeddedId does not use IN for better performance #373

Open
4 tasks done
ckosmowski opened this issue Jan 28, 2020 · 6 comments
Open
4 tasks done
Labels
type: improvement A minor improvement to an existing feature

Comments

@ckosmowski
Copy link
Contributor

ckosmowski commented Jan 28, 2020

  • Steps to reproduce provided
  • Stacktrace (if present) provided not present
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Create a micronaut Application with Entity using a composite ID with @EmbeddedId
  2. Create a corresponding JdbcRepository
  3. Add Data to the database table
  4. Invoke deleteAll(Collection<Entity>) on the Repository to delete some selected Entities

Expected Behaviour

Micronaut data generates and executes one single SQL query using the IN clause in case the selected Dialect supports IN clauses with multiple columns (most do).

Actual Behaviour

Micronaut data interates the provided entities and executes one DELETE statement per entity, this rapidly cuts down performance for large collections of entities.

Environment Information

  • Operating System: Windows 10
  • Micronaut Version: 1.3.0.RC1
  • JDK Version: 1.8

Example Application

The master branch demonstrates the issue (execute the application -> logging will show two executed delete statements).

11:19:04.351 [main] DEBUG io.micronaut.data.query - Executing Query: SELECT book_.`hash`,book_.`name`,book_.`author` FROM `Book` book_
11:19:04.367 [main] DEBUG de.ksmwsk.deleteall.BookDeleter - Found 4 books before deletion
11:19:05.389 [main] DEBUG io.micronaut.data.query - Executing Query: DELETE  FROM `Book`  WHERE (hash = ? AND name = ?) <------------------
11:19:05.392 [main] DEBUG io.micronaut.data.query - Executing Query: DELETE  FROM `Book`  WHERE (hash = ? AND name = ?) <------------------
11:19:05.392 [main] DEBUG io.micronaut.data.query - Executing Query: SELECT book_.`hash`,book_.`name`,book_.`author` FROM `Book` book_
11:19:05.393 [main] DEBUG de.ksmwsk.deleteall.BookDeleter - Found 2 books after deletion

The simple-id branch demonstrates the behaviour with simple (non-embedded) ideas which i would also expect from the version with the EmbeddedId

11:05:28.897 [main] DEBUG io.micronaut.data.query - Executing Query: SELECT book_.`hash`,book_.`name`,book_.`author` FROM `Book` book_
11:05:28.912 [main] DEBUG de.ksmwsk.deleteall.BookDeleter - Found 4 books before deletion
11:05:29.933 [main] DEBUG io.micronaut.data.query - Executing Query: DELETE  FROM `Book`  WHERE (hash IN(?,?)) <----------------------------
11:05:29.938 [main] DEBUG io.micronaut.data.query - Executing Query: SELECT book_.`hash`,book_.`name`,book_.`author` FROM `Book` book_
11:05:29.939 [main] DEBUG de.ksmwsk.deleteall.BookDeleter - Found 2 books after deletion
@ckosmowski ckosmowski changed the title Delete all method for entities with @EmbeddedId does not use IN for bete Delete all method for entities with @EmbeddedId does not use IN for better performance Jan 28, 2020
@graemerocher
Copy link
Contributor

the issue is you can't do an IN across 2 columns (as the case with embedded ID) unless I am missing something. Related post with possible solutions https://stackoverflow.com/questions/1136380/sql-where-in-clause-multiple-columns

@ckosmowski
Copy link
Contributor Author

I'm pretty sure you can. At least ANSI SQL Syntax contains corresponding syntax to express this. I think it is not supported by 100% of all databases but the most common ones seem to have support.

http://sqlfiddle.com/#!9/e69090

select * FROM books
where (id, name) IN (
  ('1','Test1'),
  ('4','Test4')
)

@graemerocher
Copy link
Contributor

Will see what can be done

@graemerocher graemerocher added this to the 1.0.1 milestone Feb 5, 2020
@graemerocher graemerocher added the type: improvement A minor improvement to an existing feature label Mar 17, 2020
@graemerocher graemerocher modified the milestones: 1.0.1, 1.0.2 Mar 18, 2020
@graemerocher graemerocher removed this from the 1.0.2 milestone Mar 31, 2020
@ksawerykarwacki
Copy link

Any plans to implement that? Currently the only way to achieve fast delete is to use native queries manually.

@dstepanov
Copy link
Contributor

I'm planning to improve expandable queries for the next release, I might be able to improve this as well

@ckosmowski
Copy link
Contributor Author

Any Updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

4 participants