-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-8370 SQL Server IN-list Performance Improvement #10335
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7642,6 +7642,41 @@ public void visitInListPredicate(InListPredicate inListPredicate) { | |
| getSqlTuple( listExpression ) | ||
| .getExpressions().get( 0 ); | ||
| } | ||
| else if ( dialect.supportsValuesListForInListExistsEmulation() ) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is only useful for SQL Server specifically, I would actually put this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, will move. I do know this syntax also works with Postgres but is it useful for performance? I don't know. |
||
| if ( inListPredicate.isNegated() ) { | ||
| appendSql( "not " ); | ||
| } | ||
|
|
||
| appendSql( "exists (select 1 from (values " ); | ||
| for ( int i = 0; i < listExpressions.size(); i++ ) { | ||
| if ( i > 0 ) { | ||
| appendSql( ", " ); | ||
| } | ||
| appendSql( OPEN_PARENTHESIS ); | ||
| renderCommaSeparatedSelectExpression( List.of( listExpressions.get( i ) ) ); | ||
| appendSql( CLOSE_PARENTHESIS ); | ||
| } | ||
| appendSql( ") as v(" ); | ||
|
Comment on lines
+7651
to
+7659
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I'm missing something, but why can't we just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to allow for separating potential composite values on their columns, creation of: |
||
|
|
||
| final List<? extends SqlAstNode> expressions = lhsTuple.getExpressions(); | ||
| for ( int i = 0; i < expressions.size(); i++ ) { | ||
| if ( i > 0 ) { | ||
| appendSql( ", " ); | ||
| } | ||
| appendSql( "col_" + i ); | ||
| } | ||
| appendSql( ") where " ); | ||
|
|
||
| for ( int i = 0; i < expressions.size(); i++ ) { | ||
| if ( i > 0 ) { | ||
| appendSql( " and " ); | ||
| } | ||
| expressions.get( i ).accept( this ); | ||
| appendSql( " = v.col_" + i ); | ||
| } | ||
| appendSql( CLOSE_PARENTHESIS ); | ||
| return; | ||
| } | ||
| else if ( !dialect.supportsRowValueConstructorSyntaxInInList() ) { | ||
| final ComparisonOperator comparisonOperator = inListPredicate.isNegated() ? | ||
| ComparisonOperator.NOT_EQUAL : | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.dialect.functional; | ||
|
|
||
| import org.hibernate.cfg.AvailableSettings; | ||
| import org.hibernate.dialect.SQLServerDialect; | ||
| import org.hibernate.orm.test.jpa.CompositeId; | ||
| import org.hibernate.orm.test.jpa.EntityWithCompositeId; | ||
| import org.hibernate.testing.jdbc.SQLStatementInspector; | ||
| import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; | ||
| import org.hibernate.testing.orm.junit.JiraKey; | ||
| import org.hibernate.testing.orm.junit.Jpa; | ||
| import org.hibernate.testing.orm.junit.RequiresDialect; | ||
| import org.hibernate.testing.orm.junit.Setting; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * @author Rob Green | ||
| */ | ||
| @JiraKey( value = "HHH-8370" ) | ||
| @RequiresDialect( value = SQLServerDialect.class, majorVersion = 10 ) | ||
| @Jpa( | ||
| annotatedClasses = { EntityWithCompositeId.class }, | ||
| integrationSettings = { | ||
| @Setting( name = AvailableSettings.USE_SQL_COMMENTS, value = "true" ), | ||
| }, | ||
| useCollectingStatementInspector = true | ||
| ) | ||
| public class SQLServerDialectCompositeTest { | ||
| @Test | ||
| public void testCompositeQueryWithInPredicate(EntityManagerFactoryScope scope) { | ||
| final SQLStatementInspector sqlStatementInterceptor = scope.getCollectingStatementInspector(); | ||
| sqlStatementInterceptor.clear(); | ||
|
|
||
| List<CompositeId> compositeIds = new ArrayList<>(); | ||
| compositeIds.add( new CompositeId( 1,2 ) ); | ||
| compositeIds.add( new CompositeId( 3,4 ) ); | ||
|
|
||
| scope.inTransaction( entityManager -> { | ||
| entityManager.createQuery( "SELECT e FROM EntityWithCompositeId e WHERE e.id IN (:ids)" ) | ||
| .setParameter( "ids", compositeIds ) | ||
| .getResultList(); | ||
| } | ||
| ); | ||
|
|
||
| var query = sqlStatementInterceptor.getSqlQueries().get( 0 ); | ||
| assertTrue( query.endsWith( "where exists (select 1 from (values (?,?), (?,?)) as v(col_0, col_1) where ewci1_0.id1 = v.col_0 and ewci1_0.id2 = v.col_1)" ) ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCompositeQueryWithNotInPredicate(EntityManagerFactoryScope scope) { | ||
| final SQLStatementInspector sqlStatementInterceptor = scope.getCollectingStatementInspector(); | ||
| sqlStatementInterceptor.clear(); | ||
|
|
||
| List<CompositeId> compositeIds = new ArrayList<>(); | ||
| compositeIds.add( new CompositeId( 1,2 ) ); | ||
| compositeIds.add( new CompositeId( 3,4 ) ); | ||
|
|
||
| scope.inTransaction( entityManager -> { | ||
| entityManager.createQuery( "SELECT e FROM EntityWithCompositeId e WHERE e.id NOT IN (:ids)" ) | ||
| .setParameter( "ids", compositeIds ) | ||
| .getResultList(); | ||
| } | ||
| ); | ||
|
|
||
| var query = sqlStatementInterceptor.getSqlQueries().get( 0 ); | ||
| assertTrue( query.endsWith( "where not exists (select 1 from (values (?,?), (?,?)) as v(col_0, col_1) where ewci1_0.id1 = v.col_0 and ewci1_0.id2 = v.col_1)" ) ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCompositeQueryWithMultiplePredicatesIncludingIn(EntityManagerFactoryScope scope) { | ||
| final SQLStatementInspector sqlStatementInterceptor = scope.getCollectingStatementInspector(); | ||
| sqlStatementInterceptor.clear(); | ||
|
|
||
| List<CompositeId> compositeIds = new ArrayList<>(); | ||
| compositeIds.add( new CompositeId( 1,2 ) ); | ||
| compositeIds.add( new CompositeId( 3,4 ) ); | ||
|
|
||
| scope.inTransaction( entityManager -> { | ||
| entityManager.createQuery( "SELECT e FROM EntityWithCompositeId e WHERE e.description = :description AND e.id IN (:ids)" ) | ||
| .setParameter( "ids", compositeIds ) | ||
| .setParameter( "description", "test" ) | ||
| .getResultList(); | ||
| } | ||
| ); | ||
|
|
||
| var query = sqlStatementInterceptor.getSqlQueries().get( 0 ); | ||
| assertTrue( query.endsWith( "where ewci1_0.description=? and exists (select 1 from (values (?,?), (?,?)) as v(col_0, col_1) where ewci1_0.id1 = v.col_0 and ewci1_0.id2 = v.col_1)" ) ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The minimum supported version is already
11.x(SQL Server 2012), so no need for this I think.