PostgreSQL: in-table blobs are fetched as hex text, 9–13× slower than MySQL

Đang mở
#1,705 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 5 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
postgresql, python
Lĩnh vực
databases

Hướng nghiên cứu

Start by reading the PostgreSQL adapter and its fetch path for bytea columns; the issue names no source files or tests. Compare the current psycopg2 result handling with psycopg 3 binary results and the binary COPY measurements given here. Done means establishing whether binary fetching can be supported and, if so, confirming large blob fetches preserve their values and improve performance; documenting the storage trade-off is the stated interim option.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

enhancement

Summary

On PostgreSQL, fetching a large in-table blob (bytes, i.e. bytea) costs about 1.2–2 ms per MB, against 0.15–0.25 ms per MB on MySQL. Almost all of it goes to transferring the value as hex text, which is twice its size:

  • psycopg2 receives results in text format;
  • the server encodes every bytea as hex (bytea_output = hex), and the client decodes it again.

Fetching the same bytes in binary format is about 4× faster.

Measurements

These come from the MySQL/PostgreSQL benchmark in datajoint/datajoint-docs#315: DataJoint master at eefe74b4 with #1671, Docker on an Apple M5 Max, warm cache.

(Recording & key).fetch1() with a 4 MB in-table blob, median of 300 calls:

MySQL 8.0 MySQL 8.4 PostgreSQL 16 PostgreSQL 17
DataJoint fetch1() 0.64 ms 0.65 ms 8.35 ms 5.63 ms
the same SQL as a bare driver call 0.60 ms 0.59 ms 8.28 ms 5.47 ms

Thirty rows of 100 KB ((Unit & session).to_dicts()) take 0.68 ms on MySQL and 3.7–5.4 ms on PostgreSQL.

Where one 4 MB value's time goes on PostgreSQL:

Step PostgreSQL 16 PostgreSQL 17
read the value from TOAST storage (get_byte(lfp, 0)) 0.25 ms 0.24 ms
hex-encode it on the server, without sending it (length(encode(lfp, 'hex'))) 7.1 ms 7.0 ms
fetch it with psycopg2 (hex text) 8.0 ms 5.3 ms
fetch the same bytes in binary format (COPY (SELECT lfp …) TO STDOUT (FORMAT binary)) 2.1 ms 0.76 ms

Reading the value is cheap. The hex round trip is the cost, and it is the same whether or not DataJoint is involved: DataJoint's own overhead is about 0.05–0.15 ms per call.

With blobs in a file store (<blob@store>), all four engines take the same time (about 6 ms for 4 MB, spent on the client reading and checksumming the file).

Options

  1. Binary result format for blob columns.
    • psycopg 3 supports binary results per cursor or per query (cursor(binary=True), execute(..., binary=True)).
    • psycopg2 does not support binary results.
    • Moving the PostgreSQL adapter to psycopg 3, at least for fetches that return bytea columns, would remove most of the gap: the binary COPY above suggests about 0.8–2 ms for 4 MB instead of 5–8 ms.
  2. Document the trade-off. Until then, recommend <blob@store> over in-table <blob>/bytes for large arrays that are fetched often on PostgreSQL.

bytea_output = escape is not an alternative: escape format is larger than hex for binary data.

Ngôn ngữ chính
Python
Star
198
Fork
107
Merge trung bình
4 ngày 7 giờ
Pull request đã merge (30 ngày)
12

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của datajoint/datajoint-python

Tất cả issue của datajoint/datajoint-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.