In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `table_1` ADD INDEX `table_1_idx_visitor_id` (`visitor_id`);
ALTER TABLE `table_2` ADD INDEX `table_2_idx_visitor_id_company_id_visited_on` (`visitor_id`,`company_id`,`visited_on`);
SELECT
t1.visitor_id
FROM
table_1 AS t1
WHERE
(
1 = 1
AND 1 = 1
)
AND (
EXISTS (
SELECT
1
FROM
table_2 AS t2
WHERE
(
(
t2.visitor_id = t1.visitor_id
)
AND (
t2.company_id IN (
528, 211, 1275, 521, 1299, 493, 492, 852, 868, 869, 1235, 486, 485, 1238, 855, 1237, 651, 538, 1241, 1240, 548, 543, 1247, 1253, 490, 468, 582, 583, 569, 477, 488, 802, 1294, 518, 1274, 476, 545, 1267, 556, 479, 1266, 1265, 541, 1189, 1263, 1152, 1260, 478, 1257, 885, 1139, 1256, 804, 708, 547, 561, 1239, 1142, 1226, 1148, 1230, 529, 1223, 1192, 1191, 874, 830, 822, 818, 817, 794, 718, 487, 709, 706, 705, 669, 513, 455
)
)
)
AND (
t2.visited_on BETWEEN '2015-01-01' AND '2017-01-31'
)
)
)
GROUP BY
t1.visitor_id
ORDER BY
t1.`visits_count` DESC LIMIT 20