views:

35

answers:

3

I am querying database with follwing query. This query takes 21 seconds to execute.
I have check it by explain query. I have index on fields groupId , batchId separately.

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

I am getting group's last post by this query.

My explain query shows me 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 this query takes this much long time?

+1  A: 

I noticed that groupId is CHAR type. You need ascending index on groupId field. But, consider changing type of GroupId to INTEGER if possible.

Ljubomir Đokić
groupId type is of INTEGER already
Maulik Vora
so why is groupId = '1750' and not groupId = 1750 ? Can you also post the results of the following: http://pastie.org/1147768
f00
I'm asking myself the same question :)
Ljubomir Đokić
ok bros, I have changed same but it doesn't make any difference to performance :)
Maulik Vora
+1  A: 

You would need a compound index on (groupId, batchId) to make ORDER BY optimisation work. MySQL can't use the batchId index alone because all the different groupIds are mixed up in it.

(I'm a bit confused about what the query is supposed to be doing though. You have GROUP BY with no aggregate columns. If batchId is the primary key of sms_histories then the GROUP BY would appear to do nothing. If it isn't the primary key then the query is not valid SQL and MySQL will be returning a row at semi-random.)

bobince
no batchId is not primaryId , I have multiple rows in a batchId , whole batch have unique message, so I want to get unique message of that batch which is sent to particular group
Maulik Vora
`message, sentOn, maskId, isDndCheck, contentType` are all duplicated in the table for every row with the same `batchId`? This seems like a bit of a dodgy schema design. MySQL will at least return consistent values if this is the case, though it's still invalid SQL that won't work in other databases. (To make it valid you'd have to say `GROUP BY batchId, message, sentOn, maskId, isDndCheck, contentType`, which is a bit clumsy.)
bobince
message, sentOn, maskId, isDndCheck, contentType are all duplicated in the table for every row with the same batchId? yes that is... my problem is, I want only one row per batch which I am getting by `group by batchId`. and I am getting multiple batches who have same groupId by this query. I am only taking one by limit 0 , 1 from it.. It is not returning in any error
Maulik Vora
You don't get one row per batchId, you are getting one row period. That row will be *one of* the rows with the highest `batchId` for the `groupId`, but where there is more than one row with the same `batchId` there is no way of telling which one you'll get. The `GROUP BY` is doing nothing at all here.
bobince
so how can I get only one row among multiple rows with batchId. Lets take an example of DB schema with rows. `message`-`groupId`-`batchId` 'hello'-1750-258 'hello'-1753-258 'hello'-1750-258 this is my rows how can I get only one row with groupId 1750 and batchId 258, there are other fields of these rows which had different data.
Maulik Vora
+1  A: 

I think you should use compound index on batchId and groupId as bobince said. I think there will be lotof data in your table sms_histories. and as bobince said you are using bit awkward database schema. you should try to normalize it and remove replications from it. You should focus more on database schema before starting a new project. so the main thing is try to index on batchId and groupId both.

Master Maya