VoidMain
VoidMain

Reputation: 155

SQLAlchemy Faster Than Psycopg2. What's Wrong with My Benchmark?

First, I create a table with 5 columns:

CREATE TABLE table1
(
  a integer,
  b character varying,
  c character varying,
  d numeric,
  e numeric
)

Second, I add 100k rows to a table

https://gist.github.com/ericsalim/1d12628826195b52c5d282c2326f5e00

Third, I select all the rows using Psycopg2 and SQLAlchemy

The result for Psycopg2

The result for SQLAlchemy:

How is it possible that SQLAlchemy, which is an ORM layer on top Psycopg2 is faster than Psycopg2 itself? Is there something wrong with my code?

Upvotes: 1

Views: 2492

Answers (1)

bimsapi
bimsapi

Reputation: 5065

It's the overhead of the DictCursor. Switching it to use a standard cursor and integer indices results in psycopg2 outperforming. I imagine SQLAlchemy uses a different, more efficient method for supporting string indexes. But other than academic curiosity, I would not worry about the performance difference between the two - focus on which is more productive for you. In most real-world scenarios, you will be I/O bound, and the difference is small enough to not matter.

Upvotes: 5

Related Questions