Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLalchemy specify which index to use

Is there a way in SQLalchemy to tell the query which index to use?

The reason I need this is that the SQL queries it generates use the "wrong" index - there exists an index for exactly the two fields that I have and it doesn't use it.

Thanks!

like image 242
vlyubin Avatar asked Oct 05 '12 01:10

vlyubin


People also ask

What is index in SQLAlchemy?

SQLAlchemy Index is used for assigning the identifiers for each of the particular row getting stored inside a table. We can have indexing based on the single column or collection of two or more columns together acting as an index to the table rows.

Does SQLAlchemy require primary key?

¶ The SQLAlchemy ORM, in order to map to a particular table, needs there to be at least one column denoted as a primary key column; multiple-column, i.e. composite, primary keys are of course entirely feasible as well.

Is SQLAlchemy faster than sqlite?

Interesting to note that querying using bare sqlite3 is still about 3 times faster than using SQLAlchemy Core.

What is foreign key in SQLAlchemy?

A foreign key in SQL is a table-level construct that constrains one or more columns in that table to only allow values that are present in a different set of columns, typically but not always located on a different table.


1 Answers

I think you can use with_hint() for this.

e.g.

session.query(Model).with_hint(Model, 'USE INDEX col1_index')

Honestly, I didn't really know about this; I discovered it by finding 'USE INDEX' in their ORM tests.

like image 170
Anuj Gupta Avatar answered Oct 08 '22 10:10

Anuj Gupta