Answer the question
In order to leave comments, you need to log in
How to optimize sql query (MySQL)?
The request is as simple as possible, there are indexes on the required fields. It takes 0.5s, which is quite a long time, provided that there are other requests on the page that take the same time to complete.
SELECT `p`.`sum`, `p`.`type`, `p`.`visit` FROM `visits` AS `v`, `payments` AS `p`
WHERE DATE(p.date) = '2021-10-04' AND `v`.`id` = `p`.`visit` AND `v`.`office` = '1'
Answer the question
In order to leave comments, you need to log in
Don't use Cartesian work - unreadable. Use the normal JOIN syntax:
SELECT `p`.`sum`, `p`.`type`, `p`.`visit`
FROM `visits` AS `v`
JOIN `payments` AS `p` ON `v`.`id` = `p`.`visit`
WHERE DATE(p.date) = '2021-10-04' AND `v`.`office` = '1'
SELECT `p`.`sum`, `p`.`type`, `p`.`visit`
FROM `visits` AS `v`
JOIN `payments` AS `p` ON `v`.`id` = `p`.`visit`
WHERE p.date >= '2021-10-04'
AND p.date < '2021-10-05'
AND `v`.`office` = '1'
visits (office, id)
. payments (`date`, visit)
and payments (visit, `date`)
, see which one is in use, remove the unused one. AND `v`.`office` = 1
. In its original form, if the field is numeric, then both the field and the value are converted to a floating-point number, and only then are compared.
The problem is that the first table (visits), where I make a selection by office, immediately gives a lot of resulting data.
(1) there are indexes on the required fields
(2) the p.date field does not have an index
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question