1

I installed MySQL Tuner and this was the result of the report:

  • Enable the slow query log to troubleshoot bad queries
  • Adjust your join queries to always utilize indexes

So, i enabled the slow query log and it reported these kind of queries:

SELECT u.PIN,u.name,u.lastname,u.id_country,t1.id_user_list,COUNT(t2.id_user_list) AS q1 FROM contact AS t1, contact AS t2, user AS u WHERE t1.id_user=N AND t2.id_user = t1.id_user_list AND u.id_user = t1.id_user_list GROUP BY t1.id_user_list ORDER BY name,lastname

And...

SELECT * FROM user WHERE city LIKE 'S' AND gender = N AND flg_pic = N AND flg_hidden = N AND flg_ban = N AND id_user != N ORDER BY id_user DESC LIMIT N,N

I just need some help regarding the optimization of my tables, which are:

  1. contact
    • id_contact (int / primary index)
    • id_user (int / index)
    • id_user_list (int / index)
  2. user
    • id_user (int / primary index)
    • name (varchar)
    • lastname (varchar)
    • PIN (varchar)
    • id_country (int)
    • city (varchar)
    • gender (1 or 2)
    • flg_pic (1 or 0)
    • flg_hidden (1 or 0)
    • flg_ban (1 or 0)

Should i index all varchars that are used in the queries (WHERE and ORDER) as well? Thanks for your help.

1 Answer 1

2

you should run explain analyze to get a better idea of what the most expensive parts of your query are, and index or optimize around those fields.

eg:

explain analyze SELECT * FROM user WHERE city LIKE 'S' AND gender = N AND flg_pic = N AND flg_hidden = N AND flg_ban = N AND id_user != N ORDER BY id_user DESC LIMIT N,N

and you should prioritize those slow queries that run the most often and the most slowly

Sign up to request clarification or add additional context in comments.

1 Comment

I think you just want EXPLAIN, not EXPLAIN ANALYZE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.