Home:ALL Converter>Very slow queries in a large database even with indexes

Very slow queries in a large database even with indexes

Ask Time:2021-05-11T19:15:10         Author:zenno2

Json Formatter

I have a large database with login and level columns. All queries were slow before I created indexes on these columns:

CREATE INDEX users_login_index on users(login);
CREATE INDEX users_level_index on users(level);

Now I can quickly find a user by login (it's unique) or many users by level. This query is fast:

SELECT * FROM users WHERE login='somelogin123';

But this query is very slow:

SELECT * FROM users WHERE login='somelogin123' AND level=1;

Why the query becomes slow after adding an additional condition which is also indexed? How to solve my problem?

Author:zenno2,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67485646/very-slow-queries-in-a-large-database-even-with-indexes
yy