N
N
neronru2016-08-06 19:20:10
PostgreSQL
neronru, 2016-08-06 19:20:10

How to make an index for a custom sort?

In general, there is a table items(id,category_id,c1,c2,c3,c4), where id is the primary key, category_id is the category of the item, and there are some parameters (c1,c2,c3,c4), and these parameters have some restrictions: c1 - can take values ​​1 or 2, c2 - from 1 to 100, c3 - from 1 to 100, c4 is a UNIX timestamp. You need to create an index for the following query: SELECT * FROM items WHERE category_id = $1 ORDER BY $2 DESC, $3 DESC, $4 DESC, $5 DESC. Where everything in ORDER BY is parameters c1 to c4, which are defined by the user. By the way, a separate case may be when some fields may be missing, that is, just be ORDER BY $2 DESC, $3 DESC. I don’t know how to make an index for this, this is actually the question. It is possible to implement this outside of SQL.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2016-08-06
@bingo347

Hang a common index on all 4 fields

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question