Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Access asyncpg Record field by key on raw query#207

Merged
vmarkovtsev merged 1 commit intoencode:masterfrom
brianpkennedy:asyncpg-raw-query-get-item
May 12, 2020
Merged

Access asyncpg Record field by key on raw query#207
vmarkovtsev merged 1 commit intoencode:masterfrom
brianpkennedy:asyncpg-raw-query-get-item

Conversation

@brianpkennedy
Copy link
Copy Markdown
Contributor

I am using postgresql (asyncpg), and have a result set of about 5,000 records with 12 columns.

When serializing the result set to another type I noticed that the step of reading the results from databases.backends.postgres.Record step was a bit of a bottleneck.

After digging into the code I noticed that when there is no column map, the code was taking the asyncpg.Record object and turning its keys into a tuple and then doing an index lookup based on the key. I'm unclear on if this step is necessary, because you can access the fields on the asyncpg.Record by key directly via a dict lookup.

After applying this change, the amount of time to serialize the result set I was working with was reduced rather dramatically (between 40-60%). I ran the unit tests and saw that they all past, but am happy to add others if there are cases you would like.

@vmarkovtsev
Copy link
Copy Markdown
Contributor

Interesting. This line was introduced in 5fdf35a by @pvanliefland I wonder if there is some secret hidden sense in it... Maybe asyncpg did not support string lookups back then. The tests cover the raw query case so LGTM.

@vmarkovtsev vmarkovtsev merged commit 45519d7 into encode:master May 12, 2020
@brianpkennedy brianpkennedy deleted the asyncpg-raw-query-get-item branch May 14, 2020 13:19
vmarkovtsev added a commit to vmarkovtsev/databases that referenced this pull request Oct 19, 2020
Changelog:

- Use backend native fetch_val() implementation when available (encode#132)
- Replace psycopg2-binary with psycopg2 (encode#198) (encode#204)
- Speed up PostgresConnection fetch() and iterate() (encode#193)
- Access asyncpg Record field by key on raw query (encode#207)
- Fix type hinting for sqlite backend (encode#227)
- Allow setting min_size and max_size in postgres DSN (encode#210)
- Add option pool_recycle in postgres DSN (encode#233)
- Fix SQLAlchemy DDL statements (encode#226)
- Make fetch_val call fetch_one for type conversion (encode#246)
- Allow extra transaction options (encode#242)
- Unquote username and password in DatabaseURL (encode#248)
vmarkovtsev added a commit to vmarkovtsev/databases that referenced this pull request Oct 19, 2020
Changelog:

- Use backend native fetch_val() implementation when available (encode#132)
- Replace psycopg2-binary with psycopg2 (encode#198) (encode#204)
- Speed up PostgresConnection fetch() and iterate() (encode#193)
- Access asyncpg Record field by key on raw query (encode#207)
- Fix type hinting for sqlite backend (encode#227)
- Allow setting min_size and max_size in postgres DSN (encode#210)
- Add option pool_recycle in postgres DSN (encode#233)
- Fix SQLAlchemy DDL statements (encode#226)
- Make fetch_val call fetch_one for type conversion (encode#246)
- Allow extra transaction options (encode#242)
- Unquote username and password in DatabaseURL (encode#248)
@vmarkovtsev vmarkovtsev mentioned this pull request Oct 19, 2020
vmarkovtsev added a commit to vmarkovtsev/databases that referenced this pull request Oct 20, 2020
Changelog:

- Use backend native fetch_val() implementation when available (encode#132)
- Replace psycopg2-binary with psycopg2 (encode#198) (encode#204)
- Speed up PostgresConnection fetch() and iterate() (encode#193)
- Access asyncpg Record field by key on raw query (encode#207)
- Fix type hinting for sqlite backend (encode#227)
- Allow setting min_size and max_size in postgres DSN (encode#210)
- Add option pool_recycle in postgres DSN (encode#233)
- Fix SQLAlchemy DDL statements (encode#226)
- Make fetch_val call fetch_one for type conversion (encode#246)
- Allow extra transaction options (encode#242)
- Unquote username and password in DatabaseURL (encode#248)
vmarkovtsev added a commit to vmarkovtsev/databases that referenced this pull request Oct 20, 2020
Changelog:

- Use backend native fetch_val() implementation when available (encode#132)
- Replace psycopg2-binary with psycopg2 (encode#198) (encode#204)
- Speed up PostgresConnection fetch() and iterate() (encode#193)
- Access asyncpg Record field by key on raw query (encode#207)
- Fix type hinting for sqlite backend (encode#227)
- Allow setting min_size and max_size in postgres DSN (encode#210)
- Add option pool_recycle in postgres DSN (encode#233)
- Fix SQLAlchemy DDL statements (encode#226)
- Make fetch_val call fetch_one for type conversion (encode#246)
- Allow extra transaction options (encode#242)
- Unquote username and password in DatabaseURL (encode#248)
vmarkovtsev added a commit that referenced this pull request Oct 20, 2020
Changelog:

- Use backend native fetch_val() implementation when available (#132)
- Replace psycopg2-binary with psycopg2 (#198) (#204)
- Speed up PostgresConnection fetch() and iterate() (#193)
- Access asyncpg Record field by key on raw query (#207)
- Fix type hinting for sqlite backend (#227)
- Allow setting min_size and max_size in postgres DSN (#210)
- Add option pool_recycle in postgres DSN (#233)
- Fix SQLAlchemy DDL statements (#226)
- Make fetch_val call fetch_one for type conversion (#246)
- Allow extra transaction options (#242)
- Unquote username and password in DatabaseURL (#248)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants