MyModel.objects.all().order_by('-timestamp')
There is a unique key on timestamp and another column:
`code` varchar(3) NOT NULL,
`timestamp` date NOT NULL,
UNIQUE KEY `exchangerate_currency_70883b95_uniq` (`code`,`timestamp`)
All I want is to obtain the latest row in the table.
The query achieves that but I am thinking of the future when it will grow to 100K rows.
Are there glaring performance problems with this query & schema ?
Without seeing your full query, and your schema, it’s impossible to do more than speculate.
But… I believe the order of columns in a multi-column index in MySQL is important. This would mean that your index on (code,timestamp) is likely unusable for ordering by timestamp. If you changed the order to instead be (timestamp,code), it probably would be useable for ORDER BY timestamp, however it may hurt performance for other queries.
If your usage requires an index for both, you may need to create a second index on just the timestamp column.