[Solved]-Poor performance of Django ORM with Oracle

1👍

After working with our DBAs, it turned out that for some reason the Django get(upi='xxxxxxxxxxxx') queries didn’t use the database index.

When the same query was rewritten using filter(upi='xxxxxxxxxxxx')[:1].get(), the query was fast.

The get query was fast only with integer primary keys (it was string in the original question).

FINAL SOLUTION

create index index_name on Mytable(SYS_OP_C2C(upi));

There seems to be some mismatch between the character sets used by cx_Oracle and Oracle. Adding the C2C index fixes the problem.

UPDATE:
Also, switching to NVARCHAR2 from VARCHAR2 in Oracle has the same effect and can be used instead of the functional index.

Here are some useful discussion threads that helped me:
http://comments.gmane.org/gmane.comp.python.db.cx-oracle/3049
http://comments.gmane.org/gmane.comp.python.db.cx-oracle/2940

2👍

Using TO_CHAR(character) should solve the performance issue:

cursor.execute("SELECT * FROM mytable WHERE upi = TO_CHAR(%s)", ['xxxxxxxxxxxxx'])

Leave a comment