Sobes.tech
Middle

There is a table with fields: user ID, gender, and age. It is often required to select users by gender and age. What index is better to create?

sobes.tech AI

Answer from AI

For frequent user queries by gender and age, it is better to create a composite index on the "gender" and "age" fields. Such an index will efficiently filter records first by gender, then by age, which speeds up queries with WHERE gender = ? AND age = ?.

Example of creating an index in SQL:

CREATE INDEX idx_gender_age ON users(gender, age);

If queries often filter only by age without considering gender, a separate index on age can be considered, but usually a composite index is more effective for combined conditions.

There is a table with fields: user ID, gender, and… - sobes.tech