开发者

Need advice on speeding up this MySQL query

开发者 https://www.devze.com 2023-03-12 08:15 出处:网络
This is the query I am currently executing: SELEC开发者_如何学PythonT * FROM `datalog` WHERE world_id IN (2)

This is the query I am currently executing:

SELEC开发者_如何学PythonT * FROM `datalog`
WHERE world_id IN (2) 
AND action IN (0,1,2,8,9,10,11,13,14,15)
AND x = -184.0 AND y = 98.0 AND z = 141.0
ORDER BY data_id DESC;

Unfortunately it is taking a long time and don't know why (5 seconds or more with 14 million entries in the database). I have an index on world_id and action (since there are only a max of 7 worlds and 20 actions). How else could i speed up searching?

EDIT - value of explain: SIMPLE datalog ALL NULL NULL NULL NULL 13510263 Using where; Using filesort


Try adding indexes to x, y, and z.

You said that you thought this would not work since they can contain a large range of values.

As long as you are using a table type that supports BTREE indexes (this is the only index type that MyISAM and INNODB support), this should not be true. If you are using a HASH index, that might be the case since it would need to index each value. But with a BTREE index, MySQL is able to quickly sort for a specific value in the index. This is why it is able to use BTREE indexes on queries with comparison operators as well (<, >, etc.)

You can see more here: http://dev.mysql.com/doc/refman/5.1/en/mysql-indexes.html


You could try replacing your condition on action field by AND action < 16?


No idea if this would work, but have you tried reordering your conditions? x = -184.0 is likely to be faster than action in (...). If MySQL uses short circuiting, this could speed it up.


In addition to what @patapizza said, you should only use IN() if you basically have random things to look for so: world_id = 2 and action < 16 will likely help.

You probably need indexes on the other columns you reference in the WHERE statement and probably on data_id as well. Like @AJ said though, post the output of EXPLAIN and it can be determined exactly why it's slow.


I would try a composite index: (actionid, worldid).

0

精彩评论

暂无评论...
验证码 换一张
取 消