Hello,
I have a ~100000 rows table from which I am doing large queries. I have noticed that object creation is very slow (I use the declarative layer), so for large queries I have taken the habit of querying specific columns rather than the whole classes.
But even then, a code like:
q = db_session.query(Logement.id_groupe, Logement.nom_voie, Logement.cp)
q = q.enable_eagerloads(False)
r = q.all()
ends up 8x as slow (more than 8s. as opposed to a mere 1s.) as the following equivalent code:
r = db_session.execute(select([Logement.nom_voie, Logement.cp](Logement.id_groupe,)))
Yet they both generate the same SQL:
# ORM query
SELECT logement.id_groupe AS logement_id_groupe, logement.nom_voie AS logement_nom_voie, logement.cp AS logement_cp
FROM logement
# ORM-less query
SELECT logement.id_groupe, logement.nom_voie, logement.cp
FROM logement
I am really astonished that there is so much overhead. The query above is not difficult to convert to an ORM-less query, but more complicated ones will be tedious to convert...
(when executed from the MySQL prompt, the query takes 0.47s.)
Hmm, actually my measurements seem wrong, in that the ORM-less query returns an object which yields results lazily. It means that in both cases, it takes 8s. to fetch all results, while under a MySQL prompt the query execution time displayed is 0.47s. The MySQL server runs locally and listens on an Unix socket.