I have this table structure:
CREATE TABLE users
(
uid bigint NOT NULL,
first_name character varying,
last_name character varying,
email character varying,
login_count integer,
CONSTRAINT users_pkey PRIMARY KEY (uid)
)
with this index:
CREATE INDEX users__login_count
ON users
USING btree
(login_count DESC NULLS LAST);
The login_count column may consists of NULL values and i need to select all users ordered descending by login_count and need NULLs to be at the end.
Unfortunately this query:
SELECT * FROM users ORDER BY login_count DESC LIMIT 30;
won’t use the index, so NULLs are at the beggining, why?
Your query is effectively
ORDER BY login_count DESCNULLS FIRSTLIMIT 30as explained here. On this page it describes how an index can satisfy an ordering:So your index is the same – it can satisfy
ASC NULLS FIRSTandDESC NULLS LAST, but your query isDESC NULLS FIRST.