mysql - How to index a table fields -
i developing large table messages inbox query like
explain select * messages (receptor='x1@yahoo.com' , sender='x2@yahoo.com') or (sender='x1@yahoo.com' , receptor='x2@yahoo.com') order id desc limit 10;
is slow , cause hang server please, let me know how index table or change query avoid problem thanks
table structure
mysql> describe messages; +----------+--------------+------+-----+---------+----------------+ | field | type | null | key | default | | +----------+--------------+------+-----+---------+----------------+ | id | bigint(20) | no | pri | null | auto_increment | | sender | varchar(65) | no | mul | null | | | is_sdel | tinyint(1) | no | | null | | | receptor | varchar(65) | no | mul | null | | | is_rdel | tinyint(1) | no | | null | | | dtime | varchar(100) | no | mul | null | | | title | longtext | no | | null | | | com | longtext | no | | null | | | ipu | varchar(15) | no | | null | | | flage | tinyint(2) | no | | null | | | view | tinyint(2) | no | mul | null | | +----------+--------------+------+-----+---------+----------------+ 11 rows in set (0.00 sec)
explain command result
explain select * messages (receptor='x1@yahoo.com' , sender='x2@yahoo.com') or (sender='x1@yahoo.com' , receptor='x2@yahoo.com') order id desc limit 10; +----+-------------+----------+-------+--------------------------------------------------------------------------------+--------------------+---------+------+------+-----------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | | +----+-------------+----------+-------+--------------------------------------------------------------------------------+--------------------+---------+------+------+-----------------------------+ | 1 | simple | messages | range | sender,receptor,receptor-id,receptor-view,sender-receptor-id,sender-is_sdel-id | sender-receptor-id | 134 | null | 4 | using where; using filesort | +----+-------------+----------+-------+--------------------------------------------------------------------------------+--------------------+---------+------+------+-----------------------------+ 1 row in set (0.01 sec)
submitting answer comments:
select * ( select * messages (receptor='x1@yahoo.com' , sender='x2@yahoo.com') or (sender='x1@yahoo.com' , receptor='x2@yahoo.com') ) x order id desc limit 10;
the problem experienced sorting big table. solution forces mysql first filter out messages in inner query, , sort these in outer query.
Comments
Post a Comment