P粉0091864692023-09-04 14:58:45
fastest
When a call comes in, change the data flow to update customers.last_call
.
Update connection
UPDATE
Compared with JOIN
, IN (SELECT ...)
works better.
or
OR
will reduce performance. The query will most likely scan the entire phone_call_log
for each customer.
One workaround is to do two UPDATE
, and use the appropriate index:
UPDATE SET customers.last_call = GREATEST( customers.last_call, ( select max(phone_call_log.start_time) FROM phone_call_log WHERE phone_call_log.callee = customers.phonenumber ) WHERE ... UPDATE SET customers.last_call = GREATEST( customers.last_call, ( ... caller ... ) ) WHERE ...
This requires creating the following index on phone_call_log
:
INDEX(callee, start_time) INDEX(caller, start_time)
AndDelete the current single column index caller and callee.
type of data
For phone numbers, using BIGINT
may be wrong, especially considering LENGTH(customers.phonenumber) > 6
.
Actually, all of this boils down to a simple test:
where customers.phonenumber is not null AND LENGTH(customers.phonenumber) > 6 AND customers.phonenumber > 1000000;
Every >
check checks NOT NULL
; only one of them is used, depending on the data type, and indexed.
(Please provide SHOW CREATE TABLE
; 'English' is not accurate enough.)
P粉3546029552023-09-04 10:38:38
Queries using OR
cannot effectively use the index. I suggest you try the following:
UPDATE customers SET last_call = GREATEST( (SELECT MAX(start_time) FROM phone_call_log WHERE callee = customers.phonenumber), (SELECT MAX(start_time) FROM phone_call_log WHERE caller = customers.phonenumber) )
Please note that GREATEST
has issues handling NULL values.