skip to Main Content

I have a table called users with a couple dozen columns such as height, weight, city, state, country, age, gender etc…

The only keys/indices on the table are for the columns id and email.

I have a search feature on my website that filters users based on these various columns. The query could contain anywhere from zero to a few dozen different where clauses (such as where `age` > 40).

The search is set to LIMIT 50 and ORDER BY `id`.

There are about 100k rows in the database right now.

If I perform a search with zero filters or loose filters, mysql basically just returns the first 50 rows and doesn’t have to read very many more rows than that. It usually takes less than 1 second to complete this type of query.

If I create a search with a lot of complex filters (for instance, 5+ where clauses), MySQL ends up reading through the entire database of 100k rows, trying to accumulate 50 valid rows, and the resulting query takes about 30 seconds.

How can I more efficiently query to improve the response time?

I am open to using caching (I already use Redis for other caching purposes, but I don’t know where to start with properly caching a MySQL table).

I am open to adding indices, although there are a lot of different combinations of where clauses that can be built. Also, several of the columns are JSON where I am searching for rows that contain certain elements. To my knowledge I don’t think an index is a viable solution for that type of query.

I am using MySQL version 8.0.15.

2

Answers


  1. Chosen as BEST ANSWER

    Per the responses in the comments from ysth and Paul, the problem was just the server capacity. After upgrading the an 8GB RAM server, to query times dropped to under 1s.


  2. In general you need to create indexes for the columns which are mentioned in the criteria of the WHERE clauses. And you can also create indexes for JSON columns, use generated column index: https://dev.mysql.com/doc/refman/8.0/en/create-table-secondary-indexes.html.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search