Skip to content

Commit

Permalink
Update note on IN in README
Browse files Browse the repository at this point in the history
Give ANY option first, then IN with subquery with a note about
performance.
  • Loading branch information
trim21 authored and tlocke committed Apr 19, 2024
1 parent 7ab7148 commit e80fbb1
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,26 @@ pg8000.exceptions.DatabaseError: ...

```

instead you can write it using the [unnest
the most straightforward way to get around this problem is to rewrie the query using the [`ANY`](
https://www.postgresql.org/docs/current/functions-comparisons.html#FUNCTIONS-COMPARISONS-ANY-SOME)
function:

```python
>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT 'silo 1' WHERE 'a' = ANY(:v)", v=['a', 'b'])
[['silo 1']]
>>> con.close()

```

However, using the array variant of `ANY` [may cause a performance problem](
https://stackoverflow.com/questions/34627026/in-vs-any-operator-in-postgresql/34627688#34627688)
and so you can use the [subquery variant of `IN`](
https://www.postgresql.org/docs/current/functions-subquery.html#FUNCTIONS-SUBQUERY-IN)
with the [unnest
](https://www.postgresql.org/docs/current/functions-array.html) function:

```python
Expand Down

0 comments on commit e80fbb1

Please sign in to comment.