diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java index 7b83efb1d9d..81b748fcc60 100644 --- a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java +++ b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java @@ -306,16 +306,19 @@ static void addIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { // "CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)". // [START query_index] static void queryUsingIndex(DatabaseClient dbClient) { - ResultSet resultSet = - dbClient - .singleUse() - .executeQuery( - // We use FORCE_INDEX hint to specify which index to use. For more details see - // https://cloud.google.com/spanner/docs/query-syntax#from-clause - Statement.of( - "SELECT AlbumId, AlbumTitle, MarketingBudget\n" - + "FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle}\n" - + "WHERE AlbumTitle >= 'Aardvark' AND AlbumTitle < 'Goo'")); + Statement statement = Statement + // We use FORCE_INDEX hint to specify which index to use. For more details see + // https://cloud.google.com/spanner/docs/query-syntax#from-clause + .newBuilder("SELECT AlbumId, AlbumTitle, MarketingBudget\n" + + "FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle}\n" + + "WHERE AlbumTitle >= @StartTitle AND @EndTitle < 'Goo'") + // We use @BoundParameters to help speed up frequently executed queries. + // For more details see https://cloud.google.com/spanner/docs/sql-best-practices + .bind("StartTitle").to("Aardvark") + .bind("EndTitle").to("Goo") + .build(); + + ResultSet resultSet = dbClient.singleUse().executeQuery(statement); while (resultSet.next()) { System.out.printf( "%d %s %s\n",