# RewriteQueriesToDropUnusedColumns

# RewriteQueriesToDropUnusedColumns

Artifact:[androidx.room:room-common](https://developer.android.com/jetpack/androidx/releases/room)  
[View Source](https://cs.android.com/search?q=file:androidx/room/RewriteQueriesToDropUnusedColumns.kt+class:androidx.room.RewriteQueriesToDropUnusedColumns)  
Added in[2.3.0](https://developer.android.com/jetpack/androidx/releases/room#2.3.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/room/RewriteQueriesToDropUnusedColumns "View this page in Java")
Common/AllAndroid/JVMNative/C/iOSJavaScript
  - Cmn  

  ```
  @Target(allowedTargets = [AnnotationTarget.FUNCTION, AnnotationTarget.CLASS])
  @Retention(value = AnnotationRetention.BINARY)
  annotation RewriteQueriesToDropUnusedColumns
  ```

*** ** * ** ***

When present,[RewriteQueriesToDropUnusedColumns](https://developer.android.com/reference/kotlin/androidx/room/RewriteQueriesToDropUnusedColumns)annotation will cause Room to rewrite your[Query](https://developer.android.com/reference/kotlin/androidx/room/Query)methods such that only the columns that are used in the response are queried from the database.

This annotation is useful if you don't need all columns returned in a query but also don't want to spell out their names in the query projection.

For example, if you have a`User`class with 10 fields and want to return only the`name`and`lastName`fields in a POJO, you could write the query like this:  

```kotlin
@Dao
interface MyDao {
    @Query("SELECT * FROM User")
    public fun getAll(): List<NameAndLastName>
}

data class NameAndLastName (
    val name: String,
    val lastName: String
)
```

Normally, Room would print a[RoomWarnings.QUERY_MISMATCH](https://developer.android.com/reference/kotlin/androidx/room/RoomWarnings#QUERY_MISMATCH())warning since the query result has additional columns that are not used in the response. You can annotate the method with[RewriteQueriesToDropUnusedColumns](https://developer.android.com/reference/kotlin/androidx/room/RewriteQueriesToDropUnusedColumns)to inform Room to rewrite your query at compile time to avoid fetching extra columns.  

```kotlin
@Dao
interface MyDao {
    @RewriteQueriesToDropUnusedColumns
    @Query("SELECT * FROM User")
    fun getAll(): List<NameAndLastName>
}
```

At compile time, Room will convert this query to`SELECT name, lastName FROM (SELECT * FROM User)`which gets flattened by**Sqlite** to`SELECT name, lastName FROM User`.

When the annotation is used on a[Dao](https://developer.android.com/reference/kotlin/androidx/room/Dao)method annotated with[Query](https://developer.android.com/reference/kotlin/androidx/room/Query), it will only affect that query. You can put the annotation on the[Dao](https://developer.android.com/reference/kotlin/androidx/room/Dao)annotated class/interface or the[Database](https://developer.android.com/reference/kotlin/androidx/room/Database)annotated class where it will impact all methods in the dao / database respectively.

Note that Room will not rewrite the query if it has multiple columns that have the same name as it does not yet have a way to distinguish which one is necessary.

## Summary

|                                                                           ### Public constructors                                                                           |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| [RewriteQueriesToDropUnusedColumns](https://developer.android.com/reference/kotlin/androidx/room/RewriteQueriesToDropUnusedColumns#RewriteQueriesToDropUnusedColumns())`()` | Cmn |

## Public constructors

### RewriteQueriesToDropUnusedColumns

Cmn  

```
RewriteQueriesToDropUnusedColumns()
```