I am migrating a Db from Mysql 8.0.23 to MariaDB 11.3.2 and I realize that some queries needs longer in MariaDB than in Mysql. It is the same query and same DB structure. In MariaDB I need to get a response o this quera 3.5 seconds and in the mysql server only 0.8 seconds. I perform a Explain
and Analyze
command and I saw that the run procedure are different. Because I would proceed to see how many rows are evaluated, and create some indexes but I dont the rows evaluated are similar. How can I optimize the mariaDB db? Have you any hints how to proceed to optimize the db? Thank you in advance!
Best Regards,
Paul
There is the query:
select `q`.*, EXISTS (
SELECT `qe`.* FROM `questions` `qe`
CROSS JOIN `companies_has_users` `cu` ON cu.company_id = 160
LEFT JOIN `answer_groups` `ag` ON ag.question_id = qe.id AND ag.archived = 0
LEFT JOIN `answer_fields` `af` ON af.answer_group_id = ag.id AND af.archived = 0
LEFT JOIN `answers` `a` ON a.answer_field_id = af.id AND a.is_answered = 1 AND a.user_id = cu.user_id AND a.questionnaire_id = 228
LEFT JOIN `answers` `a_2` ON a_2.answer_field_id = af.id AND a_2.is_default = 1
LEFT JOIN `answer_fields` `af_2` ON af_2.id = a.answer_field_id AND af_2.archived = 0
LEFT JOIN `answer_fields` `af_3` ON af_3.id = a_2.answer_field_id AND af_3.archived = 0
LEFT JOIN `questions_has_user_files` `quf` ON quf.question_id = qe.id AND quf.company_id = 160
LEFT JOIN `questionnaires_has_questions` `qhq` ON qhq.question_id = qe.id
WHERE ((`qhq`.`questionnaire_id` = 228) OR (`qhq`.`questionnaire_id` = 149)) AND (`qe`.`archived`=FALSE) AND (qe.id = q.id) AND (`a`.`id` IS NOT NULL)) AS `count_answers`,
EXISTS (SELECT `qe`.* FROM `questions` `qe`
CROSS JOIN `companies_has_users` `cu` ON cu.company_id = 160
LEFT JOIN `answer_groups` `ag` ON ag.question_id = qe.id AND ag.archived = 0
LEFT JOIN `answer_fields` `af` ON af.answer_group_id = ag.id AND af.archived = 0
LEFT JOIN `answers` `a` ON a.answer_field_id = af.id AND a.is_answered = 1 AND a.user_id = cu.user_id AND a.questionnaire_id = 228
LEFT JOIN `answers` `a_2` ON a_2.answer_field_id = af.id AND a_2.is_default = 1
LEFT JOIN `answer_fields` `af_2` ON af_2.id = a.answer_field_id AND af_2.archived = 0
LEFT JOIN `answer_fields` `af_3` ON af_3.id = a_2.answer_field_id AND af_3.archived = 0
LEFT JOIN `questions_has_user_files` `quf` ON quf.question_id = qe.id AND quf.company_id = 160
LEFT JOIN `questionnaires_has_questions` `qhq` ON qhq.question_id = qe.id
WHERE ((`qhq`.`questionnaire_id` = 228) OR (`qhq`.`questionnaire_id` = 149)) AND (`qe`.`archived`=FALSE) AND (qe.id = q.id) AND (`a_2`.`id` IS NOT NULL)) AS `count_default_answers`,
EXISTS (SELECT `qe`.* FROM `questions` `qe`
CROSS JOIN `companies_has_users` `cu` ON cu.company_id = 160
LEFT JOIN `answer_groups` `ag` ON ag.question_id = qe.id AND ag.archived = 0
LEFT JOIN `answer_fields` `af` ON af.answer_group_id = ag.id AND af.archived = 0
LEFT JOIN `answers` `a` ON a.answer_field_id = af.id AND a.is_answered = 1 AND a.user_id = cu.user_id AND a.questionnaire_id = 228
LEFT JOIN `answers` `a_2` ON a_2.answer_field_id = af.id AND a_2.is_default = 1
LEFT JOIN `answer_fields` `af_2` ON af_2.id = a.answer_field_id AND af_2.archived = 0
LEFT JOIN `answer_fields` `af_3` ON af_3.id = a_2.answer_field_id AND af_3.archived = 0
LEFT JOIN `questions_has_user_files` `quf` ON quf.question_id = qe.id AND quf.company_id = 160
LEFT JOIN `questionnaires_has_questions` `qhq` ON qhq.question_id = qe.id
WHERE ((`qhq`.`questionnaire_id` = 228) OR (`qhq`.`questionnaire_id` = 149)) AND (`qe`.`archived`=FALSE) AND (qe.id = q.id) AND ((`af_2`.`user_upload_enabled`=2) OR (`af_3`.`user_upload_enabled`=2))) AS `user_upload_enabled`,
EXISTS (SELECT `qe`.* FROM `questions` `qe`
CROSS JOIN `companies_has_users` `cu` ON cu.company_id = 160
LEFT JOIN `answer_groups` `ag` ON ag.question_id = qe.id AND ag.archived = 0
LEFT JOIN `answer_fields` `af` ON af.answer_group_id = ag.id AND af.archived = 0
LEFT JOIN `answers` `a` ON a.answer_field_id = af.id AND a.is_answered = 1 AND a.user_id = cu.user_id AND a.questionnaire_id = 228
LEFT JOIN `answers` `a_2` ON a_2.answer_field_id = af.id AND a_2.is_default = 1
LEFT JOIN `answer_fields` `af_2` ON af_2.id = a.answer_field_id AND af_2.archived = 0
LEFT JOIN `answer_fields` `af_3` ON af_3.id = a_2.answer_field_id AND af_3.archived = 0
LEFT JOIN `questions_has_user_files` `quf` ON quf.question_id = qe.id AND quf.company_id = 160
LEFT JOIN `questionnaires_has_questions` `qhq` ON qhq.question_id = qe.id
WHERE ((`qhq`.`questionnaire_id` = 228) OR (`qhq`.`questionnaire_id` = 149)) AND (`qe`.`archived`=FALSE) AND (qe.id = q.id) AND ((`quf`.`file_id` IS NOT NULL) AND (`quf`.`questionnaire_id`=228))) AS `has_user_file_id` FROM `questions` `q`
LEFT JOIN `questionnaires_has_questions` `qhq` ON qhq.question_id = q.id
WHERE ((`qhq`.`questionnaire_id` = 228) OR (`qhq`.`questionnaire_id` = 149)) AND (`q`.`archived`=FALSE)
GROUP BY `q`.`id` ORDER BY `count_answers` DESC, `count_default_answers` DESC
And I perform a Explain in both servers to know how if I found something to optimzie and I get:
in the mariaDB:
id | select_type | table | type | possible_key | key | key_len | ref | rows | r_rows | filtered | r_filtered | extra |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | PRIMARY | qhq | range | fk_questionnaires_has_questions_questionnaire_id,fk_questionnaires_has_questions_question_id | fk_questionnaires_has_questions_questionnaire_id | 4 | 50 | 50 | 100 | 100 | Using index condition; Using temporary; Using filesort | |
1 | PRIMARY | q | eq_ref | PRIMARY | PRIMARY | 4 | com_tool.qhq.question_id | 1 | 1 | 100 | 66 | Using where |
5 | DEPENDENT SUBQUERY | qe | eq_ref | PRIMARY | PRIMARY | 4 | func | 1 | 1 | 100 | 100 | Using where |
5 | DEPENDENT SUBQUERY | ag | ref | fk_answers_questions1_idx | fk_answers_questions1_idx | 4 | com_tool.qe.id | 1 | 1 | 100 | 100 | Using where |
5 | DEPENDENT SUBQUERY | quf | ref | PRIMARY,fk_questions_has_user_files_questions1,fk_questions_has_user_files_files1,fk_questions_has_user_files_companies1 | fk_questions_has_user_files_companies1 | 12 | const,const,func | 1 | 0 | 100 | 100 | Using where; Using index |
5 | DEPENDENT SUBQUERY | qhq | ref | fk_questionnaires_has_questions_questionnaire_id,fk_questionnaires_has_questions_question_id | fk_questionnaires_has_questions_question_id | 4 | func | 1 | 1.41 | Using index condition; Using where | ||
5 | DEPENDENT SUBQUERY | af | ref | fk_answer_fields_answer_groups1_idx,idx_answer_fields_archived_index | fk_answer_fields_answer_groups1_idx | 4 | com_tool.ag.id | 4 | 14.58 | Using where | ||
5 | DEPENDENT SUBQUERY | cu | ref | PRIMARY,fk_companies_has_users_companies1_idx | fk_companies_has_users_companies1_idx | 4 | const | 20 | 100 | Using index | ||
5 | DEPENDENT SUBQUERY | a | ref | fk_answers_answer_fields1_idx,fk_answers_users1_idx,fk_answers_questionnaires1_idx,idx_answers_answer_fields_3 | fk_answers_answer_fields1_idx | 4 | com_tool.af.id | 18 | 1.68 | Using where | ||
5 | DEPENDENT SUBQUERY | a_2 | ref | fk_answers_answer_fields1_idx,idx_answers_answer_fields_3 | fk_answers_answer_fields1_idx | 4 | com_tool.af.id | 18 | 100 | Using where | ||
4 | DEPENDENT SUBQUERY | qe | eq_ref | PRIMARY | PRIMARY | 4 | func | 1 | 1 | 100 | 100 | Using where |
4 | DEPENDENT SUBQUERY | ag | ref | fk_answers_questions1_idx | fk_answers_questions1_idx | 4 | com_tool.qe.id | 1 | 1 | 100 | 100 | Using where |
4 | DEPENDENT SUBQUERY | qhq | ref | fk_questionnaires_has_questions_questionnaire_id,fk_questionnaires_has_questions_question_id | fk_questionnaires_has_questions_question_id | 4 | func | 1 | 1.18 | 1.41 | 84.62 | Using index condition; Using where |
4 | DEPENDENT SUBQUERY | af | ref | fk_answer_fields_answer_groups1_idx,idx_answer_fields_archived_index | fk_answer_fields_answer_groups1_idx | 4 | com_tool.ag.id | 4 | 4.97 | 14.58 | 98.78 | Using where |
4 | DEPENDENT SUBQUERY | cu | ref | PRIMARY,fk_companies_has_users_companies1_idx | fk_companies_has_users_companies1_idx | 4 | const | 20 | 19.77 | 100 | 100 | Using index |
4 | DEPENDENT SUBQUERY | a | ref | fk_answers_answer_fields1_idx,fk_answers_users1_idx,fk_answers_questionnaires1_idx,idx_answers_answer_fields_3 | fk_answers_answer_fields1_idx | 4 | com_tool.af.id | 18 | 329.03 | 1.68 | 0 | Using where |
4 | DEPENDENT SUBQUERY | a_2 | ref | fk_answers_answer_fields1_idx,idx_answers_answer_fields_3 | fk_answers_answer_fields1_idx | 4 | com_tool.af.id | 18 | 328.91 | 100 | 4.61 | Using where |
4 | DEPENDENT SUBQUERY | af_2 | eq_ref | PRIMARY,idx_answer_fields_archived_index,idx_answers_answer_fields_1 | PRIMARY | 4 | com_tool.a.answer_field_id | 1 | 0.01 | 14.58 | 100 | Using where |
4 | DEPENDENT SUBQUERY | af_3 | eq_ref | PRIMARY,idx_answer_fields_archived_index,idx_answers_answer_fields_1 | PRIMARY | 4 | com_tool.a_2.answer_field_id | 1 | 0.98 | 14.58 | 100 | Using where |
4 | DEPENDENT SUBQUERY | quf | ref | fk_questions_has_user_files_questions1,fk_questions_has_user_files_companies1 | fk_questions_has_user_files_companies1 | 4 | const | 4 | 3 | 100 | 16.67 | Using where; Using index |
3 | DEPENDENT SUBQUERY | qe | eq_ref | PRIMARY | PRIMARY | 4 | func | 1 | 1 | 100 | 100 | Using where |
3 | DEPENDENT SUBQUERY | ag | ref | PRIMARY,fk_answers_questions1_idx | fk_answers_questions1_idx | 4 | func | 1 | 1 | 100 | 100 | Using index condition; Using where |
3 | DEPENDENT SUBQUERY | qhq | ref | fk_questionnaires_has_questions_questionnaire_id,fk_questionnaires_has_questions_question_id | fk_questionnaires_has_questions_question_id | 4 | func | 1 | 1.18 | 1.41 | 84.62 | Using index condition; Using where |
3 | DEPENDENT SUBQUERY | af | ref | PRIMARY,fk_answer_fields_answer_groups1_idx,idx_answer_fields_archived_index,idx_answers_answer_fields_1 | fk_answer_fields_answer_groups1_idx | 4 | com_tool.ag.id | 4 | 1.33 | 14.58 | 97.73 | Using where |
3 | DEPENDENT SUBQUERY | cu | ref | PRIMARY,fk_companies_has_users_companies1_idx | fk_companies_has_users_companies1_idx | 4 | const | 20 | 7.63 | 100 | 100 | Using index |
3 | DEPENDENT SUBQUERY | a | ref | fk_answers_answer_fields1_idx,fk_answers_users1_idx,fk_answers_questionnaires1_idx,idx_answers_answer_fields_3 | fk_answers_answer_fields1_idx | 4 | com_tool.af.id | 18 | 75.15 | 1.68 | 0.01 | Using where |
3 | DEPENDENT SUBQUERY | a_2 | ref | PRIMARY,fk_answers_answer_fields1_idx,idx_answers_answer_fields_2,idx_answers_answer_fields_3 | fk_answers_answer_fields1_idx | 4 | com_tool.af.id | 18 | 30.37 | 100 | 0.28 | Using index condition; Using where |
3 | DEPENDENT SUBQUERY | quf | ref | fk_questions_has_user_files_questions1,fk_questions_has_user_files_companies1 | fk_questions_has_user_files_companies1 | 4 | const | 4 | 3.93 | 100 | 0.91 | Using where; Using index |
2 | DEPENDENT SUBQUERY | qe | eq_ref | PRIMARY | PRIMARY | 4 | func | 1 | 1 | 100 | 100 | Using where |
2 | DEPENDENT SUBQUERY | ag | ref | PRIMARY,fk_answers_questions1_idx | fk_answers_questions1_idx | 4 | func | 1 | 1 | 100 | 100 | Using index condition; Using where |
2 | DEPENDENT SUBQUERY | qhq | ref | fk_questionnaires_has_questions_questionnaire_id,fk_questionnaires_has_questions_question_id | fk_questionnaires_has_questions_question_id | 4 | func | 1 | 1.18 | 1.41 | 84.62 | Using index condition; Using where |
2 | DEPENDENT SUBQUERY | af | ref | PRIMARY,fk_answer_fields_answer_groups1_idx,idx_answer_fields_archived_index,idx_answers_answer_fields_1 | fk_answer_fields_answer_groups1_idx | 4 | com_tool.ag.id | 4 | 3.73 | 14.58 | 95.12 | Using where |
2 | DEPENDENT SUBQUERY | a | ref | PRIMARY,fk_answers_answer_fields1_idx,fk_answers_users1_idx,fk_answers_questionnaires1_idx,idx_answers_answer_fields_2,idx_answers_answer_fields_3 | fk_answers_answer_fields1_idx | 4 | com_tool.af.id | 18 | 266.86 | 1.68 | 3.15 | Using index condition; Using where |
2 | DEPENDENT SUBQUERY | cu | eq_ref | PRIMARY,fk_companies_has_users_users1_idx,fk_companies_has_users_companies1_idx | fk_companies_has_users_users1_idx | 8 | com_tool.a.user_id,const | 1 | 0 | 100 | 100 | Using index |
2 | DEPENDENT SUBQUERY | a_2 | ref | fk_answers_answer_fields1_idx,idx_answers_answer_fields_3 | fk_answers_answer_fields1_idx | 4 | com_tool.af.id | 18 | 1 | 100 | 100 | Using where |
2 | DEPENDENT SUBQUERY | quf | ref | fk_questions_has_user_files_questions1,fk_questions_has_user_files_companies1 | fk_questions_has_user_files_companies1 | 4 | const | 4 | 4 | 100 | 0 | Using where; Using index |
And in mysql cannot include here because is over 30000 characters. I will include that on the comments, if required.
1