# RewriteQueriesToDropUnusedColumns

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

*** ** * ** ***


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

<br />

*** ** * ** ***

When present, `https://developer.android.com/reference/androidx/room3/RewriteQueriesToDropUnusedColumns` annotation will cause Room to rewrite your `https://developer.android.com/reference/androidx/room3/Query` functions such that only the columns that are used in the return type 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 data object class, you could write the query like this:

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

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

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

```
@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 `https://developer.android.com/reference/androidx/room3/Dao` function annotated with `https://developer.android.com/reference/androidx/room3/Query`, it will only affect that query. You can put the annotation on the `https://developer.android.com/reference/androidx/room3/Dao` annotated class / interface or the `https://developer.android.com/reference/androidx/room3/Database` annotated class where it will impact all functions 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.