Indexing this mysql query

I am querying a database with a follwing query. This request takes 21 seconds. I check it by explaining the request. I have an index by fields groupId, batchIdseparately.

EXPLAIN SELECT message, sentOn, maskId, isDndCheck, contentType
FROM sms_histories
WHERE groupId = 1750
GROUP BY batchId
ORDER BY batchId DESC
LIMIT 0 , 1

I get the last group message for this request.

My request for an explanation shows me the result

id  select_type     table       type    possible_keys   key     key_len     ref     rows    Extra
1      SIMPLE   sms_histories    index      groupId     batchId     5        NULL    888    Using where 

please tell me what is the problem, why does this request take a long time?

+3
source share
3 answers

, batchId groupId, bobince. , sms_histories. , bobince , . . . - batchId groupId .

+1

, groupId CHAR. groupId. GroupId INTEGER, .

+1

(groupId, batchId), ORDER BY. MySQL batchId, groupId .

(I'm a little confused about what the query should do. You have GROUP BYno aggregate columns. If it batchIdis the primary key sms_histories, then GROUP BYnothing will appear . If it is not the primary key, the query is invalid. SQL and MySQL will return the row in a semi-random order.)

+1
source

Source: https://habr.com/ru/post/1763968/


All Articles