Optimizing MySQL self-join query -


i have following query:

select distinct(a1.actor)  actions a1  join actions a2 on a1.ip = a2.ip  a2.actor = 143  , a2.ip != '0.0.0.0'  , a2.ip != ''  , a2.actor != a1.actor  , a1.actor != 0 

this explain of query:

+----+-------------+-------+-------+------------------+---------+---------+------------------+------+--------------------------+ | id | select_type | table | type  | possible_keys    | key     | key_len | ref              | rows |                    | +----+-------------+-------+-------+------------------+---------+---------+------------------+------+--------------------------+ |  1 | simple      | a2    | range | actor,ip,actorip | actorip | 66      | null             | 3800 | using where; using index | |  1 | simple      | a1    | ref   | ip               | ip      | 62      | formabilio.a2.ip |   11 | using              | +----+-------------+-------+-------+------------------+---------+---------+------------------+------+--------------------------+ 

even if doesn't seem problematic query, in machine takes more or less 69 seconds myisam , 56 seconds in innodb. table has more or less 1 thousand records. can see explain have indeces both on actor column, on ip column , on both columns. have mysql version 5.5.35.

do have idea on why query takes long? how can optimize it?

table scan doing in each record of table actions.

according tutorial: http://www.mysqltutorial.org/mysql-self-join/

select distinct(a1.actor) actions a1 inner join actions a2  on a1.ip = a2.ip ,    a1.actor <> a2.actor a2.actor = 143  , a2.ip <> '0.0.0.0'  , a2.ip <> '' , a1.actor <> 0 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -